ec997fafe076cf024bdfc2280389e1362f35d94c
[phpeclipse.git] /
1 package net.sourceforge.phpdt.sql.view.bookmark;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.util.Properties;
7 import java.util.Vector;
8
9 import org.eclipse.jface.viewers.ITreeContentProvider;
10 import org.eclipse.jface.viewers.Viewer;
11
12 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
13
14 public class BookmarkContentProvider implements ITreeContentProvider {
15         private Vector bookmarks = new Vector();
16         private static BookmarkContentProvider instance = null;
17         private boolean hasChanged = false;
18
19    /**
20     * Singleton accessor
21     */
22         public static synchronized BookmarkContentProvider getInstance() {
23                 if (instance == null) {
24                         instance = new BookmarkContentProvider();
25                 }
26                 return instance;
27         }
28
29         private BookmarkContentProvider() {
30         }
31
32         public void importBookmarks(File file) {
33                 hasChanged = true;
34                 System.out.println("Importing Bookmarks: Loading from file: " + file);
35                 try {
36                 Properties props = new Properties();
37                 FileInputStream in = new FileInputStream(file);
38                 props.load(in);
39                 in.close();
40                 fromProperties(false, props);
41                 } catch (Throwable e) {
42                         e.printStackTrace();
43                 }
44         }
45         public void load(File file) {
46                 System.out.println("Bookmarks: Loading from file: " + file);
47                 try {
48                 Properties props = new Properties();
49                 FileInputStream in = new FileInputStream(file);
50                 props.load(in);
51                 in.close();
52                 fromProperties(true, props);
53                 } catch (Throwable e) {
54                         e.printStackTrace();
55                 }
56         }
57         public void save(File file) {
58                 System.out.println("Bookmarks: Saving to file: " + file);
59                 try {
60                 Properties props = getProperties();
61                 FileOutputStream out = new FileOutputStream(file);
62                 props.store(out, "");
63                 out.close();
64                 } catch (Throwable e) {
65                         e.printStackTrace();
66                 }
67         }
68
69         public Object[] getChildren(Object parentElement) {
70                 if (parentElement.equals(Root.ROOT)) {
71                         return bookmarks.toArray();
72                 } else if (parentElement instanceof TreeNode) {
73                         TreeNode node = (TreeNode) parentElement;
74                         return node.getChildren();
75                 }
76                 return Root.EMPTY_ARRAY;
77         }
78
79         public Object[] getElements(Object inputElement) {
80                 return getChildren(inputElement);
81         }
82
83         public Object getParent(Object element) {
84                 if (element.equals(Root.ROOT)) {
85                         return null;
86                 } else if (element instanceof TreeNode) {
87                         TreeNode node = (TreeNode) element;
88                         return node.getParent();
89                 }
90                 return null;
91         }
92
93         public boolean hasChildren(Object element) {
94                 if (element.equals(Root.ROOT)) {
95                         return true;
96                 } else if (element instanceof TreeNode) {
97                         TreeNode node = (TreeNode) element;
98                         return node.hasChildren();
99                 }
100                 return false;
101         }
102
103     public void setChildren(BookmarkNode b, Vector tables) {
104         b.setChildren(tables);
105     }
106
107         public void addBookmark(BookmarkNode b) {
108                 hasChanged = true;
109                 if (!bookmarks.contains(b)) {
110                         bookmarks.addElement(b);
111                 }
112         }
113         public void removeBookmark(BookmarkNode b) {
114                 hasChanged = true;
115                 if (bookmarks.contains(b)) {
116                         bookmarks.removeElement(b);
117                 }
118         }
119         
120         public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
121         }
122         
123         public void dispose() {
124         }
125         
126         public boolean hasChanged() {
127                 return hasChanged;
128         }
129         
130         public int getSize() {
131                 return bookmarks.size();
132         }
133         
134         public void fromProperties(boolean overwrite, Properties props) {
135                 Vector newBookmarks = new Vector();
136                 int i = 0;
137                 while (true) {
138                         Bookmark bookmark = new BookmarkNode();
139                         String name = props.getProperty(i + ".name");
140                         if (name == null) {
141                                 break;
142                         }
143                         bookmark.setName(name);
144                         bookmark.setUsername(props.getProperty(i + ".username"));
145                         bookmark.setPassword(props.getProperty(i + ".password"));
146                         bookmark.setConnect(props.getProperty(i + ".connect"));
147                         bookmark.setDriver(props.getProperty(i + ".driver"));
148                         String schema = props.getProperty(i + ".schema");
149                         if (schema != null) {
150                                 bookmark.setSchema(schema);
151                         } else {
152                                 bookmark.setSchema("");
153                         }
154                         String type = props.getProperty(i + ".type");
155                         if (type != null) {
156                                 bookmark.setType(type);
157                         } else {
158                                 bookmark.setType("");
159                         }
160                         String driverFile = props.getProperty(i + ".driverLocation");
161                         if (driverFile != null) {
162                                 bookmark.setDriverFile(driverFile);
163                         } else {
164                                 bookmark.setDriverFile("");
165                         }
166                         System.out.println(bookmark.toString());
167                         if (!bookmark.isEmpty()) {
168                                 newBookmarks.addElement(bookmark);
169                         }
170                         i++;
171                 }
172                 if (overwrite) {
173                         bookmarks = newBookmarks;
174                 } else {
175                         bookmarks.addAll(newBookmarks);
176                 }
177         }
178         public Properties getProperties() {
179                 Properties props = new Properties();
180                 for (int i = 0; i < bookmarks.size(); i++) {
181                         Bookmark b = (Bookmark) bookmarks.elementAt(i);
182                         props.put(i + ".name", b.getName());
183                         props.put(i + ".username", b.getUsername());
184                         props.put(i + ".password", b.getPassword());
185                         props.put(i + ".schema", b.getSchema());
186                         props.put(i + ".connect", b.getConnect());
187                         props.put(i + ".driver", b.getDriver());
188                         props.put(i + ".type", b.getType());
189                         props.put(i + ".driverLocation", b.getDriverFile());
190                 }
191                 return props;
192         }
193 }