Java Code Examples for javax.swing.text.StyledDocument#getCharacterElement()

The following examples show how to use javax.swing.text.StyledDocument#getCharacterElement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MWPaneSelectionManager.java    From wpcleaner with Apache License 2.0 7 votes vote down vote up
/**
 * Select previous occurrence of text. 
 */
public void selectPreviousOccurrence() {
  StyledDocument doc = textPane.getStyledDocument();
  int lastStart = Integer.MIN_VALUE;
  for (int pos = textPane.getSelectionStart(); pos > 0; pos = lastStart) {
    Element run = doc.getCharacterElement(pos - 1);
    lastStart = run.getStartOffset();
    MutableAttributeSet attr = (MutableAttributeSet) run.getAttributes();
    if ((attr != null) &&
        (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_TYPE) != null) &&
        (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_OCCURRENCE) != Boolean.FALSE)) {
      select(run);
      return;
    }
  }
  selectLastOccurrence();
}
 
Example 2
Source File: PythonIndentation.java    From SikuliX1 with MIT License 6 votes vote down vote up
@Override
public String getLeadingWhitespace(StyledDocument doc, int head, int len) throws BadLocationException {
  String ret = "";
  int pos = head;
  while (pos < head + len) {
    Element e = doc.getCharacterElement(pos);
    if (e.getName().equals(StyleConstants.ComponentElementName)) {
      break;
    }
    int eStart = e.getStartOffset();
    int eEnd = e.getEndOffset();
    String space = getLeadingWhitespace(doc.getText(eStart, eEnd - eStart));
    ret += space;
    if (space.length() < eEnd - eStart) {
      break;
    }
    pos = eEnd;
  }
  return ret;
}
 
Example 3
Source File: HyperlinkSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    try {
        if (SwingUtilities.isLeftMouseButton(e)) {
            JTextPane pane = (JTextPane)e.getSource();
            StyledDocument doc = pane.getStyledDocument();
            Element elem = doc.getCharacterElement(pane.viewToModel(e.getPoint()));
            AttributeSet as = elem.getAttributes();
            Link link = (Link)as.getAttribute(LINK_ATTRIBUTE);
            if (link != null) {
                link.onClick(elem.getDocument().getText(elem.getStartOffset(), elem.getEndOffset() - elem.getStartOffset()));
            }
        }
    } catch(Exception ex) {
        Support.LOG.log(Level.SEVERE, null, ex);
    }
}
 
Example 4
Source File: MWPaneSelectionManager.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Select next occurrence of text. 
 */
public void selectNextOccurrence() {
  StyledDocument doc = textPane.getStyledDocument();
  int length = doc.getLength();
  int lastEnd = Integer.MAX_VALUE;
  for (int pos = textPane.getSelectionEnd() + 1; pos < length; pos = lastEnd) {
    Element run = doc.getCharacterElement(pos);
    lastEnd = run.getEndOffset();
    if (pos == lastEnd) {
      // offset + length beyond length of document, bail.
      break;
    }
    MutableAttributeSet attr = (MutableAttributeSet) run.getAttributes();
    if ((attr != null) &&
        (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_TYPE) != null) &&
        (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_OCCURRENCE) != Boolean.FALSE)) {
      select(run);
      return;
    }
  }
  selectFirstOccurrence();
}
 
Example 5
Source File: MWPaneSelectionManager.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Select last occurrence of text. 
 */
public void selectLastOccurrence() {
  StyledDocument doc = textPane.getStyledDocument();
  int lastStart = Integer.MIN_VALUE;
  for (int pos = doc.getLength(); pos > 0; pos = lastStart) {
    Element run = doc.getCharacterElement(pos - 1);
    lastStart = run.getStartOffset();
    MutableAttributeSet attr = (MutableAttributeSet) run.getAttributes();
    if ((attr != null) &&
        (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_TYPE) != null) &&
        (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_OCCURRENCE) != Boolean.FALSE)) {
      select(run);
      return;
    }
  }
}
 
Example 6
Source File: LinkUtils.java    From desktopclient-java with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
    WebTextPane textPane = (WebTextPane) e.getComponent();
    StyledDocument doc = textPane.getStyledDocument();
    Element elem = doc.getCharacterElement(textPane.viewToModel(e.getPoint()));
    if (!elem.getAttributes().isDefined(URL_ATT_NAME))
        // not a link
        return;

    int len = elem.getEndOffset() - elem.getStartOffset();
    final String url;
    try {
        url = doc.getText(elem.getStartOffset(), len);
    } catch (BadLocationException ex) {
        LOGGER.log(Level.WARNING, "can't get URL", ex);
        return;
    }

    Runnable run = new Runnable() {
        @Override
        public void run() {
            WebUtils.browseSiteSafely(fixProto(url));
        }
    };
    new Thread(run, "Link Browser").start();
}
 
Example 7
Source File: HyperlinkSupport.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void mouseMoved(MouseEvent e) {
    JTextPane pane = (JTextPane)e.getSource();
    StyledDocument doc = pane.getStyledDocument();
    Element elem = doc.getCharacterElement(pane.viewToModel(e.getPoint()));
    AttributeSet as = elem.getAttributes();
    if (StyleConstants.isUnderline(as)) {
        pane.setCursor(new Cursor(Cursor.HAND_CURSOR));
    } else {
        pane.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }
}
 
