Search in sources :

Example 6 with MenuAdapter

use of org.eclipse.swt.events.MenuAdapter in project cogtool by cogtool.

the class MenuFactory method buildMenu.

// addWindowMenu
public static void buildMenu(MenuType[] neededMenus, Shell viewShell, final Listener selectionListener, ListenerIdentifierMap lIDMap, IWindowMenuData<?> menuData) {
    int windowMenuIndex = -1;
    int fileMenuIndex = -1;
    MenuUtil.CascadingMenuItemDefinition[] defn = new MenuUtil.CascadingMenuItemDefinition[neededMenus.length];
    for (int i = 0; i < neededMenus.length; i++) {
        defn[i] = menuDefns[neededMenus[i].getOrdering()];
        if (neededMenus[i] == MenuFactory.MenuType.FileMenu) {
            fileMenuIndex = i;
        } else if (neededMenus[i] == MenuFactory.MenuType.WindowMenu) {
            windowMenuIndex = i;
            defn[i].menuItems = menuData.getWindowMenuLeadItems();
        }
    }
    Menu newMenuBar = MenuUtil.createMenu(viewShell, SWT.BAR | SWT.LEFT_TO_RIGHT, defn, ListenerIdentifierMap.NORMAL, selectionListener, lIDMap);
    if (fileMenuIndex != -1) {
        final Menu fileMenu = newMenuBar.getItem(fileMenuIndex).getMenu();
        fileMenu.addMenuListener(new MenuAdapter() {

            @Override
            public void menuShown(MenuEvent evt) {
                for (MenuItem item : fileMenu.getItems()) {
                    //This menu item corresponds to the Open Recent submenu
                    if (item.getData() == RECENT_FLAG) {
                        Menu cascade = item.getMenu();
                        for (MenuItem subItem : cascade.getItems()) {
                            subItem.dispose();
                        }
                        char recentIndex = '0';
                        for (String pathName : CogToolPref.getRecent()) {
                            if (!(MenuFactory.UNSET_FILE.equals(pathName))) {
                                if (recentIndex != 0) {
                                    if (recentIndex != '9') {
                                        recentIndex++;
                                    } else {
                                        recentIndex = ' ';
                                    }
                                }
                                String safePathName = "&" + recentIndex + " " + pathName.replaceAll("&", "&&");
                                MenuItem mi = MenuUtil.addMenuItem(cascade, safePathName, SWT.PUSH);
                                CogToolLID lid = new CogToolLID.OpenRecentLID("OpenRecent", pathName);
                                mi.addListener(SWT.Selection, selectionListener);
                                mi.setData(lid);
                            }
                        }
                        boolean hasRecent = CogToolPref.hasRecent();
                        if (hasRecent) {
                            MenuUtil.addMenuItem(cascade, "", SWT.SEPARATOR);
                        }
                        MenuItem clearItem = MenuUtil.addMenuItem(cascade, L10N.get("MI.ClearItems", "Clear items"), SWT.PUSH);
                        clearItem.addListener(SWT.Selection, selectionListener);
                        clearItem.setData(CogToolLID.ClearRecent);
                        clearItem.setEnabled(hasRecent);
                    //break;
                    } else // TODO this is a mess and needs to be tidied up
                    if (item.getData() == IMPORT_OTHER_FLAG) {
                        Menu cascade = item.getMenu();
                        for (MenuItem subItem : cascade.getItems()) {
                            subItem.dispose();
                        }
                        File directory = null;
                        String directoryName = CogToolPref.CONVERTER_DIRECTORY.getString();
                        boolean researchMode = CogToolPref.RESEARCH.getBoolean();
                        if (directoryName != null && !directoryName.equals("")) {
                            directory = new File(directoryName);
                            URL[] urls = null;
                            try {
                                // TODO: fix this deprecated method
                                URL url = directory.toURL();
                                urls = new URL[] { url };
                            } catch (MalformedURLException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            if (directory.exists()) {
                                URLClassLoader classLoader = new URLClassLoader(urls);
                                String[] children = directory.list();
                                boolean firstMenuItem = true;
                                for (String resource : children) {
                                    System.out.println("Resource " + resource);
                                    resource = (resource.lastIndexOf(".") == -1) ? resource : resource.substring(0, resource.lastIndexOf('.'));
                                    try {
                                        Class<ImportConverter> translatorClass = (Class<ImportConverter>) classLoader.loadClass(resource);
                                        try {
                                            Object converter = null;
                                            try {
                                                converter = translatorClass.newInstance();
                                                Class[] nameMethodParameters = new Class[0];
                                                Method method = translatorClass.getMethod("name", nameMethodParameters);
                                                String name = (String) method.invoke(converter);
                                                if (!name.endsWith("...")) {
                                                    name = name + "...";
                                                }
                                                if (firstMenuItem) {
                                                    MenuUtil.addMenuItem(cascade, "", SWT.SEPARATOR);
                                                    firstMenuItem = false;
                                                }
                                                String menuItemName = "Import Designs from " + name;
                                                MenuItem mi = MenuUtil.addMenuItem(cascade, menuItemName, SWT.PUSH);
                                                CogToolLID lid = new CogToolLID.ConverterFilesLID("NewDesignFromImport");
                                                ((ConverterFilesLID) lid).setClassAttribute(translatorClass);
                                                mi.setData(lid);
                                                mi.addListener(SWT.Selection, selectionListener);
                                            } catch (Exception ex) {
                                                throw new RcvrImportException("The file " + resource + " can not be loaded as a class.");
                                            }//Interact with the user and display the message.
                                             catch (Error er) {
                                                System.out.println("Error was thrown!");
                                            //TODO: How to throw this recoverable exception but move on?
                                            //throw new RcvrImportException("The file " + resource + " can not be loaded as a class.");
                                            }
                                        } catch (Exception ex) {
                                            throw new RcvrImportException("The file " + resource + " is not a valid converter file.");
                                        } catch (Error er) {
                                            System.out.println("Error was thrown2!");
                                        //TODO: How to throw this recoverable exception but move on?
                                        //throw new RcvrImportException("The file " + resource + " can not be loaded as a class.");
                                        }
                                    } catch (Exception ex) {
                                        throw new RcvrImportException("The file " + resource + " cannot be loaded as a class.");
                                    } catch (Error er) {
                                        System.out.println("Error was thrown3!");
                                    //TODO: How to throw this recoverable exception but move on?
                                    //throw new RcvrImportException("The file " + resource + " can not be loaded as a class.");
                                    }
                                }
                                break;
                            }
                        }
                    }
                }
            }
        });
    }
    if (windowMenuIndex != -1) {
        // reset!
        defn[windowMenuIndex].menuItems = null;
        addWindowMenu(newMenuBar.getItem(windowMenuIndex).getMenu(), menuData);
    }
    viewShell.setMenuBar(newMenuBar);
}
Also used : MalformedURLException(java.net.MalformedURLException) ImportConverter(edu.cmu.cs.hcii.cogtool.model.ImportConverter) URL(java.net.URL) ConverterFilesLID(edu.cmu.cs.hcii.cogtool.CogToolLID.ConverterFilesLID) Menu(org.eclipse.swt.widgets.Menu) MenuEvent(org.eclipse.swt.events.MenuEvent) RcvrImportException(edu.cmu.cs.hcii.cogtool.util.RcvrImportException) MenuAdapter(org.eclipse.swt.events.MenuAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) Method(java.lang.reflect.Method) RcvrUIException(edu.cmu.cs.hcii.cogtool.util.RcvrUIException) MalformedURLException(java.net.MalformedURLException) RcvrImportException(edu.cmu.cs.hcii.cogtool.util.RcvrImportException) CascadingMenuItemDefinition(edu.cmu.cs.hcii.cogtool.util.MenuUtil.CascadingMenuItemDefinition) CogToolLID(edu.cmu.cs.hcii.cogtool.CogToolLID) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 7 with MenuAdapter

use of org.eclipse.swt.events.MenuAdapter in project dbeaver by serge-rider.

the class ToolsContextMenuHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    final IWorkbenchPart part = HandlerUtil.getActivePartChecked(event);
    final Shell activeShell = HandlerUtil.getActiveShell(event);
    if (part == null || activeShell == null) {
        return null;
    }
    final Control focusControl = activeShell.getDisplay().getFocusControl();
    if (focusControl == null) {
        return null;
    }
    Point location = getLocationFromControl(activeShell, focusControl);
    if (menuManager != null) {
        menuManager.dispose();
    }
    menuManager = new MenuManager();
    menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_CONNECT));
    menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_DISCONNECT));
    menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_INVALIDATE));
    if (part instanceof IEditorPart) {
        menuManager.add(new Separator());
        menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_COMMIT));
        menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_ROLLBACK));
        {
            final MenuManager txnMenu = new MenuManager(DBeaverActivator.getPluginResourceBundle().getString("command.org.jkiss.dbeaver.core.transaction_mode.name"));
            txnMenu.add(new DataSourceTransactionModeContributor());
            menuManager.add(txnMenu);
        }
    }
    menuManager.add(new Separator());
    {
        menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_SQL_EDITOR_OPEN));
    /*
            final MenuManager toolsMenu = new MenuManager(
                DBeaverActivator.getPluginResourceBundle().getString("menu.database.tools"));
            toolsMenu.add(new DataSourceToolsContributor());
            menuManager.add(toolsMenu);
*/
    }
    if (part instanceof IEditorPart) {
        menuManager.add(ActionUtils.makeCommandContribution(part.getSite(), CoreCommands.CMD_LINK_EDITOR, "Find in navigator", null));
    }
    final Menu contextMenu = menuManager.createContextMenu(focusControl);
    if (location != null) {
        contextMenu.setLocation(location);
    }
    contextMenu.setVisible(true);
    contextMenu.addMenuListener(new MenuAdapter() {

        @Override
        public void menuShown(MenuEvent e) {
            int keyIndex = 1;
            for (MenuItem item : contextMenu.getItems()) {
                if (/*item.getMenu() == null && */
                !CommonUtils.isEmpty(item.getText())) {
                    item.setText(String.valueOf(keyIndex) + ". " + item.getText());
                    keyIndex++;
                    if (keyIndex >= 10) {
                        break;
                    }
                }
            }
        }
    });
    return null;
}
Also used : MenuAdapter(org.eclipse.swt.events.MenuAdapter) Point(org.eclipse.swt.graphics.Point) IEditorPart(org.eclipse.ui.IEditorPart) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) MenuManager(org.eclipse.jface.action.MenuManager) Separator(org.eclipse.jface.action.Separator) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 8 with MenuAdapter

