Quantum version 2.4.2
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / QuantumPlugin.java
1 package com.quantum;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.net.MalformedURLException;
9 import java.net.URL;
10
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import javax.xml.parsers.ParserConfigurationException;
14
15 import com.quantum.model.BookmarkCollection;
16 import com.quantum.util.xml.XMLHelper;
17 import com.quantum.view.subset.SubsetContentProvider;
18
19 import org.eclipse.core.resources.ISavedState;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IPluginDescriptor;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferenceConverter;
27 import org.eclipse.jface.resource.ImageRegistry;
28 import org.eclipse.jface.resource.JFaceResources;
29 import org.eclipse.swt.dnd.Clipboard;
30 import org.eclipse.swt.graphics.RGB;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.plugin.AbstractUIPlugin;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.xml.sax.SAXException;
41
42 /**
43  * Main class of the quantum plugin, sets defaults, saves and recovers state.
44  * @author root
45  */
46 public class QuantumPlugin extends AbstractUIPlugin {
47     public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.quantum.sql"; 
48         private static QuantumPlugin plugin;
49         private Clipboard sysClip;
50
51         /**
52          * 
53          * TODO: BCH - this constructor has changed in Eclipse 3.0.  This
54          * old version of the constructor is still necessary for running under
55          * Eclipse 2.x.
56          * 
57          * @param descriptor
58          */
59         public QuantumPlugin(IPluginDescriptor descriptor) {
60                 super(descriptor);
61                 plugin = this;
62         }
63
64         public static QuantumPlugin getDefault() {
65                 return plugin;
66         }
67         /**
68          * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
69          * @param target
70          */
71         protected void readStateFrom(File target) {
72                 String fileName = target.getName();
73                 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
74             try {
75                         // It's the 2.0 format for preferences
76                         BookmarkCollection.getInstance().load(target);
77             } catch (IOException e) {
78                 e.printStackTrace();
79             }
80                 } else {
81                         //It's the 2.1 format for preferences and subsets
82                         FileInputStream source = null;
83                         try {
84                                 source = new FileInputStream(target);
85                         } catch (FileNotFoundException e1) {
86                                 e1.printStackTrace();
87                                 return;
88                         }
89                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
90                         DocumentBuilder parser;
91                         try {
92                                 parser = factory.newDocumentBuilder();
93                 Document doc = parser.parse(source);
94
95                 Element root = doc.getDocumentElement();
96                 BookmarkCollection.getInstance().importXML(root);
97                 BookmarkCollection.getInstance().setChanged(false);
98                 SubsetContentProvider.getInstance().importXML(root);
99
100                         } catch (ParserConfigurationException e) {
101                                 e.printStackTrace();
102                         } catch (SAXException e) {
103                                 e.printStackTrace();
104                         } catch (IOException e) {
105                                 e.printStackTrace();
106                         }
107                 }               
108         }
109
110         /* (non-Javadoc)
111          * @see org.eclipse.core.runtime.Plugin#startup()
112          */
113         public void startup() throws CoreException {
114                 super.startup();
115                 ISavedState lastState =
116                         ResourcesPlugin.getWorkspace().addSaveParticipant(
117                                 this,
118                                 new QuantumSaveParticipant());
119                 if (lastState != null) {
120                 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
121                 if (location != null) {
122                         // the plugin instance should read any important state from the file. 
123                         File f = getStateLocation().append(location).toFile();
124                         readStateFrom(f);
125                         
126             }
127         }
128         sysClip = new Clipboard(null);
129         }
130
131     
132         /**
133          * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
134          * @param target
135          */
136         protected void writeImportantState(File target) {
137         try {
138             Document document = XMLHelper.createEmptyDocument();
139             
140                 Element root = (Element) document.appendChild(
141                 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
142                 
143                 BookmarkCollection.getInstance().exportXML(root);
144                 SubsetContentProvider.getInstance().exportXML(root);
145
146             FileWriter writer =  new FileWriter(target);
147             try {
148                 XMLHelper.write(writer, document);
149                 } finally {
150                         writer.close();
151                 }
152         } catch (ParserConfigurationException e) {
153             e.printStackTrace();
154         } catch (IOException e) {
155             e.printStackTrace();
156         }
157         }
158         
159         protected void initializeDefaultPluginPreferences() {
160                 PluginPreferences.initialize(getPreferenceStore());
161                 
162                 getPreferenceStore().setDefault(
163             "phpeclipse.sql.select.template",
164             "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
165
166           getPreferenceStore().setDefault(
167             "phpeclipse.sql.insert.template",
168             "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
169
170           getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
171
172           getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
173
174           getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
175
176           getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
177
178           getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
179
180           getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
181
182           getPreferenceStore().setDefault(
183             "phpeclipse.sql.filename.connect",
184             "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
185         }
186         // Returns the active page
187         public IWorkbenchPage getActivePage()
188         {
189                 IWorkbench workbench = getWorkbench();
190                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
191                 if (window == null)     return null;
192                 IWorkbenchPage page = window.getActivePage();
193                 return page;
194         }
195         /**
196          * returns a view in the active page, creating it if needed
197          * @param view, the name of the view (e.g com.quantum.view.tableview)
198          * @return true if successful, false if not
199          */
200         public IViewPart getView(String view)
201         {
202                 IViewPart tableView = null;
203                 try {
204                         IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
205                         tableView =  page.findView(view);
206                         if (tableView == null){
207                                 // showView will give focus to the created view, we don't want that
208                                 // so we save the active part
209                                 IWorkbenchPart part = page.getActivePart();
210                                 tableView = page.showView(view);
211                                 // and return the focus to it
212                                 page.activate(part);
213                         }
214                 } catch (PartInitException e) {
215                         e.printStackTrace();
216                 }
217                 return tableView;
218         }
219
220
221
222         /**
223          * @return
224          */
225         public Clipboard getSysClip() {
226                 return sysClip;
227         }
228         protected void initializeImageRegistry(ImageRegistry registry) {
229                 super.initializeImageRegistry(registry);
230                 try {
231                         ImageStore.initialize(this, registry, getIconLocation());
232                 } catch (MalformedURLException e) {
233                         // this should never happen, but if it does, we don't get images.
234                 }
235         }
236
237         /**
238          * @return
239          * @throws MalformedURLException
240          */
241         private URL getIconLocation() throws MalformedURLException {
242                 URL installURL = getDescriptor().getInstallURL();
243                 return new URL(installURL, "icons/");
244         }
245 }