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

The following examples show how to use javax.swing.text.JTextComponent#isEnabled() . 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: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    final AtomicBoolean processedHere = new AtomicBoolean(false);
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled() || !(target.getDocument() instanceof BaseDocument)) {
            target.getToolkit().beep();
            return;
        }
        final Positions positions = Positions.create(target);
        final BaseDocument doc = (BaseDocument) target.getDocument();
        doc.runAtomic(new Runnable() {

            @Override
            public void run() {
                performCustomAction(doc, positions, processedHere);
            }
        });
        if (!processedHere.get()) {
            performDefaultAction(evt, target);
        }
    }
}
 
Example 2
Source File: ToggleBlockCommentAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled() || !(target.getDocument() instanceof BaseDocument)) {
            target.getToolkit().beep();
            return;
        }
        final int caretOffset = Utilities.isSelectionShowing(target) ? target.getSelectionStart() : target.getCaretPosition();
        final BaseDocument baseDocument = (BaseDocument) target.getDocument();
        final AtomicBoolean processedByLatte = new AtomicBoolean(false);
        baseDocument.runAtomic(new Runnable() {

            @Override
            public void run() {
                performLatteAction(baseDocument, caretOffset, processedByLatte);
            }
        });
        if (!processedByLatte.get()) {
            performDefaultAction(evt, target);
        }
    }
}
 
Example 3
Source File: DarkTextFieldUI.java    From darklaf with MIT License 5 votes vote down vote up
protected ClickAction getActionUnder(final Point p) {
    JTextComponent c = getComponent();
    if (!c.isEnabled()) return ClickAction.NONE;
    if (isOver(getRightIconCoord(), getRightIcon(c), p) && doPaintRightIcon(c)) {
        return ClickAction.RIGHT_ACTION;
    }
    if (isOver(getLeftIconCoord(), getLeftIcon(c), p) && doPaintLeftIcon(c)) {
        return ClickAction.LEFT_ACTION;
    }
    return ClickAction.NONE;
}
 
Example 4
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 5
Source File: InsertSemicolonAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent evt, final JTextComponent target) {
    if (target.isEditable() && target.isEnabled()) {
        final BaseDocument doc = (BaseDocument) target.getDocument();
        final Indent indenter = Indent.get(doc);
        final class R implements Runnable {

            @Override
            public void run() {
                try {
                    Caret caret = target.getCaret();
                    int caretPosition = caret.getDot();
                    int eolOffset = Utilities.getRowEnd(target, caretPosition);
                    doc.insertString(eolOffset, SEMICOLON, null);
                    newLineProcessor.processNewLine(eolOffset, caret, indenter);
                } catch (BadLocationException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        indenter.lock();
        try {
            doc.runAtomicAsUser(new R());
        } finally {
            indenter.unlock();
        }
    }
}
 
Example 6
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 7
Source File: TextComponentPopupMenu.java    From finalspeed 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: AquaTextFieldBorder.java    From jdk8u_jdk 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 9
Source File: AquaTextFieldBorder.java    From openjdk-jdk8u 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: 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 11
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 12
Source File: AquaTextFieldBorder.java    From dragonwell8_jdk 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 13
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 14
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 15
Source File: FlatCaret.java    From FlatLaf with Apache License 2.0 5 votes vote down vote up
protected void selectAllOnFocusGained() {
	JTextComponent c = getComponent();
	Document doc = c.getDocument();
	if( doc == null || !c.isEnabled() || !c.isEditable() )
		return;

	Object selectAllOnFocusPolicy = c.getClientProperty( SELECT_ALL_ON_FOCUS_POLICY );
	if( selectAllOnFocusPolicy == null )
		selectAllOnFocusPolicy = this.selectAllOnFocusPolicy;

	if( SELECT_ALL_ON_FOCUS_POLICY_NEVER.equals( selectAllOnFocusPolicy ) )
		return;

	if( !SELECT_ALL_ON_FOCUS_POLICY_ALWAYS.equals( selectAllOnFocusPolicy ) ) {
		// policy is "once" (or null or unknown)

		// was already focused?
		if( wasFocused )
			return;

		// check whether selection was modified before gaining focus
		int dot = getDot();
		int mark = getMark();
		if( dot != mark || dot != doc.getLength() )
			return;
	}

	// select all
	if( c instanceof JFormattedTextField ) {
		EventQueue.invokeLater( () -> {
			setDot( 0 );
			moveDot( doc.getLength() );
		} );
	} else {
		setDot( 0 );
		moveDot( doc.getLength() );
	}
}
 
Example 16
Source File: JavaKit.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }
        BaseDocument doc = (BaseDocument)target.getDocument();
        StringBuffer sb = new StringBuffer("System.err.println(\""); // NOI18N
        File file = (File)doc.getProperty( "file" );
        if (file != null) {
            sb.append( file.getAbsolutePath() );
            sb.append(':');
        }
        try {
            sb.append(Utilities.getLineOffset(doc, target.getCaret().getDot()) + 1);
        } catch (BadLocationException e) {
        }
        sb.append(' ');

        BaseKit kit = Utilities.getKit(target);
        Action a = kit.getActionByName(BaseKit.insertContentAction);
        if (a != null) {
            Utilities.performAction(
                a,
                new ActionEvent(target, ActionEvent.ACTION_PERFORMED, sb.toString()),
                target
            );
        }
    }
}
 
Example 17
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 18
Source File: ExtKit.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed(ActionEvent evt, JTextComponent target) {
    if (target != null) {
        if (!target.isEditable() || !target.isEnabled()) {
            target.getToolkit().beep();
            return;
        }

        BaseDocument doc = (BaseDocument)target.getDocument();
        int dotPos = target.getCaret().getDot();
        try {
            // look for identifier around caret
            int[] block = org.netbeans.editor.Utilities.getIdentifierBlock(doc, dotPos);

            // If there is no identifier around, warn user
            if (block == null) {
                target.getToolkit().beep();
                return;
            }

            // Get the identifier to operate on
            CharSequence identifier = DocumentUtilities.getText(doc, block[0], block[1] - block[0]);

            // Handle the case we already have the work done - e.g. if we got called over 'getValue'
            if (CharSequenceUtilities.startsWith(identifier, prefix) && 
                    Character.isUpperCase(identifier.charAt(prefix.length()))) return;

            // Handle the case we have other type of known xEr: eg isRunning -> getRunning
            for (int i=0; i<prefixGroup.length; i++) {
                String actPref = prefixGroup[i];
                if (CharSequenceUtilities.startsWith(identifier, actPref)
                        && identifier.length() > actPref.length()
                        && Character.isUpperCase(identifier.charAt(actPref.length()))
                   ) {
                    doc.remove(block[0], actPref.length());
                    doc.insertString(block[0], prefix, null);
                    return;
                }
            }

            // Upcase the first letter
            Utilities.changeCase(doc, block[0], 1, Utilities.CASE_UPPER);
            // Prepend the prefix before it
            doc.insertString(block[0], prefix, null);
        } catch (BadLocationException e) {
            target.getToolkit().beep();
        }
    }
}
 
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: DarkTextFieldUI.java    From darklaf with MIT License 4 votes vote down vote up
protected Icon getSearchIcon(final JTextComponent c) {
    boolean enabled = c.isEnabled();
    return isSearchFieldWithHistoryPopup(c)
            ? enabled ? searchWithHistory : searchWithHistoryDisabled
            : enabled ? search : searchDisabled;
}