initial quantum version
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / QuantumPlugin.java
diff --git a/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/QuantumPlugin.java b/archive/net.sourceforge.phpeclipse.quantum.sql/src/com/quantum/QuantumPlugin.java
new file mode 100644 (file)
index 0000000..154604a
--- /dev/null
@@ -0,0 +1,310 @@
+package com.quantum;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.net.URL;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+
+import com.quantum.model.BookmarkCollection;
+import com.quantum.util.xml.XMLHelper;
+import com.quantum.view.subset.SubsetContentProvider;
+
+import org.eclipse.core.resources.ISaveContext;
+import org.eclipse.core.resources.ISaveParticipant;
+import org.eclipse.core.resources.ISavedState;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IPluginDescriptor;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.preference.PreferenceConverter;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.graphics.FontData;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.RGB;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchPart;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.xml.sax.SAXException;
+
+/**
+ * @author root
+ * Main class of the quantum plugin, sets defaults, saves and recovers state.
+ */
+public class QuantumPlugin extends AbstractUIPlugin {
+     
+       private static QuantumPlugin plugin;
+       private Clipboard sysClip;
+
+       
+       public QuantumPlugin(IPluginDescriptor descriptor) {
+               super(descriptor);
+               plugin = this;
+       }
+
+       public static QuantumPlugin getDefault() {
+               return plugin;
+       }
+       /**
+        * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
+        * @param target
+        */
+       protected void readStateFrom(File target) {
+               String fileName = target.getName();
+               if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
+            try {
+                       // It's the 2.0 format for preferences
+                       BookmarkCollection.getInstance().load(target);
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+               } else {
+                       //It's the 2.1 format for preferences and subsets
+                       FileInputStream source = null;
+                       try {
+                               source = new FileInputStream(target);
+                       } catch (FileNotFoundException e1) {
+                               e1.printStackTrace();
+                               return;
+                       }
+                       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+                       DocumentBuilder parser;
+                       try {
+                               parser = factory.newDocumentBuilder();
+                Document doc = parser.parse(source);
+
+                Element root = doc.getDocumentElement();
+                BookmarkCollection.getInstance().importXML(root);
+                BookmarkCollection.getInstance().setChanged(false);
+                SubsetContentProvider.getInstance().importXML(root);
+
+                       } catch (ParserConfigurationException e) {
+                               e.printStackTrace();
+                       } catch (SAXException e) {
+                               e.printStackTrace();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }               
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.core.runtime.Plugin#startup()
+        */
+       public void startup() throws CoreException {
+               super.startup();
+               ISaveParticipant saveParticipant = new QuantumSaveParticipant();
+               ISavedState lastState =
+                       ResourcesPlugin.getWorkspace().addSaveParticipant(
+                               this,
+                               saveParticipant);
+               if (lastState != null) {
+               IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
+               if (location != null) {
+                       // the plugin instance should read any important state from the file. 
+                       File f = getStateLocation().append(location).toFile();
+                       readStateFrom(f);
+                       
+            }
+        }
+        sysClip = new Clipboard(null);
+       }
+
+       /* (non-Javadoc)
+        * @see org.eclipse.core.runtime.Plugin#shutdown()
+        */
+       public void shutdown() throws CoreException {
+               super.shutdown();
+               sysClip.dispose();
+       }
+
+       /**
+        * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
+        * @param target
+        */
+       protected void writeImportantState(File target) {
+        try {
+            Document document = XMLHelper.createEmptyDocument();
+            
+               Element root = (Element) document.appendChild(
+                document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
+               
+               BookmarkCollection.getInstance().exportXML(root);
+               SubsetContentProvider.getInstance().exportXML(root);
+
+            FileWriter writer =  new FileWriter(target);
+            try {
+                XMLHelper.createDOMSerializer(writer).serialize(document);
+               } finally {
+                       writer.close();
+               }
+        } catch (ParserConfigurationException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+       }
+       
+       /**
+        *  Gets an image descriptof from a file in the icons directory 
+        * @param name of the file to get
+        * @return ImageDescriptor or null if not found
+        */
+       public static ImageDescriptor getImageDescriptor(String name) {
+               ImageDescriptor descriptor = null;
+               try {
+                       URL installURL =
+                               QuantumPlugin.getDefault().getDescriptor().getInstallURL();
+                       URL url = new URL(installURL, Messages.getString("QuantumPlugin.iconsDir") + name); //$NON-NLS-1$
+                       descriptor = ImageDescriptor.createFromURL(url);
+               } catch (Exception e) {
+                       e.printStackTrace();
+               }
+               return descriptor;
+       }
+       public static Image getImage(String name) {
+        ImageDescriptor imageDescriptor = getImageDescriptor(name);
+               return imageDescriptor == null ? null : imageDescriptor.createImage();
+       }
+
+       protected void initializeDefaultPluginPreferences() {
+               RGB BACKGROUND = new RGB(255, 255, 255);
+               RGB COMMENT = new RGB(88, 148, 64);
+               RGB KEYWORD = new RGB(126, 0, 75);
+               RGB STRING = new RGB(0, 0, 255);
+               RGB NUMERIC = new RGB(255, 0, 0);
+               RGB DEFAULT = new RGB(0, 0, 0);
+               IPreferenceStore store = getPreferenceStore();
+               PreferenceConverter.setDefault(store,
+                       "quantum.background.color", BACKGROUND); //$NON-NLS-1$
+               PreferenceConverter.setDefault(store,
+                       "quantum.text.color", DEFAULT); //$NON-NLS-1$
+               PreferenceConverter.setDefault(store,
+                       "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
+               PreferenceConverter.setDefault(store,
+                       "quantum.comment.color", COMMENT); //$NON-NLS-1$
+               PreferenceConverter.setDefault(store,
+                       "quantum.string.color", STRING); //$NON-NLS-1$
+               PreferenceConverter.setDefault(store,
+                       "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
+               getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
+               getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
+               getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
+               getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
+               getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
+               PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
+        getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
+       }
+       // Returns the active page
+       public IWorkbenchPage getActivePage()
+       {
+               IWorkbench workbench = getWorkbench();
+               IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
+               if (window == null)     return null;
+               IWorkbenchPage page = window.getActivePage();
+               return page;
+       }
+       /**
+        * returns a view in the active page, creating it if needed
+        * @param view, the name of the view (e.g com.quantum.view.tableview)
+        * @return true if successful, false if not
+        */
+       public IViewPart getView(String view)
+       {
+               IViewPart tableView = null;
+               try {
+                       IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
+                       tableView =  page.findView(view);
+                       if (tableView == null){
+                               // showView will give focus to the created view, we don't want that
+                               // so we save the active part
+                               IWorkbenchPart part = page.getActivePart();
+                               tableView = page.showView(view);
+                               // and return the focus to it
+                               page.activate(part);
+                       }
+               } catch (PartInitException e) {
+                       e.printStackTrace();
+               }
+               return tableView;
+       }
+
+
+
+       class QuantumSaveParticipant implements ISaveParticipant {
+               /**
+               * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
+               */
+               public void doneSaving(ISaveContext context) {
+               }
+               /**
+                * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
+                */
+               public void prepareToSave(ISaveContext context) throws CoreException {
+               }
+
+               /**
+                * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
+                */
+               public void rollback(ISaveContext context) {
+               }
+
+               /**
+                * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
+                */
+               public void saving(ISaveContext context) throws CoreException {
+                       switch (context.getKind()) {
+                               case ISaveContext.FULL_SAVE :
+                                       QuantumPlugin quantumPluginInstance = QuantumPlugin.getDefault();
+                                       // save the plug in state 
+                                       if (BookmarkCollection.getInstance().isAnythingChanged()
+                                               || SubsetContentProvider.getInstance().hasChanged()) {
+
+                                               int saveNumber = context.getSaveNumber();
+                                               String saveFileName = Messages.getString("QuantumPlugin.saveDir") + "-" + Integer.toString(saveNumber) + Messages.getString("QuantumPlugin.saveFileExtension"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+                                               File f = quantumPluginInstance.getStateLocation().append(saveFileName).toFile();
+
+                                               // if we fail to write, an exception is thrown and we do not update the path 
+                                               quantumPluginInstance.writeImportantState(f);
+
+                                               context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
+                                               context.needSaveNumber();
+
+                                       } else {
+                                               System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
+                                       }
+                                       break;
+                               case ISaveContext.PROJECT_SAVE :
+                                       // get the project related to this save operation 
+                                       //IProject project = context.getProject(); 
+                                       // save its information, if necessary 
+                                       break;
+                               case ISaveContext.SNAPSHOT :
+                                       // This operation needs to be really fast because 
+                                       // snapshots can be requested frequently by the 
+                                       // workspace. 
+                                       break;
+                       }
+               }
+       }
+       /**
+        * @return
+        */
+       public Clipboard getSysClip() {
+               return sysClip;
+       }
+}
\ No newline at end of file