Example 8
Source File: CommentsPanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void setVisible(boolean b) {
    if (b) {
        JTextPane pane = (JTextPane) getInvoker();
        StyledDocument doc = pane.getStyledDocument();
        Element elem = doc.getCharacterElement(pane.viewToModel(clickPoint));
        Object l = elem.getAttributes().getAttribute(HyperlinkSupport.LINK_ATTRIBUTE);
        if (l != null && l instanceof AttachmentLink) {
            BugzillaIssue.Attachment attachment = ((AttachmentLink) l).attachment;
            if (attachment != null) {
                add(new JMenuItem(attachment.getOpenAction()));
                add(new JMenuItem(attachment.getSaveAction()));
                Action openInStackAnalyzerAction = attachment.getOpenInStackAnalyzerAction();
                if(openInStackAnalyzerAction != null) {
                    add(new JMenuItem(openInStackAnalyzerAction));
                }
                if (attachment.isPatch()) { 
                    Action a = attachment.getApplyPatchAction();
                    if(a != null) {
                        add(attachment.getApplyPatchAction());
                    }
                }
                super.setVisible(true);
            }
        }
    } else {
        super.setVisible(false);
        removeAll();
    }
}
 
Example 9
Source File: KTextEdit.java    From stendhal with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void mouseClicked(MouseEvent e) {
	StyledDocument doc = (StyledDocument) textPane.getDocument();
	Element ele = doc.getCharacterElement(textPane.viewToModel(e.getPoint()));
	AttributeSet as = ele.getAttributes();
	Object fla = as.getAttribute("linkact");
	if (fla instanceof LinkListener) {
		try {
			((LinkListener) fla).linkClicked(doc.getText(ele.getStartOffset(), ele.getEndOffset() - ele.getStartOffset()));
		} catch (BadLocationException exc) {
			logger.error("Trying to extract link from invalid range", exc);
		}
	}
}
 
Example 10
Source File: TextEditorGUI.java    From trygve with GNU General Public License v2.0 5 votes vote down vote up
public ArrayList<Integer> breakpointByteOffsets() {
	State state = State.LookingForField;
	ArrayList<Integer> retval = new ArrayList<Integer>();
	
	final StyledDocument doc = (StyledDocument)editPane.getDocument();
	final SimpleAttributeSet breakpointColored = new SimpleAttributeSet(); 
	StyleConstants.setBackground(breakpointColored, Color.cyan);
	
	for(int i = 0; i < doc.getLength(); i++) {
		final Element element = doc.getCharacterElement(i);
	    final AttributeSet set = element.getAttributes();
	    final Color backgroundColor = StyleConstants.getBackground(set);
	    switch (state) {
	    case LookingForField:
	    	if (true == backgroundColor.equals(Color.cyan)) {
	    		state = State.InField;
	    		retval.add(new Integer(i));
	    	}
	    	break;
	    case InField:
	    	if (false == backgroundColor.equals(Color.cyan)) {
	    		state = State.LookingForField;
	    	} else if (element.toString().equals("\n")) {
	    		state = State.LookingForField;
	    	}
	    	break;
	    default:
	    	assert (false);
	    	break;
	    }
	}
	
	return retval;
}
 
Example 11
Source File: LinkUtils.java    From desktopclient-java with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void mouseMoved(MouseEvent e) {
    WebTextPane textPane = (WebTextPane) e.getComponent();
    StyledDocument doc = textPane.getStyledDocument();
    Element elem = doc.getCharacterElement(textPane.viewToModel(e.getPoint()));
    if (elem.getAttributes().isDefined(URL_ATT_NAME)) {
        textPane.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    } else {
        textPane.setCursor(Cursor.getDefaultCursor());
    }
}
 
Example 12
Source File: MWPaneReplaceAllAction.java    From wpcleaner with Apache License 2.0 4 votes vote down vote up
/**
 * @param e Event.
 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
 */
@Override
public void actionPerformed(ActionEvent e) {
  MWPane textPane = getMWPane(e);
  if (textPane == null) {
    return;
  }
  StyledDocument doc = textPane.getStyledDocument();
  if (doc == null) {
    return;
  }
  int length = doc.getLength();
  int lastEnd = Integer.MAX_VALUE;
  for (int pos = 0; pos < length; pos = lastEnd) {
    try {
      Element run = doc.getCharacterElement(pos);
      lastEnd = run.getEndOffset();
      if (pos == lastEnd) {
        // offset + length beyond length of document, bail.
        break;
      }
      MutableAttributeSet attr = (MutableAttributeSet) run.getAttributes();
      if ((attr != null) &&
          (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_TYPE) != null) &&
          (attr.getAttribute(MWPaneFormatter.ATTRIBUTE_INFO) != null)) {
        Object attrInfo = attr.getAttribute(MWPaneFormatter.ATTRIBUTE_INFO);
        if (attrInfo instanceof CheckErrorResult) {
          int startOffset = MWPaneFormatter.getUUIDStartOffset(textPane, run);
          int endOffset = MWPaneFormatter.getUUIDEndOffet(textPane, run);
          if (originalText.equals(textPane.getText(startOffset, endOffset - startOffset))) {
            boolean possible = false;
            CheckErrorResult info = (CheckErrorResult) attrInfo;
            List<Actionnable> actionnables = info.getPossibleActions();
            if (actionnables != null) {
              for (Actionnable actionnable : actionnables) {
                possible |= actionnable.isPossibleReplacement(newText);
              }
            }
            if (possible) {
              doc.remove(startOffset, endOffset - startOffset);
              doc.insertString(startOffset, newText, attr);
              lastEnd = startOffset + newText.length();
            }
          }
        }
      }
    } catch (BadLocationException exception) {
      // Nothing to do
    }
  }
}