/** * Close action * * @author Calixte DENIZET */ @SuppressWarnings(value = {"serial"}) public class CloseAction extends CommonCallBack { private static final KeyStroke KEY = ScilabKeyStroke.getKeyStroke("OSSCKEY W"); /** Default constructor */ public CloseAction() { super(null); } /** @return a menuitem associated with this action */ public static MenuItem createMenu() { MenuItem item = ScilabMenuItem.createMenuItem(); SwingScilabMenuItem swingItem = (SwingScilabMenuItem) item.getAsSimpleMenuItem(); swingItem.setAction(new CloseAction()); swingItem.setText(UiDataMessages.CLOSE); swingItem.setAccelerator(KEY); return item; } /** {@inheritDoc} */ public void callBack() { ClosingOperationsManager.startClosingOperation( (SwingScilabTab) ScilabFileBrowser.getFileBrowser()); } }
/** * Create a menu item * * @param editor the associated editor * @param title the menu title * @return the menu item */ public static JMenuItem createMenuItem(SwingScilabVariableEditor editor, String title) { JMenuItem mi = new JMenuItem(title); mi.addActionListener(new SetPrecisionLongAction(editor, title)); mi.setAccelerator(ScilabKeyStroke.getKeyStroke(KEY)); return mi; }
/** * Create a document in using the XConfiguration-*.xml found in SCI/modules/MODULE_NAME/etc/ * * @return the built document */ public static Document createDocument() { DocumentBuilder docBuilder; DocumentBuilderFactory factory; Document mainDoc = getDefaultDocument(); if (mainDoc == null) { return null; } Element root = mainDoc.getDocumentElement(); factory = ScilabDocumentBuilderFactory.newInstance(); try { docBuilder = factory.newDocumentBuilder(); } catch (ParserConfigurationException pce) { System.err.println("Cannot create a XML DocumentBuilder:\n" + pce); return null; } List<File> etcs = getEtcDir(); for (File etc : etcs) { File[] xmls = etc.listFiles( new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".xml") && name.startsWith("XConfiguration-"); } }); for (File xml : xmls) { try { Document doc = docBuilder.parse(xml); if (xml.getName().equals("XConfiguration-general.xml")) { try { XPath xp = xpathFactory.newXPath(); NodeList nodes = (NodeList) xp.compile( "//shortcuts/body/actions/action-folder/action[contains(@key, 'OSSCKEY')]") .evaluate(doc, XPathConstants.NODESET); for (int i = 0; i < nodes.getLength(); i++) { Element e = (Element) nodes.item(i); e.setAttribute( "key", e.getAttribute("key").replace("OSSCKEY", ScilabKeyStroke.getOSMetaKey())); } } catch (XPathExpressionException e) { System.err.println(e); } } Node node = mainDoc.importNode(doc.getDocumentElement(), true); NodeList list = root.getElementsByTagName(node.getNodeName()); if (list.getLength() != 0) { root.replaceChild(node, list.item(0)); } } catch (SAXException se) { System.err.println(ERROR_READ + xml.getName()); } catch (IOException ioe) { System.err.println(ERROR_READ + xml.getName()); } } } return mainDoc; }
/** * Retrieve the field "html" from BasicContentViewerUI and change permission (it is private by * default) */ private void retrievePrivateFieldFromBasicContentViewerUI() { Field privateField = null; try { privateField = BasicContentViewerUI.class.getDeclaredField("html"); privateField.setAccessible(true); } catch (SecurityException e) { System.err.println( "Security error: Could not change the accessibility on the html component of the help browser."); System.err.println("Please submit a bug report: http://bugzilla.scilab.org"); e.printStackTrace(); } catch (NoSuchFieldException e) { System.err.println("Could not find the field of the html component of the help browser."); System.err.println("Please submit a bug report: http://bugzilla.scilab.org"); e.printStackTrace(); } try { this.accessibleHtml = (javax.swing.JEditorPane) privateField.get(this); accessibleHtml.setMinimumSize(new Dimension(0, 0)); accessibleHtml.setPreferredSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)); accessibleHtml.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { // Crappy workaround to avoid bad html display (the icons play and edit can be // misplaced) // To improve... (it doesn't always work) if (SwingUtilities.isEventDispatchThread()) { if (evt.getPropertyName().equals("document")) { accessibleHtml.setVisible(false); } if (evt.getPropertyName().equals("page")) { modifyFontInEDT(0); if (!accessibleHtml.isVisible()) { accessibleHtml.setVisible(true); } } } } }); // The previous workaround hides the component accessibleHtml // and consequently the focus is given to an other component. // So we force the accessibleHtml to keep the focus. accessibleHtml.setFocusTraversalPolicy( new DefaultFocusTraversalPolicy() { @Override public Component getFirstComponent(Container aContainer) { return x; } }); accessibleHtml.setFocusCycleRoot(true); String keyModifier = "alt "; if (isMac) { keyModifier = "meta "; } InputMap inputmap = accessibleHtml.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); inputmap.put(ScilabKeyStroke.getKeyStroke("OSSCKEY shift EQUALS"), SHIFTEQ); accessibleHtml .getActionMap() .put( SHIFTEQ, new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { SwingScilabHelpBrowserViewer.this.increaseFont(); } }); inputmap.put(ScilabKeyStroke.getKeyStroke(keyModifier + "LEFT"), "Previous-page"); accessibleHtml .getActionMap() .put( "Previous-page", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { DefaultHelpHistoryModel history = SwingScilabHelpBrowser.getHelpHistory(); if (history.getIndex() > 0) { history.goBack(); } } }); inputmap.put(ScilabKeyStroke.getKeyStroke(keyModifier + "RIGHT"), "Next-page"); accessibleHtml .getActionMap() .put( "Next-page", new AbstractAction() { @Override public void actionPerformed(ActionEvent e) { DefaultHelpHistoryModel history = SwingScilabHelpBrowser.getHelpHistory(); if (history.getHistory().size() != (history.getIndex() + 1)) { history.goForward(); } } }); inputmap = accessibleHtml.getInputMap(JComponent.WHEN_FOCUSED); inputmap.put(ScilabKeyStroke.getKeyStroke(keyModifier + "LEFT"), new Object()); inputmap.put(ScilabKeyStroke.getKeyStroke(keyModifier + "RIGHT"), new Object()); inputmap.put( ScilabKeyStroke.getKeyStroke("shift SPACE"), inputmap.get(ScilabKeyStroke.getKeyStroke("PAGE_UP"))); inputmap.put( ScilabKeyStroke.getKeyStroke("SPACE"), inputmap.get(ScilabKeyStroke.getKeyStroke("PAGE_DOWN"))); SwingUtilities.getAncestorOfClass(JScrollPane.class, accessibleHtml) .addMouseWheelListener(this); } catch (IllegalArgumentException e) { System.err.println("Illegal argument in the retrieval of the html component of Javahelp"); e.printStackTrace(); } catch (IllegalAccessException e) { System.err.println("Illegal access in the retrieval of the html component of Javahelp"); e.printStackTrace(); } }
/** Register the key for the action */ public static void registerKeyAction() { CommandHistory.getTree().getActionMap().put(CUT, new CutAction()); CommandHistory.getTree().getInputMap().put(ScilabKeyStroke.getKeyStroke(KEY), CUT); }
/** * @param editor the editor * @param table where to put the action */ public static void registerAction(SwingScilabVariableEditor editor, JTable table) { table.getActionMap().put(PRECISION, new SetPrecisionLongAction(editor, PRECISION)); table.getInputMap().put(ScilabKeyStroke.getKeyStroke(KEY), PRECISION); }