import java.io.File;
import java.io.FileInputStream;
-import java.io.FileOutputStream;
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 org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
-public class BookmarkContentProvider implements ITreeContentProvider, IConstants {
- 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;
- 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);
+import net.sourceforge.phpdt.sql.bookmarks.Bookmark;
+import net.sourceforge.phpdt.sql.sql.metadata.MetaDataXMLInterface;
+
+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() {
+ }
+
+ /**
+ * Imports the bookmars from a File, supposed to be in XML format and have the correct tags.
+ * @param file
+ */
+ public void load(File file) {
+ System.out.println("Bookmarks: Loading from file: " + file); //$NON-NLS-1$
+ try {
+ Properties props = new Properties();
+ FileInputStream in = new FileInputStream(file);
+ props.load(in);
+ in.close();
+ fromProperties(true, props);
+ } catch (Throwable e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * Exports a Bookmark data to an XMLDocument Element
+ * The complementary function is importXML()
+ * @param root The Element to fill up with the bookmark info
+ */
+ public void exportXML(Element root) {
+ System.out.println("Bookmarks: Saving to Element"); //$NON-NLS-1$
+ Element bookmarkRoot = MetaDataXMLInterface.createElementText(root,"bookmarks", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ for (int i = 0; i < bookmarks.size(); i++) {
+ Bookmark b = (Bookmark) bookmarks.elementAt(i);
+ Element bookmark = MetaDataXMLInterface.createElementText(bookmarkRoot,"bookmark", ""); //$NON-NLS-1$ //$NON-NLS-2$
+ MetaDataXMLInterface.createElementText(bookmark,"name", b.getName()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"username", b.getUsername()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"password", b.getPassword()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"schema", b.getSchema()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"connect", b.getConnect()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"driver", b.getDriver()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"type", b.getType()); //$NON-NLS-1$
+ MetaDataXMLInterface.createElementText(bookmark,"driverLocation", b.getDriverFile()); //$NON-NLS-1$
+ }
+ }
+
+ /**
+ * Imports a Bookmark data from an XMLDocument Element
+ * The complementary function is exportXML()
+ * @param root The Element from which to load
+ */
+ public void importXML(Element root) {
+ System.out.println("Bookmarks: Loading from Element"); //$NON-NLS-1$
+ Vector newBookmarks = new Vector();
+ NodeList nodes = root.getElementsByTagName("bookmark"); //$NON-NLS-1$
+ for (int i = 0; i < nodes.getLength(); i++) {
+ Bookmark bookmark = new BookmarkNode();
+ Element column = (Element) nodes.item(i);
+
+ String name = MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
+ if (name == null) break;
+ bookmark.setName(name);
+
+ MetaDataXMLInterface.getElementText(column,"name"); //$NON-NLS-1$
+ bookmark.setUsername(MetaDataXMLInterface.getElementText(column,"username")); //$NON-NLS-1$
+ bookmark.setPassword(MetaDataXMLInterface.getElementText(column,"password")); //$NON-NLS-1$
+ bookmark.setConnect(MetaDataXMLInterface.getElementText(column,"connect")); //$NON-NLS-1$
+ bookmark.setDriver(MetaDataXMLInterface.getElementText(column,"driver")); //$NON-NLS-1$
+ bookmark.setSchema(MetaDataXMLInterface.getElementText(column,"schema")); //$NON-NLS-1$
+ bookmark.setType(MetaDataXMLInterface.getElementText(column,"type")); //$NON-NLS-1$
+ bookmark.setDriverFile(MetaDataXMLInterface.getElementText(column,"driverLocation")); //$NON-NLS-1$
+ System.out.println(bookmark.toString());
+ if (!bookmark.isEmpty()) {
+ newBookmarks.addElement(bookmark);
+ }
+ }
+ bookmarks = newBookmarks;
+ }
+
+ 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;
+ }
+ /**
+ * Finds a Bookmark with the specified name, and returs it. Null if nothing found.
+ * @param name
+ * @return
+ */
+ public BookmarkNode find(String name){
+ for (int i = 0; i < bookmarks.size(); i++) {
+ BookmarkNode b = (BookmarkNode) bookmarks.elementAt(i);
+ if (b.getName().equals(name)) return b;
+ }
+ return null;
+ }
+
+ 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 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;
- }
+ 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"); //$NON-NLS-1$
+ if (name == null) {
+ break;
+ }
+ bookmark.setName(name);
+ bookmark.setUsername(props.getProperty(i + ".username")); //$NON-NLS-1$
+ bookmark.setPassword(props.getProperty(i + ".password")); //$NON-NLS-1$
+ bookmark.setConnect(props.getProperty(i + ".connect")); //$NON-NLS-1$
+ bookmark.setDriver(props.getProperty(i + ".driver")); //$NON-NLS-1$
+ String schema = props.getProperty(i + ".schema"); //$NON-NLS-1$
+ if (schema != null) {
+ bookmark.setSchema(schema);
+ } else {
+ bookmark.setSchema(""); //$NON-NLS-1$
+ }
+ String type = props.getProperty(i + ".type"); //$NON-NLS-1$
+ if (type != null) {
+ bookmark.setType(type);
+ } else {
+ bookmark.setType(""); //$NON-NLS-1$
+ }
+ String driverFile = props.getProperty(i + ".driverLocation"); //$NON-NLS-1$
+ if (driverFile != null) {
+ bookmark.setDriverFile(driverFile);
+ } else {
+ bookmark.setDriverFile(""); //$NON-NLS-1$
+ }
+ 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()); //$NON-NLS-1$
+ props.put(i + ".username", b.getUsername()); //$NON-NLS-1$
+ props.put(i + ".password", b.getPassword()); //$NON-NLS-1$
+ props.put(i + ".schema", b.getSchema()); //$NON-NLS-1$
+ props.put(i + ".connect", b.getConnect()); //$NON-NLS-1$
+ props.put(i + ".driver", b.getDriver()); //$NON-NLS-1$
+ props.put(i + ".type", b.getType()); //$NON-NLS-1$
+ props.put(i + ".driverLocation", b.getDriverFile()); //$NON-NLS-1$
+ }
+ return props;
+ }
}