Java Code Examples for javax.swing.text.JTextComponent#isEditable()

The following examples show how to use javax.swing.text.JTextComponent#isEditable() . 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: CutCommand.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void adjust() {
    boolean   enable = false;
    Component comp   = getFocusOwner();
    if (comp instanceof JTextComponent && comp.isEnabled()) {
        JTextComponent textComp = (JTextComponent) comp;
        if (textComp.isEditable()) {
            enable = textComp.getSelectionStart() != textComp.getSelectionEnd();
        }
    } else {
        Cutable cutable = getTarget(Cutable.class);
        if (cutable != null) {
            enable = cutable.canCutSelection();
        }
    }
    setEnabled(enable);
}
 
Example 2
Source File: PasteCommand.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void adjust() {
    boolean   enable = false;
    Component comp   = getFocusOwner();
    if (comp instanceof JTextComponent && comp.isEnabled()) {
        JTextComponent textComp = (JTextComponent) comp;
        if (textComp.isEditable()) {
            try {
                enable = comp.getToolkit().getSystemClipboard().isDataFlavorAvailable(DataFlavor.stringFlavor);
            } catch (Exception exception) {
                Log.warn(exception);
            }
        }
    } else {
        Pastable pastable = getTarget(Pastable.class);
        if (pastable != null) {
            enable = pastable.canPasteSelection();
        }
    }
    setEnabled(enable);
}
 
Example 3
Source File: TextActions.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
	JTextComponent target = getTextComponent(e);
	boolean beep = true;
	if ((target != null) && (target.isEditable())) {
		try {
			Document doc = target.getDocument();
			int ss = 0;
			int se = doc.getLength();

			if (ss != se) {
				doc.remove(ss, se - ss);
				beep = false;
			}
		} catch (BadLocationException bl) {
		}
	}
	if (beep) {
		UIManager.getLookAndFeel().provideErrorFeedback(target);
	}
}
 
Example 4
Source File: ActionFactory.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent evt, final JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }

        final BaseDocument doc = (BaseDocument)target.getDocument();
        doc.runAtomicAsUser (new Runnable () {
            public void run () {
                DocumentUtilities.setTypingModification(doc, true);
                try {
                    target.replaceSelection(null);
                } finally {
                    DocumentUtilities.setTypingModification(doc, false);
                }
            }
        });
    }
}
 
Example 5
Source File: CamelCaseOperations.java    From netbeans with Apache License 2.0 6 votes vote down vote up
static void replaceText(JTextComponent textComponent, final int offset, final int length, final String text) {
    if (!textComponent.isEditable()) {
        return;
    }
    final Document document = textComponent.getDocument();
    Runnable r = new Runnable() {
        public @Override void run() {
            try {
                if (length > 0) {
                    document.remove(offset, length);
                }
                document.insertString(offset, text, null);
            } catch (BadLocationException ble) {
                ErrorManager.getDefault().notify(ble);
            }
        }
    };
    if (document instanceof BaseDocument) {
        ((BaseDocument)document).runAtomic(r);
    } else {
        r.run();
    }
}
 
Example 6
Source File: AquaTextFieldBorder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
protected static State getStateFor(final JTextComponent jc) {
    if (!AquaFocusHandler.isActive(jc)) {
        return State.INACTIVE;
    }

    if (!jc.isEnabled()) {
        return State.DISABLED;
    }

    if (!jc.isEditable()) {
        return State.DISABLED;
    }

    return State.ACTIVE;
}
 
Example 7
Source File: TextComponentPopupMenu.java    From finalspeed-91yun with GNU General Public License v2.0 5 votes vote down vote up
public void show(Component invoker, int x, int y) {
	JTextComponent tc = (JTextComponent) invoker;
	String sel = tc.getSelectedText();

	boolean selected = sel != null && !sel.equals("");
	boolean enableAndEditable = tc.isEnabled() && tc.isEditable();

	cutItem.setEnabled(selected && enableAndEditable);
	copyItem.setEnabled(selected && tc.isEnabled());
	deleteItem.setEnabled(selected && enableAndEditable);
	pasteItem.setEnabled(enableAndEditable);
	selectAllItem.setEnabled(tc.isEnabled());

	super.show(invoker, x, y);
}
 
Example 8
Source File: InsertTabAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(final ActionEvent evt, final JTextComponent target) {
    BaseKit.InsertTabAction insertTabAction = new BaseKit.InsertTabAction();
    insertTabAction.actionPerformed(evt, target);
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }
        final Caret caret = target.getCaret();
        final BaseDocument doc = (BaseDocument) target.getDocument();
        doc.runAtomic(new TabReplacer(doc, caret.getDot()));
    }
}
 
