Package org.eclipse.jface.text

Examples of org.eclipse.jface.text.ITextSelection


   *      org.eclipse.jface.viewers.ISelection)
   */
  public void toggleLineBreakpoints(IWorkbenchPart part, ISelection selection) throws CoreException {
    ITextEditor editor = (ITextEditor) part.getAdapter(ITextEditor.class);
    if (selection instanceof ITextSelection) {
      ITextSelection textSelection = (ITextSelection) selection;
      IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
      int lineNumber = -1;
      try {
        lineNumber = document.getLineOfOffset(textSelection.getOffset());
      }
      catch (BadLocationException e) {
      }
      if (lineNumber >= 0) {
        ToggleBreakpointAction toggler = new ToggleBreakpointAction(editor, null);
View Full Code Here


   * Copied from super class except instead of calling createContext(viewer,
   * region) call createContext(viewer, region, offset) instead
   */
  public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {

    ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();

    // adjust offset to end of normalized selection
    if (selection.getOffset() == offset)
      offset = selection.getOffset() + selection.getLength();

    String prefix = extractPrefix(viewer, offset);
    Region region = new Region(offset - prefix.length(), prefix.length());
    TemplateContext context = createContext(viewer, region, offset);
    if (context == null)
      return new ICompletionProposal[0];

    // name of the selection variables {line, word}_selection
    context.setVariable("selection", selection.getText()); // //$NON-NLS-1$

    Template[] templates = getTemplates(context.getContextType().getId());

    List matches = new ArrayList();
    for (int i = 0; i < templates.length; i++) {
View Full Code Here

  protected boolean insert(String text, IEditorPart targetEditor) {
    if (text == null || text.length() == 0) {
      return true;
    }

    ITextSelection textSelection = null;
    IDocument doc = null;
    ISelection selection = null;

    ISourceEditingTextTools tools = (ISourceEditingTextTools) targetEditor.getAdapter(ISourceEditingTextTools.class);
    if (tools != null) {
      doc = tools.getDocument();
      selection = tools.getSelection();
    }

    ITextEditor textEditor = null;
    if (targetEditor instanceof ITextEditor) {
      textEditor = (ITextEditor) targetEditor;
    }
    if (textEditor == null) {
      textEditor = (ITextEditor) ((IAdaptable) targetEditor).getAdapter(ITextEditor.class);
    }
    if (textEditor == null && tools != null && tools.getEditorPart() instanceof ITextEditor) {
      textEditor = (ITextEditor) tools.getEditorPart();
    }
    if (textEditor == null && tools != null && tools.getEditorPart() != null) {
      textEditor = (ITextEditor) tools.getEditorPart().getAdapter(ITextEditor.class);
    }

    if (selection == null && textEditor != null) {
      selection = textEditor.getSelectionProvider().getSelection();
    }
    if (doc == null && textEditor != null) {
      doc = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    }

    if (selection instanceof ITextSelection) {
      textSelection = (ITextSelection) selection;
      try {
        doc.replace(textSelection.getOffset(), textSelection.getLength(), text);
      }
      catch (BadLocationException e) {
        return false;
      }
    }
    if (textEditor != null && textSelection != null) {
      ISelectionProvider sp = textEditor.getSelectionProvider();
      ITextSelection sel = new TextSelection(textSelection.getOffset(), text.length());
      sp.setSelection(sel);
      textEditor.selectAndReveal(sel.getOffset(), sel.getLength());
    }

    return true;
  }
View Full Code Here

     *
     * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
     */
    protected Control createDialogArea(Composite parent) {
      ISelection selection = fTextEditor.getSelectionProvider().getSelection();
      ITextSelection textSelection = (ITextSelection) selection;
      IStructuredSelection structuredSelection = null;
      if (selection instanceof IStructuredSelection)
        structuredSelection = (IStructuredSelection) selection;

      parent.getShell().setText(SSEUIMessages.OffsetStatusLineContributionItem_0 + textSelection.getOffset() + "-" + (textSelection.getOffset() + textSelection.getLength())); //$NON-NLS-1$ //$NON-NLS-2$
      Composite composite = (Composite) super.createDialogArea(parent);

      Text documentTypeLabel = new Text(composite, SWT.SINGLE | SWT.READ_ONLY);
      GridData gd = new GridData(SWT.FILL, SWT.FILL, true, false);
      gd.horizontalSpan = 2;
View Full Code Here

      final String[] partitionings = (fDocument instanceof IDocumentExtension3) ? ((IDocumentExtension3) fDocument).getPartitionings() : new String[]{IDocumentExtension3.DEFAULT_PARTITIONING};
      partitioningCombo.setItems(partitionings);
      partitioningCombo.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
          ISelection sel = fTextEditor.getSelectionProvider().getSelection();
          ITextSelection textSelection = (ITextSelection) sel;
          try {
            String partitionerText = fDocument instanceof IDocumentExtension3 ? ((IDocumentExtension3) fDocument).getDocumentPartitioner(partitioningCombo.getItem(partitioningCombo.getSelectionIndex())).toString() : ("" + fDocument.getDocumentPartitioner()); //$NON-NLS-1$
            partitionerInstanceLabel.setText(SSEUIMessages.OffsetStatusLineContributionItem_13 + partitionerText); //$NON-NLS-1$
            fPartitionTable.setInput(TextUtilities.computePartitioning(fDocument, partitioningCombo.getItem(partitioningCombo.getSelectionIndex()), textSelection.getOffset(), textSelection.getLength(), true));
          }
          catch (BadLocationException e1) {
            fPartitionTable.setInput(new ITypedRegion[0]);
          }
        }
      });
      try {
        if (partitionings.length > 0) {
          String selectedPartitioning = partitioningCombo.getItem(0);
          if (Utilities.contains(partitionings, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING)) {
            selectedPartitioning = IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING;
            for (int i = 0; i < partitionings.length; i++) {
              if (partitionings[i].equals(IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING)) {
                partitioningCombo.select(i);
              }
            }
          }
          else {
            partitioningCombo.select(0);
          }
          ISelection sel = fTextEditor.getSelectionProvider().getSelection();
          ITextSelection textSelection = (ITextSelection) sel;
          ITypedRegion[] partitions = TextUtilities.computePartitioning(fDocument, selectedPartitioning, textSelection.getOffset(), textSelection.getLength(), true);
          fPartitionTable.setInput(partitions);
          String partitionerText = fDocument instanceof IDocumentExtension3 ? ((IDocumentExtension3) fDocument).getDocumentPartitioner(partitioningCombo.getItem(partitioningCombo.getSelectionIndex())).toString() : ("" + fDocument.getDocumentPartitioner()); //$NON-NLS-1$
          partitionerInstanceLabel.setText(SSEUIMessages.OffsetStatusLineContributionItem_13 + partitionerText); //$NON-NLS-1$
        }
        else {
          ISelection sel = fTextEditor.getSelectionProvider().getSelection();
          ITextSelection textSelection = (ITextSelection) sel;
          fPartitionTable.setInput(fDocument.computePartitioning(textSelection.getOffset(), textSelection.getLength()));
        }
      }
      catch (BadLocationException e1) {
        fPartitionTable.setInput(new ITypedRegion[0]);
      }
View Full Code Here

    boolean okay = false;
    if (fEditor instanceof ITextEditor) {
      ITextEditor textEditor = (ITextEditor) fEditor;
      IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
      if (document != null) {
        ITextSelection textSelection = getTextSelection(textEditor);
        FindOccurrencesProcessor findOccurrenceProcessor = getProcessorForCurrentSelection(document, textSelection);
        if (findOccurrenceProcessor != null) {
          if (textEditor.getEditorInput() instanceof IFileEditorInput) {
            IFile file = ((IFileEditorInput) textEditor.getEditorInput()).getFile();
            okay = findOccurrenceProcessor.findOccurrences(document, textSelection, file);
View Full Code Here

    }
    return region;
  }

  private ITextSelection getTextSelection(ITextEditor textEditor) {
    ITextSelection textSelection = null;
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    if (selection instanceof ITextSelection && !selection.isEmpty()) {
      textSelection = (ITextSelection) selection;
    }
    return textSelection;
View Full Code Here

     * @param composite
     * @return
     */
    private Composite createRegionTabContents(SashForm sashForm) {
      ISelection sel = fTextEditor.getSelectionProvider().getSelection();
      final ITextSelection textSelection = (ITextSelection) sel;
      final List documentRegions = new ArrayList();
      if (fDocument instanceof IStructuredDocument) {
        IStructuredDocument structuredDocument = (IStructuredDocument) fDocument;
        int pos = textSelection.getOffset();
        int end = textSelection.getOffset() + textSelection.getLength();
        IStructuredDocumentRegion docRegion = structuredDocument.getRegionAtCharacterOffset(pos);
        IStructuredDocumentRegion endRegion = structuredDocument.getRegionAtCharacterOffset(end);
        if (pos < end) {
          while (docRegion != endRegion) {
            documentRegions.add(docRegion);
            docRegion = docRegion.getNext();
          }
        }
        documentRegions.add(docRegion);
      }

      final TreeViewer tree = new TreeViewer(sashForm, SWT.V_SCROLL | SWT.H_SCROLL);
      final String START = SSEUIMessages.OffsetStatusLineContributionItem_15; //$NON-NLS-1$
      final String LENGTH = SSEUIMessages.OffsetStatusLineContributionItem_16; //$NON-NLS-1$
      final String TEXTLENGTH = SSEUIMessages.OffsetStatusLineContributionItem_17; //$NON-NLS-1$
      final String CONTEXT = SSEUIMessages.OffsetStatusLineContributionItem_18; //$NON-NLS-1$
      tree.setContentProvider(new ITreeContentProvider() {
        public void dispose() {
        }

        public Object[] getChildren(Object parentElement) {
          List children = new ArrayList(0);
          if (parentElement instanceof ITextSelection) {
            children.addAll(documentRegions);
          }
          if (parentElement instanceof ITextRegionCollection) {
            children.add(((ITextRegionCollection) parentElement).getRegions().toArray());
          }
          if (parentElement instanceof ITextRegion) {
            children.add(new KeyValuePair(CONTEXT, ((ITextRegion) parentElement).getType()));
            children.add(new KeyValuePair(START, Integer.toString(((ITextRegion) parentElement).getStart())));
            children.add(new KeyValuePair(TEXTLENGTH, Integer.toString(((ITextRegion) parentElement).getTextLength())));
            children.add(new KeyValuePair(LENGTH, Integer.toString(((ITextRegion) parentElement).getLength())));
          }
          if (parentElement instanceof ITextRegionList) {
            children.add(Arrays.asList(((ITextRegionList) parentElement).toArray()));
          }
          if (parentElement instanceof Collection) {
            children.addAll((Collection) parentElement);
          }
          if (parentElement instanceof Object[]) {
            children.addAll(Arrays.asList((Object[]) parentElement));
          }
          return children.toArray();
        }

        public Object[] getElements(Object inputElement) {
          return documentRegions.toArray();
        }

        public Object getParent(Object element) {
          if (element instanceof IStructuredDocumentRegion)
            return ((IStructuredDocumentRegion) element).getParentDocument();
          if (element instanceof ITextRegionContainer) {
            return ((ITextRegionContainer) element).getParent();
          }
          return fDocument;
        }

        public boolean hasChildren(Object element) {
          return !(element instanceof KeyValuePair);
        }

        public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
        }
      });
      tree.setLabelProvider(new LabelProvider() {
        public String getText(Object element) {
          if (element instanceof KeyValuePair)
            return ((KeyValuePair) element).fKey.toString().toLowerCase() + ": " + ((KeyValuePair) element).fValue; //$NON-NLS-1$
          if (element instanceof IStructuredDocumentRegion) {
            IStructuredDocumentRegion documentRegion = (IStructuredDocumentRegion) element;
            int packageNameLength = documentRegion.getClass().getPackage().getName().length();
            if (packageNameLength > 0)
              packageNameLength++;
            String name = documentRegion.getClass().getName().substring(packageNameLength);
            String text = "[" + documentRegion.getStartOffset() + "-" + documentRegion.getEndOffset() + "] " + name + "@" + element.hashCode() + " " + documentRegion.getType(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            return text;
          }
          if (element instanceof ITextRegion) {
            ITextRegion textRegion = (ITextRegion) element;
            int packageNameLength = textRegion.getClass().getPackage().getName().length();
            if (packageNameLength > 0)
              packageNameLength++;
            String name = textRegion.getClass().getName().substring(packageNameLength);
            String text = "[" + textRegion.getStart() + "-" + textRegion.getEnd() + "] " + name + "@" + element.hashCode() + " " + textRegion.getType(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
            return text;
          }
          return super.getText(element);
        }
      });
      tree.setInput(fDocument);


      final Text displayText = new Text(sashForm, SWT.V_SCROLL | SWT.H_SCROLL | SWT.READ_ONLY | SWT.BORDER);
      displayText.setBackground(sashForm.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
      tree.addSelectionChangedListener(new ISelectionChangedListener() {
        public void selectionChanged(SelectionChangedEvent event) {
          if (event.getSelection() instanceof IStructuredSelection) {
            Object o = ((IStructuredSelection) event.getSelection()).getFirstElement();
            if (o instanceof KeyValuePair)
              displayText.setText(((KeyValuePair) o).fValue.toString());
            else if (o instanceof ITextSelection) {
              ITextSelection text = (ITextSelection) o;
              try {
                displayText.setText(fDocument.get(text.getOffset(), text.getLength()));
              }
              catch (BadLocationException e) {
                displayText.setText(""); //$NON-NLS-1$
              }
            }
View Full Code Here

            public IEditorPart getEditorPart() {
              return tools.getEditorPart();
            }

            public Point getSelectionRange() {
              ITextSelection selection = tools.getSelection();
              return new Point(selection.getOffset(), selection.getOffset() + selection.getLength());
            }

          };
        }
      }
View Full Code Here

    IJavaElement[] elements = null;

    IDocument document = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput());
    ISelection selection = textEditor.getSelectionProvider().getSelection();
    if (selection instanceof ITextSelection) {
      ITextSelection textSelection = (ITextSelection) selection;
      // get the JSP translation object for this editor's document
      IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(document);
      try {
        if (model instanceof IDOMModel) {
          IDOMModel xmlModel = (IDOMModel)model;
          IDOMDocument xmlDoc = xmlModel.getDocument();

          JSPTranslationAdapter adapter = (JSPTranslationAdapter) xmlDoc.getAdapterFor(IJSPTranslation.class);
          if (adapter != null) {
            JSPTranslation translation = adapter.getJSPTranslation();
            elements = translation.getElementsFromJspRange(textSelection.getOffset(), textSelection.getOffset() + textSelection.getLength());
          }
        }
      }
      finally {
        if (model != null)
View Full Code Here

TOP

Related Classes of org.eclipse.jface.text.ITextSelection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.