⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fullwordcombokeyselectionmodel.java

📁 更方便的SWING
💻 JAVA
字号:
/*
 * PSwing Utilities -- Nifty Swing Widgets
 * Copyright (C) 2002  Pallas Technology
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * Pallas Technology
 * 1170 HOWELL MILL RD NW
 * SUITE 306
 * ATLANTA GEORGIA 30318
 * 
 * PHONE 404.983.0623
 * EMAIL info@pallastechnology.com
 * 
 * www.pallastechnology.com
 **************************************************************************
 * $Archive: SwingTools$
 * $FileName: FullWordComboKeySelectionModel.java$
 * $FileID: 2$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 7/31/03 7:33 PM$
 * $VerID: 83$
 * $Comment: $
 **************************************************************************/
package com.pallas.swing.pcombobox;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JComboBox.KeySelectionManager;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.pallas.util.Async;
import com.pallas.util.AsyncCentral;

/**
 * Title:   $FileName: FullWordComboKeySelectionModel.java$
 * @version $VerNum: 5$
 * @author $AuthorName: Rob MacGrogan$<br><br>
 * 
 * $Description: KeySelectionManager used by PComboBox.$<br>
 * $KeyWordsOff: $<br><br>
 *
 * KeySelectionManager used by PComboBox. Allows the user to
 * enter text into the editor component. Looks at text user has typed and
 * make a match accordingly. Will not allow user's caret to advance if no
 * match can be made.
 */
public class FullWordComboKeySelectionModel implements KeySelectionManager {

  private PComboBox parent = null;
  private JTextField field = null;
  private FullWordComboKeySelectionModel mgr = this;
  boolean block = true;
  private int caretPosition = 0;

  private int selection;


  public FullWordComboKeySelectionModel(PComboBox parent){
    this.parent = parent;
    this.field = parent.getTextField();
    field.getDocument().addDocumentListener(new PComboDocumentListener(this));
  }



  /**
   * Returns PComboBox that is using this FullWordComboKeySelectionModel.
   */
  PComboBox getParent(){
    return parent;
  }

  /**
   * Returns editor component of the PComboBox.
   */
  JTextField getField(){
    return field;
  }

  /**
   * Returns user's current caret position inside of editable component.
   */
  int getCaretPosition(){
    return caretPosition;
  }


  /**
   * Can't get this to work consistently, so we are not using this method.
   */
  public int selectionForKey(char key, ComboBoxModel model) {
    return 0;
  }

  /**
   * LIke selectionForKey, but we are calling this indirectly (via Async) from
   * PComboDocumentListener, passing in the caret position, and ignoring the key.
   */
  public int selectionForKey(char key, ComboBoxModel model, int caretPosition) {

    int selection = -1;
    String searchString = getCurrentSerachString(caretPosition);
//System.out.println("[FW] caret: " + caretPosition);
//System.out.println("[FW] searchString: " + searchString);
    selection = parent.search(searchString);
//System.out.println("[FW] selection is " + selection);
    updateSearchStringField(getString(model, selection), model, searchString);
    return selection;
  }

  public void updateSearchStringField(String s, ComboBoxModel model, String searchString){
    if (s != null){
      field.setText(s);
      int iCaretPos = searchString.length();
      field.setCaretPosition(iCaretPos);
      caretPosition = iCaretPos;
    }
    else if (! parent.isAllowNewEntries()){
      int iCaretPos = searchString.length() - 1;
      field.setText(getSelectedString(model));
      field.setCaretPosition(iCaretPos);
      caretPosition = iCaretPos;
    }
    else{
      //New entries allowed, so let them keep typing.
      int iCaretPos = searchString.length();
      field.setCaretPosition(iCaretPos);
      field.setText(searchString);
      caretPosition = iCaretPos;
    }
  }

  private String getCurrentSerachString(int caretPosition){
    String s = field.getText();
//System.out.println("[FW] field text: " + s);
//System.out.println("[FW] caret: " + field.getCaretPosition());
    s = s.substring(0, caretPosition);
    //s = s.substring(0, field.getCaretPosition());
//System.out.println("[FW] search string: " + s);
    return s;
  }

  private String getString(ComboBoxModel model, int index){
    Object o = model.getElementAt(index);
    String s = null;
    if (o != null){
      s = o.toString();
    }
    return s;
  }

  private String getSelectedString(ComboBoxModel model){
    Object o = model.getSelectedItem();
    String s = o.toString();
    return s;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -