当前位置: 首页>>代码示例>>Java>>正文


Java JTextComponent.getCaretPosition方法代码示例

本文整理汇总了Java中javax.swing.text.JTextComponent.getCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.getCaretPosition方法的具体用法?Java JTextComponent.getCaretPosition怎么用?Java JTextComponent.getCaretPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.text.JTextComponent的用法示例。


在下文中一共展示了JTextComponent.getCaretPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getCurrentParameterHighlight

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
 * Returns the highlight of the current parameter.
 *
 * @return The current parameter's highlight, or <code>null</code> if
 *         the caret is not in a parameter's bounds.
 * @see #getCurrentParameterStartOffset()
 */
private Highlight getCurrentParameterHighlight() {

	JTextComponent tc = ac.getTextComponent();
	int dot = tc.getCaretPosition();
	if (dot>0) {
		dot--; // Workaround for Java Highlight issues
	}

	List<Highlight> paramHighlights = getParameterHighlights();
	for (Highlight h : paramHighlights) {
		if (dot>=h.getStartOffset() && dot<h.getEndOffset()) {
			return h;
		}
	}

	return null;

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:26,代码来源:ParameterizedCompletionContext.java

示例2: reindent

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void reindent(JTextComponent component) {
    final BaseDocument doc = (BaseDocument) component.getDocument();
    final int dotPos = component.getCaretPosition();
    final Indent indent = Indent.get(doc);
    indent.lock();
    try {
        doc.runAtomic(new Runnable() {

            @Override
            public void run() {
                try {
                    int startOffset = Utilities.getRowStart(doc, dotPos);
                    int endOffset = Utilities.getRowEnd(doc, dotPos);
                    indent.reindent(startOffset, endOffset);
                } catch (BadLocationException ex) {
                    //ignore
                    }
            }
        });
    } finally {
        indent.unlock();
    }

}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:EndTagResultItem.java

示例3: TrailingWhitespaceRemoveProcessor

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public TrailingWhitespaceRemoveProcessor(Document doc, boolean removeFromModifiedLinesOnly, AtomicBoolean canceled) {
    this.doc = doc;
    this.removeFromModifiedLinesOnly = removeFromModifiedLinesOnly;
    this.canceled = canceled;
    this.docText = DocumentUtilities.getText(doc); // Persists for doc's lifetime
    lineRootElement = DocumentUtilities.getParagraphRootElement(doc);
    modRootElement = ModRootElement.get(doc);
    JTextComponent lastFocusedComponent = EditorRegistry.lastFocusedComponent();
    if (lastFocusedComponent != null && lastFocusedComponent.getDocument() == doc && !REMOVE_WHITESPACE_ON_CURRENT_LINE) {
        int caretOffset = lastFocusedComponent.getCaretPosition();
        caretLineIndex = lineRootElement.getElementIndex(caretOffset);
        // Assign the relativeCaretOffset since the subsequent modifications
        // done by physical whitespace removal would make the absolute offsets unusable.
        caretRelativeOffset = caretOffset - lineRootElement.getElement(caretLineIndex).getStartOffset();
    } else {
        caretLineIndex = -1;
        caretRelativeOffset = 0;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:20,代码来源:TrailingWhitespaceRemoveProcessor.java

示例4: isEnabled

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public boolean isEnabled() {
    JTextComponent comp = getTextComponent(null);
    if (comp == null)
        return false;
    ASTNode node = getASTNode(comp);
    if (node == null)
        return false;
    int position = comp.getCaretPosition();
    ASTPath path = node.findPath(position);
    if (path == null)
        return false;
    DatabaseContext root = DatabaseManager.getRoot((ASTNode) path.getRoot());
    if (root == null)
        return false;
    DatabaseItem item = root.getDatabaseItem (path.getLeaf ().getOffset ());
    return item != null;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:GoToDeclarationAction.java

示例5: reindent

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private void reindent(JTextComponent component) {

        final BaseDocument doc = (BaseDocument) component.getDocument();
        final int dotPos = component.getCaretPosition();
        final Indent indent = Indent.get(doc);
        indent.lock();
        try {
            doc.runAtomic(new Runnable() {
                @Override
                public void run() {
                    try {
                        int startOffset = Utilities.getRowStart(doc, dotPos);
                        int endOffset = Utilities.getRowEnd(doc, dotPos);
                        indent.reindent(startOffset, endOffset);
                    } catch (BadLocationException ex) {
                        //ignore
                    }
                }
            });
        } finally {
            indent.unlock();
        }

    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:25,代码来源:HtmlCompletionItem.java

示例6: previousCamelCasePosition

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static int previousCamelCasePosition(JTextComponent textComponent) {
    int offset = textComponent.getCaretPosition();

    // Are we at the beginning of the document?
    if (offset == 0) {
        return -1;
    }

    final Document doc = textComponent.getDocument();
    
    KeystrokeHandler bc = UiUtils.getBracketCompletion(doc, offset);
    if (bc != null) {
        int nextOffset = bc.getNextWordOffset(
                doc, offset, true);
        if (nextOffset != -1) {
            return nextOffset;
        }
    }
    
    try {
        return Utilities.getPreviousWord(textComponent, offset);
    } catch (BadLocationException ble) {
        ErrorManager.getDefault().notify(ble);
    }
    return -1;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:CamelCaseOperations.java

示例7: defaultAction

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void defaultAction(JTextComponent component) {
    if (component == null) {
        return;
    }
    Completion.get().hideAll();
    
    Document doc = component.getDocument();
    if (doc == null) {
        return;
    }
    int caret = component.getCaretPosition();
    int l = caret - fromOffset;
    try {
        doc.insertString(fromOffset, "/" + command + " ", null); // NOI18N
        doc.remove(fromOffset + command.length() + 2, l);
    } catch (BadLocationException ex) {
        // ignore
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:21,代码来源:CommandCompletionProvider.java

示例8: delegates

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
protected boolean delegates(JTextComponent target) {
    ConsoleModel model = ConsoleModel.get(target.getDocument());
    if (model == null) {
        return true;
    }
    ConsoleSection input = model.getInputSection();
    if (input == null) {
        return true;
    }
    int offset = target.getCaretPosition();
    if (offset < input.getPartBegin() || offset > input.getEnd()) {
        return true;
    }
    Document doc = target.getDocument();
    
    LineDocument ld = LineDocumentUtils.as(doc, LineDocument.class);
    if (ld == null) {
        return true;
    }
    try {
        int end = LineDocumentUtils.getLineEnd(ld, input.getPartBegin());
        return offset > end;
    } catch (BadLocationException ex) {
        return true;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:OverrideEditorActions.java

示例9: isFirstJavaLine

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static int isFirstJavaLine(JTextComponent component) {
    ShellSession s = ShellSession.get(component.getDocument());
    if (s == null) {
        return -1;
    }
    ConsoleSection sec = s.getModel().getInputSection();
    if (sec == null) {
        return -1;
    }
    LineDocument ld = LineDocumentUtils.as(component.getDocument(), LineDocument.class);
    if (ld == null) {
        return -1;
    }

    int off = sec.getStart();
    int caret = component.getCaretPosition();
    int s1 = LineDocumentUtils.getLineStart(ld, caret);
    int s2 = LineDocumentUtils.getLineStart(ld, off);
    try {
        return s1 == s2 ?
                component.getDocument().getText(sec.getPartBegin(), sec.getPartLen()).trim().length() 
                : -1;
    } catch (BadLocationException ex) {
        return 0;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:27,代码来源:HistoryCompletionProvider.java

示例10: enterAction

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
boolean enterAction() {
    TextSync textSync = activeTextSync();
    if (textSync != null) {
        TextRegion<?> master = textSync.validMasterRegion();
        JTextComponent component = component();
        if (master.startOffset() <= component.getCaretPosition() && component.getCaretPosition() <= master.endOffset()) {
            TextSyncGroup<?> group = textSync.group();
            activateTextSync(null, group,
                    findEditableTextSyncIndex(group, group.activeTextSyncIndex() + 1, +1, false, false),
                    true);
            return true;
        }
        releaseLastGroups(1);
    } else {
        // #145443 - I'm not sure why this is called when there is no active
        // TextSync, but apparently it happens under certain circumstances. So
        // let's try closing all active groups, which will terminate the special editing mode.
        releaseLastGroups(editGroups.size());
    }
    return false;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:22,代码来源:TextRegionManager.java

示例11: actionPerformed

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent e) {

	JTextComponent tc = ac.getTextComponent();
	int dot = tc.getCaretPosition();
	char end = pc.getProvider().getParameterListEnd();

	// Are they at or past the end of the parameters?
	if (dot>=maxPos.getOffset()-2) { // ">=" for overwrite mode

		// Try to decide if we're closing a paren that is a part
		// of the (last) arg being typed.
		String text = getArgumentText(dot);
		if (text!=null) {
			char start = pc.getProvider().getParameterListStart();
			int startCount = getCount(text, start);
			int endCount = getCount(text, end);
			if (startCount>endCount) { // Just closing a paren
				tc.replaceSelection(Character.toString(end));
				return;
			}
		}
		//tc.setCaretPosition(maxPos.getOffset());
		tc.setCaretPosition(Math.min(tc.getCaretPosition()+1,
				tc.getDocument().getLength()));

		deactivate();

	}

	// If not (in the middle of parameters), just insert the paren.
	else {
		tc.replaceSelection(Character.toString(end));
	}

}
 
开发者ID:Thecarisma,项目名称:powertext,代码行数:37,代码来源:ParameterizedCompletionContext.java

示例12: resetRectangularSelection

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static void resetRectangularSelection(JTextComponent c) {
    c.getCaretPosition();
    c.putClientProperty(RECTANGULAR_SELECTION_REGIONS_PROPERTY, new ArrayList<Position>());
    boolean value = !isRectangularSelection(c);
    RectangularSelectionUtils.setRectangularSelection(c, Boolean.valueOf(value) );
    RectangularSelectionUtils.setRectangularSelection(c, Boolean.valueOf(!value));
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:8,代码来源:RectangularSelectionUtils.java

示例13: navigateToOccurence

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static void navigateToOccurence(boolean next, JTextComponent txt) {
    if (txt != null && txt.getDocument() != null) {
        Document doc = txt.getDocument();
        int position = txt.getCaretPosition();
        int goTo = findOccurrencePosition(next, doc, position);
        if (goTo > 0) {
            txt.setCaretPosition(goTo);
            doc.putProperty(markedOccurence, new long[] {DocumentUtilities.getDocumentVersion(doc), goTo});
        } else {
            StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "java-no-marked-occurrence"));
            doc.putProperty(markedOccurence, null);
        }
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:15,代码来源:GoToMarkOccurrencesAction.java

示例14: getLineNumberForIndex

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
static int getLineNumberForIndex(final Accessible a, int index) {
    final Accessible sa = CAccessible.getSwingAccessible(a);
    if (!(sa instanceof JTextComponent)) return -1;

    final JTextComponent jc = (JTextComponent) sa;
    final Element root = jc.getDocument().getDefaultRootElement();

    // treat -1 special, returns the current caret position
    if (index == -1) index = jc.getCaretPosition();

    // Determine line number (can be -1)
    return root.getElementIndex(index);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:CAccessibleText.java

示例15: navigateToOccurence

import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
private static void navigateToOccurence(boolean next, JTextComponent txt) {
    if (txt != null && txt.getDocument() != null) {
        Document doc = txt.getDocument();
        int position = txt.getCaretPosition();
        int goTo = findOccurrencePosition(next, doc, position);
        if (goTo > 0) {
            txt.setCaretPosition(goTo);
        } else {
            StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(GoToMarkOccurrencesAction.class, "csl-no-marked-occurrence"));
        }
    }

}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:14,代码来源:GoToMarkOccurrencesAction.java


注:本文中的javax.swing.text.JTextComponent.getCaretPosition方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。