initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / model / BookmarkCollection.java
1 package com.quantum.model;
2
3 import java.beans.PropertyChangeListener;
4 import java.beans.PropertyChangeSupport;
5 import java.io.File;
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;
13
14 import com.quantum.IQuantumConstants;
15 import com.quantum.Messages;
16 import com.quantum.model.xml.ModelToXMLConverter;
17 import com.quantum.sql.metadata.MetaDataXMLInterface;
18
19 import org.w3c.dom.Element;
20 import org.w3c.dom.Node;
21 import org.w3c.dom.NodeList;
22
23 /**
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.
27  * 
28  * @author BC
29  */
30 public class BookmarkCollection {
31     
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);
36     
37     private BookmarkCollection() {
38     }
39
40     /**
41      * Singleton accessor
42      */
43     public static BookmarkCollection getInstance() {
44         return BookmarkCollection.instance;
45     }
46     
47     /**
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.
51      * @param file
52      */
53     public void load(File file) throws IOException {
54         Properties props = new Properties();
55         FileInputStream in = new FileInputStream(file);
56         props.load(in);
57         in.close();
58         fromProperties(true, props);
59     }
60      
61     private void fromProperties(boolean overwrite, Properties props) {
62         List newBookmarks = new Vector();
63         int i = 0;
64         while (true) {
65             Bookmark bookmark = new Bookmark();
66             String name = props.getProperty(i + ".name"); //$NON-NLS-1$
67             if (name == null) {
68                 break;
69             }
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$
76             if (schema != null) {
77                 bookmark.addSchema(schema);
78             }
79             String type = props.getProperty(i + ".type"); //$NON-NLS-1$
80             if (type != null) {
81                 bookmark.setType(type);
82             } else {
83                 bookmark.setType(""); //$NON-NLS-1$
84             }
85             String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
86             if (driverFile != null) {
87                 bookmark.setDriverFile(driverFile);
88             } else {
89                 bookmark.setDriverFile(""); //$NON-NLS-1$
90             }
91             System.out.println(bookmark.toString());
92             if (!bookmark.isEmpty()) {
93                 newBookmarks.add(bookmark);
94             }
95             i++;
96         }
97         if (overwrite) {
98             this.bookmarks = newBookmarks;
99         } else {
100             this.bookmarks.addAll(newBookmarks);
101         }
102     }
103     /**
104      * Finds a Bookmark with the specified name.
105      * 
106      * @param name
107      * @return the bookmark with the specified name, or null if no bookmark can be found
108      */
109     public Bookmark find(String name){
110         Bookmark result = null;
111         if (name != null) {
112             for (Iterator i = this.bookmarks.iterator(); result == null && i.hasNext(); ) {
113                 Bookmark temp = (Bookmark) i.next();
114                 if (name.equals(temp.getName())) {
115                     result = temp;
116                 }
117             }
118         }
119         return result;
120     }
121
122     /**
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
126      */
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);
133         }
134     }
135
136     /**
137      * Imports a Bookmark data from an XMLDocument Element
138      * The complementary function is exportXML()
139      * @param root  The Element from which to load
140      */
141     public void importXML(Element root) {
142         this.changed = true;
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);
149             
150             String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
151             if (name == null) break;
152             bookmark.setName(name);
153             
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);
170             }
171             System.out.println(bookmark.toString());
172             if (!bookmark.isEmpty()) {
173                 newBookmarks.addElement(bookmark);
174             }
175             importQuickList(bookmark, column);
176             importQueryList(bookmark, column);
177         }
178         this.bookmarks.addAll(newBookmarks);
179         this.support.firePropertyChange("bookmarks", null, null);
180     }
181
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));
188         }
189         bookmark.setSchemas((Schema[]) list.toArray(new Schema[list.size()]));
190     }
191
192     private void importQuickList(Bookmark bookmark, Element bookmarkElement) {
193         NodeList quickList = bookmarkElement.getElementsByTagName("quickList");
194         for (int j = 0,
195             length = (quickList == null) ? 0 : quickList.getLength();
196             j < length;
197             j++) {
198             
199             Element element = (Element) quickList.item(j);
200             NodeList childNodes = element.getChildNodes();
201                 
202             for (int k = 0,
203                 length2 = (childNodes == null) ? 0 : childNodes.getLength();
204                 k < length2;
205                 k++) {
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"));
210                 }
211             }
212         }
213     }
214     
215     private void importQueryList(Bookmark bookmark, Element bookmarkElement) {
216         NodeList queryList = bookmarkElement.getElementsByTagName("queryList");
217         for (int i = 0,
218             length = (queryList == null) ? 0 : queryList.getLength();
219             i < length;
220             i++) {
221             
222             Element element = (Element) queryList.item(i);
223             NodeList childNodes = element.getElementsByTagName("query");
224                 
225             for (int k = 0,
226                 length2 = (childNodes == null) ? 0 : childNodes.getLength();
227                 k < length2;
228                 k++) {
229
230                 Element query = (Element) childNodes.item(k);
231                 bookmark.addQuery(MetaDataXMLInterface.getElementText(query,"queryString"));
232         
233             }
234         }
235     }
236     
237     public void addBookmark(Bookmark b) {
238         this.changed = true;
239         if (!bookmarks.contains(b)) {
240             Bookmark[] original = getBookmarks();
241             bookmarks.add(b);
242             this.support.firePropertyChange("bookmarks", original, getBookmarks());
243         }
244     }
245     public void removeBookmark(Bookmark b) {
246         this.changed = true;
247         if (bookmarks.contains(b)) {
248             Bookmark[] original = getBookmarks();
249             bookmarks.remove(b);
250             this.support.firePropertyChange("bookmarks", original, getBookmarks());
251         }
252     }
253     
254     public Bookmark[] getBookmarks() {
255         return (Bookmark[]) this.bookmarks.toArray(new Bookmark[this.bookmarks.size()]);
256     }
257     /**
258      * @return
259      */
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();
265         }
266         return anythingChanged;
267     }
268     
269     public boolean isChanged() {
270         return this.changed;
271     }
272
273     /**
274      * @param b
275      */
276     public void setChanged(boolean changed) {
277         this.changed = changed;
278     }
279
280     /**
281      * @param listener
282      */
283     public synchronized void addPropertyChangeListener(PropertyChangeListener listener) {
284         this.support.addPropertyChangeListener(listener);
285     }
286
287     /**
288      * @param listener
289      */
290     public synchronized void removePropertyChangeListener(PropertyChangeListener listener) {
291         this.support.removePropertyChangeListener(listener);
292     }
293
294         /**
295          * @param string
296          * @return
297          */
298         public String getCopyName(String name) {
299                 
300                 String copyName = Messages.getString("BookmarkView.CopyOf") + name;
301                 int i = 1;
302                 while (find(copyName) != null)
303                 {
304                         copyName = Messages.getString("BookmarkView.CopyOf") + name + "(" + String.valueOf(i) + ")";
305                         i++;
306                 }
307                 
308                 return copyName;
309         }
310
311
312 }