本文整理汇总了Java中javax.swing.text.Utilities.getWordStart方法的典型用法代码示例。如果您正苦于以下问题:Java Utilities.getWordStart方法的具体用法?Java Utilities.getWordStart怎么用?Java Utilities.getWordStart使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Utilities
的用法示例。
在下文中一共展示了Utilities.getWordStart方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWordStart
import javax.swing.text.Utilities; //导入方法依赖的package包/类
protected int getWordStart(JTextComponent c, int pos)
{
try
{
return Utilities.getWordStart(c, pos);
}
catch(Exception e)
{ }
return -1;
}
示例2: setClickText
import javax.swing.text.Utilities; //导入方法依赖的package包/类
private void setClickText(MouseEvent evt) {
try {
int clickIndex = getHyperCardTextPane().viewToModel(evt.getPoint());
int startWordIndex = Utilities.getWordStart(getHyperCardTextPane(), clickIndex);
int endWordIndex = Utilities.getWordEnd(getHyperCardTextPane(), clickIndex);
String clickText = getHyperCardTextPane().getStyledDocument().getText(startWordIndex, endWordIndex - startWordIndex);
ExecutionContext.getContext().getGlobalProperties().defineProperty(HyperCardProperties.PROP_CLICKTEXT, new Value(clickText), true);
} catch (BadLocationException e) {
// Nothing to do
}
}
示例3: getCurrentWord
import javax.swing.text.Utilities; //导入方法依赖的package包/类
public String getCurrentWord() throws BadLocationException {
int start = Utilities.getWordStart(this, getSelectionStart());
int end = Utilities.getWordEnd(this, getSelectionStart());
String word = getDocument().getText(start, end - start);
if (word.indexOf(".") != -1) {
word = word.substring(0, word.lastIndexOf("."));
}
//System.out.println( "Selected word: " + word );
return word;
}
示例4: actionPerformed
import javax.swing.text.Utilities; //导入方法依赖的package包/类
/**
* At first the same code as in {@link
* EmacsKeyBindings.DowncaseWordAction} is performed, to ensure the
* word is in lower case, then the first letter is capialized.
*/
@Override
public void actionPerformed(ActionEvent event) {
JTextComponent jtc = getTextComponent(event);
if (jtc != null) {
try {
/* downcase code */
int start = jtc.getCaretPosition();
int end = EmacsKeyBindings.getWordEnd(jtc, start);
jtc.setSelectionStart(start);
jtc.setSelectionEnd(end);
String word = jtc.getText(start, end - start);
jtc.replaceSelection(word.toLowerCase(Locale.ROOT));
/* actual capitalize code */
int offs = Utilities.getWordStart(jtc, start);
// get first letter
String c = jtc.getText(offs, 1);
// we're at the end of the previous word
if (" ".equals(c)) {
/* ugly java workaround to get the beginning of the
word. */
offs = Utilities.getWordStart(jtc, ++offs);
c = jtc.getText(offs, 1);
}
if (Character.isLetter(c.charAt(0))) {
jtc.setSelectionStart(offs);
jtc.setSelectionEnd(offs + 1);
jtc.replaceSelection(c.toUpperCase(Locale.ROOT));
}
end = Utilities.getWordEnd(jtc, offs);
jtc.setCaretPosition(end);
} catch (BadLocationException ble) {
jtc.getToolkit().beep();
}
}
}
示例5: actionPerformed
import javax.swing.text.Utilities; //导入方法依赖的package包/类
/***
* At first the same code as in
* {@link EmacsKeyBindings.DowncaseWordAction} is performed, to ensure
* the word is in lower case, then the first letter is capialized.
*/
public void actionPerformed(ActionEvent event) {
JTextComponent jtc = getTextComponent(event);
if (jtc != null) {
try {
/* downcase code */
int start = jtc.getCaretPosition();
int end = getWordEnd(jtc, start);
jtc.setSelectionStart(start);
jtc.setSelectionEnd(end);
String word = jtc.getText(start, end - start);
jtc.replaceSelection(word.toLowerCase());
/* actual capitalize code */
int offs = Utilities.getWordStart(jtc, start);
// get first letter
String c = jtc.getText(offs, 1);
// we're at the end of the previous word
if (c.equals(" ")) {
/*
* ugly java workaround to get the beginning of the
* word.
*/
offs = Utilities.getWordStart(jtc, ++offs);
c = jtc.getText(offs, 1);
}
if (Character.isLetter(c.charAt(0))) {
jtc.setSelectionStart(offs);
jtc.setSelectionEnd(offs + 1);
jtc.replaceSelection(c.toUpperCase());
}
end = Utilities.getWordEnd(jtc, offs);
jtc.setCaretPosition(end);
} catch (BadLocationException ble) {
jtc.getToolkit().beep();
}
}
}
示例6: getWordStart
import javax.swing.text.Utilities; //导入方法依赖的package包/类
protected int getWordStart(RTextArea textArea, int offs)
throws BadLocationException {
return Utilities.getWordStart(textArea, offs);
}
示例7: popupMenuWillBecomeVisible
import javax.swing.text.Utilities; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
public void popupMenuWillBecomeVisible( PopupMenuEvent ev ) {
if( dictionary == null) {
menu.setEnabled( false );
return;
}
JPopupMenu popup = (JPopupMenu)ev.getSource();
Component invoker = popup.getInvoker();
if( invoker instanceof JTextComponent ) {
final JTextComponent jText = (JTextComponent)invoker;
if( !jText.isEditable() ) {
// Suggestions only for editable text components
menu.setEnabled( false );
return;
}
try {
int offs = getCursorPosition( jText );
if( offs < 0 ) {
// occur if there nothing under the mouse pointer
menu.setEnabled( false );
return;
}
// get the word from current position
final int begOffs = Utilities.getWordStart( jText, offs );
final int endOffs = Utilities.getWordEnd( jText, offs );
final String word = jText.getText( begOffs, endOffs - begOffs );
//find the first invalid word from current position, use the Tokenizer that it is ever compatible with the red zigzag line
Tokenizer tokenizer = new Tokenizer( jText, dictionary, locale, offs, options );
String invalidWord;
do {
invalidWord = tokenizer.nextInvalidWord();
} while( tokenizer.getWordOffset() < begOffs );
menu.removeAll();
if( !word.equals( invalidWord ) ) {
// the current word is not invalid
menu.setEnabled( false );
return;
}
List<Suggestion> list = dictionary.searchSuggestions( word );
//Disable then menu item if there are no suggestions
menu.setEnabled( list.size() > 0 );
boolean needCapitalization = tokenizer.isFirstWordInSentence() && Utils.isFirstCapitalized( word );
addSuggestionMenuItem( jText, begOffs, endOffs, list, needCapitalization );
addMenuItemAddToDictionary( jText, word, list.size() > 0 );
} catch( BadLocationException ex ) {
SpellChecker.getMessageHandler().handleException( ex );
}
}
}
示例8: getNextVisualPositionFrom
import javax.swing.text.Utilities; //导入方法依赖的package包/类
public int getNextVisualPositionFrom(JTextComponent text,
int pos,
Position.Bias bias,
int direction,
Position.Bias[] biasRet)
throws BadLocationException
{
Point pt;
int newpos = pos;
switch (direction)
{
case SwingConstants.NORTH:
// Find out where the caret want to be positioned ideally.
pt = text.getCaret().getMagicCaretPosition();
// Calculate its position above.
newpos = Utilities.getPositionAbove(text, pos, (pt != null) ? pt.x : 0);
// If we have a valid position, then calculate the next word start
// from there.
if (newpos != -1)
return Utilities.getWordStart(text, newpos);
else
return pos;
case SwingConstants.SOUTH:
// Find out where the caret want to be positioned ideally.
pt = text.getCaret().getMagicCaretPosition();
// Calculate its position below.
newpos = Utilities.getPositionBelow(text, pos, (pt != null) ? pt.x : 0);
// If we have a valid position, then calculate the next word start
// from there.
if (newpos != -1)
return Utilities.getWordStart(text, newpos);
else
return pos;
case SwingConstants.WEST:
// Calculate the next word start.
newpos = Utilities.getWordStart(text, newpos);
// If that means that the caret will not move, return
// the start of the previous word.
if (newpos != pos)
return newpos;
else
return Utilities.getPreviousWord(text, newpos);
case SwingConstants.EAST:
return Utilities.getNextWord(text, newpos);
default:
// Do whatever the super implementation did.
return super.getNextVisualPositionFrom(text, pos, bias,
direction, biasRet);
}
}
示例9: getNextVisualPositionFrom
import javax.swing.text.Utilities; //导入方法依赖的package包/类
public int getNextVisualPositionFrom(JTextComponent text,
int pos,
Position.Bias bias,
int direction,
Position.Bias[] biasRet)
throws BadLocationException
{
Point pt;
int newpos = pos;
switch (direction)
{
case SwingConstants.NORTH:
// Find out where the caret want to be positioned ideally.
pt = text.getCaret().getMagicCaretPosition();
// Calculate its position above.
newpos = Utilities.getPositionAbove(text, pos, (pt != null) ? pt.x : 0);
// If we have a valid position, then calculate the next word start
// from there.
if (newpos != -1)
return Utilities.getWordStart(text, newpos);
else
return pos;
case SwingConstants.SOUTH:
// Find out where the caret want to be positioned ideally.
pt = text.getCaret().getMagicCaretPosition();
// Calculate its position below.
newpos = Utilities.getPositionBelow(text, pos, (pt != null) ? pt.x : 0);
// If we have a valid position, then calculate the next word start
// from there.
if (newpos != -1)
return Utilities.getWordStart(text, newpos);
else
return pos;
case SwingConstants.WEST:
// Calculate the next word start.
newpos = Utilities.getWordStart(text, newpos);
// If that means that the caret will not move, return
// the start of the previous word.
if (newpos != pos)
return newpos;
else
return Utilities.getPreviousWord(text, newpos);
case SwingConstants.EAST:
return Utilities.getNextWord(text, newpos);
default:
// Do whatever the super implementation did.
return super.getNextVisualPositionFrom(text, pos, bias,
direction, biasRet);
}
}
示例10: popupMenuWillBecomeVisible
import javax.swing.text.Utilities; //导入方法依赖的package包/类
public void popupMenuWillBecomeVisible( PopupMenuEvent ev ) {
JPopupMenu popup = (JPopupMenu)ev.getSource();
Component invoker = popup.getInvoker();
if( invoker instanceof JTextComponent ) {
final JTextComponent jText = (JTextComponent)invoker;
if( !jText.isEditable() ) {
// Suggestions only for editable text components
menu.setEnabled( false );
return;
}
Caret caret = jText.getCaret();
int offs = Math.min( caret.getDot(), caret.getMark() );
Point p = jText.getMousePosition();
if( p != null ) {
// use position from mouse click and not from editor cursor position
offs = jText.viewToModel( p );
}
try {
Document doc = jText.getDocument();
if( offs > 0 && (offs >= doc.getLength() || Character.isWhitespace( doc.getText( offs, 1 ).charAt( 0 ) )) ) {
// if the next character is a white space then use the word on the left site
offs--;
}
// get the word from current position
final int begOffs = Utilities.getWordStart( jText, offs );
final int endOffs = Utilities.getWordEnd( jText, offs );
String word = jText.getText( begOffs, endOffs - begOffs );
//find the first invalid word from current position
Tokenizer tokenizer = new Tokenizer( jText, dictionary, locale, offs, options );
String invalidWord;
do {
invalidWord = tokenizer.nextInvalidWord();
} while( tokenizer.getWordOffset() < begOffs );
menu.removeAll();
if( !word.equals( invalidWord ) ) {
// the current word is not invalid
menu.setEnabled( false );
return;
}
if( dictionary == null ) {
// without dictionary it is disabled
menu.setEnabled( false );
return;
}
List<Suggestion> list = dictionary.searchSuggestions( word );
//Disable then menu item if there are no suggestions
menu.setEnabled( list.size() > 0 );
boolean needCapitalization = tokenizer.isFirstWordInSentence() && Utils.isCapitalized( word );
for( int i = 0; i < list.size() && i < options.getSuggestionsLimitMenu(); i++ ) {
Suggestion sugestion = list.get( i );
String sugestionWord = sugestion.getWord();
if( needCapitalization ) {
sugestionWord = Utils.getCapitalized( sugestionWord );
}
JMenuItem item = new JMenuItem( sugestionWord );
menu.add( item );
final String newWord = sugestionWord;
item.addActionListener( new ActionListener() {
public void actionPerformed( ActionEvent e ) {
jText.setSelectionStart( begOffs );
jText.setSelectionEnd( endOffs );
jText.replaceSelection( newWord );
}
} );
}
} catch( BadLocationException ex ) {
ex.printStackTrace();
}
}
}