1 package net.sourceforge.phpdt.sql.view.bookmark;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6 import java.util.Vector;
8 import org.eclipse.jface.viewers.ITreeContentProvider;
9 import org.eclipse.jface.viewers.Viewer;
10 import org.w3c.dom.Element;
11 import org.w3c.dom.NodeList;
13 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
14 import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
16 public class BookmarkContentProvider implements ITreeContentProvider {
17 private Vector bookmarks = new Vector();
18 private static BookmarkContentProvider instance = null;
19 private boolean hasChanged = false;
24 public static synchronized BookmarkContentProvider getInstance() {
25 if (instance == null) {
26 instance = new BookmarkContentProvider();
31 private BookmarkContentProvider() {
35 * Imports the bookmars from a File, supposed to be in XML format and have the correct tags.
38 public void load(File file) {
39 System.out.println("Bookmarks: Loading from file: " + file); //$NON-NLS-1$
41 Properties props = new Properties();
42 FileInputStream in = new FileInputStream(file);
45 fromProperties(true, props);
46 } catch (Throwable e) {
52 * Exports a Bookmark data to an XMLDocument Element
53 * The complementary function is importXML()
54 * @param root The Element to fill up with the bookmark info
56 public void exportXML(Element root) {
57 System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
58 Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
59 for (int i = 0; i < bookmarks.size(); i++) {
60 Bookmark b = (Bookmark) bookmarks.elementAt(i);
61 Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
62 MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
63 MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
64 MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
65 MetaDataXMLInterface.createElementText(bookmark,"schema", b.getSchema()); //$NON-NLS-1$
66 MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
67 MetaDataXMLInterface.createElementText(bookmark,"driver", b.getDriver()); //$NON-NLS-1$
68 MetaDataXMLInterface.createElementText(bookmark,"type", b.getType()); //$NON-NLS-1$
69 MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getDriverFile()); //$NON-NLS-1$
74 * Imports a Bookmark data from an XMLDocument Element
75 * The complementary function is exportXML()
76 * @param root The Element from which to load
78 public void importXML(Element root) {
79 System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
80 Vector newBookmarks = new Vector();
81 NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
82 for (int i = 0; i < nodes.getLength(); i++) {
83 Bookmark bookmark = new BookmarkNode();
84 Element column = (Element) nodes.item(i);
86 String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
87 if (name == null) break;
88 bookmark.setName(name);
90 MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
91 bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
92 bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
93 bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
94 bookmark.setDriver(MetaDataXMLInterface.getElementText(column,"driver")); //$NON-NLS-1$
95 bookmark.setSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
96 bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
97 bookmark.setDriverFile(MetaDataXMLInterface.getElementText(column,"driverLocation")); //$NON-NLS-1$
98 System.out.println(bookmark.toString());
99 if (!bookmark.isEmpty()) {
100 newBookmarks.addElement(bookmark);
103 bookmarks = newBookmarks;
106 public Object[] getChildren(Object parentElement) {
107 if (parentElement.equals(Root.ROOT)) {
108 return bookmarks.toArray();
109 } else if (parentElement instanceof TreeNode) {
110 TreeNode node = (TreeNode) parentElement;
111 return node.getChildren();
113 return Root.EMPTY_ARRAY;
116 * Finds a Bookmark with the specified name, and returs it. Null if nothing found.
120 public BookmarkNode find(String name){
121 for (int i = 0; i < bookmarks.size(); i++) {
122 BookmarkNode b = (BookmarkNode) bookmarks.elementAt(i);
123 if (b.getName().equals(name)) return b;
128 public Object[] getElements(Object inputElement) {
129 return getChildren(inputElement);
132 public Object getParent(Object element) {
133 if (element.equals(Root.ROOT)) {
135 } else if (element instanceof TreeNode) {
136 TreeNode node = (TreeNode) element;
137 return node.getParent();
142 public boolean hasChildren(Object element) {
143 if (element.equals(Root.ROOT)) {
145 } else if (element instanceof TreeNode) {
146 TreeNode node = (TreeNode) element;
147 return node.hasChildren();
152 public void setChildren(BookmarkNode b, Vector tables) {
153 b.setChildren(tables);
156 public void addBookmark(BookmarkNode b) {
158 if (!bookmarks.contains(b)) {
159 bookmarks.addElement(b);
162 public void removeBookmark(BookmarkNode b) {
164 if (bookmarks.contains(b)) {
165 bookmarks.removeElement(b);
169 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
172 public void dispose() {
175 public boolean hasChanged() {
179 public int getSize() {
180 return bookmarks.size();
183 public void fromProperties(boolean overwrite, Properties props) {
184 Vector newBookmarks = new Vector();
187 Bookmark bookmark = new BookmarkNode();
188 String name = props.getProperty(i + ".name"); //$NON-NLS-1$
192 bookmark.setName(name);
193 bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
194 bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
195 bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
196 bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
197 String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
198 if (schema != null) {
199 bookmark.setSchema(schema);
201 bookmark.setSchema(""); //$NON-NLS-1$
203 String type = props.getProperty(i + ".type"); //$NON-NLS-1$
205 bookmark.setType(type);
207 bookmark.setType(""); //$NON-NLS-1$
209 String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
210 if (driverFile != null) {
211 bookmark.setDriverFile(driverFile);
213 bookmark.setDriverFile(""); //$NON-NLS-1$
215 System.out.println(bookmark.toString());
216 if (!bookmark.isEmpty()) {
217 newBookmarks.addElement(bookmark);
222 bookmarks = newBookmarks;
224 bookmarks.addAll(newBookmarks);
227 public Properties getProperties() {
228 Properties props = new Properties();
229 for (int i = 0; i < bookmarks.size(); i++) {
230 Bookmark b = (Bookmark) bookmarks.elementAt(i);
231 props.put(i + ".name", b.getName()); //$NON-NLS-1$
232 props.put(i + ".username", b.getUsername()); //$NON-NLS-1$
233 props.put(i + ".password", b.getPassword()); //$NON-NLS-1$
234 props.put(i + ".schema", b.getSchema()); //$NON-NLS-1$
235 props.put(i + ".connect", b.getConnect()); //$NON-NLS-1$
236 props.put(i + ".driver", b.getDriver()); //$NON-NLS-1$
237 props.put(i + ".type", b.getType()); //$NON-NLS-1$
238 props.put(i + ".driverLocation", b.getDriverFile()); //$NON-NLS-1$