fixed ID bug
[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  * @author root
46  * Main class of the quantum plugin, sets defaults, saves and recovers state.
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         public QuantumPlugin(IPluginDescriptor descriptor) {
55                 super(descriptor);
56                 plugin = this;
57         }
58
59         public static QuantumPlugin getDefault() {
60                 return plugin;
61         }
62         /**
63          * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
64          * @param target
65          */
66         protected void readStateFrom(File target) {
67                 String fileName = target.getName();
68                 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
69             try {
70                         // It's the 2.0 format for preferences
71                         BookmarkCollection.getInstance().load(target);
72             } catch (IOException e) {
73                 e.printStackTrace();
74             }
75                 } else {
76                         //It's the 2.1 format for preferences and subsets
77                         FileInputStream source = null;
78                         try {
79                                 source = new FileInputStream(target);
80                         } catch (FileNotFoundException e1) {
81                                 e1.printStackTrace();
82                                 return;
83                         }
84                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85                         DocumentBuilder parser;
86                         try {
87                                 parser = factory.newDocumentBuilder();
88                 Document doc = parser.parse(source);
89
90                 Element root = doc.getDocumentElement();
91                 BookmarkCollection.getInstance().importXML(root);
92                 BookmarkCollection.getInstance().setChanged(false);
93                 SubsetContentProvider.getInstance().importXML(root);
94
95                         } catch (ParserConfigurationException e) {
96                                 e.printStackTrace();
97                         } catch (SAXException e) {
98                                 e.printStackTrace();
99                         } catch (IOException e) {
100                                 e.printStackTrace();
101                         }
102                 }               
103         }
104
105         /* (non-Javadoc)
106          * @see org.eclipse.core.runtime.Plugin#startup()
107          */
108         public void startup() throws CoreException {
109                 super.startup();
110                 ISaveParticipant saveParticipant = new QuantumSaveParticipant();
111                 ISavedState lastState =
112                         ResourcesPlugin.getWorkspace().addSaveParticipant(
113                                 this,
114                                 saveParticipant);
115                 if (lastState != null) {
116                 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
117                 if (location != null) {
118                         // the plugin instance should read any important state from the file. 
119                         File f = getStateLocation().append(location).toFile();
120                         readStateFrom(f);
121                         
122             }
123         }
124         sysClip = new Clipboard(null);
125         }
126
127         /* (non-Javadoc)
128          * @see org.eclipse.core.runtime.Plugin#shutdown()
129          */
130         public void shutdown() throws CoreException {
131                 super.shutdown();
132 //              sysClip.dispose();
133         }
134
135         /**
136          * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
137          * @param target
138          */
139         protected void writeImportantState(File target) {
140         try {
141             Document document = XMLHelper.createEmptyDocument();
142             
143                 Element root = (Element) document.appendChild(
144                 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
145                 
146                 BookmarkCollection.getInstance().exportXML(root);
147                 SubsetContentProvider.getInstance().exportXML(root);
148
149             FileWriter writer =  new FileWriter(target);
150             try {
151                 XMLHelper.write(writer, document);
152                 } finally {
153                         writer.close();
154                 }
155         } catch (ParserConfigurationException e) {
156             e.printStackTrace();
157         } catch (IOException e) {
158             e.printStackTrace();
159         }
160         }
161         
162         /**
163          *  Gets an image descriptof from a file in the icons directory 
164          * @param name of the file to get
165          * @return ImageDescriptor or null if not found
166          */
167         public static ImageDescriptor getImageDescriptor(String name) {
168                 ImageDescriptor descriptor = null;
169                 try {
170                         URL installURL =
171                                 QuantumPlugin.getDefault().getDescriptor().getInstallURL();
172                         URL url = new URL(installURL, Messages.getString("QuantumPlugin.iconsDir") + name); //$NON-NLS-1$
173                         descriptor = ImageDescriptor.createFromURL(url);
174                 } catch (Exception e) {
175                         e.printStackTrace();
176                 }
177                 return descriptor;
178         }
179         public static Image getImage(String name) {
180         ImageDescriptor imageDescriptor = getImageDescriptor(name);
181                 return imageDescriptor == null ? null : imageDescriptor.createImage();
182         }
183
184         protected void initializeDefaultPluginPreferences() {
185                 RGB BACKGROUND = new RGB(255, 255, 255);
186                 RGB COMMENT = new RGB(88, 148, 64);
187                 RGB KEYWORD = new RGB(126, 0, 75);
188                 RGB STRING = new RGB(0, 0, 255);
189                 RGB NUMERIC = new RGB(255, 0, 0);
190                 RGB DEFAULT = new RGB(0, 0, 0);
191                 IPreferenceStore store = getPreferenceStore();
192                 PreferenceConverter.setDefault(store,
193                         "quantum.background.color", BACKGROUND); //$NON-NLS-1$
194                 PreferenceConverter.setDefault(store,
195                         "quantum.text.color", DEFAULT); //$NON-NLS-1$
196                 PreferenceConverter.setDefault(store,
197                         "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
198                 PreferenceConverter.setDefault(store,
199                         "quantum.comment.color", COMMENT); //$NON-NLS-1$
200                 PreferenceConverter.setDefault(store,
201                         "quantum.string.color", STRING); //$NON-NLS-1$
202                 PreferenceConverter.setDefault(store,
203                         "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
204                 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
205                 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
206                 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
207                 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
208                 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
209                 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
210         getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
211         
212         getPreferenceStore().setDefault(
213             "phpeclipse.sql.select.template",
214             "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
215
216           getPreferenceStore().setDefault(
217             "phpeclipse.sql.insert.template",
218             "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
219
220           getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
221
222           getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
223
224           getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
225
226           getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
227
228           getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
229
230           getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
231
232           getPreferenceStore().setDefault(
233             "phpeclipse.sql.filename.connect",
234             "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
235         }
236         // Returns the active page
237         public IWorkbenchPage getActivePage()
238         {
239                 IWorkbench workbench = getWorkbench();
240                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
241                 if (window == null)     return null;
242                 IWorkbenchPage page = window.getActivePage();
243                 return page;
244         }
245         /**
246          * returns a view in the active page, creating it if needed
247          * @param view, the name of the view (e.g com.quantum.view.tableview)
248          * @return true if successful, false if not
249          */
250         public IViewPart getView(String view)
251         {
252                 IViewPart tableView = null;
253                 try {
254                         IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
255                         tableView =  page.findView(view);
256                         if (tableView == null){
257                                 // showView will give focus to the created view, we don't want that
258                                 // so we save the active part
259                                 IWorkbenchPart part = page.getActivePart();
260                                 tableView = page.showView(view);
261                                 // and return the focus to it
262                                 page.activate(part);
263                         }
264                 } catch (PartInitException e) {
265                         e.printStackTrace();
266                 }
267                 return tableView;
268         }
269
270
271
272         class QuantumSaveParticipant implements ISaveParticipant {
273                 /**
274                 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
275                 */
276                 public void doneSaving(ISaveContext context) {
277                 }
278                 /**
279                  * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
280                  */
281                 public void prepareToSave(ISaveContext context) throws CoreException {
282                 }
283
284                 /**
285                  * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
286                  */
287                 public void rollback(ISaveContext context) {
288                 }
289
290                 /**
291                  * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
292                  */
293                 public void saving(ISaveContext context) throws CoreException {
294                         switch (context.getKind()) {
295                                 case ISaveContext.FULL_SAVE :
296                                         QuantumPlugin quantumPluginInstance = QuantumPlugin.getDefault();
297                                         // save the plug in state 
298                                         if (BookmarkCollection.getInstance().isAnythingChanged()
299                                                 || SubsetContentProvider.getInstance().hasChanged()) {
300
301                                                 int saveNumber = context.getSaveNumber();
302                                                 String saveFileName = Messages.getString("QuantumPlugin.saveDir") + "-" + Integer.toString(saveNumber) + Messages.getString("QuantumPlugin.saveFileExtension"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
303                                                 File f = quantumPluginInstance.getStateLocation().append(saveFileName).toFile();
304
305                                                 // if we fail to write, an exception is thrown and we do not update the path 
306                                                 quantumPluginInstance.writeImportantState(f);
307
308                                                 context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
309                                                 context.needSaveNumber();
310
311                                         } else {
312                                                 System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
313                                         }
314                                         break;
315                                 case ISaveContext.PROJECT_SAVE :
316                                         // get the project related to this save operation 
317                                         //IProject project = context.getProject(); 
318                                         // save its information, if necessary 
319                                         break;
320                                 case ISaveContext.SNAPSHOT :
321                                         // This operation needs to be really fast because 
322                                         // snapshots can be requested frequently by the 
323                                         // workspace. 
324                                         break;
325                         }
326                 }
327         }
328         /**
329          * @return
330          */
331         public Clipboard getSysClip() {
332                 return sysClip;
333         }
334 }