?? psearchpopupwindow.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: PSearchPopupWindow.java$
* $FileID: 27$
*
* Last change:
* $AuthorName: Rob MacGrogan$
* $Date: 2/14/03 7:32 PM$
* $VerID: 71$
* $Comment: $
**************************************************************************/
package com.pallas.swing.psearchcombo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Enumeration;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JWindow;
import javax.swing.ListModel;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
* Title: $FileName: PSearchPopupWindow.java$
* @version $VerNum: 2$
* @author $AuthorName: Rob MacGrogan$<br><br>
*
* $Description: $<br>
* $KeyWordsOff: $<br><br>
*/
public class PSearchPopupWindow extends JWindow {
private PSearchCombo comboBox;
private JTextField searchTerm;
private JList resultList;
private JButton goButton;
private Color selectedBackground;
private Color selectedForeground;
private Color background;
private Color foreground;
private static final String INITIAL_LIST_MESSAGE = "Enter a term above to search the list";
public PSearchPopupWindow(PSearchCombo combo, Window parent){
super(parent);
this.comboBox = combo;
background = UIManager.getColor("Panel.background");
foreground = UIManager.getColor("Panel.foreground");
selectedBackground = UIManager.getColor("ComboBox.selectionBackground");
selectedForeground = UIManager.getColor("ComboBox.selectionForeground");
initializePopup();
}
public PSearchPopupWindow(PSearchCombo combo, Dialog parent){
super(parent);
this.comboBox = combo;
background = UIManager.getColor("Panel.background");
foreground = UIManager.getColor("Panel.foreground");
selectedBackground = UIManager.getColor("ComboBox.selectionBackground");
selectedForeground = UIManager.getColor("ComboBox.selectionForeground");
initializePopup();
}
public void showPopup(){
Dimension sd = comboBox.getSize();
int width = (int)(( sd.width ));
int height = ( sd.height) * 10;
Dimension comboDim = comboBox.getComboBox().getSize();
Point pt = comboBox.getLocationOnScreen();
//goButton.setSize(new Dimension(comboDim.height, comboDim.height));
searchTerm.setPreferredSize(new Dimension(comboDim.width, comboDim.height));
int x = pt.x;
int y = pt.y + sd.height;
setLocation(x, y);
setSize(new Dimension(width, height));
setVisible(true);
}
protected JButton buildGoButton() {
JButton btn = new JButton("go");
btn.addActionListener(
new ActionListener(){
/**
* @see java.awt.event.ActionListener#actionPerformed(ActionEvent)
*/
public void actionPerformed(ActionEvent ev) {
Vector results = searchComboContents(searchTerm.getText());
resultList.setListData(results);
//setVisible(false);
}
}
);
//btn.setMaximumSize(new Dimension(16, 16));
btn.setPreferredSize(new Dimension(22, 22));
return btn;
}
protected void initializePopup() {
JLabel label;
label = new JLabel("Enter search term:");
searchTerm = new JTextField();
//searchTerm.setPreferredSize(new Dimension(200, 18));
searchTerm.setEnabled(true);
goButton = buildGoButton();
goButton.setToolTipText("Enter search term and click to perform serach");
JPanel headerTop = new JPanel(new FlowLayout(FlowLayout.LEFT));
headerTop.add(label);
JPanel searchPanel1 = new JPanel(new GridLayout(1,1, 0, 3));
searchPanel1.add(searchTerm);
JPanel searchPanel = new JPanel();
searchPanel.add(searchPanel1);
JPanel buttonPanel = new JPanel();
buttonPanel.add(goButton);
JPanel headerBottom = new JPanel(new BorderLayout());
headerBottom.add(searchPanel, BorderLayout.CENTER);
headerBottom.add(buttonPanel, BorderLayout.EAST);
JPanel header = new JPanel(new BorderLayout(6, 0));
header.setBackground(background);
header.setOpaque(true);
header.add(Box.createHorizontalStrut(6), BorderLayout.WEST);
header.add(Box.createHorizontalStrut(6), BorderLayout.EAST);
JPanel middle = new JPanel();
middle.setLayout(new BoxLayout(middle, BoxLayout.Y_AXIS));
middle.add(headerTop);
header.add(middle, BorderLayout.CENTER);
resultList = buildResultListBox();
JScrollPane scroller = new JScrollPane(resultList);
scroller.setPreferredSize(new Dimension(comboBox.getWidth(), 100));
JPanel mainPanel = new JPanel();
//mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
mainPanel.setLayout(new BorderLayout());
mainPanel.setBorder(BorderFactory.createLineBorder(Color.black));
mainPanel.setBackground(background);
mainPanel.add(headerBottom, BorderLayout.NORTH);
mainPanel.add(scroller, BorderLayout.CENTER);
getContentPane().add(mainPanel, BorderLayout.CENTER);
PSearchFocusListener lsn = new PSearchFocusListener(this);
addWindowFocusListener(lsn);
}
private JList buildResultListBox(){
Vector vec = new Vector();
vec.add(INITIAL_LIST_MESSAGE);
JList list = new JList(vec);
list.addListSelectionListener(new PSearchListSelectionListener(this));
return list;
}
private Vector searchComboContents(String search){
Vector results = new Vector();
String upperSearch = search.toUpperCase();
ListModel model = comboBox.getModel();
for (int i = 0; i < model.getSize(); i++){
Object o = model.getElementAt(i);
String s = o.toString().toUpperCase();
if (s.indexOf(upperSearch) > -1){
results.add(o);
}
}
return results;
}
/**
* Returns the comboBox.
* @return PSearchCombo
*/
public PSearchCombo getComboBox() {
return comboBox;
}
/**
* Returns the resultList.
* @return JList
*/
public JList getResultList() {
return resultList;
}
/**
* Returns the searchTerm.
* @return JTextField
*/
public JTextField getSearchTerm() {
return searchTerm;
}
//This method forum a forum on java.sun.com, posted by fabot4.
//Post is here--http://forum.java.sun.com/thread.jsp?forum=257&thread=113615
protected javax.swing.JRootPane createRootPane(){
javax.swing.JRootPane rootPane = new javax.swing.JRootPane();
rootPane.registerKeyboardAction(
new java.awt.event.ActionListener(){
public void actionPerformed(java.awt.event.ActionEvent e){
hidePopup();
}
},
javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0),
javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW);
return rootPane;
}
public void hidePopup(){
setVisible(false);
getComboBox().getComboBox().getEditor().getEditorComponent().requestFocus();
}
private class PSearchFocusListener extends WindowAdapter{
private PSearchPopupWindow parent = null;
public PSearchFocusListener(PSearchPopupWindow p){
parent = p;
}
/**
* @see java.awt.event.WindowFocusListener#windowLostFocus(WindowEvent)
*/
public void windowLostFocus(WindowEvent ev) {
parent.hidePopup();
}
}
private class PSearchListSelectionListener implements ListSelectionListener{
private PSearchPopupWindow parent = null;
public PSearchListSelectionListener(PSearchPopupWindow parent){
this.parent = parent;
}
public void valueChanged(ListSelectionEvent ev){
if (! ev.getValueIsAdjusting()){
Object oSelected = parent.getResultList().getSelectedValue();
if (oSelected != null &&
oSelected != INITIAL_LIST_MESSAGE){
parent.getComboBox().setSelectedItem(oSelected);
parent.hidePopup();
}
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -