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.Collections;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Properties;
15 import java.util.Vector;
17 import org.w3c.dom.Element;
18 import org.w3c.dom.Node;
19 import org.w3c.dom.NodeList;
21 import com.quantum.IQuantumConstants;
22 import com.quantum.Messages;
23 import com.quantum.model.xml.ModelToXMLConverter;
24 import com.quantum.sql.metadata.MetaDataXMLInterface;
27 * The collection of database bookmarks that the Quantum plug-in knows about.
28 * This collection is loaded by the QuantumPlugin class before any Quantum plugin
29 * extension is invoked.
33 public class BookmarkCollection {
35 private static BookmarkCollection instance = new BookmarkCollection();
36 private List bookmarks = new Vector();
37 private boolean changed = false;
38 private PropertyChangeSupport support = new PropertyChangeSupport(this);
39 private Set drivers = Collections.synchronizedSet(new HashSet());
41 private BookmarkCollection() {
47 public static BookmarkCollection getInstance() {
48 return BookmarkCollection.instance;
52 * Imports the bookmars from a properties file. This load method is
53 * provided for backwards compatability only; we no longer persist
54 * bookmarks as properties files.
57 public void load(File file) throws IOException {
58 Properties props = new Properties();
59 FileInputStream in = new FileInputStream(file);
62 fromProperties(true, props);
65 private void fromProperties(boolean overwrite, Properties props) {
66 List newBookmarks = new Vector();
69 Bookmark bookmark = new Bookmark();
70 String name = props.getProperty(i + ".name"); //$NON-NLS-1$
74 bookmark.setName(name);
75 bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
76 bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
77 bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
78 bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
79 String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
81 bookmark.addSchema(schema);
83 String type = props.getProperty(i + ".type"); //$NON-NLS-1$
85 bookmark.setType(type);
87 bookmark.setType(""); //$NON-NLS-1$
89 String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
90 if (driverFile != null) {
91 bookmark.setDriverFile(driverFile);
93 bookmark.setDriverFile(""); //$NON-NLS-1$
95 System.out.println(bookmark.toString());
96 if (!bookmark.isEmpty()) {
97 newBookmarks.add(bookmark);
100 this.drivers.add(new JDBCDriver(bookmark.getDriver(), driverFile));
103 this.bookmarks = newBookmarks;
105 this.bookmarks.addAll(newBookmarks);
109 * Finds a Bookmark with the specified name.
112 * @return the bookmark with the specified name, or null if no bookmark can be found
114 public Bookmark find(String name){
115 Bookmark result = null;
117 for (Iterator i = this.bookmarks.iterator(); result == null && i.hasNext(); ) {
118 Bookmark temp = (Bookmark) i.next();
119 if (name.equals(temp.getName())) {
128 * Exports a Bookmark data to an XMLDocument Element
129 * The complementary function is importXML()
130 * @param root The Element to fill up with the bookmark info
132 public void exportXML(Element root) {
133 System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
134 Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
135 for (Iterator i = this.drivers.iterator(); i.hasNext(); ) {
136 JDBCDriver driver = (JDBCDriver) i.next();
137 ModelToXMLConverter.getInstance().convert(bookmarkRoot, driver);
139 for (int i = 0; i < bookmarks.size(); i++) {
140 Bookmark b = (Bookmark) bookmarks.get(i);
141 ModelToXMLConverter.getInstance().convert(bookmarkRoot, b);
146 * Imports a Bookmark data from an XMLDocument Element
147 * The complementary function is exportXML()
148 * @param root The Element from which to load
150 public void importXML(Element root) {
152 System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
154 Vector newBookmarks = importBookmarks(root);
155 this.bookmarks.addAll(newBookmarks);
156 this.support.firePropertyChange("bookmarks", null, null);
163 private void importDrivers(Element root) {
164 NodeList nodes = root.getElementsByTagName("jdbcDriver"); //$NON-NLS-1$
165 for (int i = 0; i < nodes.getLength(); i++) {
166 Element driver = (Element) nodes.item(i);
167 addDriver(new JDBCDriver(
168 driver.getAttribute("className"),
169 driver.getAttribute("jarFileName"),
170 driver.getAttribute("name"),
171 driver.getAttribute("version")));
178 public void addDriver(JDBCDriver driver) {
179 if (!this.drivers.contains(driver)) {
180 this.drivers.add(driver);
181 this.support.firePropertyChange("drivers", null, driver);
189 private Vector importBookmarks(Element root) {
190 Vector newBookmarks = new Vector();
191 NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
192 for (int i = 0; i < nodes.getLength(); i++) {
193 Bookmark bookmark = new Bookmark();
194 Element column = (Element) nodes.item(i);
196 String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
197 if (name == null) break;
198 bookmark.setName(name);
200 MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
201 bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
202 bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
203 bookmark.setPromptForPassword(Boolean.TRUE.toString().equalsIgnoreCase(
204 MetaDataXMLInterface.getElementText(column,"prompt"))); //$NON-NLS-1$
205 bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
206 bookmark.setAutoCommit(Boolean.TRUE.toString().equalsIgnoreCase(
207 MetaDataXMLInterface.getElementText(column,"autoCommit", "True"))); //$NON-NLS-1$
208 bookmark.setAutoCommitPreference(MetaDataXMLInterface.getElementText(column,"autoCommitPreference", IQuantumConstants.autoCommitTrue)); //$NON-NLS-1$
209 bookmark.addSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
210 bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
211 NodeList children = column.getElementsByTagName(Messages.getString("ExportXMLAction.OtherSchemas"));
213 String driverClassName = MetaDataXMLInterface.getElementText(column,"driver"); //$NON-NLS-1$
214 String driverFile = MetaDataXMLInterface.getElementText(column,"driverLocation"); //$NON-NLS-1$
216 bookmark.setJDBCDriver(new JDBCDriver(driverClassName, driverFile));
218 if (children.getLength() > 0) {
219 importSchemas((Element) children.item(0), bookmark);
221 System.out.println(bookmark.toString());
222 if (!bookmark.isEmpty()) {
223 newBookmarks.addElement(bookmark);
225 importQuickList(bookmark, column);
226 importQueryList(bookmark, column);
231 private void importSchemas(Element otherSchemas, Bookmark bookmark) {
232 Vector vector = MetaDataXMLInterface.getVectorText(otherSchemas, Messages.getString("ExportXMLAction.SchemaName"));
233 List list = new ArrayList();
234 for (Iterator i = vector.iterator(); i.hasNext();) {
235 String schemaName = (String) i.next();
236 list.add(new Schema(schemaName));
238 bookmark.setSchemas((Schema[]) list.toArray(new Schema[list.size()]));
241 private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
242 NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
244 length = (quickList == null) ? 0 : quickList.getLength();
248 Element element = (Element) quickList.item(j);
249 NodeList childNodes = element.getChildNodes();
252 length2 = (childNodes == null) ? 0 : childNodes.getLength();
255 if (Node.ELEMENT_NODE == childNodes.item(k).getNodeType()) {
256 Element entity = (Element) childNodes.item(k);
257 bookmark.addQuickListEntry(entity.getTagName(),
258 entity.getAttribute("schema"), entity.getAttribute("name"));
264 private void importQueryList(Bookmark bookmark, Element bookmarkElement) {
265 NodeList queryList = bookmarkElement.getElementsByTagName("queryList");
267 length = (queryList == null) ? 0 : queryList.getLength();
271 Element element = (Element) queryList.item(i);
272 NodeList childNodes = element.getElementsByTagName("query");
275 length2 = (childNodes == null) ? 0 : childNodes.getLength();
279 Element query = (Element) childNodes.item(k);
280 bookmark.addQuery(MetaDataXMLInterface.getElementText(query,"queryString"));
286 public void addBookmark(Bookmark b) {
288 if (!bookmarks.contains(b)) {
289 Bookmark[] original = getBookmarks();
291 this.support.firePropertyChange("bookmarks", original, getBookmarks());
294 public void removeBookmark(Bookmark b) {
296 if (bookmarks.contains(b)) {
297 Bookmark[] original = getBookmarks();
299 this.support.firePropertyChange("bookmarks", original, getBookmarks());
303 public Bookmark[] getBookmarks() {
304 return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
307 public JDBCDriver[] getJDBCDrivers() {
308 return (JDBCDriver[]) this.drivers.toArray(new JDBCDriver[this.drivers.size()]);
313 public boolean isAnythingChanged() {
314 boolean anythingChanged = this.changed;
315 for (Iterator i = this.bookmarks.iterator(); !anythingChanged && i.hasNext();) {
316 Bookmark bookmark = (Bookmark) i.next();
317 anythingChanged |= bookmark.isChanged();
319 return anythingChanged;
322 public boolean isChanged() {
329 public void setChanged(boolean changed) {
330 this.changed = changed;
336 public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
337 this.support.addPropertyChangeListener(listener);
343 public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
344 this.support.removePropertyChangeListener(listener);
351 public String getCopyName(String name) {
353 String copyName = Messages.getString("BookmarkView.CopyOf") + name;
355 while (find(copyName) != null)
357 copyName = Messages.getString("BookmarkView.CopyOf") + name + "(" + String.valueOf(i) + ")";
368 public JDBCDriver findDriver(String driverClassName, String driverFile) {
369 JDBCDriver temp = new JDBCDriver(driverClassName, driverFile);
370 return findDriver(temp);
377 public JDBCDriver findDriver(JDBCDriver temp) {
378 JDBCDriver result = null;
379 for (Iterator i = this.drivers.iterator(); result == null && i.hasNext();) {
380 JDBCDriver driver = (JDBCDriver) i.next();
381 if (temp.equals(driver)) {
385 if (result == null) {