本文整理汇总了Java中javax.swing.text.JTextComponent.getSelectedText方法的典型用法代码示例。如果您正苦于以下问题:Java JTextComponent.getSelectedText方法的具体用法?Java JTextComponent.getSelectedText怎么用?Java JTextComponent.getSelectedText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.JTextComponent
的用法示例。
在下文中一共展示了JTextComponent.getSelectedText方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getSelectionOrIdentifier
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/** 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 (Utilities.isSelectionShowing(caret)) {
ret = c.getSelectedText();
if (ret != null) return ret;
}
if (doc instanceof BaseDocument){
ret = getIdentifier((BaseDocument) doc, offset);
} else {
ret = getWord(c, offset);
}
return ret;
}
示例2: mouseClicked
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void mouseClicked(MouseEvent e) {
if (e.getModifiers() == InputEvent.BUTTON3_MASK) {
if (!(e.getSource() instanceof JTextComponent)) {
return;
}
textComponent = (JTextComponent) e.getSource();
textComponent.requestFocus();
boolean enabled = textComponent.isEnabled();
boolean editable = textComponent.isEditable();
boolean nonempty = !(textComponent.getText() == null || textComponent.getText().equals(""));
boolean marked = textComponent.getSelectedText() != null;
boolean pasteAvailable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null).isDataFlavorSupported(DataFlavor.stringFlavor);
undoAction.setEnabled(enabled && editable && (lastActionSelected == Actions.CUT || lastActionSelected == Actions.PASTE));
cutAction.setEnabled(enabled && editable && marked);
copyAction.setEnabled(enabled && marked);
pasteAction.setEnabled(enabled && editable && pasteAvailable);
selectAllAction.setEnabled(enabled && nonempty);
int nx = e.getX();
if (nx > 500) {
nx = nx - popup.getSize().width;
}
popup.show(e.getComponent(), nx, e.getY() - popup.getSize().height);
}
}
示例3: getCurrentSelectionEndLocation
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static CodeProfilingPoint.Location getCurrentSelectionEndLocation(int lineOffset) {
EditorContext mostActiveContext = EditorSupport.getMostActiveJavaEditorContext();
if (mostActiveContext == null) {
return CodeProfilingPoint.Location.EMPTY;
}
FileObject mostActiveJavaSource = mostActiveContext.getFileObject();
if (mostActiveJavaSource == null) {
return CodeProfilingPoint.Location.EMPTY;
}
JTextComponent mostActiveTextComponent = mostActiveContext.getTextComponent();
if (mostActiveTextComponent.getSelectedText() == null) {
return CodeProfilingPoint.Location.EMPTY;
}
String fileName = FileUtil.toFile(mostActiveJavaSource).getAbsolutePath();
int lineNumber = EditorSupport.getLineForOffset(mostActiveJavaSource, mostActiveTextComponent.getSelectionEnd()) + 1;
if (lineNumber == -1) {
lineNumber = 1;
}
return new CodeProfilingPoint.Location(fileName, lineNumber,
lineOffset /* TODO: get real line offset if lineOffset isn't OFFSET_START nor OFFSET_END */);
}
示例4: getCurrentSelectionStartLocation
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static CodeProfilingPoint.Location getCurrentSelectionStartLocation(int lineOffset) {
EditorContext mostActiveContext = EditorSupport.getMostActiveJavaEditorContext();
if (mostActiveContext == null) {
return CodeProfilingPoint.Location.EMPTY;
}
FileObject mostActiveJavaSource = mostActiveContext.getFileObject();
if (mostActiveJavaSource == null) {
return CodeProfilingPoint.Location.EMPTY;
}
JTextComponent mostActiveTextComponent = mostActiveContext.getTextComponent();
if (mostActiveTextComponent.getSelectedText() == null) {
return CodeProfilingPoint.Location.EMPTY;
}
String fileName = FileUtil.toFile(mostActiveJavaSource).getAbsolutePath();
int lineNumber = EditorSupport.getLineForOffset(mostActiveJavaSource, mostActiveTextComponent.getSelectionStart()) + 1;
if (lineNumber == -1) {
lineNumber = 1;
}
return new CodeProfilingPoint.Location(fileName, lineNumber,
lineOffset /* TODO: get real line offset if lineOffset isn't OFFSET_START nor OFFSET_END */);
}
示例5: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
if (Utilities.isSelectionShowing(caret)) {
EditorFindSupport findSupport = EditorFindSupport.getInstance();
HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
String searchWord = target.getSelectedText();
int n = searchWord.indexOf('\n');
if (n >= 0) {
searchWord = searchWord.substring(0, n);
}
props.put(EditorFindSupport.FIND_WHAT, searchWord);
props.put(EditorFindSupport.ADD_MULTICARET, Boolean.TRUE);
EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N
findSupport.setFocusedTextComponent(eui.getComponent());
}
findSupport.putFindProperties(props);
findSupport.find(null, false);
props.put(EditorFindSupport.ADD_MULTICARET, Boolean.FALSE);
findSupport.putFindProperties(props);
} else {
try {
int[] identifierBlock = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot());
if (identifierBlock != null) {
caret.setDot(identifierBlock[0]);
caret.moveDot(identifierBlock[1]);
}
} catch (BadLocationException e) {
LOGGER.log(Level.WARNING, null, e);
}
}
}
}
示例6: show
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
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);
}
示例7: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public void actionPerformed(ActionEvent e) {
JTextComponent tc = (JTextComponent) getInvoker();
String sel = tc.getSelectedText();
if (e.getSource() == cutItem) {
tc.cut();
} else if (e.getSource() == copyItem) {
tc.copy();
} else if (e.getSource() == pasteItem) {
tc.paste();
} else if (e.getSource() == selectAllItem) {
tc.selectAll();
} else if (e.getSource() == deleteItem) {
Document doc = tc.getDocument();
int start = tc.getSelectionStart();
int end = tc.getSelectionEnd();
try {
Position p0 = doc.createPosition(start);
Position p1 = doc.createPosition(end);
if ((p0 != null) && (p1 != null)
&& (p0.getOffset() != p1.getOffset())) {
doc.remove(p0.getOffset(), p1.getOffset() - p0.getOffset());
}
} catch (BadLocationException be) {
}
}
}
示例8: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
public void actionPerformed(ActionEvent evt) {
if(searchDialog == null) {
Window parent = SwingUtilities.getWindowAncestor(DocumentEditor.this);
searchDialog =
(parent instanceof Dialog)
? new SearchDialog((Dialog)parent)
: new SearchDialog((Frame)parent);
searchDialog.pack();
searchDialog.setLocationRelativeTo(DocumentEditor.this);
searchDialog.setResizable(true);
MainFrame.getGuiRoots().add(searchDialog);
}
JTextComponent textPane = getTextComponent();
// if the user never gives the focus to the textPane then
// there will never be any selection in it so we force it
textPane.requestFocusInWindow();
// put the selection of the document into the search text field
if(textPane.getSelectedText() != null) {
searchDialog.patternTextField.setText(textPane.getSelectedText());
}
if(searchDialog.isVisible()) {
searchDialog.toFront();
} else {
searchDialog.setVisible(true);
}
searchDialog.patternTextField.selectAll();
searchDialog.patternTextField.requestFocusInWindow();
}
示例9: TextTransferable
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
TextTransferable(JTextComponent c, int start, int end) {
this.c = c;
Document doc = c.getDocument();
try {
p0 = doc.createPosition(start);
p1 = doc.createPosition(end);
plainData = c.getSelectedText();
} catch (BadLocationException ble) {
}
}
示例10: getCurrentSelectionLocations
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
public static CodeProfilingPoint.Location[] getCurrentSelectionLocations() {
EditorContext mostActiveContext = EditorSupport.getMostActiveJavaEditorContext();
if (mostActiveContext == null) {
return new CodeProfilingPoint.Location[0];
}
FileObject mostActiveJavaSource = mostActiveContext.getFileObject();
if (mostActiveJavaSource == null) {
return new CodeProfilingPoint.Location[0];
}
JTextComponent mostActiveTextComponent = mostActiveContext.getTextComponent();
if (mostActiveTextComponent.getSelectedText() == null) {
return new CodeProfilingPoint.Location[0];
}
File file = FileUtil.toFile(mostActiveJavaSource);
if (file == null) {
return new CodeProfilingPoint.Location[0]; // Most likely Java source
}
String fileName = file.getAbsolutePath();
int startLineNumber = EditorSupport.getLineForOffset(mostActiveJavaSource, mostActiveTextComponent.getSelectionStart()) + 1;
if (startLineNumber == -1) {
startLineNumber = 1;
}
// #211681
int endLineNumber = EditorSupport.getLineForOffset(mostActiveJavaSource, mostActiveTextComponent.getSelectionEnd() - 1) + 1;
endLineNumber = Math.max(startLineNumber, endLineNumber);
return new CodeProfilingPoint.Location[] {
new CodeProfilingPoint.Location(fileName, startLineNumber,
CodeProfilingPoint.Location.OFFSET_START /* TODO: get real line offset if lineOffset isn't OFFSET_START nor OFFSET_END */),
new CodeProfilingPoint.Location(fileName, endLineNumber,
CodeProfilingPoint.Location.OFFSET_END /* TODO: get real line offset if lineOffset isn't OFFSET_START nor OFFSET_END */)
};
}
示例11: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
EditorFindSupport findSupport = EditorFindSupport.getInstance();
Caret caret = target.getCaret();
int dotPos = caret.getDot();
HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
String searchWord = null;
boolean revert = false;
Boolean originalValue = null;
Map<String, Object> revertMap = (Map<String, Object>) props.get(EditorFindSupport.REVERT_MAP);
Boolean revertValue = revertMap != null ? (Boolean) revertMap.get(EditorFindSupport.FIND_WHOLE_WORDS) : null;
if (Utilities.isSelectionShowing(caret)) {
// valid selection
searchWord = target.getSelectedText();
originalValue = (Boolean) props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.FALSE);
if (Boolean.FALSE.equals(revertValue)) {
revertMap.remove(EditorFindSupport.FIND_WHOLE_WORDS);
} else {
revert = !Boolean.FALSE.equals(originalValue);
}
} else {
// no selection, get current word
try {
searchWord = Utilities.getIdentifier((BaseDocument) target.getDocument(), dotPos);
originalValue = (Boolean) props.put(EditorFindSupport.FIND_WHOLE_WORDS, Boolean.TRUE);
if (Boolean.TRUE.equals(revertValue)) {
revertMap.remove(EditorFindSupport.FIND_WHOLE_WORDS);
} else {
revert = !Boolean.TRUE.equals(originalValue);
}
} catch (BadLocationException e) {
LOGGER.log(Level.WARNING, null, e);
}
}
if (searchWord != null) {
int n = searchWord.indexOf('\n');
if (n >= 0) {
searchWord = searchWord.substring(0, n);
}
props.put(EditorFindSupport.FIND_WHAT, searchWord);
if (revert) {
revertMap = new HashMap<>();
revertMap.put(EditorFindSupport.FIND_WHOLE_WORDS, originalValue != null ? originalValue : Boolean.FALSE);
props.put(EditorFindSupport.REVERT_MAP, revertMap);
}
props.put(EditorFindSupport.FIND_BLOCK_SEARCH, Boolean.FALSE);
props.put(EditorFindSupport.FIND_BLOCK_SEARCH_START, null);
props.put(EditorFindSupport.FIND_BLOCK_SEARCH_END, null);
EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
if (eui.getComponent().getClientProperty("AsTextField") == null) {
//NOI18N
findSupport.setFocusedTextComponent(eui.getComponent());
}
findSupport.putFindProperties(props);
if (findSupport.find(null, false)) {
findSupport.addToHistory(new EditorFindSupport.SPW((String) props.get(EditorFindSupport.FIND_WHAT), (Boolean) props.get(EditorFindSupport.FIND_WHOLE_WORDS), (Boolean) props.get(EditorFindSupport.FIND_MATCH_CASE), (Boolean) props.get(EditorFindSupport.FIND_REG_EXP)));
}
}
}
}
示例12: actionPerformed
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void actionPerformed(ActionEvent evt, JTextComponent target) {
if (target != null) {
Caret caret = target.getCaret();
if (!Utilities.isSelectionShowing(caret)) {
try {
int[] identifierBlock = Utilities.getIdentifierBlock((BaseDocument) target.getDocument(), caret.getDot());
if (identifierBlock != null) {
caret.setDot(identifierBlock[0]);
caret.moveDot(identifierBlock[1]);
}
} catch (BadLocationException e) {
LOGGER.log(Level.WARNING, null, e);
}
}
EditorFindSupport findSupport = EditorFindSupport.getInstance();
HashMap<String, Object> props = new HashMap<>(findSupport.createDefaultFindProperties());
String searchWord = target.getSelectedText();
if (searchWord != null) {
int n = searchWord.indexOf('\n');
if (n >= 0) {
searchWord = searchWord.substring(0, n);
}
props.put(EditorFindSupport.FIND_WHAT, searchWord);
Document doc = target.getDocument();
EditorUI eui = org.netbeans.editor.Utilities.getEditorUI(target);
if (eui.getComponent().getClientProperty("AsTextField") == null) { //NOI18N
findSupport.setFocusedTextComponent(eui.getComponent());
}
findSupport.putFindProperties(props);
findSupport.find(null, false);
if (caret instanceof EditorCaret) {
EditorCaret editorCaret = (EditorCaret) caret;
try {
int[] blocks = findSupport.getBlocks(new int[]{-1, -1}, doc, 0, doc.getLength());
if (blocks[0] >= 0 && blocks.length % 2 == 0) {
List<Position> newCarets = new ArrayList<>();
for (int i = 0; i < blocks.length; i += 2) {
int start = blocks[i];
int end = blocks[i + 1];
if (start == -1 || end == -1) {
break;
}
Position startPos = doc.createPosition(start);
Position endPos = doc.createPosition(end);
newCarets.add(endPos);
newCarets.add(startPos);
}
editorCaret.replaceCarets(newCarets, null); // TODO handle biases
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
}
}
}
示例13: getPopup
import javax.swing.text.JTextComponent; //导入方法依赖的package包/类
/**
* Returns the popup menu.
* @param component the text component which calls the method
* @return a poup menu
*/
JPopupMenu getPopup(JTextComponent component) {
if(popupPresenter == null){
build(component.getActionMap());
}
boolean textSelected = component.getSelectedText() != null;
cutAction.setEnabled(textSelected);
copyAction.setEnabled(textSelected);
boolean hasClipboardText = Toolkit.getDefaultToolkit().getSystemClipboard()
.isDataFlavorAvailable(DataFlavor.stringFlavor);
pasteAction.setEnabled(hasClipboardText);
return popupPresenter;
}