4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileWriter;
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 com.quantum.model.BookmarkCollection;
15 import com.quantum.util.xml.XMLHelper;
16 import com.quantum.view.subset.SubsetContentProvider;
18 import org.eclipse.core.resources.ISaveContext;
19 import org.eclipse.core.resources.ISaveParticipant;
20 import org.eclipse.core.resources.ISavedState;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.IPluginDescriptor;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.preference.PreferenceConverter;
28 import org.eclipse.jface.resource.ImageDescriptor;
29 import org.eclipse.swt.dnd.Clipboard;
30 import org.eclipse.swt.graphics.FontData;
31 import org.eclipse.swt.graphics.Image;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.ui.IViewPart;
34 import org.eclipse.ui.IWorkbench;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.IWorkbenchPart;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.PartInitException;
39 import org.eclipse.ui.plugin.AbstractUIPlugin;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.xml.sax.SAXException;
46 * Main class of the quantum plugin, sets defaults, saves and recovers state.
48 public class QuantumPlugin extends AbstractUIPlugin {
49 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.quantum.sql";
50 private static QuantumPlugin plugin;
51 private Clipboard sysClip;
54 public QuantumPlugin(IPluginDescriptor descriptor) {
59 public static QuantumPlugin getDefault() {
63 * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
66 protected void readStateFrom(File target) {
67 String fileName = target.getName();
68 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
70 // It's the 2.0 format for preferences
71 BookmarkCollection.getInstance().load(target);
72 } catch (IOException e) {
76 //It's the 2.1 format for preferences and subsets
77 FileInputStream source = null;
79 source = new FileInputStream(target);
80 } catch (FileNotFoundException e1) {
84 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85 DocumentBuilder parser;
87 parser = factory.newDocumentBuilder();
88 Document doc = parser.parse(source);
90 Element root = doc.getDocumentElement();
91 BookmarkCollection.getInstance().importXML(root);
92 BookmarkCollection.getInstance().setChanged(false);
93 SubsetContentProvider.getInstance().importXML(root);
95 } catch (ParserConfigurationException e) {
97 } catch (SAXException e) {
99 } catch (IOException e) {
106 * @see org.eclipse.core.runtime.Plugin#startup()
108 public void startup() throws CoreException {
110 ISaveParticipant saveParticipant = new QuantumSaveParticipant();
111 ISavedState lastState =
112 ResourcesPlugin.getWorkspace().addSaveParticipant(
115 if (lastState != null) {
116 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
117 if (location != null) {
118 // the plugin instance should read any important state from the file.
119 File f = getStateLocation().append(location).toFile();
124 sysClip = new Clipboard(null);
128 * @see org.eclipse.core.runtime.Plugin#shutdown()
130 public void shutdown() throws CoreException {
132 // sysClip.dispose();
136 * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
139 protected void writeImportantState(File target) {
141 Document document = XMLHelper.createEmptyDocument();
143 Element root = (Element) document.appendChild(
144 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
146 BookmarkCollection.getInstance().exportXML(root);
147 SubsetContentProvider.getInstance().exportXML(root);
149 FileWriter writer = new FileWriter(target);
151 XMLHelper.write(writer, document);
155 } catch (ParserConfigurationException e) {
157 } catch (IOException e) {
163 * Gets an image descriptof from a file in the icons directory
164 * @param name of the file to get
165 * @return ImageDescriptor or null if not found
167 public static ImageDescriptor getImageDescriptor(String name) {
168 ImageDescriptor descriptor = null;
171 QuantumPlugin.getDefault().getDescriptor().getInstallURL();
172 URL url = new URL(installURL, Messages.getString("QuantumPlugin.iconsDir") + name); //$NON-NLS-1$
173 descriptor = ImageDescriptor.createFromURL(url);
174 } catch (Exception e) {
179 public static Image getImage(String name) {
180 ImageDescriptor imageDescriptor = getImageDescriptor(name);
181 return imageDescriptor == null ? null : imageDescriptor.createImage();
184 protected void initializeDefaultPluginPreferences() {
185 RGB BACKGROUND = new RGB(255, 255, 255);
186 RGB COMMENT = new RGB(88, 148, 64);
187 RGB KEYWORD = new RGB(126, 0, 75);
188 RGB STRING = new RGB(0, 0, 255);
189 RGB NUMERIC = new RGB(255, 0, 0);
190 RGB DEFAULT = new RGB(0, 0, 0);
191 IPreferenceStore store = getPreferenceStore();
192 PreferenceConverter.setDefault(store,
193 "quantum.background.color", BACKGROUND); //$NON-NLS-1$
194 PreferenceConverter.setDefault(store,
195 "quantum.text.color", DEFAULT); //$NON-NLS-1$
196 PreferenceConverter.setDefault(store,
197 "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
198 PreferenceConverter.setDefault(store,
199 "quantum.comment.color", COMMENT); //$NON-NLS-1$
200 PreferenceConverter.setDefault(store,
201 "quantum.string.color", STRING); //$NON-NLS-1$
202 PreferenceConverter.setDefault(store,
203 "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
204 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
205 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
206 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
207 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
208 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
209 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
210 getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
212 getPreferenceStore().setDefault(
213 "phpeclipse.sql.select.template",
214 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
216 getPreferenceStore().setDefault(
217 "phpeclipse.sql.insert.template",
218 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
220 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
222 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
224 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
226 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
228 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
230 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
232 getPreferenceStore().setDefault(
233 "phpeclipse.sql.filename.connect",
234 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
236 // Returns the active page
237 public IWorkbenchPage getActivePage()
239 IWorkbench workbench = getWorkbench();
240 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
241 if (window == null) return null;
242 IWorkbenchPage page = window.getActivePage();
246 * returns a view in the active page, creating it if needed
247 * @param view, the name of the view (e.g com.quantum.view.tableview)
248 * @return true if successful, false if not
250 public IViewPart getView(String view)
252 IViewPart tableView = null;
254 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
255 tableView = page.findView(view);
256 if (tableView == null){
257 // showView will give focus to the created view, we don't want that
258 // so we save the active part
259 IWorkbenchPart part = page.getActivePart();
260 tableView = page.showView(view);
261 // and return the focus to it
264 } catch (PartInitException e) {
272 class QuantumSaveParticipant implements ISaveParticipant {
274 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
276 public void doneSaving(ISaveContext context) {
279 * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
281 public void prepareToSave(ISaveContext context) throws CoreException {
285 * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
287 public void rollback(ISaveContext context) {
291 * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
293 public void saving(ISaveContext context) throws CoreException {
294 switch (context.getKind()) {
295 case ISaveContext.FULL_SAVE :
296 QuantumPlugin quantumPluginInstance = QuantumPlugin.getDefault();
297 // save the plug in state
298 if (BookmarkCollection.getInstance().isAnythingChanged()
299 || SubsetContentProvider.getInstance().hasChanged()) {
301 int saveNumber = context.getSaveNumber();
302 String saveFileName = Messages.getString("QuantumPlugin.saveDir") + "-" + Integer.toString(saveNumber) + Messages.getString("QuantumPlugin.saveFileExtension"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
303 File f = quantumPluginInstance.getStateLocation().append(saveFileName).toFile();
305 // if we fail to write, an exception is thrown and we do not update the path
306 quantumPluginInstance.writeImportantState(f);
308 context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
309 context.needSaveNumber();
312 System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
315 case ISaveContext.PROJECT_SAVE :
316 // get the project related to this save operation
317 //IProject project = context.getProject();
318 // save its information, if necessary
320 case ISaveContext.SNAPSHOT :
321 // This operation needs to be really fast because
322 // snapshots can be requested frequently by the
331 public Clipboard getSysClip() {