*** empty log message ***
[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.ImageRegistry;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.swt.dnd.Clipboard;
26 import org.eclipse.swt.graphics.RGB;
27 import org.eclipse.ui.IViewPart;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.PartInitException;
33 import org.eclipse.ui.plugin.AbstractUIPlugin;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.xml.sax.SAXException;
37
38 import com.quantum.model.BookmarkCollection;
39 import com.quantum.util.xml.XMLHelper;
40 import com.quantum.view.subset.SubsetContentProvider;
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         
160 //      public static Image getImage(String name) {
161 //        ImageDescriptor imageDescriptor = getImageDescriptor(name);
162 //              return imageDescriptor == null ? null : imageDescriptor.createImage();
163 //      }
164
165         protected void initializeDefaultPluginPreferences() {
166                 RGB BACKGROUND = new RGB(255, 255, 255);
167                 RGB COMMENT = new RGB(88, 148, 64);
168                 RGB KEYWORD = new RGB(126, 0, 75);
169                 RGB STRING = new RGB(0, 0, 255);
170                 RGB NUMERIC = new RGB(255, 0, 0);
171                 RGB DEFAULT = new RGB(0, 0, 0);
172                 IPreferenceStore store = getPreferenceStore();
173                 PreferenceConverter.setDefault(store,
174                         "quantum.background.color", BACKGROUND); //$NON-NLS-1$
175                 PreferenceConverter.setDefault(store,
176                         "quantum.text.color", DEFAULT); //$NON-NLS-1$
177                 PreferenceConverter.setDefault(store,
178                         "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
179                 PreferenceConverter.setDefault(store,
180                         "quantum.comment.color", COMMENT); //$NON-NLS-1$
181                 PreferenceConverter.setDefault(store,
182                         "quantum.string.color", STRING); //$NON-NLS-1$
183                 PreferenceConverter.setDefault(store,
184                         "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
185                 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
186                 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
187                 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
188                 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
189                 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
190                 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", //$NON-NLS-1$
191                         JFaceResources.getTextFont().getFontData()); 
192                 getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
193             
194
195         getPreferenceStore().setDefault(
196             "phpeclipse.sql.select.template",
197             "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
198
199           getPreferenceStore().setDefault(
200             "phpeclipse.sql.insert.template",
201             "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
202
203           getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
204
205           getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
206
207           getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
208
209           getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
210
211           getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
212
213           getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
214
215           getPreferenceStore().setDefault(
216             "phpeclipse.sql.filename.connect",
217             "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
218         }
219         // Returns the active page
220         public IWorkbenchPage getActivePage()
221         {
222                 IWorkbench workbench = getWorkbench();
223                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
224                 if (window == null)     return null;
225                 IWorkbenchPage page = window.getActivePage();
226                 return page;
227         }
228         /**
229          * returns a view in the active page, creating it if needed
230          * @param view, the name of the view (e.g com.quantum.view.tableview)
231          * @return true if successful, false if not
232          */
233         public IViewPart getView(String view)
234         {
235                 IViewPart tableView = null;
236                 try {
237                         IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
238                         tableView =  page.findView(view);
239                         if (tableView == null){
240                                 // showView will give focus to the created view, we don't want that
241                                 // so we save the active part
242                                 IWorkbenchPart part = page.getActivePart();
243                                 tableView = page.showView(view);
244                                 // and return the focus to it
245                                 page.activate(part);
246                         }
247                 } catch (PartInitException e) {
248                         e.printStackTrace();
249                 }
250                 return tableView;
251         }
252
253         /**
254          * @return
255          */
256         public Clipboard getSysClip() {
257                 return sysClip;
258         }
259         protected void initializeImageRegistry(ImageRegistry registry) {
260                 super.initializeImageRegistry(registry);
261                 try {
262                         ImageStore.initialize(registry, getIconLocation());
263                 } catch (MalformedURLException e) {
264                         // this should never happen, but if it does, we don't get images.
265                 }
266         }
267
268         /**
269          * @return
270          * @throws MalformedURLException
271          */
272         URL getIconLocation() throws MalformedURLException {
273                 URL installURL = QuantumPlugin.getDefault().getDescriptor().getInstallURL();
274                 return new URL(installURL, "icons/");
275         }
276 }