Example 9
Source File: AquaTextFieldBorder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected static State getStateFor(final JTextComponent jc) {
    if (!AquaFocusHandler.isActive(jc)) {
        return State.INACTIVE;
    }

    if (!jc.isEnabled()) {
        return State.DISABLED;
    }

    if (!jc.isEditable()) {
        return State.DISABLED;
    }

    return State.ACTIVE;
}
 
Example 10
Source File: ActionFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(final ActionEvent evt, final JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }

        final Caret caret = target.getCaret();
        final BaseDocument doc = (BaseDocument)target.getDocument();
        doc.runAtomicAsUser (new Runnable () {
            public void run () {
                DocumentUtilities.setTypingModification(doc, true);
                try {
                    int dotPos = caret.getDot();
                    int bolPos = Utilities.getRowStart(doc, dotPos);
                    int wsPos = Utilities.getPreviousWord(target, dotPos);
                    wsPos = (dotPos == bolPos) ? wsPos : Math.max(bolPos, wsPos);
                    doc.remove(wsPos, dotPos - wsPos);
                } catch (BadLocationException e) {
                    target.getToolkit().beep();
                } finally {
                    DocumentUtilities.setTypingModification(doc, false);
                }
            }
        });
    }
}
 
Example 11
Source File: XMLKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target == null) return;
    if (!target.isEditable() || !target.isEnabled()) {
        problem(null);
        return;
    }
    Caret caret = target.getCaret();
    BaseDocument doc = (BaseDocument)target.getDocument();
    try {
        doc.dump(System.out);    
        if (target == null)  throw new BadLocationException(null,0);  // folish compiler
    } catch (BadLocationException e) {
        problem(null);
    }
}
 
Example 12
Source File: ActionFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed (final ActionEvent evt, final JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }

        final Caret caret = target.getCaret();
        final BaseDocument doc = (BaseDocument)target.getDocument();
        doc.runAtomicAsUser (new Runnable () {
            public void run () {
                DocumentUtilities.setTypingModification(doc, true);
                try {
                    int dotPos = caret.getDot();
                    int bolPos = Utilities.getRowStart(doc, dotPos);
                    if (dotPos == bolPos) { // at begining of the line
                        if (dotPos > 0) {
                            doc.remove(dotPos - 1, 1); // remove previous new-line
                        }
                    } else { // not at the line begining
                        char[] chars = doc.getChars(bolPos, dotPos - bolPos);
                        if (Analyzer.isWhitespace(chars, 0, chars.length)) {
                            doc.remove(bolPos, dotPos - bolPos); // remove whitespace
                        } else {
                            int firstNW = Utilities.getRowFirstNonWhite(doc, bolPos);
                            if (firstNW >= 0 && firstNW < dotPos) {
                                doc.remove(firstNW, dotPos - firstNW);
                            }
                        }
                    }
                } catch (BadLocationException e) {
                    target.getToolkit().beep();
                } finally {
                    DocumentUtilities.setTypingModification(doc, false);
                }
            }
        });
    }
}
 
Example 13
Source File: AquaTextFieldBorder.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
protected static State getStateFor(final JTextComponent jc) {
    if (!AquaFocusHandler.isActive(jc)) {
        return State.INACTIVE;
    }

    if (!jc.isEnabled()) {
        return State.DISABLED;
    }

    if (!jc.isEditable()) {
        return State.DISABLED;
    }

    return State.ACTIVE;
}
 
Example 14
Source File: TextComponentPopupMenu.java    From xtunnel with GNU General Public License v2.0 5 votes vote down vote up
public void show(Component invoker, int x, int y) {
	JTextComponent tc = (JTextComponent) invoker;
	String sel = tc.getSelectedText();

	boolean selected = sel != null && !sel.equals("");
	boolean enableAndEditable = tc.isEnabled() && tc.isEditable();

	cutItem.setEnabled(selected && enableAndEditable);
	copyItem.setEnabled(selected && tc.isEnabled());
	deleteItem.setEnabled(selected && enableAndEditable);
	pasteItem.setEnabled(enableAndEditable);
	selectAllItem.setEnabled(tc.isEnabled());

	super.show(invoker, x, y);
}
 
Example 15
Source File: FlatEditorPaneUI.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
@Override
protected void paintBackground( Graphics g ) {
	JTextComponent c = getComponent();

	// for compatibility with IntelliJ themes
	if( isIntelliJTheme && (!c.isEnabled() || !c.isEditable()) && (c.getBackground() instanceof UIResource) ) {
		FlatUIUtils.paintParentBackground( g, c );
		return;
	}

	super.paintBackground( g );
}
 
