Exemplo n.º 1
0
/**
 * 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;
  }
  /**
   * 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();
    }
  }
Exemplo n.º 4
0
 /** 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);
 }