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 javax.swing.JPanel;
022: import javax.swing.event.TreeModelEvent;
023: import javax.swing.event.TreeModelListener;
024: import javax.swing.tree.DefaultTreeModel;
025:
026: import net.sourceforge.squirrel_sql.client.IApplication;
027: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
028: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
029:
030: final class QueryTreeModel extends DefaultTreeModel {
031:
032: /** Logger for this class. */
033: private static ILogger s_log = LoggerController
034: .createLogger(QueryTreeModel.class);
035:
036: private IApplication _app;
037: private FoldersCache _cache;
038:
039: private JPanel _emptyPnl = new JPanel();
040:
041: private MyModelListener _modelListener = new MyModelListener();
042:
043: QueryTreeModel(IApplication app, FoldersCache cache)
044: throws IllegalArgumentException {
045: super (new FolderNode(new Folder(null, "Root")));
046: if (app == null) {
047: throw new IllegalArgumentException(
048: "Null IApplication passed");
049: }
050: if (cache == null) {
051: throw new IllegalArgumentException(
052: "Null FoldersCache passed");
053: }
054: addTreeModelListener(_modelListener);
055: _app = app;
056: _cache = cache;
057: loadTree();
058: }
059:
060: private void loadTree() {
061: _modelListener.stopListening();
062: try {
063: Folder rootFolder = _cache.getRootFolder();
064: if (rootFolder != null) {
065: FolderNode rootNode = new FolderNode(rootFolder);
066: setRoot(rootNode);
067: loadSubFolders(rootNode);
068: } else {
069: rootFolder = new Folder(null, "Root");
070: setRoot(new FolderNode(rootFolder));
071: _cache.setRootFolder(rootFolder);
072: }
073: reload();
074: } finally {
075: _modelListener.startListening();
076: }
077: }
078:
079: private void loadSubFolders(FolderNode node) {
080: Folder[] subFolders = node.getFolder().getSubFolders();
081: for (int i = 0; i < subFolders.length; ++i) {
082: FolderNode childNode = new FolderNode(subFolders[i]);
083: node.add(childNode);
084: loadSubFolders(childNode);
085: }
086: }
087:
088: private static class MyModelListener implements TreeModelListener {
089: private boolean _listening = true;
090:
091: public void treeStructureChanged(TreeModelEvent evt) {
092: s_log.debug("treeStructureChanged");
093: }
094:
095: public void treeNodesInserted(TreeModelEvent evt) {
096: s_log.debug("treeNodesInserted");
097: }
098:
099: public void treeNodesChanged(TreeModelEvent evt) {
100: s_log.debug("treeNodesChanged");
101: Object objs[] = evt.getChildren();
102: if (objs != null) {
103: for (int i = 0; i < objs.length; ++i) {
104: FolderNode node = (FolderNode) objs[i];
105: Folder folder = node.getFolder();
106: String newName = node.toString();
107: String oldName = folder.getName();
108: s_log.debug(oldName);
109: if (!newName.equals(oldName)) {
110: try {
111: folder.setName(newName);
112: } catch (Exception ex) {
113: s_log.error("Error", ex); // ??
114: }
115: }
116: }
117: }
118: }
119:
120: public void treeNodesRemoved(TreeModelEvent evt) {
121: s_log.debug("treeNodesRemoved");
122: }
123:
124: void startListening() {
125: _listening = true;
126: }
127:
128: void stopListening() {
129: _listening = false;
130: }
131: }
132: }
|