@Override
 public void init() {
   if (inited) {
     return;
   }
   inited = true;
   if (selector != null) {
     selector.dispose();
     select(null);
   }
   selector = new ViewerSelector(viewer);
   viewer.addSelectionChangedListener(this);
 }
  public void registerContextMenu() {
    addLineCommentAction = new AddLineCommentToFileAction(this);
    //				addLineCommentAction.setImageDescriptor(CrucibleImages.ADD_COMMENT);
    //				addGeneralCommentAction = new
    // AddGeneralCommentToFileAction(crucibleAnnotationModel.getCrucibleFile());

    if (sourceViewer != null) {
      sourceViewer.addSelectionChangedListener(addLineCommentAction);
      //					sourceViewer.addSelectionChangedListener(addGeneralCommentAction);
    }
    mergeSourceViewer.addTextAction(addLineCommentAction);
    //				mergeSourceViewer.addTextAction(addGeneralCommentAction);
  }
  private void configureContextMenu() {
    final boolean editable = isEditable(sourceViewer);
    final TextViewerAction cutAction;
    final TextViewerAction undoAction;
    final TextViewerAction redoAction;
    final TextViewerAction pasteAction;
    if (editable) {
      cutAction = new TextViewerAction(sourceViewer, ITextOperationTarget.CUT);
      cutAction.setText(UIText.SpellCheckingMessageArea_cut);
      cutAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);

      undoAction = new TextViewerAction(sourceViewer, ITextOperationTarget.UNDO);
      undoAction.setText(UIText.SpellcheckableMessageArea_undo);
      undoAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_UNDO);

      redoAction = new TextViewerAction(sourceViewer, ITextOperationTarget.REDO);
      redoAction.setText(UIText.SpellcheckableMessageArea_redo);
      redoAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_REDO);

      pasteAction = new TextViewerAction(sourceViewer, ITextOperationTarget.PASTE);
      pasteAction.setText(UIText.SpellCheckingMessageArea_paste);
      pasteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);
    } else {
      cutAction = null;
      undoAction = null;
      redoAction = null;
      pasteAction = null;
    }

    final TextViewerAction copyAction =
        new TextViewerAction(sourceViewer, ITextOperationTarget.COPY);
    copyAction.setText(UIText.SpellCheckingMessageArea_copy);
    copyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);

    final TextViewerAction selectAllAction =
        new TextViewerAction(sourceViewer, ITextOperationTarget.SELECT_ALL);
    selectAllAction.setText(UIText.SpellCheckingMessageArea_selectAll);
    selectAllAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_SELECT_ALL);

    final TextEditorPropertyAction showWhitespaceAction =
        new TextEditorPropertyAction(
            UIText.SpellcheckableMessageArea_showWhitespace,
            sourceViewer,
            AbstractTextEditor.PREFERENCE_SHOW_WHITESPACE_CHARACTERS) {

          private IPainter whitespaceCharPainter;

          @Override
          public void propertyChange(PropertyChangeEvent event) {
            String property = event.getProperty();
            if (property.equals(getPreferenceKey())
                || AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN.equals(property)
                || AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED.equals(property)
                || AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE.equals(
                    property)) {
              synchronizeWithPreference();
            }
          }

          @Override
          protected void toggleState(boolean checked) {
            if (checked) installPainter();
            else uninstallPainter();
          }

          /** Installs the painter on the viewer. */
          private void installPainter() {
            Assert.isTrue(whitespaceCharPainter == null);
            ITextViewer v = getTextViewer();
            if (v instanceof ITextViewerExtension2) {
              IPreferenceStore store = getStore();
              whitespaceCharPainter =
                  new WhitespaceCharacterPainter(
                      v,
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_SPACES),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_SPACES),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_SPACES),
                      store.getBoolean(
                          AbstractTextEditor.PREFERENCE_SHOW_LEADING_IDEOGRAPHIC_SPACES),
                      store.getBoolean(
                          AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_IDEOGRAPHIC_SPACES),
                      store.getBoolean(
                          AbstractTextEditor.PREFERENCE_SHOW_TRAILING_IDEOGRAPHIC_SPACES),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LEADING_TABS),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_ENCLOSED_TABS),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_TRAILING_TABS),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_CARRIAGE_RETURN),
                      store.getBoolean(AbstractTextEditor.PREFERENCE_SHOW_LINE_FEED),
                      store.getInt(AbstractTextEditor.PREFERENCE_WHITESPACE_CHARACTER_ALPHA_VALUE));
              ((ITextViewerExtension2) v).addPainter(whitespaceCharPainter);
            }
          }

          /** Remove the painter from the viewer. */
          private void uninstallPainter() {
            if (whitespaceCharPainter == null) return;
            ITextViewer v = getTextViewer();
            if (v instanceof ITextViewerExtension2)
              ((ITextViewerExtension2) v).removePainter(whitespaceCharPainter);
            whitespaceCharPainter.deactivate(true);
            whitespaceCharPainter = null;
          }
        };

    MenuManager contextMenu = new MenuManager();
    if (cutAction != null) contextMenu.add(cutAction);
    contextMenu.add(copyAction);
    if (pasteAction != null) contextMenu.add(pasteAction);
    contextMenu.add(selectAllAction);
    if (undoAction != null) contextMenu.add(undoAction);
    if (redoAction != null) contextMenu.add(redoAction);
    contextMenu.add(new Separator());
    contextMenu.add(showWhitespaceAction);
    contextMenu.add(new Separator());

    if (editable) {
      final SubMenuManager quickFixMenu = new SubMenuManager(contextMenu);
      quickFixMenu.setVisible(true);
      quickFixMenu.addMenuListener(
          new IMenuListener() {
            @Override
            public void menuAboutToShow(IMenuManager manager) {
              quickFixMenu.removeAll();
              addProposals(quickFixMenu);
            }
          });
    }

    final StyledText textWidget = getTextWidget();
    textWidget.setMenu(contextMenu.createContextMenu(textWidget));

    textWidget.addFocusListener(
        new FocusListener() {

          private IHandlerActivation cutHandlerActivation;
          private IHandlerActivation copyHandlerActivation;
          private IHandlerActivation pasteHandlerActivation;
          private IHandlerActivation selectAllHandlerActivation;
          private IHandlerActivation undoHandlerActivation;
          private IHandlerActivation redoHandlerActivation;
          private IHandlerActivation quickFixHandlerActivation;
          private IHandlerActivation contentAssistHandlerActivation;

          @Override
          public void focusGained(FocusEvent e) {
            IHandlerService service = getHandlerService();
            if (service == null) return;

            if (cutAction != null) {
              cutAction.update();
              cutHandlerActivation =
                  service.activateHandler(
                      IWorkbenchCommandConstants.EDIT_CUT,
                      new ActionHandler(cutAction),
                      new ActiveShellExpression(getParent().getShell()));
            }
            copyAction.update();

            copyHandlerActivation =
                service.activateHandler(
                    IWorkbenchCommandConstants.EDIT_COPY,
                    new ActionHandler(copyAction),
                    new ActiveShellExpression(getParent().getShell()));
            if (pasteAction != null)
              this.pasteHandlerActivation =
                  service.activateHandler(
                      IWorkbenchCommandConstants.EDIT_PASTE,
                      new ActionHandler(pasteAction),
                      new ActiveShellExpression(getParent().getShell()));
            selectAllHandlerActivation =
                service.activateHandler(
                    IWorkbenchCommandConstants.EDIT_SELECT_ALL,
                    new ActionHandler(selectAllAction),
                    new ActiveShellExpression(getParent().getShell()));
            if (undoAction != null)
              undoHandlerActivation =
                  service.activateHandler(
                      IWorkbenchCommandConstants.EDIT_UNDO,
                      new ActionHandler(undoAction),
                      new ActiveShellExpression(getParent().getShell()));
            if (redoAction != null)
              redoHandlerActivation =
                  service.activateHandler(
                      IWorkbenchCommandConstants.EDIT_REDO,
                      new ActionHandler(redoAction),
                      new ActiveShellExpression(getParent().getShell()));
            if (quickFixActionHandler != null)
              quickFixHandlerActivation =
                  getHandlerService()
                      .activateHandler(
                          quickFixActionHandler.getAction().getActionDefinitionId(),
                          quickFixActionHandler,
                          new ActiveShellExpression(getParent().getShell()));
            if (contentAssistActionHandler != null)
              contentAssistHandlerActivation =
                  getHandlerService()
                      .activateHandler(
                          contentAssistActionHandler.getAction().getActionDefinitionId(),
                          contentAssistActionHandler,
                          new ActiveShellExpression(getParent().getShell()));
          }

          @Override
          public void focusLost(FocusEvent e) {
            IHandlerService service = getHandlerService();
            if (service == null) return;

            if (cutHandlerActivation != null) service.deactivateHandler(cutHandlerActivation);

            if (copyHandlerActivation != null) service.deactivateHandler(copyHandlerActivation);

            if (pasteHandlerActivation != null) service.deactivateHandler(pasteHandlerActivation);

            if (selectAllHandlerActivation != null)
              service.deactivateHandler(selectAllHandlerActivation);

            if (undoHandlerActivation != null) service.deactivateHandler(undoHandlerActivation);

            if (redoHandlerActivation != null) service.deactivateHandler(redoHandlerActivation);

            if (quickFixHandlerActivation != null)
              service.deactivateHandler(quickFixHandlerActivation);

            if (contentAssistHandlerActivation != null)
              service.deactivateHandler(contentAssistHandlerActivation);
          }
        });

    sourceViewer.addSelectionChangedListener(
        new ISelectionChangedListener() {

          @Override
          public void selectionChanged(SelectionChangedEvent event) {
            if (cutAction != null) cutAction.update();
            copyAction.update();
          }
        });

    if (editable) {
      sourceViewer.addTextListener(
          new ITextListener() {
            @Override
            public void textChanged(TextEvent event) {
              if (undoAction != null) undoAction.update();
              if (redoAction != null) redoAction.update();
            }
          });
    }

    textWidget.addDisposeListener(
        new DisposeListener() {
          @Override
          public void widgetDisposed(DisposeEvent disposeEvent) {
            showWhitespaceAction.dispose();
          }
        });
  }
