5a31b2603015277453078e1aa6a10e2bee5ffe77
[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.URL;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import com.quantum.model.BookmarkCollection;
15 import com.quantum.util.xml.XMLHelper;
16 import com.quantum.view.subset.SubsetContentProvider;
17
18 import org.eclipse.core.resources.ISaveContext;
19 import org.eclipse.core.resources.ISaveParticipant;
20 import org.eclipse.core.resources.ISavedState;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IPluginDescriptor;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.preference.PreferenceConverter;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.swt.dnd.Clipboard;
30 import org.eclipse.swt.graphics.FontData;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.ui.IViewPart;
34 import org.eclipse.ui.IWorkbench;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.plugin.AbstractUIPlugin;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.xml.sax.SAXException;
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                 ISaveParticipant saveParticipant = new QuantumSaveParticipant();
118                 ISavedState lastState =
119                         ResourcesPlugin.getWorkspace().addSaveParticipant(
120                                 this,
121                                 saveParticipant);
122                 if (lastState != null) {
123                 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
124                 if (location != null) {
125                         // the plugin instance should read any important state from the file. 
126                         File f = getStateLocation().append(location).toFile();
127                         readStateFrom(f);
128                         
129             }
130         }
131         sysClip = new Clipboard(null);
132         }
133
134         /* (non-Javadoc)
135          * @see org.eclipse.core.runtime.Plugin#shutdown()
136          */
137         public void shutdown() throws CoreException {
138                 super.shutdown();
139                 sysClip.dispose();
140         }
141
142         /**
143          * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
144          * @param target
145          */
146         protected void writeImportantState(File target) {
147         try {
148             Document document = XMLHelper.createEmptyDocument();
149             
150                 Element root = (Element) document.appendChild(
151                 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
152                 
153                 BookmarkCollection.getInstance().exportXML(root);
154                 SubsetContentProvider.getInstance().exportXML(root);
155
156             FileWriter writer =  new FileWriter(target);
157             try {
158                 XMLHelper.write(writer, document);
159                 } finally {
160                         writer.close();
161                 }
162         } catch (ParserConfigurationException e) {
163             e.printStackTrace();
164         } catch (IOException e) {
165             e.printStackTrace();
166         }
167         }
168         
169         /**
170          *  Gets an image descriptof from a file in the icons directory 
171          * @param name of the file to get
172          * @return ImageDescriptor or null if not found
173          */
174         public static ImageDescriptor getImageDescriptor(String name) {
175                 ImageDescriptor descriptor = null;
176                 try {
177                         URL installURL =
178                                 QuantumPlugin.getDefault().getDescriptor().getInstallURL();
179                         URL url = new URL(installURL, Messages.getString("QuantumPlugin.iconsDir") + name); //$NON-NLS-1$
180                         descriptor = ImageDescriptor.createFromURL(url);
181                 } catch (Exception e) {
182                         e.printStackTrace();
183                 }
184                 return descriptor;
185         }
186         public static Image getImage(String name) {
187         ImageDescriptor imageDescriptor = getImageDescriptor(name);
188                 return imageDescriptor == null ? null : imageDescriptor.createImage();
189         }
190
191         protected void initializeDefaultPluginPreferences() {
192                 RGB BACKGROUND = new RGB(255, 255, 255);
193                 RGB COMMENT = new RGB(88, 148, 64);
194                 RGB KEYWORD = new RGB(126, 0, 75);
195                 RGB STRING = new RGB(0, 0, 255);
196                 RGB NUMERIC = new RGB(255, 0, 0);
197                 RGB DEFAULT = new RGB(0, 0, 0);
198                 IPreferenceStore store = getPreferenceStore();
199                 PreferenceConverter.setDefault(store,
200                         "quantum.background.color", BACKGROUND); //$NON-NLS-1$
201                 PreferenceConverter.setDefault(store,
202                         "quantum.text.color", DEFAULT); //$NON-NLS-1$
203                 PreferenceConverter.setDefault(store,
204                         "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
205                 PreferenceConverter.setDefault(store,
206                         "quantum.comment.color", COMMENT); //$NON-NLS-1$
207                 PreferenceConverter.setDefault(store,
208                         "quantum.string.color", STRING); //$NON-NLS-1$
209                 PreferenceConverter.setDefault(store,
210                         "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
211                 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
212                 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
213                 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
214                 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
215                 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
216                 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
217         getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
218             
219
220         getPreferenceStore().setDefault(
221             "phpeclipse.sql.select.template",
222             "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
223
224           getPreferenceStore().setDefault(
225             "phpeclipse.sql.insert.template",
226             "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
227
228           getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
229
230           getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
231
232           getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
233
234           getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
235
236           getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
237
238           getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
239
240           getPreferenceStore().setDefault(
241             "phpeclipse.sql.filename.connect",
242             "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
243         }
244         // Returns the active page
245         public IWorkbenchPage getActivePage()
246         {
247                 IWorkbench workbench = getWorkbench();
248                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
249                 if (window == null)     return null;
250                 IWorkbenchPage page = window.getActivePage();
251                 return page;
252         }
253         /**
254          * returns a view in the active page, creating it if needed
255          * @param view, the name of the view (e.g com.quantum.view.tableview)
256          * @return true if successful, false if not
257          */
258         public IViewPart getView(String view)
259         {
260                 IViewPart tableView = null;
261                 try {
262                         IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
263                         tableView =  page.findView(view);
264                         if (tableView == null){
265                                 // showView will give focus to the created view, we don't want that
266                                 // so we save the active part
267                                 IWorkbenchPart part = page.getActivePart();
268                                 tableView = page.showView(view);
269                                 // and return the focus to it
270                                 page.activate(part);
271                         }
272                 } catch (PartInitException e) {
273                         e.printStackTrace();
274                 }
275                 return tableView;
276         }
277
278
279
280         class QuantumSaveParticipant implements ISaveParticipant {
281                 /**
282                 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
283                 */
284                 public void doneSaving(ISaveContext context) {
285                 }
286                 /**
287                  * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
288                  */
289                 public void prepareToSave(ISaveContext context) throws CoreException {
290                 }
291
292                 /**
293                  * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
294                  */
295                 public void rollback(ISaveContext context) {
296                 }
297
298                 /**
299                  * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
300                  */
301                 public void saving(ISaveContext context) throws CoreException {
302                         switch (context.getKind()) {
303                                 case ISaveContext.FULL_SAVE :
304                                         QuantumPlugin quantumPluginInstance = QuantumPlugin.getDefault();
305                                         // save the plug in state 
306                                         if (BookmarkCollection.getInstance().isAnythingChanged()
307                                                 || SubsetContentProvider.getInstance().hasChanged()) {
308
309                                                 int saveNumber = context.getSaveNumber();
310                                                 String saveFileName = Messages.getString("QuantumPlugin.saveDir") + "-" + Integer.toString(saveNumber) + Messages.getString("QuantumPlugin.saveFileExtension"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
311                                                 File f = quantumPluginInstance.getStateLocation().append(saveFileName).toFile();
312
313                                                 // if we fail to write, an exception is thrown and we do not update the path 
314                                                 quantumPluginInstance.writeImportantState(f);
315
316                                                 context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
317                                                 context.needSaveNumber();
318
319                                         } else {
320                                                 System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
321                                         }
322                                         break;
323                                 case ISaveContext.PROJECT_SAVE :
324                                         // get the project related to this save operation 
325                                         //IProject project = context.getProject(); 
326                                         // save its information, if necessary 
327                                         break;
328                                 case ISaveContext.SNAPSHOT :
329                                         // This operation needs to be really fast because 
330                                         // snapshots can be requested frequently by the 
331                                         // workspace. 
332                                         break;
333                         }
334                 }
335         }
336         /**
337          * @return
338          */
339         public Clipboard getSysClip() {
340                 return sysClip;
341         }
342 }