/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info: http://jsynoptic.sourceforge.net/index.html
*
* This program 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 program 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
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
* Corporate:
* Astrium SAS
* EADS CRC
* Individual:
* Nicolas Brodu
*
* $Id: FilteredSourceTree.java,v 1.3 2008/09/05 10:06:15 ogor Exp $
*
* Changes
* -------
* 09-Juill-2008 : Initial public release (NB);
*
*/
package simtools.ui;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
/**
* This class contains a source tree and provides a GUI to specify to source tree filter pattern.
* The source tree is notified of each filter pattern update.
*
* @author zxpletran007
*
*/
public class FilteredSourceTree extends GridBagPanel implements ActionListener {
public static MenuResourceBundle resources = ResourceFinder.getMenu(FilteredSourceTree.class);
protected SourceTree sourceTree;
protected JTextField sourceFilter;
protected JButton clearFilterButton;
protected JButton collapseAllButton;
protected JButton expandAllButton;
protected JScrollPane sourceTreeScrollPane;
protected String textToFilter;
/**
* A pool of source tree panels to reduce memory foot print
* Keep in mind that each JComponent can be displayed only one time
* @param id an identifier to define the reuse or not from the pool
* @return the source tree panel
*/
public static FilteredSourceTree getFromPool(String id){
SourceTree sourceTree = SourceTree.getFromPool(id);
return new FilteredSourceTree(sourceTree);
}
public FilteredSourceTree(SourceTree st){
this(st, null);
}
public FilteredSourceTree(SourceTree st, String title){
super(title);
// Source tree
this.sourceTree = st;
sourceTreeScrollPane = new JScrollPane(sourceTree);
sourceTreeScrollPane.setViewportView(sourceTree);
// Filter panel
sourceFilter = new JTextField();
sourceFilter.setPreferredSize(new Dimension(200, 20)); // The source filter filed shall be display on at least 200 px
sourceFilter.addKeyListener(
new KeyListener() {
public void keyPressed(KeyEvent keyEvent) {}
public void keyReleased(KeyEvent keyEvent) {
textToFilter = sourceFilter.getText();
clearFilterButton.setEnabled(!sourceFilter.getText().equals(""));
}
public void keyTyped(KeyEvent keyEvent) {}
}
);
// buttons
JToolBar toolbar = new JToolBar();
toolbar.setRollover(true);
toolbar.setFloatable(false);
clearFilterButton = resources.getBox("clearFilter", this);
clearFilterButton.setEnabled(false);
toolbar.add(clearFilterButton);
collapseAllButton = resources.getBox("collapseAll", this);
toolbar.add(collapseAllButton);
expandAllButton = resources.getBox("expandAll", this);
toolbar.add(expandAllButton);
addOnCurrentRow(new JLabel(resources.getString("filter")));
addOnCurrentRow(sourceFilter,3,true, false, false);
addOnCurrentRow(toolbar,1,false, false, true);
addOnCurrentRow(sourceTreeScrollPane,5,true, true, true);
new Thread(){
public void run(){
while(true){
try {
Thread.sleep(200);
if(textToFilter!= null){
String s = textToFilter;
textToFilter = null;
sourceTree.setFilterValue(s);
sourceFilter.requestFocus();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
/* (non-Javadoc)
* @see javax.swing.JComponent#setEnabled(boolean)
*/
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
sourceFilter.setEnabled(enabled);
clearFilterButton.setEnabled(enabled && !sourceFilter.getText().equals(""));
sourceTree.setEnabled(enabled);
}
/* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
*/
public void actionPerformed(ActionEvent e) {
if ((e.getSource() == clearFilterButton)) {
setFilterValue("");
} else if ((e.getSource() == collapseAllButton)) {
sourceTree.collapseAll();
} else if ((e.getSource() == expandAllButton)) {
sourceTree.expandAll();
}
}
public SourceTree getSourceTree() {
return sourceTree;
}
public JScrollPane getScrollPane(){
return sourceTreeScrollPane;
}
public void setSelectedValue(Object o) {
sourceTree.setSelectedValue(o);
}
public Object getSelectedSourceOrCollection(){
return sourceTree.getSelectedSourceOrCollection();
}
public void setFilterValue(String value){
sourceFilter.setText(value);
sourceTree.setFilterValue(value);
clearFilterButton.setEnabled(!value.equals(""));
}
}