當前位置: 首頁>>代碼示例>>Java>>正文


Java Utilities.getWordStart方法代碼示例

本文整理匯總了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;
}
 
開發者ID:andy-goryachev,項目名稱:PasswordSafe,代碼行數:11,代碼來源:CEditorAction.java

示例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
    }
}
 
開發者ID:defano,項目名稱:hypertalk-java,代碼行數:14,代碼來源:FieldPart.java

示例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;
}
 
開發者ID:silverslade,項目名稱:jif,代碼行數:11,代碼來源:JifTextPane.java

示例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();
        }
    }
}
 
開發者ID:JabRef,項目名稱:jabref,代碼行數:43,代碼來源:EmacsKeyBindings.java

示例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();
		}
	}
}
 
開發者ID:jpverkamp,項目名稱:wombat-ide,代碼行數:44,代碼來源:EmacsKeyBindings.java

示例6: getWordStart

import javax.swing.text.Utilities; //導入方法依賴的package包/類
protected int getWordStart(RTextArea textArea, int offs)
								throws BadLocationException {
	return Utilities.getWordStart(textArea, offs);
}
 
開發者ID:Thecarisma,項目名稱:powertext,代碼行數:5,代碼來源:RTextAreaEditorKit.java

示例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 );
        }
    }
}
 
開發者ID:kolchagov,項目名稱:jlokalize,代碼行數:61,代碼來源:CheckerListener.java

示例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);
  }
}
 
開發者ID:vilie,項目名稱:javify,代碼行數:57,代碼來源:NavigationFilterDemo.java

示例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);
  }
}
 
開發者ID:nmldiegues,項目名稱:jvm-stm,代碼行數:57,代碼來源:NavigationFilterDemo.java

示例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();
        }
    }
}
 
開發者ID:dasatti,項目名稱:urduhtmlmaster,代碼行數:81,代碼來源:CheckerListener.java


注:本文中的javax.swing.text.Utilities.getWordStart方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。