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;
45 * 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;
55 * TODO: BCH - this constructor has changed in Eclipse 3.0. This
56 * old version of the constructor is still necessary for running under
61 public QuantumPlugin(IPluginDescriptor descriptor) {
66 public static QuantumPlugin getDefault() {
70 * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
73 protected void readStateFrom(File target) {
74 String fileName = target.getName();
75 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
77 // It's the 2.0 format for preferences
78 BookmarkCollection.getInstance().load(target);
79 } catch (IOException e) {
83 //It's the 2.1 format for preferences and subsets
84 FileInputStream source = null;
86 source = new FileInputStream(target);
87 } catch (FileNotFoundException e1) {
91 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
92 DocumentBuilder parser;
94 parser = factory.newDocumentBuilder();
95 Document doc = parser.parse(source);
97 Element root = doc.getDocumentElement();
98 BookmarkCollection.getInstance().importXML(root);
99 BookmarkCollection.getInstance().setChanged(false);
100 SubsetContentProvider.getInstance().importXML(root);
102 } catch (ParserConfigurationException e) {
104 } catch (SAXException e) {
106 } catch (IOException e) {
113 * @see org.eclipse.core.runtime.Plugin#startup()
115 public void startup() throws CoreException {
117 ISaveParticipant saveParticipant = new QuantumSaveParticipant();
118 ISavedState lastState =
119 ResourcesPlugin.getWorkspace().addSaveParticipant(
122 if (lastState != null) {
123 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
124 if (location != null) {
125 // the plugin instance should read any important state from the file.
126 File f = getStateLocation().append(location).toFile();
131 sysClip = new Clipboard(null);
135 * @see org.eclipse.core.runtime.Plugin#shutdown()
137 public void shutdown() throws CoreException {
143 * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
146 protected void writeImportantState(File target) {
148 Document document = XMLHelper.createEmptyDocument();
150 Element root = (Element) document.appendChild(
151 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
153 BookmarkCollection.getInstance().exportXML(root);
154 SubsetContentProvider.getInstance().exportXML(root);
156 FileWriter writer = new FileWriter(target);
158 XMLHelper.write(writer, document);
162 } catch (ParserConfigurationException e) {
164 } catch (IOException e) {
170 * Gets an image descriptof from a file in the icons directory
171 * @param name of the file to get
172 * @return ImageDescriptor or null if not found
174 public static ImageDescriptor getImageDescriptor(String name) {
175 ImageDescriptor descriptor = null;
178 QuantumPlugin.getDefault().getDescriptor().getInstallURL();
179 URL url = new URL(installURL, Messages.getString("QuantumPlugin.iconsDir") + name); //$NON-NLS-1$
180 descriptor = ImageDescriptor.createFromURL(url);
181 } catch (Exception e) {
186 public static Image getImage(String name) {
187 ImageDescriptor imageDescriptor = getImageDescriptor(name);
188 return imageDescriptor == null ? null : imageDescriptor.createImage();
191 protected void initializeDefaultPluginPreferences() {
192 RGB BACKGROUND = new RGB(255, 255, 255);
193 RGB COMMENT = new RGB(88, 148, 64);
194 RGB KEYWORD = new RGB(126, 0, 75);
195 RGB STRING = new RGB(0, 0, 255);
196 RGB NUMERIC = new RGB(255, 0, 0);
197 RGB DEFAULT = new RGB(0, 0, 0);
198 IPreferenceStore store = getPreferenceStore();
199 PreferenceConverter.setDefault(store,
200 "quantum.background.color", BACKGROUND); //$NON-NLS-1$
201 PreferenceConverter.setDefault(store,
202 "quantum.text.color", DEFAULT); //$NON-NLS-1$
203 PreferenceConverter.setDefault(store,
204 "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
205 PreferenceConverter.setDefault(store,
206 "quantum.comment.color", COMMENT); //$NON-NLS-1$
207 PreferenceConverter.setDefault(store,
208 "quantum.string.color", STRING); //$NON-NLS-1$
209 PreferenceConverter.setDefault(store,
210 "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
211 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
212 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
213 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
214 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
215 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
216 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
217 getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
220 getPreferenceStore().setDefault(
221 "phpeclipse.sql.select.template",
222 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
224 getPreferenceStore().setDefault(
225 "phpeclipse.sql.insert.template",
226 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
228 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
230 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
232 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
234 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
236 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
238 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
240 getPreferenceStore().setDefault(
241 "phpeclipse.sql.filename.connect",
242 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
244 // Returns the active page
245 public IWorkbenchPage getActivePage()
247 IWorkbench workbench = getWorkbench();
248 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
249 if (window == null) return null;
250 IWorkbenchPage page = window.getActivePage();
254 * returns a view in the active page, creating it if needed
255 * @param view, the name of the view (e.g com.quantum.view.tableview)
256 * @return true if successful, false if not
258 public IViewPart getView(String view)
260 IViewPart tableView = null;
262 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
263 tableView = page.findView(view);
264 if (tableView == null){
265 // showView will give focus to the created view, we don't want that
266 // so we save the active part
267 IWorkbenchPart part = page.getActivePart();
268 tableView = page.showView(view);
269 // and return the focus to it
272 } catch (PartInitException e) {
280 class QuantumSaveParticipant implements ISaveParticipant {
282 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
284 public void doneSaving(ISaveContext context) {
287 * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
289 public void prepareToSave(ISaveContext context) throws CoreException {
293 * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
295 public void rollback(ISaveContext context) {
299 * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
301 public void saving(ISaveContext context) throws CoreException {
302 switch (context.getKind()) {
303 case ISaveContext.FULL_SAVE :
304 QuantumPlugin quantumPluginInstance = QuantumPlugin.getDefault();
305 // save the plug in state
306 if (BookmarkCollection.getInstance().isAnythingChanged()
307 || SubsetContentProvider.getInstance().hasChanged()) {
309 int saveNumber = context.getSaveNumber();
310 String saveFileName = Messages.getString("QuantumPlugin.saveDir") + "-" + Integer.toString(saveNumber) + Messages.getString("QuantumPlugin.saveFileExtension"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
311 File f = quantumPluginInstance.getStateLocation().append(saveFileName).toFile();
313 // if we fail to write, an exception is thrown and we do not update the path
314 quantumPluginInstance.writeImportantState(f);
316 context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
317 context.needSaveNumber();
320 System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
323 case ISaveContext.PROJECT_SAVE :
324 // get the project related to this save operation
325 //IProject project = context.getProject();
326 // save its information, if necessary
328 case ISaveContext.SNAPSHOT :
329 // This operation needs to be really fast because
330 // snapshots can be requested frequently by the
339 public Clipboard getSysClip() {