7d3cbecb220c408d97b904fba1b113d876ae2b28
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / PHPEclipseSQLPlugin.java
1 package net.sourceforge.phpdt.sql;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
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 org.apache.crimson.tree.XmlDocument;
15 import org.eclipse.core.resources.ISaveContext;
16 import org.eclipse.core.resources.ISaveParticipant;
17 import org.eclipse.core.resources.ISavedState;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IPluginDescriptor;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.preference.PreferenceConverter;
25 import org.eclipse.jface.resource.ImageDescriptor;
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.IWorkbenchWindow;
33 import org.eclipse.ui.PartInitException;
34 import org.eclipse.ui.plugin.AbstractUIPlugin;
35 import org.w3c.dom.Element;
36 import org.xml.sax.SAXException;
37
38 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider;
39 import net.sourceforge.phpdt.sql.view.bookmark.SubsetContentProvider;
40
41 /**
42  * @author 
43  */
44 public class PHPEclipseSQLPlugin
45 extends AbstractUIPlugin
46 implements IConstants {
47
48         private static PHPEclipseSQLPlugin plugin;
49         public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.sql";
50
51         public PHPEclipseSQLPlugin(IPluginDescriptor descriptor) {
52                 super(descriptor);
53                 plugin = this;
54         }
55
56         public static PHPEclipseSQLPlugin getDefault() {
57                 return plugin;
58         }
59         /**
60          * Reads the PHPEclipseSQL Plugin state from a file. The file has been created with writeImportantState
61          * @param target
62          */
63         protected void readStateFrom(File target) {
64                 String fileName = target.getName();
65                 if (!fileName.endsWith("ext")) { //$NON-NLS-1$
66                         // It's the 2.0 format for preferences
67                         BookmarkContentProvider.getInstance().load(target);
68                 } else {
69                         //It's the 2.1 format for preferences and subsets
70                         XmlDocument doc = new XmlDocument();
71                         FileInputStream source = null;
72                         try {
73                                 source = new FileInputStream(target);
74                         } catch (FileNotFoundException e1) {
75                                 e1.printStackTrace();
76                                 return;
77                         }
78                         DocumentBuilderFactory factory =
79                         DocumentBuilderFactory.newInstance();
80                         DocumentBuilder parser;
81                         try {
82                                 parser = factory.newDocumentBuilder();
83                                 doc = (XmlDocument) parser.parse(source);
84                         } catch (ParserConfigurationException e) {
85                                 e.printStackTrace();
86                                 return;
87                         } catch (SAXException e) {
88                                 e.printStackTrace();
89                                 return;
90                         } catch (IOException e) {
91                                 e.printStackTrace();
92                                 return;
93                         }
94                         Element root = doc.getDocumentElement();
95                         BookmarkContentProvider.getInstance().importXML(root);
96                         SubsetContentProvider.getInstance().importXML(root);
97                 }
98         }
99
100         public void startup() throws CoreException {
101                 super.startup();
102                 ISaveParticipant saveParticipant = new PHPSQLSaveParticipant();
103                 ISavedState lastState =
104                 ResourcesPlugin.getWorkspace().addSaveParticipant(
105                                                                                                                  this,
106                                                                                                                  saveParticipant);
107                 if (lastState == null)
108                         return;
109                 IPath location = lastState.lookup(new Path("save"));
110                 if (location == null)
111                         return;
112                 // the plugin instance should read any important state from the file. 
113                 File f = getStateLocation().append(location).toFile();
114                 readStateFrom(f);
115         }
116
117         protected void writeImportantState(File target) {
118                 XmlDocument doc = new XmlDocument();
119                 FileOutputStream out = null;
120                 try {
121                         out = new FileOutputStream(target);
122                 } catch (FileNotFoundException e1) {
123                         e1.printStackTrace();
124                         return;
125                 }
126                 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
127
128                 BookmarkContentProvider.getInstance().exportXML(root);
129                 SubsetContentProvider.getInstance().exportXML(root);
130
131                 try {
132                         doc.write(out);
133                         out.close();
134                 } catch (IOException e) {
135                         e.printStackTrace();
136                 }
137
138         }
139
140         /**
141          *  Gets an image descriptof from a file in the icons directory 
142          * @param name of the file to get
143          * @return ImageDescriptor or null if not found
144          */
145         public static ImageDescriptor getImageDescriptor(String name) {
146                 ImageDescriptor descriptor = null;
147                 try {
148                         URL installURL =
149                         PHPEclipseSQLPlugin
150                         .getDefault()
151                         .getDescriptor()
152                         .getInstallURL();
153                         URL url = new URL(installURL, "icons/" + name);
154                         descriptor = ImageDescriptor.createFromURL(url);
155                 } catch (Exception e) {
156                         e.printStackTrace();
157                 }
158                 return descriptor;
159         }
160         public static Image getImage(String name) {
161                 return getImageDescriptor(name).createImage();
162         }
163
164         public void dispose() throws CoreException {
165         }
166
167         protected void initializeDefaultPluginPreferences() {
168                 RGB BACKGROUND = new RGB(255, 255, 255);
169                 RGB COMMENT = new RGB(88, 148, 64);
170                 RGB KEYWORD = new RGB(126, 0, 75);
171                 RGB STRING = new RGB(0, 0, 255);
172                 RGB NUMERIC = new RGB(255, 0, 0);
173                 RGB DEFAULT = new RGB(0, 0, 0);
174                 IPreferenceStore store = getPreferenceStore();
175                 PreferenceConverter.setDefault(
176                                                                           store,
177                                                                           "phpeclipse.sql.background.color",
178                                                                           BACKGROUND);
179                 PreferenceConverter.setDefault(
180                                                                           store,
181                                                                           "phpeclipse.sql.text.color",
182                                                                           DEFAULT);
183                 PreferenceConverter.setDefault(
184                                                                           store,
185                                                                           "phpeclipse.sql.keyword.color",
186                                                                           KEYWORD);
187                 PreferenceConverter.setDefault(
188                                                                           store,
189                                                                           "phpeclipse.sql.comment.color",
190                                                                           COMMENT);
191                 PreferenceConverter.setDefault(
192                                                                           store,
193                                                                           "phpeclipse.sql.string.color",
194                                                                           STRING);
195                 PreferenceConverter.setDefault(
196                                                                           store,
197                                                                           "phpeclipse.sql.numeric.color",
198                                                                           NUMERIC);
199                 getPreferenceStore().setDefault("phpeclipse.sql.text.bold", false);
200                 getPreferenceStore().setDefault("phpeclipse.sql.keyword.bold", true);
201                 getPreferenceStore().setDefault("phpeclipse.sql.string.bold", false);
202                 getPreferenceStore().setDefault("phpeclipse.sql.comment.bold", false);
203                 getPreferenceStore().setDefault("phpeclipse.sql.numeric.bold", false);
204                 PreferenceConverter.setDefault(getPreferenceStore(), "phpeclipsesql.font", (FontData) null); //$NON-NLS-1$
205         }
206
207         public boolean showView(String view) {
208                 IWorkbench workbench = getWorkbench();
209                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
210                 IWorkbenchPage page = window.getActivePage();
211                 IViewPart viewPart = null;
212                 try {
213                         viewPart = page.showView(view);
214                 } catch (PartInitException e) {
215                         e.printStackTrace();
216                         return false;
217                 }
218
219                 getPreferenceStore().setDefault(
220                                                                            "phpeclipse.sql.select.template",
221                                                                            "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
222
223                 getPreferenceStore().setDefault(
224                                                                            "phpeclipse.sql.insert.template",
225                                                                            "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
226
227                 getPreferenceStore().setDefault(
228                                                                            "phpeclipse.sql.update.template",
229                                                                            "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
230
231                 getPreferenceStore().setDefault(
232                                                                            "phpeclipse.sql.delete.template",
233                                                                            "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
234
235                 getPreferenceStore().setDefault(
236                                                                            "phpeclipse.sql.username.connect",
237                                                                            "root");
238
239                 getPreferenceStore().setDefault(
240                                                                            "phpeclipse.sql.connect.connect",
241                                                                            "jdbc:mysql://localhost/test");
242
243                 getPreferenceStore().setDefault(
244                                                                            "phpeclipse.sql.driver.connect",
245                                                                            "com.mysql.jdbc.Driver");
246
247                 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
248
249                 getPreferenceStore().setDefault(
250                                                                            "phpeclipse.sql.filename.connect",
251                                                                            "C:\\apache\\mysql\\lib\\mysql-connector-java-2.0.14-bin.jar");
252
253                 PreferenceConverter.setDefault(
254                                                                           getPreferenceStore(),
255                                                                           "phpeclipse.sql.font",
256                                                                           (FontData) null);
257                 return(viewPart != null);
258         }
259
260 }
261 class PHPSQLSaveParticipant implements ISaveParticipant, IConstants {
262         /**
263          * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
264          */
265         public void doneSaving(ISaveContext context) {
266         }
267
268         /**
269          * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
270          */
271         public void prepareToSave(ISaveContext context) throws CoreException {
272         }
273
274         /**
275          * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
276          */
277         public void rollback(ISaveContext context) {
278         }
279
280         /**
281          * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
282          */
283         public void saving(ISaveContext context) throws CoreException {
284                 switch (context.getKind()) {
285                 case ISaveContext.FULL_SAVE :
286                         PHPEclipseSQLPlugin sqlPluginInstance =
287                         PHPEclipseSQLPlugin.getDefault();
288                         // save the plug in state 
289                         if (BookmarkContentProvider.getInstance().hasChanged()) {
290                                 int saveNumber = context.getSaveNumber();
291                                 String saveFileName =
292                                 "save " + Integer.toString(saveNumber);
293                                 File f =
294                                 sqlPluginInstance
295                                 .getStateLocation()
296                                 .append(saveFileName)
297                                 .toFile();
298                                 // if we fail to write, an exception is thrown and we do not update the path 
299                                 sqlPluginInstance.writeImportantState(f);
300                                 context.map(new Path("save"), new Path(saveFileName));
301                                 context.needSaveNumber();
302                         } else {
303                                 if (DEBUG) {
304                                         System.out.println("Not saving unchanged bookmarks");
305                                 }
306                         }
307                         break;
308                 case ISaveContext.PROJECT_SAVE :
309                         // get the project related to this save operation 
310                         //IProject project = context.getProject(); 
311                         // save its information, if necessary 
312                         break;
313                 case ISaveContext.SNAPSHOT :
314                         // This operation needs to be really fast because 
315                         // snapshots can be requested frequently by the 
316                         // workspace. 
317                         break;
318                 }
319         }
320 }