View Javadoc
1   package org.argeo.connect.ui.widgets;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.argeo.cms.util.CmsUtils;
7   import org.argeo.connect.ui.ConnectUiStyles;
8   import org.argeo.eclipse.ui.EclipseUiUtils;
9   import org.eclipse.jface.viewers.IStructuredSelection;
10  import org.eclipse.swt.SWT;
11  import org.eclipse.swt.events.SelectionAdapter;
12  import org.eclipse.swt.events.SelectionEvent;
13  import org.eclipse.swt.events.ShellEvent;
14  import org.eclipse.swt.graphics.Point;
15  import org.eclipse.swt.layout.GridData;
16  import org.eclipse.swt.widgets.Button;
17  import org.eclipse.swt.widgets.Composite;
18  import org.eclipse.swt.widgets.Control;
19  import org.eclipse.swt.widgets.Display;
20  import org.eclipse.swt.widgets.Label;
21  import org.eclipse.swt.widgets.Shell;
22  
23  /**
24   * Generic popup context menu for TableViewer to enable single sourcing between
25   * CMS and Workbench
26   */
27  public abstract class AbstractConnectContextMenu {
28  
29  	private Shell parentShell;
30  	private Shell shell;
31  	// Local context
32  
33  	private final static String KEY_ACTION_ID = "actionId";
34  	private final String[] defaultActions;
35  	private Map<String, Button> actionButtons = new HashMap<String, Button>();
36  
37  	public AbstractConnectContextMenu(Display display, String[] defaultActions) {
38  		parentShell = display.getActiveShell();
39  		shell = new Shell(parentShell, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
40  		this.defaultActions = defaultActions;
41  	}
42  
43  	protected void createControl() {
44  		shell.setLayout(EclipseUiUtils.noSpaceGridLayout());
45  		Composite boxCmp = new Composite(shell, SWT.NO_FOCUS | SWT.BORDER);
46  		boxCmp.setLayout(EclipseUiUtils.noSpaceGridLayout());
47  		CmsUtils.style(boxCmp, ConnectUiStyles.CONTEXT_MENU_BOX);
48  		createContextMenu(boxCmp);
49  		shell.addShellListener(new ActionsShellListener());
50  	}
51  
52  	protected void createContextMenu(Composite boxCmp) {
53  		ActionsSelListener asl = new ActionsSelListener();
54  		for (String actionId : defaultActions) {
55  			Button btn = new Button(boxCmp, SWT.FLAT | SWT.LEAD);
56  			btn.setText(getLabel(actionId));
57  			btn.setLayoutData(EclipseUiUtils.fillWidth());
58  			CmsUtils.markup(btn);
59  			CmsUtils.style(btn, actionId + ConnectUiStyles.BUTTON_SUFFIX);
60  			btn.setData(KEY_ACTION_ID, actionId);
61  			btn.addSelectionListener(asl);
62  			actionButtons.put(actionId, btn);
63  		}
64  	}
65  
66  	protected void setVisible(boolean visible, String... buttonIds) {
67  		for (String id : buttonIds) {
68  			Button button = actionButtons.get(id);
69  			button.setVisible(visible);
70  			GridData gd = (GridData) button.getLayoutData();
71  			gd.heightHint = visible ? SWT.DEFAULT : 0;
72  		}
73  	}
74  
75  	public void show(Control source, Point location, IStructuredSelection selection) {
76  		if (shell.isDisposed()) {
77  			shell = new Shell(Display.getCurrent(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP);
78  			createControl();
79  		}
80  		if (shell.isVisible())
81  			shell.setVisible(false);
82  
83  		if (aboutToShow(source, location, selection)) {
84  			shell.pack();
85  			shell.layout();
86  			if (source instanceof Control)
87  				shell.setLocation(((Control) source).toDisplay(location.x, location.y));
88  			shell.open();
89  		}
90  	}
91  
92  	protected Shell getParentShell() {
93  		return parentShell;
94  	}
95  
96  	class StyleButton extends Label {
97  		private static final long serialVersionUID = 7731102609123946115L;
98  
99  		public StyleButton(Composite parent, int swtStyle) {
100 			super(parent, swtStyle);
101 		}
102 	}
103 
104 	class ActionsSelListener extends SelectionAdapter {
105 		private static final long serialVersionUID = -1041871937815812149L;
106 
107 		@Override
108 		public void widgetSelected(SelectionEvent e) {
109 			Object eventSource = e.getSource();
110 			if (eventSource instanceof Button) {
111 				Button pressedBtn = (Button) eventSource;
112 				performAction((String) pressedBtn.getData(KEY_ACTION_ID));
113 				shell.close();
114 			}
115 		}
116 	}
117 
118 	class ActionsShellListener extends org.eclipse.swt.events.ShellAdapter {
119 		private static final long serialVersionUID = -5092341449523150827L;
120 
121 		@Override
122 		public void shellDeactivated(ShellEvent e) {
123 			setVisible(false);
124 			shell.setVisible(false);
125 			//shell.close();
126 		}
127 	}
128 
129 	protected abstract boolean performAction(String actionId);
130 
131 	protected abstract boolean aboutToShow(Control source, Point location, IStructuredSelection selection);
132 
133 	protected abstract String getLabel(String actionId);
134 }