Search in sources :

Example 1 with ConverterFilesLID

use of edu.cmu.cs.hcii.cogtool.CogToolLID.ConverterFilesLID 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 2 with ConverterFilesLID

use of edu.cmu.cs.hcii.cogtool.CogToolLID.ConverterFilesLID in project cogtool by cogtool.

the class UI method transmute.

/**
     * Transforms an "object-oriented" <code>ListenerIdentifier</code>
     * into a more specific value representing an actual, concrete
     * application function, depending upon the internal state of the
     * application itself (such as, based on what application elements
     * are currently selected).
     * <p>
     * If there is no more specific value for the given <code>id</code>,
     * the input value should be returned unchanged.
     *
     * @param id the key specifying the semantic nature of the
     *           action to be performed
     * @param isContextSelection true if we should specialize based on the
     *                           current contextual selection;
     *                           false to use the standard selection
     * @return the specific value representing an actual, concrete
     *         application function, or, if none exists, the input value
     * @author mlh
     */
public ListenerIdentifier transmute(ListenerIdentifier id, boolean isContextSelection) {
    boolean useTextLID = true;
    TextLID specificLID;
    // Check if a text field has the focus
    Text textWidgetWithFocus = WindowUtil.getFocusedText();
    if (textWidgetWithFocus != null) {
        specificLID = textLIDs.get(id);
        if (specificLID != null) {
            switch(specificLID.getLIDCode()) {
                case CutTextCode:
                    {
                        textWidgetWithFocus.cut();
                        break;
                    }
                case CopyTextCode:
                    {
                        textWidgetWithFocus.copy();
                        getStandardInteraction().setStatusMessage(TEXT_COPIED);
                        break;
                    }
                case PasteTextCode:
                    {
                        if (CogToolClipboard.hasCogToolObjects()) {
                            useTextLID = false;
                        } else if (ClipboardUtil.hasTextData()) {
                            textWidgetWithFocus.paste();
                        } else {
                            // Controller will do nothing
                            useTextLID = false;
                            getStandardInteraction().setStatusMessage(NOTHING_PASTED);
                        }
                        break;
                    }
                case SelectAllTextCode:
                    {
                        textWidgetWithFocus.selectAll();
                        break;
                    }
                case DeselectAllTextCode:
                    {
                        int start = textWidgetWithFocus.getSelection().x;
                        textWidgetWithFocus.setSelection(start, start);
                        break;
                    }
            }
            if (useTextLID) {
                return null;
            }
        }
    }
    // Check if a text field has the focus
    Combo comboWidgetWithFocus = WindowUtil.getFocusedCombo();
    if (comboWidgetWithFocus != null) {
        specificLID = textLIDs.get(id);
        if (specificLID != null) {
            switch(specificLID.getLIDCode()) {
                case CutTextCode:
                    {
                        comboWidgetWithFocus.cut();
                        break;
                    }
                case CopyTextCode:
                    {
                        comboWidgetWithFocus.copy();
                        break;
                    }
                case PasteTextCode:
                    {
                        if (CogToolClipboard.hasCogToolObjects()) {
                            useTextLID = false;
                        } else if (ClipboardUtil.hasTextData()) {
                            comboWidgetWithFocus.paste();
                        } else {
                            // Controller will do nothing
                            useTextLID = false;
                        }
                        break;
                    }
                case SelectAllTextCode:
                    {
                        // When it becomes available, replace the following with: comboWidgetWithFocus.selectAll();
                        int textLen = comboWidgetWithFocus.getText().length();
                        comboWidgetWithFocus.setSelection(new Point(0, textLen));
                        break;
                    }
                case DeselectAllTextCode:
                    {
                        Point currentSel = comboWidgetWithFocus.getSelection();
                        currentSel.y = currentSel.x;
                        comboWidgetWithFocus.setSelection(currentSel);
                        break;
                    }
            }
            if (useTextLID) {
                return null;
            }
        }
    }
    setUpPerformAction(id);
    if (id instanceof CogToolLID.OpenRecentLID) {
        return CogToolLID.OpenProjectFile;
    }
    if (id instanceof CogToolLID.ConverterFilesLID) {
        System.out.println("return newdesign2");
        CogToolLID.NewDesignFromImport.setClassAttribute(((ConverterFilesLID) id).getClassAttribute());
        return CogToolLID.NewDesignFromImport;
    }
    return id;
}
Also used : ConverterFilesLID(edu.cmu.cs.hcii.cogtool.CogToolLID.ConverterFilesLID) Text(org.eclipse.swt.widgets.Text) Combo(org.eclipse.swt.widgets.Combo) Point(org.eclipse.swt.graphics.Point) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint)

Aggregations

ConverterFilesLID (edu.cmu.cs.hcii.cogtool.CogToolLID.ConverterFilesLID)2 CogToolLID (edu.cmu.cs.hcii.cogtool.CogToolLID)1 DoublePoint (edu.cmu.cs.hcii.cogtool.model.DoublePoint)1 ImportConverter (edu.cmu.cs.hcii.cogtool.model.ImportConverter)1 CascadingMenuItemDefinition (edu.cmu.cs.hcii.cogtool.util.MenuUtil.CascadingMenuItemDefinition)1 RcvrImportException (edu.cmu.cs.hcii.cogtool.util.RcvrImportException)1 RcvrUIException (edu.cmu.cs.hcii.cogtool.util.RcvrUIException)1 File (java.io.File)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 URLClassLoader (java.net.URLClassLoader)1 MenuAdapter (org.eclipse.swt.events.MenuAdapter)1 MenuEvent (org.eclipse.swt.events.MenuEvent)1 Point (org.eclipse.swt.graphics.Point)1 Combo (org.eclipse.swt.widgets.Combo)1 Menu (org.eclipse.swt.widgets.Menu)1 MenuItem (org.eclipse.swt.widgets.MenuItem)1 Text (org.eclipse.swt.widgets.Text)1