X-Git-Url: https://git.argeo.org/?p=gpl%2Fargeo-suite.git;a=blobdiff_plain;f=org.argeo.app.ui%2Fsrc%2Forg%2Fargeo%2Fapp%2Fui%2Fwidgets%2FAbstractConnectContextMenu.java;fp=org.argeo.app.ui%2Fsrc%2Forg%2Fargeo%2Fapp%2Fui%2Fwidgets%2FAbstractConnectContextMenu.java;h=7824691b84959075fdcd8f6b56e24fac92563f19;hp=0000000000000000000000000000000000000000;hb=6e56ffa34cb02ab04d028423aea342e3dfed4358;hpb=c285180bece610b2c2921d44fe14b6dde2123efa diff --git a/org.argeo.app.ui/src/org/argeo/app/ui/widgets/AbstractConnectContextMenu.java b/org.argeo.app.ui/src/org/argeo/app/ui/widgets/AbstractConnectContextMenu.java new file mode 100644 index 0000000..7824691 --- /dev/null +++ b/org.argeo.app.ui/src/org/argeo/app/ui/widgets/AbstractConnectContextMenu.java @@ -0,0 +1,133 @@ +package org.argeo.app.ui.widgets; + +import java.util.HashMap; +import java.util.Map; + +import org.argeo.cms.swt.CmsSwtUtils; +import org.argeo.eclipse.ui.EclipseUiUtils; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.ShellEvent; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; + +/** + * Generic popup context menu for TableViewer to enable single sourcing between + * CMS and Workbench + */ +public abstract class AbstractConnectContextMenu { + + private Shell parentShell; + private Shell shell; + // Local context + + private final static String KEY_ACTION_ID = "actionId"; + private final String[] defaultActions; + private Map actionButtons = new HashMap(); + + public AbstractConnectContextMenu(Display display, String[] defaultActions) { + parentShell = display.getActiveShell(); + shell = new Shell(parentShell, SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP); + this.defaultActions = defaultActions; + } + + protected void createControl() { + shell.setLayout(EclipseUiUtils.noSpaceGridLayout()); + Composite boxCmp = new Composite(shell, SWT.NO_FOCUS | SWT.BORDER); + boxCmp.setLayout(EclipseUiUtils.noSpaceGridLayout()); +// CmsUiUtils.style(boxCmp, ConnectUiStyles.CONTEXT_MENU_BOX); + createContextMenu(boxCmp); + shell.addShellListener(new ActionsShellListener()); + } + + protected void createContextMenu(Composite boxCmp) { + ActionsSelListener asl = new ActionsSelListener(); + for (String actionId : defaultActions) { + Button btn = new Button(boxCmp, SWT.FLAT | SWT.LEAD); + btn.setText(getLabel(actionId)); + btn.setLayoutData(EclipseUiUtils.fillWidth()); + CmsSwtUtils.markup(btn); +// CmsUiUtils.style(btn, actionId + ConnectUiStyles.BUTTON_SUFFIX); + btn.setData(KEY_ACTION_ID, actionId); + btn.addSelectionListener(asl); + actionButtons.put(actionId, btn); + } + } + + protected void setVisible(boolean visible, String... buttonIds) { + for (String id : buttonIds) { + Button button = actionButtons.get(id); + button.setVisible(visible); + GridData gd = (GridData) button.getLayoutData(); + gd.heightHint = visible ? SWT.DEFAULT : 0; + } + } + + public void show(Control source, Point location, IStructuredSelection selection) { + if (shell.isDisposed()) { + shell = new Shell(Display.getCurrent(), SWT.NO_TRIM | SWT.BORDER | SWT.ON_TOP); + createControl(); + } + if (shell.isVisible()) + shell.setVisible(false); + + if (aboutToShow(source, location, selection)) { + shell.pack(); + shell.layout(); + if (source instanceof Control) + shell.setLocation(((Control) source).toDisplay(location.x, location.y)); + shell.open(); + } + } + + protected Shell getParentShell() { + return parentShell; + } + + class StyleButton extends Label { + private static final long serialVersionUID = 7731102609123946115L; + + public StyleButton(Composite parent, int swtStyle) { + super(parent, swtStyle); + } + } + + class ActionsSelListener extends SelectionAdapter { + private static final long serialVersionUID = -1041871937815812149L; + + @Override + public void widgetSelected(SelectionEvent e) { + Object eventSource = e.getSource(); + if (eventSource instanceof Button) { + Button pressedBtn = (Button) eventSource; + performAction((String) pressedBtn.getData(KEY_ACTION_ID)); + shell.close(); + } + } + } + + class ActionsShellListener extends org.eclipse.swt.events.ShellAdapter { + private static final long serialVersionUID = -5092341449523150827L; + + @Override + public void shellDeactivated(ShellEvent e) { + setVisible(false); + shell.setVisible(false); + //shell.close(); + } + } + + protected abstract boolean performAction(String actionId); + + protected abstract boolean aboutToShow(Control source, Point location, IStructuredSelection selection); + + protected abstract String getLabel(String actionId); +}