import java.util.Properties;
import java.util.Vector;
+import net.sourceforge.phpdt.sql.IConstants;
+import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
+
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
-import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
+public class BookmarkContentProvider implements ITreeContentProvider, IConstants {
+ private Vector bookmarks = new Vector();
+ private static BookmarkContentProvider instance = null;
+ private boolean hasChanged = false;
-public class BookmarkContentProvider implements ITreeContentProvider {
- private Vector bookmarks = new Vector();
- private static BookmarkContentProvider instance = null;
- private boolean hasChanged = false;
-
- /**
- * Singleton accessor
- */
- public static synchronized BookmarkContentProvider getInstance() {
- if (instance == null) {
- instance = new BookmarkContentProvider();
- }
- return instance;
- }
-
- private BookmarkContentProvider() {
- }
-
- public void importBookmarks(File file) {
- hasChanged = true;
- System.out.println("Importing Bookmarks: Loading from file: " + file);
- try {
- Properties props = new Properties();
- FileInputStream in = new FileInputStream(file);
- props.load(in);
- in.close();
- fromProperties(false, props);
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- public void load(File file) {
- System.out.println("Bookmarks: Loading from file: " + file);
- try {
- Properties props = new Properties();
- FileInputStream in = new FileInputStream(file);
- props.load(in);
- in.close();
- fromProperties(true, props);
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
- public void save(File file) {
- System.out.println("Bookmarks: Saving to file: " + file);
- try {
- Properties props = getProperties();
- FileOutputStream out = new FileOutputStream(file);
- props.store(out, "");
- out.close();
- } catch (Throwable e) {
- e.printStackTrace();
- }
- }
-
- public Object[] getChildren(Object parentElement) {
- if (parentElement.equals(Root.ROOT)) {
- return bookmarks.toArray();
- } else if (parentElement instanceof TreeNode) {
- TreeNode node = (TreeNode) parentElement;
- return node.getChildren();
- }
- return Root.EMPTY_ARRAY;
- }
-
- public Object[] getElements(Object inputElement) {
- return getChildren(inputElement);
- }
-
- public Object getParent(Object element) {
- if (element.equals(Root.ROOT)) {
- return null;
- } else if (element instanceof TreeNode) {
- TreeNode node = (TreeNode) element;
- return node.getParent();
- }
- return null;
- }
-
- public boolean hasChildren(Object element) {
- if (element.equals(Root.ROOT)) {
- return true;
- } else if (element instanceof TreeNode) {
- TreeNode node = (TreeNode) element;
- return node.hasChildren();
- }
- return false;
- }
-
- public void setChildren(BookmarkNode b, Vector tables) {
- b.setChildren(tables);
+ /**
+ * Singleton accessor
+ */
+ public static synchronized BookmarkContentProvider getInstance() {
+ if (instance == null) {
+ instance = new BookmarkContentProvider();
}
+ return instance;
+ }
+
+ private BookmarkContentProvider() {
+ }
- public void addBookmark(BookmarkNode b) {
- hasChanged = true;
- if (!bookmarks.contains(b)) {
- bookmarks.addElement(b);
- }
- }
- public void removeBookmark(BookmarkNode b) {
- hasChanged = true;
- if (bookmarks.contains(b)) {
- bookmarks.removeElement(b);
- }
- }
-
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
- }
-
- public void dispose() {
- }
-
- public boolean hasChanged() {
- return hasChanged;
- }
-
- public int getSize() {
- return bookmarks.size();
- }
-
- public void fromProperties(boolean overwrite, Properties props) {
- Vector newBookmarks = new Vector();
- int i = 0;
- while (true) {
- Bookmark bookmark = new BookmarkNode();
- String name = props.getProperty(i + ".name");
- if (name == null) {
- break;
- }
- bookmark.setName(name);
- bookmark.setUsername(props.getProperty(i + ".username"));
- bookmark.setPassword(props.getProperty(i + ".password"));
- bookmark.setConnect(props.getProperty(i + ".connect"));
- bookmark.setDriver(props.getProperty(i + ".driver"));
- String schema = props.getProperty(i + ".schema");
- if (schema != null) {
- bookmark.setSchema(schema);
- } else {
- bookmark.setSchema("");
- }
- String type = props.getProperty(i + ".type");
- if (type != null) {
- bookmark.setType(type);
- } else {
- bookmark.setType("");
- }
- String driverFile = props.getProperty(i + ".driverLocation");
- if (driverFile != null) {
- bookmark.setDriverFile(driverFile);
- } else {
- bookmark.setDriverFile("");
- }
- System.out.println(bookmark.toString());
- if (!bookmark.isEmpty()) {
- newBookmarks.addElement(bookmark);
- }
- i++;
- }
- if (overwrite) {
- bookmarks = newBookmarks;
- } else {
- bookmarks.addAll(newBookmarks);
- }
- }
- public Properties getProperties() {
- Properties props = new Properties();
- for (int i = 0; i < bookmarks.size(); i++) {
- Bookmark b = (Bookmark) bookmarks.elementAt(i);
- props.put(i + ".name", b.getName());
- props.put(i + ".username", b.getUsername());
- props.put(i + ".password", b.getPassword());
- props.put(i + ".schema", b.getSchema());
- props.put(i + ".connect", b.getConnect());
- props.put(i + ".driver", b.getDriver());
- props.put(i + ".type", b.getType());
- props.put(i + ".driverLocation", b.getDriverFile());
- }
- return props;
- }
+ public void importBookmarks(File file) {
+ hasChanged = true;
+ if (DEBUG) {
+ System.out.println("Importing Bookmarks: Loading from file: " + file);
+ }
+ try {
+ Properties props = new Properties();
+ FileInputStream in = new FileInputStream(file);
+ props.load(in);
+ in.close();
+ fromProperties(false, props);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+ public void load(File file) {
+ if (DEBUG) {
+ System.out.println("Bookmarks: Loading from file: " + file);
+ }
+ try {
+ Properties props = new Properties();
+ FileInputStream in = new FileInputStream(file);
+ props.load(in);
+ in.close();
+ fromProperties(true, props);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+ public void save(File file) {
+ if (DEBUG) {
+ System.out.println("Bookmarks: Saving to file: " + file);
+ }
+ try {
+ Properties props = getProperties();
+ FileOutputStream out = new FileOutputStream(file);
+ props.store(out, "");
+ out.close();
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+
+ public Object[] getChildren(Object parentElement) {
+ if (parentElement.equals(Root.ROOT)) {
+ return bookmarks.toArray();
+ } else if (parentElement instanceof TreeNode) {
+ TreeNode node = (TreeNode) parentElement;
+ return node.getChildren();
+ }
+ return Root.EMPTY_ARRAY;
+ }
+
+ public Object[] getElements(Object inputElement) {
+ return getChildren(inputElement);
+ }
+
+ public Object getParent(Object element) {
+ if (element.equals(Root.ROOT)) {
+ return null;
+ } else if (element instanceof TreeNode) {
+ TreeNode node = (TreeNode) element;
+ return node.getParent();
+ }
+ return null;
+ }
+
+ public boolean hasChildren(Object element) {
+ if (element.equals(Root.ROOT)) {
+ return true;
+ } else if (element instanceof TreeNode) {
+ TreeNode node = (TreeNode) element;
+ return node.hasChildren();
+ }
+ return false;
+ }
+
+ public void setChildren(BookmarkNode b, Vector tables) {
+ b.setChildren(tables);
+ }
+
+ public void addBookmark(BookmarkNode b) {
+ hasChanged = true;
+ if (!bookmarks.contains(b)) {
+ bookmarks.addElement(b);
+ }
+ }
+ public void removeBookmark(BookmarkNode b) {
+ hasChanged = true;
+ if (bookmarks.contains(b)) {
+ bookmarks.removeElement(b);
+ }
+ }
+
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ }
+
+ public void dispose() {
+ }
+
+ public boolean hasChanged() {
+ return hasChanged;
+ }
+
+ public int getSize() {
+ return bookmarks.size();
+ }
+
+ public void fromProperties(boolean overwrite, Properties props) {
+ Vector newBookmarks = new Vector();
+ int i = 0;
+ while (true) {
+ Bookmark bookmark = new BookmarkNode();
+ String name = props.getProperty(i + ".name");
+ if (name == null) {
+ break;
+ }
+ bookmark.setName(name);
+ bookmark.setUsername(props.getProperty(i + ".username"));
+ bookmark.setPassword(props.getProperty(i + ".password"));
+ bookmark.setConnect(props.getProperty(i + ".connect"));
+ bookmark.setDriver(props.getProperty(i + ".driver"));
+ String schema = props.getProperty(i + ".schema");
+ if (schema != null) {
+ bookmark.setSchema(schema);
+ } else {
+ bookmark.setSchema("");
+ }
+ String type = props.getProperty(i + ".type");
+ if (type != null) {
+ bookmark.setType(type);
+ } else {
+ bookmark.setType("");
+ }
+ String driverFile = props.getProperty(i + ".driverLocation");
+ if (driverFile != null) {
+ bookmark.setDriverFile(driverFile);
+ } else {
+ bookmark.setDriverFile("");
+ }
+ if (DEBUG) {
+ System.out.println(bookmark.toString());
+ }
+ if (!bookmark.isEmpty()) {
+ newBookmarks.addElement(bookmark);
+ }
+ i++;
+ }
+ if (overwrite) {
+ bookmarks = newBookmarks;
+ } else {
+ bookmarks.addAll(newBookmarks);
+ }
+ }
+ public Properties getProperties() {
+ Properties props = new Properties();
+ for (int i = 0; i < bookmarks.size(); i++) {
+ Bookmark b = (Bookmark) bookmarks.elementAt(i);
+ props.put(i + ".name", b.getName());
+ props.put(i + ".username", b.getUsername());
+ props.put(i + ".password", b.getPassword());
+ props.put(i + ".schema", b.getSchema());
+ props.put(i + ".connect", b.getConnect());
+ props.put(i + ".driver", b.getDriver());
+ props.put(i + ".type", b.getType());
+ props.put(i + ".driverLocation", b.getDriverFile());
+ }
+ return props;
+ }
}