1 package com.quantum.model;
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
6 import java.io.FileInputStream;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Iterator;
10 import java.util.List;
11 import java.util.Properties;
12 import java.util.Vector;
14 import com.quantum.IQuantumConstants;
15 import com.quantum.Messages;
16 import com.quantum.model.xml.ModelToXMLConverter;
17 import com.quantum.sql.metadata.MetaDataXMLInterface;
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
24 * The collection of database bookmarks that the Quantum plug-in knows about.
25 * This collection is loaded by the QuantumPlugin class before any Quantum plugin
26 * extension is invoked.
30 public class BookmarkCollection {
32 private static BookmarkCollection instance = new BookmarkCollection();
33 private List bookmarks = new Vector();
34 private boolean changed = false;
35 private PropertyChangeSupport support = new PropertyChangeSupport(this);
37 private BookmarkCollection() {
43 public static BookmarkCollection getInstance() {
44 return BookmarkCollection.instance;
48 * Imports the bookmars from a properties file. This load method is
49 * provided for backwards compatability only; we no longer persist
50 * bookmarks as properties files.
53 public void load(File file) throws IOException {
54 Properties props = new Properties();
55 FileInputStream in = new FileInputStream(file);
58 fromProperties(true, props);
61 private void fromProperties(boolean overwrite, Properties props) {
62 List newBookmarks = new Vector();
65 Bookmark bookmark = new Bookmark();
66 String name = props.getProperty(i + ".name"); //$NON-NLS-1$
70 bookmark.setName(name);
71 bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
72 bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
73 bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
74 bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
75 String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
77 bookmark.addSchema(schema);
79 String type = props.getProperty(i + ".type"); //$NON-NLS-1$
81 bookmark.setType(type);
83 bookmark.setType(""); //$NON-NLS-1$
85 String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
86 if (driverFile != null) {
87 bookmark.setDriverFile(driverFile);
89 bookmark.setDriverFile(""); //$NON-NLS-1$
91 System.out.println(bookmark.toString());
92 if (!bookmark.isEmpty()) {
93 newBookmarks.add(bookmark);
98 this.bookmarks = newBookmarks;
100 this.bookmarks.addAll(newBookmarks);
104 * Finds a Bookmark with the specified name.
107 * @return the bookmark with the specified name, or null if no bookmark can be found
109 public Bookmark find(String name){
110 Bookmark result = null;
112 for (Iterator i = this.bookmarks.iterator(); result == null && i.hasNext(); ) {
113 Bookmark temp = (Bookmark) i.next();
114 if (name.equals(temp.getName())) {
123 * Exports a Bookmark data to an XMLDocument Element
124 * The complementary function is importXML()
125 * @param root The Element to fill up with the bookmark info
127 public void exportXML(Element root) {
128 System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
129 Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
130 for (int i = 0; i < bookmarks.size(); i++) {
131 Bookmark b = (Bookmark) bookmarks.get(i);
132 ModelToXMLConverter.getInstance().convert(bookmarkRoot, b);
137 * Imports a Bookmark data from an XMLDocument Element
138 * The complementary function is exportXML()
139 * @param root The Element from which to load
141 public void importXML(Element root) {
143 System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
144 Vector newBookmarks = new Vector();
145 NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
146 for (int i = 0; i < nodes.getLength(); i++) {
147 Bookmark bookmark = new Bookmark();
148 Element column = (Element) nodes.item(i);
150 String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
151 if (name == null) break;
152 bookmark.setName(name);
154 MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
155 bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
156 bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
157 bookmark.setPromptForPassword(Boolean.TRUE.toString().equalsIgnoreCase(
158 MetaDataXMLInterface.getElementText(column,"prompt"))); //$NON-NLS-1$
159 bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
160 bookmark.setAutoCommit(Boolean.TRUE.toString().equalsIgnoreCase(
161 MetaDataXMLInterface.getElementText(column,"autoCommit", "True"))); //$NON-NLS-1$
162 bookmark.setAutoCommitPreference(MetaDataXMLInterface.getElementText(column,"autoCommitPreference", IQuantumConstants.autoCommitTrue)); //$NON-NLS-1$
163 bookmark.setDriver(MetaDataXMLInterface.getElementText(column,"driver")); //$NON-NLS-1$
164 bookmark.addSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
165 bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
166 bookmark.setDriverFile(MetaDataXMLInterface.getElementText(column,"driverLocation")); //$NON-NLS-1$
167 NodeList children = column.getElementsByTagName(Messages.getString("ExportXMLAction.OtherSchemas"));
168 if (children.getLength() > 0) {
169 importSchemas((Element) children.item(0), bookmark);
171 System.out.println(bookmark.toString());
172 if (!bookmark.isEmpty()) {
173 newBookmarks.addElement(bookmark);
175 importQuickList(bookmark, column);
176 importQueryList(bookmark, column);
178 this.bookmarks.addAll(newBookmarks);
179 this.support.firePropertyChange("bookmarks", null, null);
182 private void importSchemas(Element otherSchemas, Bookmark bookmark) {
183 Vector vector = MetaDataXMLInterface.getVectorText(otherSchemas, Messages.getString("ExportXMLAction.SchemaName"));
184 List list = new ArrayList();
185 for (Iterator i = vector.iterator(); i.hasNext();) {
186 String schemaName = (String) i.next();
187 list.add(new Schema(schemaName));
189 bookmark.setSchemas((Schema[]) list.toArray(new Schema[list.size()]));
192 private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
193 NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
195 length = (quickList == null) ? 0 : quickList.getLength();
199 Element element = (Element) quickList.item(j);
200 NodeList childNodes = element.getChildNodes();
203 length2 = (childNodes == null) ? 0 : childNodes.getLength();
206 if (Node.ELEMENT_NODE == childNodes.item(k).getNodeType()) {
207 Element entity = (Element) childNodes.item(k);
208 bookmark.addQuickListEntry(entity.getTagName(),
209 entity.getAttribute("schema"), entity.getAttribute("name"));
215 private void importQueryList(Bookmark bookmark, Element bookmarkElement) {
216 NodeList queryList = bookmarkElement.getElementsByTagName("queryList");
218 length = (queryList == null) ? 0 : queryList.getLength();
222 Element element = (Element) queryList.item(i);
223 NodeList childNodes = element.getElementsByTagName("query");
226 length2 = (childNodes == null) ? 0 : childNodes.getLength();
230 Element query = (Element) childNodes.item(k);
231 bookmark.addQuery(MetaDataXMLInterface.getElementText(query,"queryString"));
237 public void addBookmark(Bookmark b) {
239 if (!bookmarks.contains(b)) {
240 Bookmark[] original = getBookmarks();
242 this.support.firePropertyChange("bookmarks", original, getBookmarks());
245 public void removeBookmark(Bookmark b) {
247 if (bookmarks.contains(b)) {
248 Bookmark[] original = getBookmarks();
250 this.support.firePropertyChange("bookmarks", original, getBookmarks());
254 public Bookmark[] getBookmarks() {
255 return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
260 public boolean isAnythingChanged() {
261 boolean anythingChanged = this.changed;
262 for (Iterator i = this.bookmarks.iterator(); !anythingChanged && i.hasNext();) {
263 Bookmark bookmark = (Bookmark) i.next();
264 anythingChanged |= bookmark.isChanged();
266 return anythingChanged;
269 public boolean isChanged() {
276 public void setChanged(boolean changed) {
277 this.changed = changed;
283 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
284 this.support.addPropertyChangeListener(listener);
290 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
291 this.support.removePropertyChangeListener(listener);
298 public String getCopyName(String name) {
300 String copyName = Messages.getString("BookmarkView.CopyOf") + name;
302 while (find(copyName) != null)
304 copyName = Messages.getString("BookmarkView.CopyOf") + name + "(" + String.valueOf(i) + ")";