Example 16
Source File: XMLKit.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target == null) return;
    if (!target.isEditable() || !target.isEnabled()) {
        problem(null);
        return;
    }
    Caret caret = target.getCaret();
    BaseDocument doc = (BaseDocument)target.getDocument();
    try {
        if (caret.isSelectionVisible()) {
            int startPos = Utilities.getRowStart(doc, target.getSelectionStart());
            int endPos = target.getSelectionEnd();
            doc.atomicLock();
            try {

                if (endPos > 0 && Utilities.getRowStart(doc, endPos) == endPos) {
                    endPos--;
                }

                int pos = startPos;
                int lineCnt = Utilities.getRowCount(doc, startPos, endPos);                            

                for (;lineCnt > 0; lineCnt--) {
                    doc.insertString(pos, commentStartString, null); 
                    doc.insertString(Utilities.getRowEnd(doc,pos), commentEndString, null);
                    pos = Utilities.getRowStart(doc, pos, +1);
                }

            } finally {
                doc.atomicUnlock();
            }
        } else { // selection not visible
            doc.insertString(Utilities.getRowStart(doc, target.getSelectionStart()),
                commentStartString, null);
            doc.insertString(Utilities.getRowEnd(doc, target.getSelectionStart()),
                commentEndString, null);
        }
    } catch (BadLocationException e) {
        problem(null);
    }
}
 
Example 17
Source File: ActionFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
public void actionPerformed (final ActionEvent evt, final JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }
        final BaseDocument doc = (BaseDocument) target.getDocument();
        if (doc instanceof GuardedDocument && ((GuardedDocument) doc).isPosGuarded(target.getCaretPosition())) {
            target.getToolkit().beep();
            return;
        }
        doc.runAtomicAsUser (new Runnable () {
            @Override
            public void run () {
                DocumentUtilities.setTypingModification(doc, true);
                try {
                    Element rootElement = doc.getDefaultRootElement();

                    Caret caret = target.getCaret();
                    boolean selection = false;
                    boolean backwardSelection = false;
                    int start = target.getCaretPosition();
                    int end = start;

                    // check if there is a selection
                    if (Utilities.isSelectionShowing(caret)) {
                        int selStart = caret.getDot();
                        int selEnd = caret.getMark();
                        start = Math.min(selStart, selEnd);
                        end =   Math.max(selStart, selEnd) - 1;
                        selection = true;
                        backwardSelection = (selStart >= selEnd);
                    }

                    int zeroBaseStartLineNumber = rootElement.getElementIndex(start);
                    int zeroBaseEndLineNumber = rootElement.getElementIndex(end);

                    if (zeroBaseStartLineNumber == -1) {
                        // could not get line number
                        target.getToolkit().beep();
                    } else if (zeroBaseStartLineNumber == 0) {
                        // already first line
                    } else {
                        try {
                            // get line text
                            Element startLineElement = rootElement.getElement(zeroBaseStartLineNumber);
                            int startLineStartOffset = startLineElement.getStartOffset();

                            Element endLineElement = rootElement.getElement(zeroBaseEndLineNumber);
                            int endLineEndOffset = endLineElement.getEndOffset();

                            String linesText = doc.getText(startLineStartOffset, (endLineEndOffset - startLineStartOffset));

                            Element previousLineElement = rootElement.getElement(zeroBaseStartLineNumber - 1);
                            int previousLineStartOffset = previousLineElement.getStartOffset();

                            int column = start - startLineStartOffset;

                            // insert the text before the previous line
                            doc.insertString(previousLineStartOffset, linesText, null);
                            
                            // remove the line
                            if (endLineEndOffset + linesText.length() > doc.getLength()) {
                                removeLineByLine(doc, startLineStartOffset + linesText.length() - 1, endLineEndOffset - startLineStartOffset);
                            } else {
                                removeLineByLine(doc, startLineStartOffset + linesText.length(), endLineEndOffset - startLineStartOffset);
                            }
                            
                            if (selection) {
                                // select moved lines
                                if (backwardSelection) {
                                    caret.setDot(previousLineStartOffset + column);
                                    caret.moveDot(previousLineStartOffset + (endLineEndOffset - startLineStartOffset) - (endLineEndOffset - end - 1));
                                } else {
                                    caret.setDot(previousLineStartOffset + (endLineEndOffset - startLineStartOffset) - (endLineEndOffset - end - 1));
                                    caret.moveDot(previousLineStartOffset + column);
                                }
                            } else {
                                // set caret position
                                target.setCaretPosition(previousLineStartOffset + column);
                            }
                        } catch (BadLocationException ex) {
                            target.getToolkit().beep();
                        }
                    }
                } finally {
                    DocumentUtilities.setTypingModification(doc, false);
                }
            }
        });
    }
}
 