Beispiel #4
0
  public void createPartControl(Composite parent) {
    SashForm sash = new SashForm(parent, SWT.VERTICAL | SWT.SMOOTH);

    queryViewer = new SourceViewer(sash, null, SWT.MULTI | SWT.WRAP);
    queryString = queryViewer.getTextWidget();
    queryString.setFont(JFaceResources.getFont(JFaceResources.TEXT_FONT));
    Color background = queryString.getBackground();
    IThemeManager themeManager = PlatformUI.getWorkbench().getThemeManager();
    ITheme current = themeManager.getCurrentTheme();
    ColorRegistry colorRegistry = current.getColorRegistry();
    commentCol = colorRegistry.get(ColorProvider.COMMENT_COLOR_PREF);
    keywordCol = colorRegistry.get(ColorProvider.KEYWORD_COLOR_PREF);
    IDocument d = createDocument();
    d.set(Messages.OQLPane_F1ForHelp);

    queryViewer.addSelectionChangedListener(
        new ISelectionChangedListener() {
          public void selectionChanged(SelectionChangedEvent event) {
            updateActions();
          }
        });

    queryViewer.setDocument(d);
    queryViewer.configure(new OQLTextViewerConfiguration(getSnapshot(), commentCol, keywordCol));
    // Eclipse 4 seems to need this otherwise in high contrast mode the background is white
    queryString.setBackground(background);
    queryString.selectAll();

    PlatformUI.getWorkbench()
        .getHelpSystem()
        .setHelp(queryString, "org.eclipse.mat.ui.help.oql"); // $NON-NLS-1$
    queryString.addKeyListener(
        new KeyAdapter() {
          public void keyPressed(KeyEvent e) {
            if (e.keyCode == '\r' && (e.stateMask & SWT.MOD1) != 0) {
              executeAction.run();
              e.doit = false;
            } else if (e.keyCode == ' ' && (e.stateMask & SWT.CTRL) != 0) {
              // ctrl space combination for content assist
              contentAssistAction.run();
            } else if (e.keyCode == SWT.F5) {
              executeAction.run();
              e.doit = false;
            }
          }
        });

    queryString.addFocusListener(
        new FocusListener() {

          public void focusGained(FocusEvent e) {
            IActionBars actionBars = getEditor().getEditorSite().getActionBars();
            actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), copyQueryStringAction);
            actionBars.updateActionBars();
          }

          public void focusLost(FocusEvent e) {}
        });
    queryString.setFocus();

    createContainer(sash);

    sash.setWeights(new int[] {1, 4});

    makeActions();
    hookContextMenu();
  }