本文整理汇总了Java中javax.swing.text.Utilities.getWordEnd方法的典型用法代码示例。如果您正苦于以下问题:Java Utilities.getWordEnd方法的具体用法?Java Utilities.getWordEnd怎么用?Java Utilities.getWordEnd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.swing.text.Utilities
的用法示例。
在下文中一共展示了Utilities.getWordEnd方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getWordEnd
import javax.swing.text.Utilities; //导入方法依赖的package包/类
protected int getWordEnd(JTextComponent c, int pos)
{
try
{
return Utilities.getWordEnd(c, pos);
}
catch(Exception e)
{ }
return -1;
}
示例2: actionPerformed
import javax.swing.text.Utilities; //导入方法依赖的package包/类
/**
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
JTextComponent target = getTextComponent(e);
boolean beep = true;
if ((target != null) && (target.isEditable())) {
try {
// select the next word
int offs = target.getCaretPosition();
int endOffs;
String s = target.getDocument().getText(offs, 1);
if (Character.isWhitespace(s.charAt(0))) {
endOffs = Utilities.getNextWord(target, offs);
endOffs = Utilities.getWordEnd(target, endOffs);
} else {
endOffs = Utilities.getWordEnd(target, offs);
}
target.moveCaretPosition(endOffs);
// and then delete it
target.replaceSelection("");
beep = false;
} catch (BadLocationException exc) {
// nothing to do, because we set beep to true already
}
}
if (beep) {
provideErrorFeedback(target);
}
}
示例3: 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
}
}
示例4: 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;
}
示例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.
*/
@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();
}
}
}
示例6: 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();
}
}
}
示例7: getWordEnd
import javax.swing.text.Utilities; //导入方法依赖的package包/类
protected int getWordEnd(RTextArea textArea, int offs)
throws BadLocationException {
return Utilities.getWordEnd(textArea, offs);
}
示例8: 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 );
}
}
}
示例9: 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();
}
}
}