001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.model;
024:
025: import java.sql.SQLException;
026:
027: import javax.swing.event.TreeModelEvent;
028: import javax.swing.tree.TreePath;
029:
030: import org.isqlviewer.bookmarks.BookmarkFolder;
031: import org.isqlviewer.bookmarks.BookmarkReference;
032: import org.isqlviewer.sql.embedded.EmbeddedDatabase;
033: import org.isqlviewer.swing.AbstractTreeModel;
034:
035: /**
036: * Datamodel for managing Bookmark objects similar to a local-filesystem.
037: * <p>
038: * This model mimics a file-system such that there are folders and bookmarks. This model provides various utility
039: * methods for creating paths and adding bookmarks. The filesystem if will is based of an internal hashmap of
040: * fully-qualified paths as the key(s) and for each path an array list of full-paths as sub paths and Bookmark object(s)
041: * represent a folder in a logical sense.
042: * <p>
043: * In terms of paths they are of the form '/my folder/sub-1/sub-2/' using '/' is obviously always the root-path as well
044: * as the root-node of this tree model.
045: * <p>
046: * This object provides it's own persistence methods to ensure that this model is properly encoded in UTF-8 for XML
047: * format. iSQL-Viewer will store a 'bookmarks.xml' file in the designated $isql.home property. The persistence
048: * mechanisims ensure UTF8 format by using Charset object of the nio packages.
049: *
050: * @see java.nio.charset.Charset
051: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
052: * @version 1.0
053: */
054: public class BookmarkTreeModel extends AbstractTreeModel {
055:
056: private BookmarkFolder rootFolder = null;
057:
058: /**
059: * @param rootFolder
060: */
061: public BookmarkTreeModel(BookmarkFolder rootFolder) {
062:
063: this .rootFolder = rootFolder;
064: }
065:
066: public Object getChild(Object parent, int index) {
067:
068: if (parent != null && parent instanceof BookmarkFolder) {
069: BookmarkFolder folder = (BookmarkFolder) parent;
070: return folder.getChild(index);
071: }
072: return null;
073: }
074:
075: public int getChildCount(Object parent) {
076:
077: if (parent != null && parent instanceof BookmarkFolder) {
078: BookmarkFolder folder = (BookmarkFolder) parent;
079: return folder.getChildCount();
080: }
081: return 0;
082: }
083:
084: public int getIndexOfChild(Object parent, Object child) {
085:
086: if (parent != null && parent instanceof BookmarkFolder) {
087: BookmarkFolder folder = (BookmarkFolder) parent;
088: return folder.indexOfChild(child);
089: }
090: return 0;
091: }
092:
093: public Object getRoot() {
094:
095: return rootFolder;
096: }
097:
098: public boolean isLeaf(Object node) {
099:
100: return (node == null || node instanceof BookmarkReference);
101: }
102:
103: public void valueForPathChanged(TreePath path, Object newValue) {
104:
105: System.out.println("path:" + path + "; " + newValue);
106: Object changedObject = path.getLastPathComponent();
107: String changedName = newValue.toString();
108: EmbeddedDatabase database = EmbeddedDatabase
109: .getSharedInstance();
110: if (changedObject instanceof BookmarkReference) {
111: BookmarkReference reference = (BookmarkReference) changedObject;
112: String previousName = reference.getName();
113: try {
114: reference.setName(changedName);
115: database.updateBookmark(reference);
116: } catch (SQLException e) {
117: reference.setName(previousName);
118: }
119: } else if (changedObject instanceof BookmarkFolder) {
120: BookmarkFolder folder = (BookmarkFolder) changedObject;
121: String previousName = folder.getName();
122: String existingPath = folder.getPath();
123: try {
124: folder.setName(changedName);
125: String newPath = folder.getPath();
126: database.renameBookmarkFolder(existingPath, newPath);
127: } catch (SQLException e) {
128: folder.setName(previousName);
129: }
130: }
131: }
132:
133: public void removeBookmark(BookmarkReference bookmark) {
134:
135: BookmarkFolder parentFolder = rootFolder.findChildPath(bookmark
136: .getPath());
137: parentFolder = (parentFolder == null) ? rootFolder
138: : parentFolder;
139:
140: Object[] objectPath = BookmarkFolder
141: .getPathElements(parentFolder);
142: Object[] children = new Object[] { bookmark };
143: int[] indicies = new int[] { parentFolder
144: .indexOfChild(bookmark) };
145: parentFolder.removeBookmark(bookmark);
146:
147: TreeModelEvent removeEvent = new TreeModelEvent(this ,
148: objectPath, indicies, children);
149: fireTreeNodesRemoved(removeEvent);
150: }
151:
152: public void removeBookmarkFolder(BookmarkFolder folder) {
153:
154: BookmarkFolder parentFolder = folder.getParentFolder();
155: parentFolder = (parentFolder == null) ? rootFolder
156: : parentFolder;
157:
158: Object[] objectPath = BookmarkFolder
159: .getPathElements(parentFolder);
160: Object[] children = new Object[] { folder };
161: int[] indicies = new int[] { parentFolder.indexOfChild(folder) };
162: parentFolder.removeFolder(folder);
163:
164: TreeModelEvent removeEvent = new TreeModelEvent(this ,
165: objectPath, indicies, children);
166: fireTreeNodesRemoved(removeEvent);
167: }
168:
169: public void addBookmark(BookmarkReference bookmark,
170: BookmarkFolder parentFolder) {
171:
172: parentFolder.addBookmark(bookmark);
173: Object[] objectPath = BookmarkFolder
174: .getPathElements(parentFolder);
175: Object[] children = new Object[] { bookmark };
176: int[] indicies = new int[] { parentFolder
177: .indexOfChild(bookmark) };
178:
179: TreeModelEvent addEvent = new TreeModelEvent(this , objectPath,
180: indicies, children);
181: fireTreeNodesInserted(addEvent);
182: }
183:
184: public void addBookmarkFolder(BookmarkFolder parentFolder,
185: String folderName) {
186:
187: BookmarkFolder childFolder = parentFolder
188: .addChildFolder(folderName);
189:
190: Object[] objectPath = BookmarkFolder
191: .getPathElements(parentFolder);
192: Object[] children = new Object[] { childFolder };
193: int[] indicies = new int[] { parentFolder
194: .indexOfChild(childFolder) };
195:
196: TreeModelEvent removeEvent = new TreeModelEvent(this ,
197: objectPath, indicies, children);
198: fireTreeNodesInserted(removeEvent);
199: }
200:
201: public void reload(BookmarkFolder parentFolder,
202: BookmarkReference bookmark) {
203:
204: Object[] objectPath = BookmarkFolder
205: .getPathElements(parentFolder);
206: Object[] children = new Object[] { bookmark };
207: int[] indicies = new int[] { parentFolder
208: .indexOfChild(bookmark) };
209: parentFolder.addBookmark(bookmark);
210:
211: TreeModelEvent changeEvent = new TreeModelEvent(this,
212: objectPath, indicies, children);
213: fireTreeNodesChanged(changeEvent);
214: }
215: }
|