001: package net.sourceforge.squirrel_sql.plugins.favs;
002:
003: /*
004: * Copyright (C) 2001 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: import java.awt.BorderLayout;
022: import java.awt.event.MouseAdapter;
023: import java.awt.event.MouseEvent;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.swing.Action;
029: import javax.swing.JMenuItem;
030: import javax.swing.JTree;
031: import javax.swing.ToolTipManager;
032: import javax.swing.tree.TreePath;
033:
034: import net.sourceforge.squirrel_sql.client.IApplication;
035: import net.sourceforge.squirrel_sql.client.action.ActionCollection;
036: import net.sourceforge.squirrel_sql.fw.gui.BasePopupMenu;
037:
038: final class QueryTree extends JTree {
039: private static final long serialVersionUID = 1L;
040:
041: private IApplication _app;
042:
043: @SuppressWarnings("unused")
044: private QueryTreeModel _model;
045:
046: /** Popup menu for this component. */
047: private MyPopupMenu _popupMenu = new MyPopupMenu();
048:
049: private List<BaseFavouriteAction> _actions = new ArrayList<BaseFavouriteAction>();
050:
051: public QueryTree(IApplication app, FoldersCache cache)
052: throws IllegalArgumentException {
053: super (new QueryTreeModel(app, cache));
054: if (app == null) {
055: throw new IllegalArgumentException(
056: "Null IApplication passed");
057: }
058: if (cache == null) {
059: throw new IllegalArgumentException(
060: "Null FolderCache passed");
061: }
062: _app = app;
063: _model = (QueryTreeModel) getModel();
064: setRootVisible(false);
065: //setModel(_model);
066: setLayout(new BorderLayout());
067: setShowsRootHandles(true);
068: setEditable(true);
069:
070: // Add mouse listener for displaying popup menu.
071: addMouseListener(new MouseAdapter() {
072: public void mousePressed(MouseEvent evt) {
073: if (evt.isPopupTrigger()) {
074: displayPopupMenu(evt);
075: }
076: }
077:
078: public void mouseReleased(MouseEvent evt) {
079: if (evt.isPopupTrigger()) {
080: displayPopupMenu(evt);
081: }
082: }
083: });
084:
085: // addTreeExpansionListener(new MyExpansionListener());
086:
087: // Register so that we can display different tooltips depending
088: // which entry in tree mouse is over.
089: ToolTipManager.sharedInstance().registerComponent(this );
090:
091: /*
092: final TreeSelectionModel selModel = getSelectionModel();
093: if (selModel != null) {
094: selModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
095: }
096: */
097: }
098:
099: /**
100: * Return the name of the object that the mouse is currently
101: * over as the tooltip text.
102: *
103: * @param event Used to determine the current mouse position.
104: */
105: public String getToolTipText(MouseEvent evt) {
106: String tip = null;
107: final TreePath path = getPathForLocation(evt.getX(), evt.getY());
108: if (path != null) {
109: tip = path.getLastPathComponent().toString();
110: } else {
111: tip = super .getToolTipText();
112: }
113: return tip;
114: }
115:
116: QueryTreeModel getTypedModel() {
117: return (QueryTreeModel) getModel();
118: }
119:
120: /**
121: * Display the popup menu for the drivers list.
122: */
123: private void displayPopupMenu(MouseEvent evt) {
124: int x = evt.getX();
125: int y = evt.getY();
126: TreePath path = getPathForLocation(x, y);
127: _popupMenu.show(evt, path);
128: }
129:
130: /**
131: * Popup menu for this tree.
132: */
133: private class MyPopupMenu extends BasePopupMenu {
134: private static final long serialVersionUID = 1L;
135:
136: /** Set to <CODE>true</CODE> once list is built. */
137: private boolean _built = false;
138:
139: /**
140: * Show the menu. Build it if it hasn't already been built.
141: */
142: public void show(MouseEvent evt, TreePath path) {
143: if (!_built) {
144: ActionCollection actColl = QueryTree.this ._app
145: .getActionCollection();
146: add(actColl.get(NewSavedQueriesFolderAction.class));
147: addSeparator();
148: add(actColl.get(RenameSavedQueriesFolderAction.class));
149: addSeparator();
150: add(actColl.get(DeleteSavedQueriesFolderAction.class));
151: _built = true;
152: }
153: for (Iterator<BaseFavouriteAction> it = QueryTree.this ._actions
154: .iterator(); it.hasNext();) {
155: (it.next()).setTreePath(path);
156: }
157: super .show(evt);
158: }
159:
160: public JMenuItem add(Action action) {
161: if (action instanceof BaseFavouriteAction) {
162: ((BaseFavouriteAction) action)
163: .setQueryTree(QueryTree.this );
164: _actions.add((BaseFavouriteAction) action);
165: }
166: return super.add(action);
167: }
168: }
169: }
|