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