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 extends AbstractUIPlugin implements IConstants {
46 private static PHPEclipseSQLPlugin plugin;
47 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.sql";
49 public PHPEclipseSQLPlugin(IPluginDescriptor descriptor) {
54 public static PHPEclipseSQLPlugin getDefault() {
58 * Reads the PHPEclipseSQL Plugin state from a file. The file has been created with writeImportantState
61 protected void readStateFrom(File target) {
62 String fileName = target.getName();
63 // if (!fileName.endsWith("ext")) { //$NON-NLS-1$
64 // // It's the 2.0 format for preferences
65 // BookmarkContentProvider.getInstance().load(target);
67 //It's the 2.1 format for preferences and subsets
68 XmlDocument doc = new XmlDocument();
69 FileInputStream source = null;
71 source = new FileInputStream(target);
72 } catch (FileNotFoundException e1) {
76 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
77 DocumentBuilder parser;
79 parser = factory.newDocumentBuilder();
80 doc = (XmlDocument) parser.parse(source);
81 } catch (ParserConfigurationException e) {
84 } catch (SAXException e) {
87 } catch (IOException e) {
91 Element root = doc.getDocumentElement();
92 BookmarkContentProvider.getInstance().importXML(root);
93 SubsetContentProvider.getInstance().importXML(root);
97 public void startup() throws CoreException {
99 ISaveParticipant saveParticipant = new PHPSQLSaveParticipant();
100 ISavedState lastState = ResourcesPlugin.getWorkspace().addSaveParticipant(this, saveParticipant);
101 if (lastState == null)
103 IPath location = lastState.lookup(new Path("save"));
104 if (location == null)
106 // the plugin instance should read any important state from the file.
107 File f = getStateLocation().append(location).toFile();
111 protected void writeImportantState(File target) {
112 XmlDocument doc = new XmlDocument();
113 FileOutputStream out = null;
115 out = new FileOutputStream(target);
116 } catch (FileNotFoundException e1) {
117 e1.printStackTrace();
120 Element root = (Element) doc.appendChild(doc.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
122 BookmarkContentProvider.getInstance().exportXML(root);
123 SubsetContentProvider.getInstance().exportXML(root);
128 } catch (IOException e) {
135 * Gets an image descriptof from a file in the icons directory
136 * @param name of the file to get
137 * @return ImageDescriptor or null if not found
139 public static ImageDescriptor getImageDescriptor(String name) {
140 ImageDescriptor descriptor = null;
142 URL installURL = PHPEclipseSQLPlugin.getDefault().getDescriptor().getInstallURL();
143 URL url = new URL(installURL, "icons/" + name);
144 descriptor = ImageDescriptor.createFromURL(url);
145 } catch (Exception e) {
150 public static Image getImage(String name) {
151 return getImageDescriptor(name).createImage();
154 public void dispose() throws CoreException {
157 protected void initializeDefaultPluginPreferences() {
158 RGB BACKGROUND = new RGB(255, 255, 255);
159 RGB COMMENT = new RGB(88, 148, 64);
160 RGB KEYWORD = new RGB(126, 0, 75);
161 RGB STRING = new RGB(0, 0, 255);
162 RGB NUMERIC = new RGB(255, 0, 0);
163 RGB DEFAULT = new RGB(0, 0, 0);
164 IPreferenceStore store = getPreferenceStore();
165 PreferenceConverter.setDefault(store, "phpeclipse.sql.background.color", BACKGROUND);
166 PreferenceConverter.setDefault(store, "phpeclipse.sql.text.color", DEFAULT);
167 PreferenceConverter.setDefault(store, "phpeclipse.sql.keyword.color", KEYWORD);
168 PreferenceConverter.setDefault(store, "phpeclipse.sql.comment.color", COMMENT);
169 PreferenceConverter.setDefault(store, "phpeclipse.sql.string.color", STRING);
170 PreferenceConverter.setDefault(store, "phpeclipse.sql.numeric.color", NUMERIC);
171 getPreferenceStore().setDefault("phpeclipse.sql.text.bold", false);
172 getPreferenceStore().setDefault("phpeclipse.sql.keyword.bold", true);
173 getPreferenceStore().setDefault("phpeclipse.sql.string.bold", false);
174 getPreferenceStore().setDefault("phpeclipse.sql.comment.bold", false);
175 getPreferenceStore().setDefault("phpeclipse.sql.numeric.bold", false);
176 PreferenceConverter.setDefault(getPreferenceStore(), "phpeclipsesql.font", (FontData) null); //$NON-NLS-1$
177 getPreferenceStore().setDefault(
178 "phpeclipse.sql.select.template",
179 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
181 getPreferenceStore().setDefault(
182 "phpeclipse.sql.insert.template",
183 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
185 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
187 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
189 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
191 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
193 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
195 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
197 getPreferenceStore().setDefault(
198 "phpeclipse.sql.filename.connect",
199 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
202 public boolean showView(String view) {
203 IWorkbench workbench = getWorkbench();
204 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
205 IWorkbenchPage page = window.getActivePage();
206 IViewPart viewPart = null;
208 viewPart = page.showView(view);
209 } catch (PartInitException e) {
214 PreferenceConverter.setDefault(getPreferenceStore(), "phpeclipse.sql.font", (FontData) null);
215 return (viewPart != null);
219 class PHPSQLSaveParticipant implements ISaveParticipant, IConstants {
221 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
223 public void doneSaving(ISaveContext context) {
227 * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
229 public void prepareToSave(ISaveContext context) throws CoreException {
233 * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
235 public void rollback(ISaveContext context) {
239 * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
241 public void saving(ISaveContext context) throws CoreException {
242 switch (context.getKind()) {
243 case ISaveContext.FULL_SAVE :
244 PHPEclipseSQLPlugin sqlPluginInstance = PHPEclipseSQLPlugin.getDefault();
245 // save the plug in state
246 if (BookmarkContentProvider.getInstance().hasChanged()) {
247 int saveNumber = context.getSaveNumber();
248 String saveFileName = "save " + Integer.toString(saveNumber);
249 File f = sqlPluginInstance.getStateLocation().append(saveFileName).toFile();
250 // if we fail to write, an exception is thrown and we do not update the path
251 sqlPluginInstance.writeImportantState(f);
252 context.map(new Path("save"), new Path(saveFileName));
253 context.needSaveNumber();
256 System.out.println("Not saving unchanged bookmarks");
260 case ISaveContext.PROJECT_SAVE :
261 // get the project related to this save operation
262 //IProject project = context.getProject();
263 // save its information, if necessary
265 case ISaveContext.SNAPSHOT :
266 // This operation needs to be really fast because
267 // snapshots can be requested frequently by the