public void run() { if (component != null) { BaseKit kit = Utilities.getKit(component); if (kit != null) { boolean selectionVisible = ((Caret) evt.getSource()).isSelectionVisible(); Action a = kit.getActionByName(BaseKit.cutAction); if (a != null) { a.setEnabled(selectionVisible); } a = kit.getActionByName(BaseKit.copyAction); if (a != null) { a.setEnabled(selectionVisible); } a = kit.getActionByName(BaseKit.removeSelectionAction); if (a != null) { a.setEnabled(selectionVisible); } } } } });
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { FindSupport findSupport = FindSupport.getFindSupport(); Caret caret = target.getCaret(); int dotPos = caret.getDot(); HashMap props = new HashMap(findSupport.getFindProperties()); String searchWord = null; if (caret.isSelectionVisible()) { // valid selection searchWord = target.getSelectedText(); props.put(SettingsNames.FIND_WHOLE_WORDS, Boolean.FALSE); } else { // no selection, get current word try { searchWord = Utilities.getIdentifier((BaseDocument) target.getDocument(), dotPos); props.put(SettingsNames.FIND_WHOLE_WORDS, Boolean.TRUE); } catch (BadLocationException e) { e.printStackTrace(); } } if (searchWord != null) { int n = searchWord.indexOf('\n'); if (n >= 0) searchWord = searchWord.substring(0, n); props.put(SettingsNames.FIND_WHAT, searchWord); findSupport.putFindProperties(props); findSupport.find(null, false); } } } }
/** Get the selection if there's any or get the identifier around * the position if there's no selection. */ public static String getSelectionOrIdentifier(JTextComponent c, int offset) throws BadLocationException { Document doc = c.getDocument(); Caret caret = c.getCaret(); String ret; if (caret.isSelectionVisible()) { ret = c.getSelectedText(); if (ret != null) return ret; } if (doc instanceof BaseDocument){ ret = getIdentifier((BaseDocument) doc, caret.getDot()); } else { ret = getWord(c, offset); } return ret; }
/** * Get the selection if there's any or get the identifier around the * position if there's no selection. */ public static String getSelectionOrIdentifier(JTextComponent c, int offset) throws BadLocationException { Document doc = c.getDocument(); Caret caret = c.getCaret(); String ret; if (caret.isSelectionVisible()) { ret = c.getSelectedText(); if (ret != null) return ret; } if (doc instanceof BaseDocument) { ret = getIdentifier((BaseDocument) doc, caret.getDot()); } else { ret = getWord(c, offset); } return ret; }
/** * Get the selection if there's any or get the identifier around the * position if there's no selection. * * @param c * component to work with * @param offset * position in document - usually the caret.getDot() * @return the block (starting and ending position) enclosing the identifier * or null if no identifier was found */ public static int[] getSelectionOrIdentifierBlock(JTextComponent c, int offset) throws BadLocationException { BaseDocument doc = (BaseDocument) c.getDocument(); Caret caret = c.getCaret(); int[] ret; if (caret.isSelectionVisible()) { ret = new int[] { c.getSelectionStart(), c.getSelectionEnd() }; } else { ret = getIdentifierBlock(doc, caret.getDot()); } return ret; }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { Caret caret = target.getCaret(); try { if (caret.isSelectionVisible()) { caret.setSelectionVisible(false); // unselect if // anything selected } else { // selection not visible int block[] = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot()); if (block != null) { caret.setDot(block[0]); caret.moveDot(block[1]); } } } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { Caret caret = target.getCaret(); try { if (caret.isSelectionVisible()) { caret.setSelectionVisible(false); // unselect if anything selected } else { // selection not visible int block[] = Utilities.getIdentifierBlock((BaseDocument)target.getDocument(), caret.getDot()); if (block != null) { caret.setDot(block[0]); caret.moveDot(block[1]); } } } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
/** Get the selection if there's any or get the identifier around * the position if there's no selection. * @param c component to work with * @param offset position in document - usually the caret.getDot() * @return the block (starting and ending position) enclosing the identifier * or null if no identifier was found */ public static int[] getSelectionOrIdentifierBlock(JTextComponent c, int offset) throws BadLocationException { Document doc = c.getDocument(); Caret caret = c.getCaret(); int[] ret; if (caret.isSelectionVisible()) { ret = new int[] { c.getSelectionStart(), c.getSelectionEnd() }; } else if (doc instanceof BaseDocument){ ret = getIdentifierBlock((BaseDocument)doc, caret.getDot()); } else { ret = getIdentifierBlock(c, offset); } return ret; }
public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { boolean active; if (mark != null) { active = mark.activateLayer; } else { JTextComponent c = ctx.getEditorUI().getComponent(); active = (c != null) && c.getCaret().isSelectionVisible() && ctx.getFragmentOffset() >= c.getSelectionStart() && ctx.getFragmentOffset() < c.getSelectionEnd(); } return active; }
public boolean isActive(DrawContext ctx, MarkFactory.DrawMark mark) { boolean active; if (mark != null) { active = mark.activateLayer; } else { JTextComponent c = ctx.getEditorUI().getComponent(); active = (c != null) && c.getCaret().isSelectionVisible() && ctx.getFragmentOffset() >= c.getSelectionStart() && ctx.getFragmentOffset() < c.getSelectionEnd(); } return active; }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument)target.getDocument(); if (caret.isSelectionVisible()) { // valid selection int startPos = target.getSelectionStart(); int endPos = target.getSelectionEnd(); Utilities.changeCase(doc, startPos, endPos - startPos, changeCaseMode); caret.setSelectionVisible(false); caret.setDot(endPos); } else { // no selection - change current char int dotPos = caret.getDot(); Utilities.changeCase(doc, dotPos, 1, changeCaseMode); caret.setDot(dotPos + 1); } } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = (BaseDocument) target.getDocument(); if (caret.isSelectionVisible()) { // valid selection int startPos = target.getSelectionStart(); int endPos = target.getSelectionEnd(); Utilities.changeCase(doc, startPos, endPos - startPos, changeCaseMode); caret.setSelectionVisible(false); caret.setDot(endPos); } else { // no selection - change current char int dotPos = caret.getDot(); Utilities.changeCase(doc, dotPos, 1, changeCaseMode); caret.setDot(dotPos + 1); } } catch (BadLocationException e) { target.getToolkit().beep(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { Caret caret = target.getCaret(); try { int pos; if (!select && caret.isSelectionVisible()) { pos = target.getSelectionEnd(); if (pos != caret.getDot()) pos--; } else pos = caret.getDot(); int dot = target.getUI().getNextVisualPositionFrom(target, pos, null, SwingConstants.EAST, null); if (select) { caret.moveDot(dot); } else { caret.setDot(dot); } } catch (BadLocationException ex) { target.getToolkit().beep(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { Caret caret = target.getCaret(); try { int pos; if (!select && caret.isSelectionVisible()) { pos = target.getSelectionEnd(); if (pos != caret.getDot()) pos--; } else pos = caret.getDot(); int dot = target.getUI().getNextVisualPositionFrom(target, pos, Position.Bias.Forward, SwingConstants.EAST, null); if (select) { caret.moveDot(dot); } else { caret.setDot(dot); } } catch (BadLocationException ex) { target.getToolkit().beep(); } } } }
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = Utilities.getDocument(target); if (caret.isSelectionVisible()) { doc.getFormatter().changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), right ? +1 : -1); } else { doc.getFormatter().shiftLine(doc, caret.getDot(), right); } } catch (GuardedException e) { target.getToolkit().beep(); } catch (BadLocationException e) { e.printStackTrace(); } } } }
try { int pos; if (!select && caret.isSelectionVisible()) { pos = target.getSelectionStart(); if (pos != caret.getDot())
public void actionPerformed(ActionEvent evt, JTextComponent target) { if (target != null) { if (!target.isEditable() || !target.isEnabled()) { target.getToolkit().beep(); return; } try { Caret caret = target.getCaret(); BaseDocument doc = Utilities.getDocument(target); if (caret.isSelectionVisible()) { doc.getFormatter().changeBlockIndent(doc, target.getSelectionStart(), target.getSelectionEnd(), right ? +1 : -1); } else { doc.getFormatter().shiftLine(doc, caret.getDot(), right); } } catch (GuardedException e) { target.getToolkit().beep(); } catch (BadLocationException e) { e.printStackTrace(); } } } }
/** * Invalidates the scanner, to make sure a new range is set later. * * @param g * the graphics context. * @param a * the shape. * @see View#paint(Graphics g, Shape a) */ public void paint(Graphics g, Shape a) { JTextComponent component = (JTextComponent) getContainer(); Highlighter highlighter = component.getHighlighter(); Color unselected = component.isEnabled() ? component.getForeground() : component.getDisabledTextColor(); Caret caret = component.getCaret(); selected = !caret.isSelectionVisible() || highlighter == null ? unselected : component.getSelectedTextColor(); super.paint(g, a); scanner.setValid(false); }
public @Override void run() { try { Caret caret = target.getCaret(); int startPos; int endPos; if (caret.isSelectionVisible()) { startPos = Utilities.getRowStart(doc, target.getSelectionStart()); endPos = target.getSelectionEnd(); if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos && endPos > startPos) { endPos--; } endPos = Utilities.getRowEnd(doc, endPos); } else { // selection not visible startPos = Utilities.getRowStart(doc, caret.getDot()); endPos = Utilities.getRowEnd(doc, caret.getDot()); } int lineCount = Utilities.getRowCount(doc, startPos, endPos); boolean comment = forceComment != null ? forceComment : !allComments(doc, startPos, lineCount); if (comment) { comment(doc, startPos, lineCount); } else { uncomment(doc, startPos, lineCount); } } catch (BadLocationException e) { target.getToolkit().beep(); } } });
@Override public void paint(Graphics g, Shape a) { ((Graphics2D) g).addRenderingHints(getHints()); Container container = getContainer(); if (container instanceof JTextComponent) { final JTextComponent textComp = (JTextComponent) container; selStart = textComp.getSelectionStart(); selEnd = textComp.getSelectionEnd(); unselectedFg = textComp.isEnabled() ? textComp.getForeground() : textComp.getDisabledTextColor(); selectedFg = textComp.getCaret().isSelectionVisible() ? textComp.getSelectedTextColor() : unselectedFg; } super.paint(g, a); }