001: /*
002: * FilterPickerAction.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.actions;
013:
014: import java.awt.Dimension;
015: import java.awt.Window;
016: import java.awt.event.ActionEvent;
017: import java.awt.event.ActionListener;
018: import java.beans.PropertyChangeEvent;
019: import java.beans.PropertyChangeListener;
020: import java.util.List;
021: import javax.swing.JButton;
022: import javax.swing.JMenu;
023: import javax.swing.JMenuItem;
024: import javax.swing.SwingUtilities;
025: import workbench.gui.WbSwingUtilities;
026: import workbench.gui.components.DropDownButton;
027: import workbench.gui.components.WbMenuItem;
028: import workbench.gui.components.WbTable;
029: import workbench.gui.filter.FilterDefinitionManager;
030: import workbench.log.LogMgr;
031: import workbench.resource.ResourceMgr;
032: import workbench.storage.filter.FilterExpression;
033: import workbench.util.WbFile;
034:
035: /**
036: * Select a different filter for a table.
037: * @author support@sql-workbench.net
038: */
039: public class FilterPickerAction extends WbAction implements
040: PropertyChangeListener, ActionListener {
041: private WbTable client;
042: private DropDownButton dropDownButton;
043:
044: public FilterPickerAction(WbTable aClient) {
045: super ();
046: this .setClient(aClient);
047: this .initMenuDefinition("MnuTxtPickFilter");
048: this .setIcon(ResourceMgr.getImage("dropdown"));
049: this .setMenuItemName(ResourceMgr.MNU_TXT_DATA);
050: FilterDefinitionManager mgr = FilterDefinitionManager
051: .getInstance();
052: mgr.addPropertyChangeListener(this );
053: checkEnabled();
054: buildPopup();
055: }
056:
057: public void actionPerformed(ActionEvent e) {
058: if (this .client == null)
059: return;
060:
061: if (e.getSource() instanceof JMenuItem) {
062: String file = e.getActionCommand();
063: try {
064: FilterExpression f = FilterDefinitionManager
065: .getInstance().loadFilter(file);
066: this .client.applyFilter(f);
067: } catch (Exception ex) {
068: LogMgr.logError("FilterPickerAction.actionPerformed()",
069: "Error loading filter", ex);
070: Window w = SwingUtilities
071: .getWindowAncestor(this .client);
072: WbSwingUtilities.showErrorMessage(w,
073: "Could not load filter: " + ex.getMessage());
074: }
075:
076: }
077: }
078:
079: private void checkEnabled() {
080: if (this .client == null) {
081: this .setEnabled(false);
082: } else {
083: int availableFilters = FilterDefinitionManager
084: .getInstance().getEntries().size();
085: this .setEnabled(availableFilters > 0);
086: }
087: }
088:
089: public void setClient(WbTable c) {
090: this .client = c;
091: checkEnabled();
092: }
093:
094: public JButton getToolbarButton(boolean createNew) {
095: JButton result = null;
096: if (this .dropDownButton == null || createNew) {
097: DropDownButton b = new DropDownButton(ResourceMgr
098: .getImage("dropdown"));
099: b.setAction(this );
100: b.setText(null);
101: b.setMnemonic(0);
102: Dimension d = new Dimension(12, 24);
103: b.setMaximumSize(d);
104: b.setPreferredSize(d);
105: b.setMinimumSize(d);
106: if (this .dropDownButton == null)
107: this .dropDownButton = b;
108: result = b;
109: } else {
110: result = this .dropDownButton;
111: }
112: return result;
113: }
114:
115: public void propertyChange(PropertyChangeEvent evt) {
116: int availableFilters = FilterDefinitionManager.getInstance()
117: .getEntries().size();
118: this .setEnabled(availableFilters > 0);
119: buildPopup();
120: }
121:
122: private void buildPopup() {
123: List<WbFile> entries = FilterDefinitionManager.getInstance()
124: .getEntries();
125: if (entries == null || entries.size() == 0)
126: return;
127:
128: JMenu menu = new JMenu("filters");
129: for (WbFile f : entries) {
130: JMenuItem item = new WbMenuItem(f.getName());
131: item.setToolTipText(f.getFullPath());
132: item.setActionCommand(f.getFullPath());
133: item.addActionListener(this);
134: menu.add(item);
135: }
136: if (this.dropDownButton == null) {
137: this.getToolbarButton();
138: }
139: dropDownButton.setDropDownMenu(menu.getPopupMenu());
140: }
141: }
|