use of org.eclipse.swt.events.MenuAdapter in project cubrid-manager by CUBRID.

the class BrokerConfigEditComposite method registerContextMenu.

/**
	 * Register context menu
	 */
private void registerContextMenu() {
    final Table confTable = confTableViewer.getTable();
    confTable.addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent event) {
            ActionManager.getInstance().changeFocusProvider(confTable);
        }
    });
    final MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    final Menu contextMenu = menuManager.createContextMenu(confTable);
    confTable.setMenu(contextMenu);
    final Menu menu = new Menu(editorPart.getSite().getShell(), SWT.POP_UP);
    final MenuItem itemEditAnnotation = new MenuItem(menu, SWT.PUSH);
    itemEditAnnotation.setText(Messages.cubridBrokerConfEditorTableMenuEditAnnotation);
    itemEditAnnotation.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("all")
        public void widgetSelected(SelectionEvent event) {
            // It seems like MenuEvent can't get the mouse click Point
            // so use the point which table MouseDown event marked
            final Point pt = clickPoint;
            int selectIndex = confTable.getSelectionIndex();
            if (selectIndex < 0) {
                return;
            }
            final TableItem item = confTable.getItem(selectIndex);
            if (item == null) {
                return;
            }
            for (int i = 0, len = confTable.getColumnCount(); i < len; i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    if (i == 0) {
                        openErrorBox(editorPart.getSite().getShell(), Messages.cubridBrokerConfEditAnnotationDialogOpenErrorMsg);
                        return;
                    }
                    IStructuredSelection selection = (IStructuredSelection) confTableViewer.getSelection();
                    HashMap<String, String> valueMap = (HashMap<String, String>) selection.getFirstElement();
                    String brokerName = confListData.get(0).get(String.valueOf(i));
                    String parentPropertyKey = valueMap.get("0");
                    String parentKey = " ";
                    if (selectIndex == 0) {
                        parentKey += brokerName;
                    } else {
                        parentKey += brokerName + "->" + parentPropertyKey;
                    }
                    String annotationKey = i + ANNOTATION;
                    CubridBrokerConfEditAnnotationDialog dialog = new CubridBrokerConfEditAnnotationDialog(editorPart.getSite().getShell(), parentKey, annotationKey, valueMap);
                    if (dialog.open() == OK_ID) {
                        editorPart.setDirty(true);
                    }
                }
            }
        }
    });
    final MenuItem itemAddBrokerConf = new MenuItem(menu, SWT.PUSH);
    itemAddBrokerConf.setText(Messages.cubridBrokerConfEditorAddBrokerConfItemLabel);
    itemAddBrokerConf.setImage(CommonUIPlugin.getImage("icons/action/column_insert.png"));
    itemAddBrokerConf.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            addBrokerConfColumn();
            editorPart.setDirty(true);
        }
    });
    final MenuItem itemDeleteBrokerConf = new MenuItem(menu, SWT.PUSH);
    itemDeleteBrokerConf.setText(Messages.cubridBrokerConfEditorDeleteBrokerConfItemLabel);
    itemDeleteBrokerConf.setImage(CommonUIPlugin.getImage("icons/action/column_delete.png"));
    itemDeleteBrokerConf.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            // It seems like MenuEvent can't get the mouse click Point
            // so use the point which table MouseDown event marked
            final Point pt = clickPoint;
            final TableItem item = findSelectItem(confTable);
            if (item == null) {
                return;
            }
            int columnCount = confTable.getColumnCount();
            for (int i = 0; i < columnCount; i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    Map<String, String> valueMap = confListData.get(0);
                    String confName = valueMap.get(String.valueOf(i));
                    String msg = Messages.bind(Messages.cubridBrokerConfEditorDeleteBrokerConfConfirm, confName);
                    if (!CommonUITool.openConfirmBox(msg)) {
                        return;
                    }
                    confTable.getColumn(i).dispose();
                    for (int j = 1; j < columnCount; j++) {
                        confTable.getColumn(j).setText(Messages.cubridBrokerConfEditorBrokerTitle + (j - 1));
                    }
                    // delete data from cubridBrokerConfig, so regenerate cubridBrokerConfListData from cubridBrokerConfig
                    editorPart.getBrokerConfPersistUtil().deleteBrokerPropertyByBrokerName(brokerConfig, confName);
                    confListData.clear();
                    confListData.addAll(editorPart.parseBrokerConfigToCommonTableValue(brokerConfig));
                    confTableViewer.refresh();
                    editorPart.setDirty(true);
                    return;
                }
            }
        }
    });
    menu.addMenuListener(new MenuAdapter() {

        public void menuShown(MenuEvent event) {
            // It seems like MenuEvent can't get the mouse click Point
            // so use the point which table MouseDown event marked
            final Point pt = clickPoint;
            // It will allow that the click timing is more than 300 msec.
            if (System.currentTimeMillis() - clickPointTiming > 300) {
                itemEditAnnotation.setEnabled(false);
                itemDeleteBrokerConf.setEnabled(false);
                itemAddBrokerConf.setEnabled(false);
                return;
            }
            int selectIndex = confTable.getSelectionIndex();
            if (selectIndex < 0) {
                itemEditAnnotation.setEnabled(false);
                itemDeleteBrokerConf.setEnabled(false);
                itemAddBrokerConf.setEnabled(true);
                return;
            }
            final TableItem item = confTable.getItem(selectIndex);
            if (item == null) {
                return;
            }
            for (int i = 0, len = confTable.getColumnCount(); i < len; i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    boolean enableToAccess = i > 0;
                    itemEditAnnotation.setEnabled(enableToAccess);
                    itemDeleteBrokerConf.setEnabled(enableToAccess);
                }
            }
            itemAddBrokerConf.setEnabled(true);
        }
    });
    confTable.setMenu(menu);
}
Also used : FocusAdapter(org.eclipse.swt.events.FocusAdapter) Table(org.eclipse.swt.widgets.Table) HashMap(java.util.HashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableItem(org.eclipse.swt.widgets.TableItem) Rectangle(org.eclipse.swt.graphics.Rectangle) CubridBrokerConfEditAnnotationDialog(com.cubrid.common.ui.common.dialog.CubridBrokerConfEditAnnotationDialog) MenuAdapter(org.eclipse.swt.events.MenuAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StringUtils.defaultString(org.apache.commons.lang.StringUtils.defaultString) FocusEvent(org.eclipse.swt.events.FocusEvent) MenuManager(org.eclipse.jface.action.MenuManager) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Map(java.util.Map) HashMap(java.util.HashMap) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 9 with MenuAdapter

use of org.eclipse.swt.events.MenuAdapter in project cubrid-manager by CUBRID.

the class UnifyHostConfigEditor method registerCubridBrokerTableContextMenu.

/**
	 * register CubridConfTable context menu
	 */
private void registerCubridBrokerTableContextMenu() {
    brokerConfTabTableViewer.getTable().addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent event) {
            ActionManager.getInstance().changeFocusProvider(brokerConfTabTableViewer.getTable());
        }
    });
    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    Menu contextMenu = menuManager.createContextMenu(brokerConfTabTableViewer.getTable());
    brokerConfTabTableViewer.getTable().setMenu(contextMenu);
    Menu menu = new Menu(this.getSite().getShell(), SWT.POP_UP);
    final MenuItem itemEditAnnotation = new MenuItem(menu, SWT.PUSH);
    itemEditAnnotation.setText(Messages.confEditorTableMenuEditAnnotation);
    itemEditAnnotation.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("all")
        public void widgetSelected(SelectionEvent event) {
            //seems like MenuEvent can't get the mouse click Point
            //so use the point which table MouseDown event marked 
            Point pt = cubridBrokerTableClickPoint;
            int selectIndex = brokerConfTabTableViewer.getTable().getSelectionIndex();
            final TableItem item = brokerConfTabTableViewer.getTable().getItem(selectIndex);
            if (item == null) {
                return;
            }
            for (int i = 0; i < brokerConfTabTableViewer.getTable().getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    if (i == 0) {
                        CommonUITool.openErrorBox(getSite().getShell(), Messages.annotationDialogOpenErrorMsg);
                        return;
                    }
                    IStructuredSelection selection = (IStructuredSelection) brokerConfTabTableViewer.getSelection();
                    HashMap<String, String> valueMap = (HashMap<String, String>) selection.getFirstElement();
                    String serverName = cubridBrokerConfListData.get(0).get(i + "");
                    String parentPropertyKey = valueMap.get("0");
                    String parentKey = " ";
                    Map<String, String> brokerMap = cubridBrokerConfListData.get(1);
                    String brokerName = "";
                    if (brokerMap != null) {
                        brokerName = brokerMap.get(i + "");
                    }
                    if (selectIndex == 0) {
                        parentKey += serverName;
                    } else {
                        if (selectIndex == 1) {
                            parentKey += serverName + "->" + brokerName;
                        } else {
                            parentKey += serverName + "->" + brokerName + "->" + parentPropertyKey;
                        }
                    }
                    String annotationKey = Integer.toString(i) + BrokerConfPersistUtil.ANNOTATION;
                    CubridBrokerConfEditAnnotationDialog dialog = new CubridBrokerConfEditAnnotationDialog(getSite().getShell(), parentKey, annotationKey, valueMap);
                    if (IDialogConstants.OK_ID == dialog.open()) {
                        setDirty(true);
                    }
                }
            }
        }
    });
    final MenuItem itemAddBrokerConf = new MenuItem(menu, SWT.PUSH);
    itemAddBrokerConf.setText(com.cubrid.common.ui.common.Messages.cubridBrokerConfEditorAddBrokerConfItemLabel);
    itemAddBrokerConf.setImage(CommonUIPlugin.getImage("icons/action/column_insert.png"));
    itemAddBrokerConf.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            try {
                ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
                progress.setCancelable(true);
                progress.run(true, true, new IRunnableWithProgress() {

                    public void run(final IProgressMonitor monitor) throws InvocationTargetException {
                        Display.getDefault().asyncExec(new Runnable() {

                            public void run() {
                                //									int horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - 
                                //									brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                //									 = brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                monitor.beginTask(Messages.unifyHostConfigEditorAddColumnMsg, 1);
                                addBrokerConfColumn();
                                monitor.worked(1);
                            //									horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - horizontalSelectionInt;
                            //									brokerConfTabTableViewer.getTable().getHorizontalBar().setSelection(horizontalSelectionInt + 160);
                            }
                        });
                    }
                });
            } catch (Exception e) {
                LOGGER.error("", e);
            }
            setDirty(true);
        }
    });
    final MenuItem itemDeleteBrokerConf = new MenuItem(menu, SWT.PUSH);
    itemDeleteBrokerConf.setText(com.cubrid.common.ui.common.Messages.cubridBrokerConfEditorDeleteBrokerConfItemLabel);
    itemDeleteBrokerConf.setImage(CommonUIPlugin.getImage("icons/action/column_delete.png"));
    itemDeleteBrokerConf.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            try {
                ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
                progress.setCancelable(true);
                progress.run(true, true, new IRunnableWithProgress() {

                    public void run(final IProgressMonitor monitor) throws InvocationTargetException {
                        Display.getDefault().asyncExec(new Runnable() {

                            public void run() {
                                //									int horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - 
                                //									brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                //									 = brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                monitor.beginTask(Messages.unifyHostConfigEditorDelColumnMsg, 1);
                                delBrokerConfColumn();
                                monitor.worked(1);
                            //									horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - horizontalSelectionInt;
                            //									brokerConfTabTableViewer.getTable().getHorizontalBar().setSelection(horizontalSelectionInt + 160);
                            }
                        });
                    }
                });
            } catch (Exception e) {
                LOGGER.error("", e);
            }
            setDirty(true);
        }
    });
    menu.addMenuListener(new MenuAdapter() {

        public void menuShown(MenuEvent event) {
            //seems like MenuEvent can't get the mouse click Point
            //so use the point which table MouseDown event marked 
            Point pt = cubridBrokerTableClickPoint;
            // click timing
            if (System.currentTimeMillis() - cubridBrokerTableClickPointTiming > 300) {
                itemEditAnnotation.setEnabled(false);
                itemDeleteBrokerConf.setEnabled(false);
                itemAddBrokerConf.setEnabled(false);
                return;
            }
            int selectIndex = brokerConfTabTableViewer.getTable().getSelectionIndex();
            if (selectIndex == -1) {
                itemEditAnnotation.setEnabled(false);
                itemDeleteBrokerConf.setEnabled(false);
                itemAddBrokerConf.setEnabled(false);
                return;
            }
            if (selectIndex == 0) {
                itemEditAnnotation.setEnabled(false);
                itemDeleteBrokerConf.setEnabled(false);
                itemAddBrokerConf.setEnabled(true);
                return;
            }
            final TableItem item = brokerConfTabTableViewer.getTable().getItem(selectIndex);
            if (item == null) {
                return;
            }
            for (int i = 0; i < brokerConfTabTableViewer.getTable().getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    if (i == 0) {
                        itemEditAnnotation.setEnabled(false);
                        itemDeleteBrokerConf.setEnabled(false);
                    } else {
                        itemEditAnnotation.setEnabled(true);
                        itemDeleteBrokerConf.setEnabled(true);
                    }
                }
            }
            itemAddBrokerConf.setEnabled(true);
        }
    });
    brokerConfTabTableViewer.getTable().setMenu(menu);
}
Also used : FocusAdapter(org.eclipse.swt.events.FocusAdapter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableItem(org.eclipse.swt.widgets.TableItem) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) Rectangle(org.eclipse.swt.graphics.Rectangle) CubridBrokerConfEditAnnotationDialog(com.cubrid.common.ui.common.dialog.CubridBrokerConfEditAnnotationDialog) MenuAdapter(org.eclipse.swt.events.MenuAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) FocusEvent(org.eclipse.swt.events.FocusEvent) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MenuManager(org.eclipse.jface.action.MenuManager) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MenuEvent(org.eclipse.swt.events.MenuEvent)

