1 package net.sourceforge.phpdt.sql;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
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;
38 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider;
39 import net.sourceforge.phpdt.sql.view.bookmark.SubsetContentProvider;
44 public class PHPEclipseSQLPlugin
45 extends AbstractUIPlugin
46 implements IConstants {
48 private static PHPEclipseSQLPlugin plugin;
49 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.sql";
51 public PHPEclipseSQLPlugin(IPluginDescriptor descriptor) {
56 public static PHPEclipseSQLPlugin getDefault() {
60 * Reads the PHPEclipseSQL Plugin state from a file. The file has been created with writeImportantState
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);
69 //It's the 2.1 format for preferences and subsets
70 XmlDocument doc = new XmlDocument();
71 FileInputStream source = null;
73 source = new FileInputStream(target);
74 } catch (FileNotFoundException e1) {
78 DocumentBuilderFactory factory =
79 DocumentBuilderFactory.newInstance();
80 DocumentBuilder parser;
82 parser = factory.newDocumentBuilder();
83 doc = (XmlDocument) parser.parse(source);
84 } catch (ParserConfigurationException e) {
87 } catch (SAXException e) {
90 } catch (IOException e) {
94 Element root = doc.getDocumentElement();
95 BookmarkContentProvider.getInstance().importXML(root);
96 SubsetContentProvider.getInstance().importXML(root);
100 public void startup() throws CoreException {
102 ISaveParticipant saveParticipant = new PHPSQLSaveParticipant();
103 ISavedState lastState =
104 ResourcesPlugin.getWorkspace().addSaveParticipant(
107 if (lastState == null)
109 IPath location = lastState.lookup(new Path("save"));
110 if (location == null)
112 // the plugin instance should read any important state from the file.
113 File f = getStateLocation().append(location).toFile();
117 protected void writeImportantState(File target) {
118 XmlDocument doc = new XmlDocument();
119 FileOutputStream out = null;
121 out = new FileOutputStream(target);
122 } catch (FileNotFoundException e1) {
123 e1.printStackTrace();
126 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
128 BookmarkContentProvider.getInstance().exportXML(root);
129 SubsetContentProvider.getInstance().exportXML(root);
134 } catch (IOException e) {
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
145 public static ImageDescriptor getImageDescriptor(String name) {
146 ImageDescriptor descriptor = null;
153 URL url = new URL(installURL, "icons/" + name);
154 descriptor = ImageDescriptor.createFromURL(url);
155 } catch (Exception e) {
160 public static Image getImage(String name) {
161 return getImageDescriptor(name).createImage();
164 public void dispose() throws CoreException {
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(
177 "phpeclipse.sql.background.color",
179 PreferenceConverter.setDefault(
181 "phpeclipse.sql.text.color",
183 PreferenceConverter.setDefault(
185 "phpeclipse.sql.keyword.color",
187 PreferenceConverter.setDefault(
189 "phpeclipse.sql.comment.color",
191 PreferenceConverter.setDefault(
193 "phpeclipse.sql.string.color",
195 PreferenceConverter.setDefault(
197 "phpeclipse.sql.numeric.color",
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$
207 public boolean showView(String view) {
208 IWorkbench workbench = getWorkbench();
209 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
210 IWorkbenchPage page = window.getActivePage();
211 IViewPart viewPart = null;
213 viewPart = page.showView(view);
214 } catch (PartInitException e) {
219 getPreferenceStore().setDefault(
220 "phpeclipse.sql.select.template",
221 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
223 getPreferenceStore().setDefault(
224 "phpeclipse.sql.insert.template",
225 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
227 getPreferenceStore().setDefault(
228 "phpeclipse.sql.update.template",
229 "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
231 getPreferenceStore().setDefault(
232 "phpeclipse.sql.delete.template",
233 "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
235 getPreferenceStore().setDefault(
236 "phpeclipse.sql.username.connect",
239 getPreferenceStore().setDefault(
240 "phpeclipse.sql.connect.connect",
241 "jdbc:mysql://localhost/test");
243 getPreferenceStore().setDefault(
244 "phpeclipse.sql.driver.connect",
245 "com.mysql.jdbc.Driver");
247 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
249 getPreferenceStore().setDefault(
250 "phpeclipse.sql.filename.connect",
251 "C:\\apache\\mysql\\lib\\mysql-connector-java-2.0.14-bin.jar");
253 PreferenceConverter.setDefault(
254 getPreferenceStore(),
255 "phpeclipse.sql.font",
257 return(viewPart != null);
261 class PHPSQLSaveParticipant implements ISaveParticipant, IConstants {
263 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
265 public void doneSaving(ISaveContext context) {
269 * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
271 public void prepareToSave(ISaveContext context) throws CoreException {
275 * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
277 public void rollback(ISaveContext context) {
281 * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
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);
296 .append(saveFileName)
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();
304 System.out.println("Not saving unchanged bookmarks");
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
313 case ISaveContext.SNAPSHOT :
314 // This operation needs to be really fast because
315 // snapshots can be requested frequently by the