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 org.eclipse.jface.viewers.ITreeContentProvider;
10 import org.eclipse.jface.viewers.Viewer;
12 import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
14 public class BookmarkContentProvider implements ITreeContentProvider {
15 private Vector bookmarks = new Vector();
16 private static BookmarkContentProvider instance = null;
17 private boolean hasChanged = false;
22 public static synchronized BookmarkContentProvider getInstance() {
23 if (instance == null) {
24 instance = new BookmarkContentProvider();
29 private BookmarkContentProvider() {
32 public void importBookmarks(File file) {
34 System.out.println("Importing Bookmarks: Loading from file: " + file);
36 Properties props = new Properties();
37 FileInputStream in = new FileInputStream(file);
40 fromProperties(false, props);
41 } catch (Throwable e) {
45 public void load(File file) {
46 System.out.println("Bookmarks: Loading from file: " + file);
48 Properties props = new Properties();
49 FileInputStream in = new FileInputStream(file);
52 fromProperties(true, props);
53 } catch (Throwable e) {
57 public void save(File file) {
58 System.out.println("Bookmarks: Saving to file: " + file);
60 Properties props = getProperties();
61 FileOutputStream out = new FileOutputStream(file);
64 } catch (Throwable e) {
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();
76 return Root.EMPTY_ARRAY;
79 public Object[] getElements(Object inputElement) {
80 return getChildren(inputElement);
83 public Object getParent(Object element) {
84 if (element.equals(Root.ROOT)) {
86 } else if (element instanceof TreeNode) {
87 TreeNode node = (TreeNode) element;
88 return node.getParent();
93 public boolean hasChildren(Object element) {
94 if (element.equals(Root.ROOT)) {
96 } else if (element instanceof TreeNode) {
97 TreeNode node = (TreeNode) element;
98 return node.hasChildren();
103 public void setChildren(BookmarkNode b, Vector tables) {
104 b.setChildren(tables);
107 public void addBookmark(BookmarkNode b) {
109 if (!bookmarks.contains(b)) {
110 bookmarks.addElement(b);
113 public void removeBookmark(BookmarkNode b) {
115 if (bookmarks.contains(b)) {
116 bookmarks.removeElement(b);
120 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
123 public void dispose() {
126 public boolean hasChanged() {
130 public int getSize() {
131 return bookmarks.size();
134 public void fromProperties(boolean overwrite, Properties props) {
135 Vector newBookmarks = new Vector();
138 Bookmark bookmark = new BookmarkNode();
139 String name = props.getProperty(i + ".name");
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);
152 bookmark.setSchema("");
154 String type = props.getProperty(i + ".type");
156 bookmark.setType(type);
158 bookmark.setType("");
160 String driverFile = props.getProperty(i + ".driverLocation");
161 if (driverFile != null) {
162 bookmark.setDriverFile(driverFile);
164 bookmark.setDriverFile("");
166 System.out.println(bookmark.toString());
167 if (!bookmark.isEmpty()) {
168 newBookmarks.addElement(bookmark);
173 bookmarks = newBookmarks;
175 bookmarks.addAll(newBookmarks);
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());