本文整理了Java中javax.swing.text.Caret.isSelectionVisible()
方法的一些代码示例,展示了Caret.isSelectionVisible()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Caret.isSelectionVisible()
方法的具体详情如下:
包路径:javax.swing.text.Caret
类名称:Caret
方法名:isSelectionVisible
暂无
代码示例来源:origin: net.java.abeille/abeille
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);
}
}
}
}
});
代码示例来源:origin: net.java.abeille/abeille
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);
}
}
}
}
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
/** 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;
}
代码示例来源:origin: net.java.abeille/abeille
/**
* 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;
}
代码示例来源:origin: net.java.abeille/abeille
/**
* 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;
}
代码示例来源:origin: net.java.abeille/abeille
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();
}
}
}
}
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
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();
}
}
}
}
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
/** 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;
}
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
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;
}
代码示例来源:origin: net.java.abeille/abeille
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;
}
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
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();
}
}
}
}
代码示例来源:origin: net.java.abeille/abeille
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();
}
}
}
}
代码示例来源:origin: net.java.abeille/abeille
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();
}
}
}
}
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
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();
}
}
}
}
代码示例来源:origin: net.java.abeille/abeille
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();
}
}
}
}
代码示例来源:origin: net.java.abeille/abeille
try {
int pos;
if (!select && caret.isSelectionVisible()) {
pos = target.getSelectionStart();
if (pos != caret.getDot())
代码示例来源:origin: net.sf.squirrel-sql.thirdparty-non-maven/org-netbeans-modules-editor-lib
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();
}
}
}
}
代码示例来源:origin: nz.ac.waikato.cms.weka.thirdparty/bounce
/**
* 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);
}
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-ruby-rhtml
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();
}
}
});
代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-gsf-testrunner-ui
@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);
}
内容来源于网络,如有侵权,请联系作者删除!