001: /*
002: * Copyright (C) 2003 Joseph Mocker
003: * mock-sf@misfit.dhs.org
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019:
020: package net.sourceforge.squirrel_sql.plugins.sqlbookmark;
021:
022: import java.io.File;
023: import java.io.IOException;
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Vector;
028:
029: import net.sourceforge.squirrel_sql.fw.completion.CompletionCandidates;
030: import net.sourceforge.squirrel_sql.fw.completion.ICompletorModel;
031: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanReader;
032: import net.sourceforge.squirrel_sql.fw.xml.XMLBeanWriter;
033: import net.sourceforge.squirrel_sql.fw.xml.XMLException;
034:
035: /**
036: * Manages the users bookmarks. Including loading and saving to
037: * an XML file.
038: *
039: * @author Joseph Mocker
040: */
041: public class BookmarkManager implements ICompletorModel {
042:
043: /**
044: * The file to save/load bookmarks to/from
045: */
046: private File bookmarkFile;
047:
048: /**
049: * List of all the loaded bookmarks
050: */
051: private ArrayList<Bookmark> bookmarks = new ArrayList<Bookmark>();
052:
053: /**
054: * Index of bookmark names to indexes in the bookmarks array
055: */
056: private HashMap<String, Integer> bookmarkIdx = new HashMap<String, Integer>();
057: private SQLBookmarkPlugin _plugin;
058:
059: public BookmarkManager(SQLBookmarkPlugin plugin) {
060: try {
061: _plugin = plugin;
062: bookmarkFile = new File(_plugin
063: .getPluginUserSettingsFolder(), "bookmarks.xml");
064: } catch (IOException e) {
065: throw new RuntimeException(e);
066: }
067: }
068:
069: /**
070: * Add a new bookmark, or replace an existing bookmark.
071: *
072: * @param bookmark bookmark to add/change.
073: * @return true if a replacement, false if a new bookmark.
074: */
075: protected boolean add(Bookmark bookmark) {
076: Integer idxInt = bookmarkIdx.get(bookmark.getName());
077: if (idxInt != null) {
078: bookmarks.set(idxInt.intValue(), bookmark);
079: return true;
080: } else {
081: bookmarks.add(bookmark);
082: idxInt = bookmarks.size() - 1;
083: bookmarkIdx.put(bookmark.getName(), idxInt);
084: return false;
085: }
086: }
087:
088: /**
089: * Retrieve a bookmark by name.
090: *
091: * @param name Name of the bookmark.
092: * @return the bookmark.
093: */
094: protected Bookmark get(String name) {
095: Integer idxInt = bookmarkIdx.get(name);
096: if (idxInt != null) {
097: return bookmarks.get(idxInt.intValue());
098: }
099: return null;
100: }
101:
102: /**
103: * Load the stored bookmarks.
104: */
105: protected void load() throws IOException {
106:
107: try {
108: XMLBeanReader xmlin = new XMLBeanReader();
109:
110: if (bookmarkFile.exists()) {
111: xmlin.load(bookmarkFile, getClass().getClassLoader());
112: for (Iterator<?> i = xmlin.iterator(); i.hasNext();) {
113: Object bean = i.next();
114: if (bean instanceof Bookmark) {
115: add((Bookmark) bean);
116: }
117: }
118: }
119: } catch (XMLException e) {
120: throw new RuntimeException(e);
121: }
122: }
123:
124: /**
125: * Save the bookmarks.
126: */
127: protected void save() {
128: try {
129: XMLBeanWriter xmlout = new XMLBeanWriter();
130:
131: for (Iterator<Bookmark> i = bookmarks.iterator(); i
132: .hasNext();) {
133: Bookmark bookmark = i.next();
134:
135: xmlout.addToRoot(bookmark);
136: }
137:
138: xmlout.save(bookmarkFile);
139: } catch (Exception e) {
140: throw new RuntimeException(e);
141: }
142: }
143:
144: protected Iterator<Bookmark> iterator() {
145: return bookmarks.iterator();
146: }
147:
148: public CompletionCandidates getCompletionCandidates(
149: String bookmarkNameBegin) {
150: Vector<BookmarkCompletionInfo> ret = new Vector<BookmarkCompletionInfo>();
151:
152: int maxNameLen = 0;
153: for (int i = 0; i < bookmarks.size(); i++) {
154: Bookmark bookmark = bookmarks.get(i);
155: if (bookmark.getName().startsWith(bookmarkNameBegin)) {
156: ret.add(new BookmarkCompletionInfo(bookmark));
157: maxNameLen = Math.max(maxNameLen, bookmark.getName()
158: .length());
159: }
160: }
161:
162: String defaultMarksInPopup = _plugin
163: .getBookmarkProperties()
164: .getProperty(
165: SQLBookmarkPlugin.BOOKMARK_PROP_DEFAULT_MARKS_IN_POPUP,
166: "" + false);
167:
168: if (Boolean.valueOf(defaultMarksInPopup).booleanValue()) {
169: Bookmark[] defaultBookmarks = DefaultBookmarksFactory
170: .getDefaultBookmarks();
171:
172: for (int i = 0; i < defaultBookmarks.length; i++) {
173: if (defaultBookmarks[i].getName().startsWith(
174: bookmarkNameBegin)) {
175: ret.add(new BookmarkCompletionInfo(
176: defaultBookmarks[i]));
177: maxNameLen = Math.max(maxNameLen,
178: defaultBookmarks[i].getName().length());
179: }
180: }
181: }
182:
183: BookmarkCompletionInfo[] candidates = ret
184: .toArray(new BookmarkCompletionInfo[ret.size()]);
185:
186: for (int i = 0; i < candidates.length; i++) {
187: candidates[i].setMaxCandidateNameLen(maxNameLen);
188: }
189:
190: return new CompletionCandidates(candidates);
191: }
192:
193: public void removeAll() {
194: bookmarks = new ArrayList<Bookmark>();
195: bookmarkIdx = new HashMap<String, Integer>();
196: }
197: }
|