1 package org
.argeo
.suite
.ui
.widgets
;
3 import java
.util
.HashMap
;
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
;
23 * Generic popup context menu for TableViewer to enable single sourcing between
26 public abstract class AbstractConnectContextMenu
{
28 private Shell parentShell
;
32 private final static String KEY_ACTION_ID
= "actionId";
33 private final String
[] defaultActions
;
34 private Map
<String
, Button
> actionButtons
= new HashMap
<String
, Button
>();
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
;
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());
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
);
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;
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
);
79 if (shell
.isVisible())
80 shell
.setVisible(false);
82 if (aboutToShow(source
, location
, selection
)) {
85 if (source
instanceof Control
)
86 shell
.setLocation(((Control
) source
).toDisplay(location
.x
, location
.y
));
91 protected Shell
getParentShell() {
95 class StyleButton
extends Label
{
96 private static final long serialVersionUID
= 7731102609123946115L;
98 public StyleButton(Composite parent
, int swtStyle
) {
99 super(parent
, swtStyle
);
103 class ActionsSelListener
extends SelectionAdapter
{
104 private static final long serialVersionUID
= -1041871937815812149L;
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
));
117 class ActionsShellListener
extends org
.eclipse
.swt
.events
.ShellAdapter
{
118 private static final long serialVersionUID
= -5092341449523150827L;
121 public void shellDeactivated(ShellEvent e
) {
123 shell
.setVisible(false);
128 protected abstract boolean performAction(String actionId
);
130 protected abstract boolean aboutToShow(Control source
, Point location
, IStructuredSelection selection
);
132 protected abstract String
getLabel(String actionId
);