1) Added missing strings for italic, underline and strike through.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / view / bookmark / BookmarkContentProvider.java
1 package net.sourceforge.phpdt.sql.view.bookmark;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.util.Properties;
6 import java.util.Vector;
7
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;
12
13 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
14 import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
15
16 public class BookmarkContentProvider implements ITreeContentProvider {
17         private Vector bookmarks = new Vector();
18         private static BookmarkContentProvider instance = null;
19         private boolean hasChanged = false;
20
21    /**
22     * Singleton accessor
23     */
24         public static synchronized BookmarkContentProvider getInstance() {
25                 if (instance == null) {
26                         instance = new BookmarkContentProvider();
27                 }
28                 return instance;
29         }
30
31         private BookmarkContentProvider() {
32         }
33
34         /**
35          * Imports the bookmars from a File, supposed to be in XML format and have the correct tags.
36          * @param file
37          */
38         public void load(File file) {
39                 System.out.println("Bookmarks: Loading from file: " + file); //$NON-NLS-1$
40                 try {
41                 Properties props = new Properties();
42                 FileInputStream in = new FileInputStream(file);
43                 props.load(in);
44                 in.close();
45                 fromProperties(true, props);
46                 } catch (Throwable e) {
47                         e.printStackTrace();
48                 }
49         }
50         
51         /**
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
55          */
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$
70                 }
71         }
72         
73         /**
74          * Imports a Bookmark data from an XMLDocument Element
75          * The complementary function is exportXML()
76          * @param root  The Element from which to load
77          */
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);
85                         
86                         String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
87                         if (name == null) break;
88                         bookmark.setName(name);
89                         
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);
101                         }
102                 }
103                 bookmarks = newBookmarks;
104         }
105         
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();
112                 }
113                 return Root.EMPTY_ARRAY;
114         }
115         /**
116          * Finds a Bookmark with the specified name, and returs it. Null if nothing found.
117          * @param name
118          * @return
119          */
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;
124                 }
125                 return null;
126         }
127         
128         public Object[] getElements(Object inputElement) {
129                 return getChildren(inputElement);
130         }
131
132         public Object getParent(Object element) {
133                 if (element.equals(Root.ROOT)) {
134                         return null;
135                 } else if (element instanceof TreeNode) {
136                         TreeNode node = (TreeNode) element;
137                         return node.getParent();
138                 }
139                 return null;
140         }
141
142         public boolean hasChildren(Object element) {
143                 if (element.equals(Root.ROOT)) {
144                         return true;
145                 } else if (element instanceof TreeNode) {
146                         TreeNode node = (TreeNode) element;
147                         return node.hasChildren();
148                 }
149                 return false;
150         }
151
152     public void setChildren(BookmarkNode b, Vector tables) {
153         b.setChildren(tables);
154     }
155
156         public void addBookmark(BookmarkNode b) {
157                 hasChanged = true;
158                 if (!bookmarks.contains(b)) {
159                         bookmarks.addElement(b);
160                 }
161         }
162         public void removeBookmark(BookmarkNode b) {
163                 hasChanged = true;
164                 if (bookmarks.contains(b)) {
165                         bookmarks.removeElement(b);
166                 }
167         }
168         
169         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
170         }
171         
172         public void dispose() {
173         }
174         
175         public boolean hasChanged() {
176                 return hasChanged;
177         }
178         
179         public int getSize() {
180                 return bookmarks.size();
181         }
182         
183         public void fromProperties(boolean overwrite, Properties props) {
184                 Vector newBookmarks = new Vector();
185                 int i = 0;
186                 while (true) {
187                         Bookmark bookmark = new BookmarkNode();
188                         String name = props.getProperty(i + ".name"); //$NON-NLS-1$
189                         if (name == null) {
190                                 break;
191                         }
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);
200                         } else {
201                                 bookmark.setSchema(""); //$NON-NLS-1$
202                         }
203                         String type = props.getProperty(i + ".type"); //$NON-NLS-1$
204                         if (type != null) {
205                                 bookmark.setType(type);
206                         } else {
207                                 bookmark.setType(""); //$NON-NLS-1$
208                         }
209                         String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
210                         if (driverFile != null) {
211                                 bookmark.setDriverFile(driverFile);
212                         } else {
213                                 bookmark.setDriverFile(""); //$NON-NLS-1$
214                         }
215                         System.out.println(bookmark.toString());
216                         if (!bookmark.isEmpty()) {
217                                 newBookmarks.addElement(bookmark);
218                         }
219                         i++;
220                 }
221                 if (overwrite) {
222                         bookmarks = newBookmarks;
223                 } else {
224                         bookmarks.addAll(newBookmarks);
225                 }
226         }
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$
239                 }
240                 return props;
241         }
242 }