Example 10 with MenuAdapter

use of org.eclipse.swt.events.MenuAdapter in project cubrid-manager by CUBRID.

the class UnifyHostConfigEditor method registerCubridConfTableContextMenu.

/**
	 * register CubridConfTable context menu
	 */
private void registerCubridConfTableContextMenu() {
    cubridConfTabTableViewer.getTable().addFocusListener(new FocusAdapter() {

        @Override
        public void focusGained(FocusEvent event) {
            ActionManager.getInstance().changeFocusProvider(cubridConfTabTableViewer.getTable());
        }
    });
    MenuManager menuManager = new MenuManager();
    menuManager.setRemoveAllWhenShown(true);
    Menu contextMenu = menuManager.createContextMenu(cubridConfTabTableViewer.getTable());
    cubridConfTabTableViewer.getTable().setMenu(contextMenu);
    Menu menu = new Menu(this.getSite().getShell(), SWT.POP_UP);
    final MenuItem itemEditAnnotation = new MenuItem(menu, SWT.PUSH);
    itemEditAnnotation.setText(Messages.confEditorTableMenuEditAnnotation);
    itemEditAnnotation.addSelectionListener(new SelectionAdapter() {

        @SuppressWarnings("all")
        public void widgetSelected(SelectionEvent event) {
            //seems like MenuEvent can't get the mouse click Point
            //so use the point which table MouseDown event marked 
            Point pt = cubridConfTableClickPoint;
            int selectIndex = cubridConfTabTableViewer.getTable().getSelectionIndex();
            final TableItem item = cubridConfTabTableViewer.getTable().getItem(selectIndex);
            if (item == null) {
                return;
            }
            for (int i = 0; i < cubridConfTabTableViewer.getTable().getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    if (i == 0) {
                        CommonUITool.openErrorBox(getSite().getShell(), Messages.annotationDialogOpenErrorMsg);
                        return;
                    }
                    IStructuredSelection selection = (IStructuredSelection) cubridConfTabTableViewer.getSelection();
                    HashMap<String, String> valueMap = (HashMap<String, String>) selection.getFirstElement();
                    String serverName = cubridConfConfigListData.get(0).get(i + "");
                    String parentPropertyKey = valueMap.get("0");
                    String parentKey = " ";
                    Map<String, String> cubridMap = cubridConfConfigListData.get(1);
                    String cubridName = "";
                    if (cubridMap != null) {
                        cubridName = cubridMap.get(i + "");
                    }
                    if (selectIndex == 0) {
                        parentKey += serverName;
                    } else {
                        if (selectIndex == 1) {
                            parentKey += serverName + "->" + cubridName;
                        } else {
                            parentKey += serverName + "->" + cubridName + "->" + parentPropertyKey;
                        }
                    }
                    String annotationKey = Integer.toString(i) + BrokerConfPersistUtil.ANNOTATION;
                    CubridBrokerConfEditAnnotationDialog dialog = new CubridBrokerConfEditAnnotationDialog(getSite().getShell(), parentKey, annotationKey, valueMap);
                    if (IDialogConstants.OK_ID == dialog.open()) {
                        setDirty(true);
                    }
                }
            }
        }
    });
    final MenuItem itemAddCubridConf = new MenuItem(menu, SWT.PUSH);
    itemAddCubridConf.setText(Messages.unifyHostConfigEditorAddCubridConfColumn);
    itemAddCubridConf.setImage(CommonUIPlugin.getImage("icons/action/column_insert.png"));
    itemAddCubridConf.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            try {
                ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
                progress.setCancelable(true);
                progress.run(true, true, new IRunnableWithProgress() {

                    public void run(final IProgressMonitor monitor) throws InvocationTargetException {
                        Display.getDefault().asyncExec(new Runnable() {

                            public void run() {
                                //									int horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - 
                                //									brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                //									 = brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                monitor.beginTask(Messages.unifyHostConfigEditorAddColumnMsg, 1);
                                addCubridConfColumn();
                                monitor.worked(1);
                            //									horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - horizontalSelectionInt;
                            //									brokerConfTabTableViewer.getTable().getHorizontalBar().setSelection(horizontalSelectionInt + 160);
                            }
                        });
                    }
                });
            } catch (Exception e) {
                LOGGER.error("", e);
            }
            setDirty(true);
        }
    });
    final MenuItem itemDelCubridConf = new MenuItem(menu, SWT.PUSH);
    itemDelCubridConf.setText(Messages.unifyHostConfigEditorDelCubridConfColumn);
    itemDelCubridConf.setImage(CommonUIPlugin.getImage("icons/action/column_delete.png"));
    itemDelCubridConf.addSelectionListener(new SelectionAdapter() {

        public void widgetSelected(SelectionEvent event) {
            try {
                ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
                progress.setCancelable(true);
                progress.run(true, true, new IRunnableWithProgress() {

                    public void run(final IProgressMonitor monitor) throws InvocationTargetException {
                        Display.getDefault().asyncExec(new Runnable() {

                            public void run() {
                                //									int horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - 
                                //									brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                //									 = brokerConfTabTableViewer.getTable().getHorizontalBar().getSelection();
                                monitor.beginTask(Messages.unifyHostConfigEditorDelColumnMsg, 1);
                                delCubridConfColumn();
                                monitor.worked(1);
                            //									horizontalSelectionInt = brokerConfTabTableViewer.getTable().getHorizontalBar().getSize().y - horizontalSelectionInt;
                            //									brokerConfTabTableViewer.getTable().getHorizontalBar().setSelection(horizontalSelectionInt + 160);
                            }
                        });
                    }
                });
            } catch (Exception e) {
                LOGGER.error("", e);
            }
        }
    });
    menu.addMenuListener(new MenuAdapter() {

        public void menuShown(MenuEvent event) {
            //seems like MenuEvent can't get the mouse click Point
            //so use the point which table MouseDown event marked 
            Point pt = cubridConfTableClickPoint;
            // click timing
            if (System.currentTimeMillis() - cubridConfTableClickPointTiming > 300) {
                itemEditAnnotation.setEnabled(false);
                itemDelCubridConf.setEnabled(false);
                itemAddCubridConf.setEnabled(false);
                return;
            }
            int selectIndex = cubridConfTabTableViewer.getTable().getSelectionIndex();
            if (selectIndex == -1) {
                itemEditAnnotation.setEnabled(false);
                itemDelCubridConf.setEnabled(false);
                itemAddCubridConf.setEnabled(false);
                return;
            }
            if (selectIndex == 0) {
                itemEditAnnotation.setEnabled(false);
                itemDelCubridConf.setEnabled(false);
                itemAddCubridConf.setEnabled(true);
                return;
            }
            final TableItem item = cubridConfTabTableViewer.getTable().getItem(selectIndex);
            if (item == null) {
                return;
            }
            for (int i = 0; i < cubridConfTabTableViewer.getTable().getColumnCount(); i++) {
                Rectangle rect = item.getBounds(i);
                if (rect.contains(pt)) {
                    if (i == 0) {
                        itemEditAnnotation.setEnabled(false);
                        itemDelCubridConf.setEnabled(false);
                    } else {
                        itemEditAnnotation.setEnabled(true);
                        itemDelCubridConf.setEnabled(true);
                    }
                }
            }
            itemAddCubridConf.setEnabled(true);
        }
    });
    cubridConfTabTableViewer.getTable().setMenu(menu);
}
Also used : FocusAdapter(org.eclipse.swt.events.FocusAdapter) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) TableItem(org.eclipse.swt.widgets.TableItem) ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) Rectangle(org.eclipse.swt.graphics.Rectangle) CubridBrokerConfEditAnnotationDialog(com.cubrid.common.ui.common.dialog.CubridBrokerConfEditAnnotationDialog) MenuAdapter(org.eclipse.swt.events.MenuAdapter) MenuItem(org.eclipse.swt.widgets.MenuItem) Point(org.eclipse.swt.graphics.Point) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) FocusEvent(org.eclipse.swt.events.FocusEvent) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) MenuManager(org.eclipse.jface.action.MenuManager) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MenuEvent(org.eclipse.swt.events.MenuEvent)

Aggregations

MenuAdapter (org.eclipse.swt.events.MenuAdapter)11 MenuEvent (org.eclipse.swt.events.MenuEvent)11 Menu (org.eclipse.swt.widgets.Menu)10 MenuItem (org.eclipse.swt.widgets.MenuItem)10 MenuManager (org.eclipse.jface.action.MenuManager)9 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)9 SelectionEvent (org.eclipse.swt.events.SelectionEvent)9 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)7 Point (org.eclipse.swt.graphics.Point)7 FocusEvent (org.eclipse.swt.events.FocusEvent)6 TableItem (org.eclipse.swt.widgets.TableItem)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 FocusAdapter (org.eclipse.swt.events.FocusAdapter)5 Rectangle (org.eclipse.swt.graphics.Rectangle)5 CubridBrokerConfEditAnnotationDialog (com.cubrid.common.ui.common.dialog.CubridBrokerConfEditAnnotationDialog)4 LinkedHashMap (java.util.LinkedHashMap)3 DoubleClickEvent (org.eclipse.jface.viewers.DoubleClickEvent)3 IDoubleClickListener (org.eclipse.jface.viewers.IDoubleClickListener)3 TableViewer (org.eclipse.jface.viewers.TableViewer)3