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

📄 pcombodocumentlistener.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: PComboDocumentListener.java$
 * $FileID: 4$
 *
 * Last change:
 * $AuthorName: Rob MacGrogan$
 * $Date: 1/28/03 8:05 PM$
 * $VerID: 53$
 * $Comment: Add GLPL text.$
 **************************************************************************/
package com.pallas.swing.pcombobox;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import com.pallas.util.AsyncCentral;

/**
 * Title:   $FileName: PComboDocumentListener.java$
 * @version $VerNum: 7$
 * @author $AuthorName: Rob MacGrogan$<br><br>
 * 
 * $Description: Document Listener used by attached JTextField
 *              in PComboBox.$<br>
 * $KeyWordsOff: $<br><br>
 *
 * Document Listener used by attached JTextField in PComboBox.
 */
public class PComboDocumentListener implements DocumentListener {

    private FireKeys keys = null;
    private FireClear clear = null;
    private FullWordComboKeySelectionModel mgr = null;

    public PComboDocumentListener(FullWordComboKeySelectionModel mgr){
      keys = new FireKeys(mgr);
      clear = new FireClear(mgr.getParent());
      this.mgr = mgr;
    }

    public void changedUpdate(DocumentEvent ev){
      int useOffset = ev.getOffset();
      if (mgr.getField().getCaretPosition() > ev.getOffset())
        useOffset = mgr.getField().getCaretPosition();
      generalUpdate(ev, useOffset);
    }

    public void insertUpdate(DocumentEvent ev){
      int useOffset = ev.getOffset();
      if (mgr.getField().getCaretPosition() > ev.getOffset())
        useOffset = mgr.getField().getCaretPosition();
      generalUpdate(ev, useOffset);
    }

    public void removeUpdate(DocumentEvent ev){
      int useOffset = ev.getOffset();
      if (mgr.getField().getCaretPosition() < ev.getOffset())
        useOffset = mgr.getField().getCaretPosition();
      generalUpdate(ev, useOffset);
    }

    /**
     * All event methods call this method. <br><br>
     *
     * Fires asyncronous execute method in FireKeys if caret position in
     * field is greater than 0, or if this is an insert and length of insert is
     * 1 (this means the user has made a single keystroke).
     */
    private synchronized void generalUpdate(DocumentEvent ev, int useOffset){
      int lengthOfChange = ev.getLength();
      if (lengthOfChange == 1){

        if (useOffset > 0 ||
            (ev.getType() == DocumentEvent.EventType.INSERT && ev.getLength() == 1)){
          int offset = ev.getOffset() + 1;
          if (ev.getType() == DocumentEvent.EventType.REMOVE &&
              ev.getOffset() > 0)
            offset = ev.getOffset();
          keys.setValues(' ', mgr.getParent().getModel(), offset);
          AsyncCentral.getInstance().runAsync(keys);
        }
        else if (ev.getType() == DocumentEvent.EventType.REMOVE && 
                  ev.getDocument().getEndPosition().getOffset() > 1){
          AsyncCentral.getInstance().runAsync(clear);
        }
      }
    }

}

⌨️ 快捷键说明

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