1 package net.sourceforge.phpdt.sql.view.bookmark;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.util.Properties;
7 import java.util.Vector;
9 import net.sourceforge.phpdt.sql.IConstants;
10 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
12 import org.eclipse.jface.viewers.ITreeContentProvider;
13 import org.eclipse.jface.viewers.Viewer;
15 public class BookmarkContentProvider implements ITreeContentProvider, IConstants {
16 private Vector bookmarks = new Vector();
17 private static BookmarkContentProvider instance = null;
18 private boolean hasChanged = false;
23 public static synchronized BookmarkContentProvider getInstance() {
24 if (instance == null) {
25 instance = new BookmarkContentProvider();
30 private BookmarkContentProvider() {
33 public void importBookmarks(File file) {
36 System.out.println("Importing Bookmarks: Loading from file: " + file);
39 Properties props = new Properties();
40 FileInputStream in = new FileInputStream(file);
43 fromProperties(false, props);
44 } catch (Throwable e) {
48 public void load(File file) {
50 System.out.println("Bookmarks: Loading from file: " + file);
53 Properties props = new Properties();
54 FileInputStream in = new FileInputStream(file);
57 fromProperties(true, props);
58 } catch (Throwable e) {
62 public void save(File file) {
64 System.out.println("Bookmarks: Saving to file: " + file);
67 Properties props = getProperties();
68 FileOutputStream out = new FileOutputStream(file);
71 } catch (Throwable e) {
76 public Object[] getChildren(Object parentElement) {
77 if (parentElement.equals(Root.ROOT)) {
78 return bookmarks.toArray();
79 } else if (parentElement instanceof TreeNode) {
80 TreeNode node = (TreeNode) parentElement;
81 return node.getChildren();
83 return Root.EMPTY_ARRAY;
86 public Object[] getElements(Object inputElement) {
87 return getChildren(inputElement);
90 public Object getParent(Object element) {
91 if (element.equals(Root.ROOT)) {
93 } else if (element instanceof TreeNode) {
94 TreeNode node = (TreeNode) element;
95 return node.getParent();
100 public boolean hasChildren(Object element) {
101 if (element.equals(Root.ROOT)) {
103 } else if (element instanceof TreeNode) {
104 TreeNode node = (TreeNode) element;
105 return node.hasChildren();
110 public void setChildren(BookmarkNode b, Vector tables) {
111 b.setChildren(tables);
114 public void addBookmark(BookmarkNode b) {
116 if (!bookmarks.contains(b)) {
117 bookmarks.addElement(b);
120 public void removeBookmark(BookmarkNode b) {
122 if (bookmarks.contains(b)) {
123 bookmarks.removeElement(b);
127 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
130 public void dispose() {
133 public boolean hasChanged() {
137 public int getSize() {
138 return bookmarks.size();
141 public void fromProperties(boolean overwrite, Properties props) {
142 Vector newBookmarks = new Vector();
145 Bookmark bookmark = new BookmarkNode();
146 String name = props.getProperty(i + ".name");
150 bookmark.setName(name);
151 bookmark.setUsername(props.getProperty(i + ".username"));
152 bookmark.setPassword(props.getProperty(i + ".password"));
153 bookmark.setConnect(props.getProperty(i + ".connect"));
154 bookmark.setDriver(props.getProperty(i + ".driver"));
155 String schema = props.getProperty(i + ".schema");
156 if (schema != null) {
157 bookmark.setSchema(schema);
159 bookmark.setSchema("");
161 String type = props.getProperty(i + ".type");
163 bookmark.setType(type);
165 bookmark.setType("");
167 String driverFile = props.getProperty(i + ".driverLocation");
168 if (driverFile != null) {
169 bookmark.setDriverFile(driverFile);
171 bookmark.setDriverFile("");
174 System.out.println(bookmark.toString());
176 if (!bookmark.isEmpty()) {
177 newBookmarks.addElement(bookmark);
182 bookmarks = newBookmarks;
184 bookmarks.addAll(newBookmarks);
187 public Properties getProperties() {
188 Properties props = new Properties();
189 for (int i = 0; i < bookmarks.size(); i++) {
190 Bookmark b = (Bookmark) bookmarks.elementAt(i);
191 props.put(i + ".name", b.getName());
192 props.put(i + ".username", b.getUsername());
193 props.put(i + ".password", b.getPassword());
194 props.put(i + ".schema", b.getSchema());
195 props.put(i + ".connect", b.getConnect());
196 props.put(i + ".driver", b.getDriver());
197 props.put(i + ".type", b.getType());
198 props.put(i + ".driverLocation", b.getDriverFile());