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