private ActionToolbar createToolbar() {
    DefaultActionGroup group = new DefaultActionGroup();

    BackAction back = new BackAction();
    back.registerCustomShortcutSet(
        new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0)), this);
    group.add(back);

    ForwardAction forward = new ForwardAction();
    forward.registerCustomShortcutSet(
        new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0)), this);
    group.add(forward);

    EditSourceActionBase edit = new EditSourceAction();
    edit.registerCustomShortcutSet(
        new CompositeShortcutSet(CommonShortcuts.getEditSource(), CommonShortcuts.ENTER), this);
    group.add(edit);

    edit = new ShowSourceAction();
    edit.registerCustomShortcutSet(
        new CompositeShortcutSet(CommonShortcuts.getViewSource(), CommonShortcuts.CTRL_ENTER),
        this);
    group.add(edit);

    return ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true);
  }
Пример #2
0
  /**
   * Creates a redirect rule object from the element node which should have turned up in the
   * configuration file.
   *
   * <p>It does full checking of the inspected element to see if the required XML grammar is okay.
   * In a perfect world this would not be necessary, I would turn on XML validation and a file with
   * bad grammar would be rejected up-front for not matching the schema or DTD. But as things are,
   * people are not always setting up a local catalog for frequently used files. This then means
   * that every restart of every server worldwide which uses this software would trigger a HTTP
   * request to my server in order to fetch the DTD for validation, thus generating traffic I at the
   * end must pay for. To avoid that, XML validation is turned off and the bad grammar we gracefully
   * ignore here. As the result, the user is not notified about problems, the filter simply won't
   * work if there are any, but there is nothing I can do about that right now.
   *
   * @param elem: A rule element from the configuration file.
   * @return a new redirect rule object or null if the element cannot be recognised.
   */
  protected RedirectRule loadRule(Element elem) {

    // Ignore if required attributes are missing
    if (!elem.hasAttribute("match")) return null;

    String action = elem.getTagName();

    if (action.equals("forward")) {
      ForwardAction rule = new ForwardAction();
      if (!elem.hasAttribute("target")) {
        return null;
      }
      rule.match = Pattern.compile(elem.getAttribute("match"));
      rule.target = elem.getAttribute("target");

      rule.localAddress =
          elem.hasAttribute("local-address") ? elem.getAttribute("local-address") : null;
      rule.remoteRange =
          elem.hasAttribute("remote-address") ? elem.getAttribute("remote-address") : null;

      return rule;
    }

    if (action.equals("redirect")) {
      RedirectAction rule = new RedirectAction();
      if (!elem.hasAttribute("target")) {
        return null;
      }
      rule.match = Pattern.compile(elem.getAttribute("match"));
      rule.target = elem.getAttribute("target");

      rule.localAddress =
          elem.hasAttribute("local-address") ? elem.getAttribute("local-address") : null;
      rule.remoteRange =
          elem.hasAttribute("remote-address") ? elem.getAttribute("remote-address") : null;

      rule.permanent =
          elem.hasAttribute("permanent") ? elem.getAttribute("permanent").equals("yes") : false;
      rule.encodeUrl =
          elem.hasAttribute("encode-url") ? elem.getAttribute("encode-url").equals("yes") : false;
      rule.entireUrl =
          elem.hasAttribute("entire-url") ? elem.getAttribute("entire-url").equals("yes") : false;
      rule.cache = elem.hasAttribute("cache") ? elem.getAttribute("cache") : null;

      return rule;
    }

    if (action.equals("ignore")) {
      IgnoreAction rule = new IgnoreAction();

      rule.match = Pattern.compile(elem.getAttribute("match"));

      rule.localAddress =
          elem.hasAttribute("local-address") ? elem.getAttribute("local-address") : null;
      rule.remoteRange =
          elem.hasAttribute("remote-address") ? elem.getAttribute("remote-address") : null;
      return rule;
    }
    return null;
  }
  public DocumentationComponent(
      final DocumentationManager manager, final AnAction[] additionalActions) {
    myManager = manager;
    myIsEmpty = true;
    myIsShown = false;

    myEditorPane =
        new JEditorPane(UIUtil.HTML_MIME, "") {
          @Override
          public Dimension getPreferredScrollableViewportSize() {
            if (getWidth() == 0 || getHeight() == 0) {
              setSize(MAX_WIDTH, MAX_HEIGHT);
            }
            Insets ins = myEditorPane.getInsets();
            View rootView = myEditorPane.getUI().getRootView(myEditorPane);
            rootView.setSize(
                MAX_WIDTH,
                MAX_HEIGHT); // Necessary! Without this line, size will not increase then you go
            // from small page to bigger one
            int prefHeight = (int) rootView.getPreferredSpan(View.Y_AXIS);
            prefHeight +=
                ins.bottom
                    + ins.top
                    + myScrollPane.getHorizontalScrollBar().getMaximumSize().height;
            return new Dimension(MAX_WIDTH, Math.max(MIN_HEIGHT, Math.min(MAX_HEIGHT, prefHeight)));
          }

          {
            enableEvents(AWTEvent.KEY_EVENT_MASK);
          }

          @Override
          protected void processKeyEvent(KeyEvent e) {
            KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(e);
            ActionListener listener = myKeyboardActions.get(keyStroke);
            if (listener != null) {
              listener.actionPerformed(new ActionEvent(DocumentationComponent.this, 0, ""));
              e.consume();
              return;
            }
            super.processKeyEvent(e);
          }

          @Override
          protected void paintComponent(Graphics g) {
            GraphicsUtil.setupAntialiasing(g);
            super.paintComponent(g);
          }
        };
    DataProvider helpDataProvider =
        new DataProvider() {
          @Override
          public Object getData(@NonNls String dataId) {
            return PlatformDataKeys.HELP_ID.is(dataId) ? DOCUMENTATION_TOPIC_ID : null;
          }
        };
    myEditorPane.putClientProperty(DataManager.CLIENT_PROPERTY_DATA_PROVIDER, helpDataProvider);
    myText = "";
    myEditorPane.setEditable(false);
    myEditorPane.setBackground(HintUtil.INFORMATION_COLOR);
    myEditorPane.setEditorKit(UIUtil.getHTMLEditorKit());
    myScrollPane =
        new JBScrollPane(myEditorPane) {
          @Override
          protected void processMouseWheelEvent(MouseWheelEvent e) {
            if (!EditorSettingsExternalizable.getInstance().isWheelFontChangeEnabled()
                || !EditorUtil.isChangeFontSize(e)) {
              super.processMouseWheelEvent(e);
              return;
            }

            int change = Math.abs(e.getWheelRotation());
            boolean increase = e.getWheelRotation() <= 0;
            EditorColorsManager colorsManager = EditorColorsManager.getInstance();
            EditorColorsScheme scheme = colorsManager.getGlobalScheme();
            FontSize newFontSize = scheme.getQuickDocFontSize();
            for (; change > 0; change--) {
              if (increase) {
                newFontSize = newFontSize.larger();
              } else {
                newFontSize = newFontSize.smaller();
              }
            }

            if (newFontSize == scheme.getQuickDocFontSize()) {
              return;
            }

            scheme.setQuickDocFontSize(newFontSize);
            applyFontSize();
            setFontSizeSliderSize(newFontSize);
          }
        };
    myScrollPane.setBorder(null);
    myScrollPane.putClientProperty(DataManager.CLIENT_PROPERTY_DATA_PROVIDER, helpDataProvider);

    final MouseAdapter mouseAdapter =
        new MouseAdapter() {
          @Override
          public void mousePressed(MouseEvent e) {
            myManager.requestFocus();
            myShowSettingsButton.hideSettings();
          }
        };
    myEditorPane.addMouseListener(mouseAdapter);
    Disposer.register(
        this,
        new Disposable() {
          @Override
          public void dispose() {
            myEditorPane.removeMouseListener(mouseAdapter);
          }
        });

    final FocusAdapter focusAdapter =
        new FocusAdapter() {
          @Override
          public void focusLost(FocusEvent e) {
            Component previouslyFocused =
                WindowManagerEx.getInstanceEx()
                    .getFocusedComponent(manager.getProject(getElement()));

            if (!(previouslyFocused == myEditorPane)) {
              if (myHint != null && !myHint.isDisposed()) myHint.cancel();
            }
          }
        };
    myEditorPane.addFocusListener(focusAdapter);

    Disposer.register(
        this,
        new Disposable() {
          @Override
          public void dispose() {
            myEditorPane.removeFocusListener(focusAdapter);
          }
        });

    setLayout(new BorderLayout());
    JLayeredPane layeredPane =
        new JBLayeredPane() {
          @Override
          public void doLayout() {
            final Rectangle r = getBounds();
            for (Component component : getComponents()) {
              if (component instanceof JScrollPane) {
                component.setBounds(0, 0, r.width, r.height);
              } else {
                int insets = 2;
                Dimension d = component.getPreferredSize();
                component.setBounds(r.width - d.width - insets, insets, d.width, d.height);
              }
            }
          }

          @Override
          public Dimension getPreferredSize() {
            Dimension editorPaneSize = myEditorPane.getPreferredScrollableViewportSize();
            Dimension controlPanelSize = myControlPanel.getPreferredSize();
            return new Dimension(
                Math.max(editorPaneSize.width, controlPanelSize.width),
                editorPaneSize.height + controlPanelSize.height);
          }
        };
    layeredPane.add(myScrollPane);
    layeredPane.setLayer(myScrollPane, 0);

    mySettingsPanel = createSettingsPanel();
    layeredPane.add(mySettingsPanel);
    layeredPane.setLayer(mySettingsPanel, JLayeredPane.POPUP_LAYER);
    add(layeredPane, BorderLayout.CENTER);
    setOpaque(true);
    myScrollPane.setViewportBorder(JBScrollPane.createIndentBorder());

    final DefaultActionGroup actions = new DefaultActionGroup();
    final BackAction back = new BackAction();
    final ForwardAction forward = new ForwardAction();
    actions.add(back);
    actions.add(forward);
    actions.add(myExternalDocAction = new ExternalDocAction());
    back.registerCustomShortcutSet(CustomShortcutSet.fromString("LEFT"), this);
    forward.registerCustomShortcutSet(CustomShortcutSet.fromString("RIGHT"), this);
    myExternalDocAction.registerCustomShortcutSet(CustomShortcutSet.fromString("UP"), this);
    if (additionalActions != null) {
      for (final AnAction action : additionalActions) {
        actions.add(action);
      }
    }

    myToolBar =
        ActionManager.getInstance()
            .createActionToolbar(ActionPlaces.JAVADOC_TOOLBAR, actions, true);

    myControlPanel = new JPanel();
    myControlPanel.setLayout(new BorderLayout());
    myControlPanel.setBorder(IdeBorderFactory.createBorder(SideBorder.BOTTOM));
    JPanel dummyPanel = new JPanel();

    myElementLabel = new JLabel();

    dummyPanel.setLayout(new BorderLayout());
    dummyPanel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));

    dummyPanel.add(myElementLabel, BorderLayout.EAST);

    myControlPanel.add(myToolBar.getComponent(), BorderLayout.WEST);
    myControlPanel.add(dummyPanel, BorderLayout.CENTER);
    myControlPanel.add(myShowSettingsButton = new MyShowSettingsButton(), BorderLayout.EAST);
    myControlPanelVisible = false;

    final HyperlinkListener hyperlinkListener =
        new HyperlinkListener() {
          @Override
          public void hyperlinkUpdate(HyperlinkEvent e) {
            HyperlinkEvent.EventType type = e.getEventType();
            if (type == HyperlinkEvent.EventType.ACTIVATED) {
              manager.navigateByLink(DocumentationComponent.this, e.getDescription());
            }
          }
        };
    myEditorPane.addHyperlinkListener(hyperlinkListener);
    Disposer.register(
        this,
        new Disposable() {
          @Override
          public void dispose() {
            myEditorPane.removeHyperlinkListener(hyperlinkListener);
          }
        });

    registerActions();

    updateControlState();
  }