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