Example 18
Source File: ActionFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed (final ActionEvent evt, final JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }
        final BaseDocument doc = (BaseDocument) target.getDocument();
        doc.runAtomicAsUser (new Runnable () {
            public void run () {
                DocumentUtilities.setTypingModification(doc, true);
                try {
                    Element rootElement = doc.getDefaultRootElement();

                    Caret caret = target.getCaret();
                    boolean selection = false;
                    boolean backwardSelection = false;
                    int start = target.getCaretPosition();
                    int end = start;

                    // check if there is a selection
                    if (Utilities.isSelectionShowing(caret)) {
                        int selStart = caret.getDot();
                        int selEnd = caret.getMark();
                        start = Math.min(selStart, selEnd);
                        end =   Math.max(selStart, selEnd) - 1;
                        selection = true;
                        backwardSelection = (selStart >= selEnd);
                    }

                    int zeroBaseStartLineNumber = rootElement.getElementIndex(start);
                    int zeroBaseEndLineNumber = rootElement.getElementIndex(end);

                    if (zeroBaseStartLineNumber == -1) {
                        // could not get line number
                        target.getToolkit().beep();
                        return;
                    } else {
                        try {
                            // get line text
                            Element startLineElement = rootElement.getElement(zeroBaseStartLineNumber);
                            int startLineStartOffset = startLineElement.getStartOffset();

                            Element endLineElement = rootElement.getElement(zeroBaseEndLineNumber);
                            int endLineEndOffset = endLineElement.getEndOffset();

                            String linesText = doc.getText(startLineStartOffset, (endLineEndOffset - startLineStartOffset));

                            int column = start - startLineStartOffset;

                            try {
                                NavigationHistory.getEdits().markWaypoint(target, startLineStartOffset, false, true);
                            } catch (BadLocationException e) {
                                LOG.log(Level.WARNING, "Can't add position to the history of edits.", e); //NOI18N
                            }
                            // insert it
                            doc.insertString(startLineStartOffset, linesText, null);

                            if (selection) {
                                // select moved lines
                                if (backwardSelection) {
                                    caret.setDot(startLineStartOffset + column);
                                    caret.moveDot(startLineStartOffset + (endLineEndOffset - startLineStartOffset) - (endLineEndOffset - end - 1));
                                } else {
                                    caret.setDot(startLineStartOffset + (endLineEndOffset - startLineStartOffset) - (endLineEndOffset - end - 1));
                                    caret.moveDot(startLineStartOffset + column);
                                }
                            } else {
                                // set caret position
                                target.setCaretPosition(startLineStartOffset + column);
                            }
                        } catch (BadLocationException ex) {
                            target.getToolkit().beep();
                        }
                    }
                } finally {
                    DocumentUtilities.setTypingModification(doc, false);
                }
            }
        });
    }
}
 
Example 19
Source File: ActionFactory.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed (final ActionEvent evt, final JTextComponent target) {
    // shift-enter while editing aka startNewLineAction
    if (!target.isEditable() || !target.isEnabled()) {
        target.getToolkit().beep();
        return;
    }
    
    final BaseDocument doc = (BaseDocument)target.getDocument();
    
    doc.runAtomicAsUser (new Runnable () {
        public void run () {
        DocumentUtilities.setTypingModification(doc, true);
        try {
            ActionMap actionMap = target.getActionMap();
            Action cutAction;
            if (actionMap != null && (cutAction = actionMap.get(DefaultEditorKit.cutAction)) != null) {
                Caret caret = target.getCaret();
                int caretOffset = caret.getDot();
                boolean toLineEnd = BaseKit.cutToLineEndAction.equals(getValue(Action.NAME));
                int boundOffset = toLineEnd
                        ? Utilities.getRowEnd(target, caretOffset)
                        : Utilities.getRowStart(target, caretOffset);

                // Check whether there is only whitespace from caret position
                // till end of line
                if (toLineEnd) {
                    String text = target.getText(caretOffset, boundOffset - caretOffset);
                    if (boundOffset < doc.getLength() && text != null && text.matches("^[\\s]*$")) { // NOI18N
                        boundOffset += 1; // Include line separator
                    }
                }

                caret.moveDot(boundOffset);

                // Call the cut action to cut out the selection
                cutAction.actionPerformed(evt);
            }
        } catch (BadLocationException ex) {
            ex.printStackTrace();
        } finally{
            DocumentUtilities.setTypingModification(doc, false);
        }
        }
    });
}
 
Example 20
Source File: CamelCaseOperations.java    From netbeans with Apache License 2.0 4 votes vote down vote up
static void replaceChar(JTextComponent textComponent, int offset, char c) {
    if (!textComponent.isEditable()) {
        return;
    }
    replaceText(textComponent, offset, 1, String.valueOf(c));
}