4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.net.MalformedURLException;
11 import javax.xml.parsers.DocumentBuilder;
12 import javax.xml.parsers.DocumentBuilderFactory;
13 import javax.xml.parsers.ParserConfigurationException;
15 import com.quantum.model.BookmarkCollection;
16 import com.quantum.util.xml.XMLHelper;
17 import com.quantum.view.subset.SubsetContentProvider;
19 import org.eclipse.core.resources.ISavedState;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IPluginDescriptor;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.jface.preference.PreferenceConverter;
27 import org.eclipse.jface.resource.ImageRegistry;
28 import org.eclipse.jface.resource.JFaceResources;
29 import org.eclipse.swt.dnd.Clipboard;
30 import org.eclipse.swt.graphics.RGB;
31 import org.eclipse.ui.IViewPart;
32 import org.eclipse.ui.IWorkbench;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.plugin.AbstractUIPlugin;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.Element;
40 import org.xml.sax.SAXException;
43 * Main class of the quantum plugin, sets defaults, saves and recovers state.
46 public class QuantumPlugin extends AbstractUIPlugin {
47 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.quantum.sql";
48 private static QuantumPlugin plugin;
49 private Clipboard sysClip;
53 * TODO: BCH - this constructor has changed in Eclipse 3.0. This
54 * old version of the constructor is still necessary for running under
59 public QuantumPlugin(IPluginDescriptor descriptor) {
64 public static QuantumPlugin getDefault() {
68 * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
71 protected void readStateFrom(File target) {
72 String fileName = target.getName();
73 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
75 // It's the 2.0 format for preferences
76 BookmarkCollection.getInstance().load(target);
77 } catch (IOException e) {
81 //It's the 2.1 format for preferences and subsets
82 FileInputStream source = null;
84 source = new FileInputStream(target);
85 } catch (FileNotFoundException e1) {
89 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
90 DocumentBuilder parser;
92 parser = factory.newDocumentBuilder();
93 Document doc = parser.parse(source);
95 Element root = doc.getDocumentElement();
96 BookmarkCollection.getInstance().importXML(root);
97 BookmarkCollection.getInstance().setChanged(false);
98 SubsetContentProvider.getInstance().importXML(root);
100 } catch (ParserConfigurationException e) {
102 } catch (SAXException e) {
104 } catch (IOException e) {
111 * @see org.eclipse.core.runtime.Plugin#startup()
113 public void startup() throws CoreException {
115 ISavedState lastState =
116 ResourcesPlugin.getWorkspace().addSaveParticipant(
118 new QuantumSaveParticipant());
119 if (lastState != null) {
120 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
121 if (location != null) {
122 // the plugin instance should read any important state from the file.
123 File f = getStateLocation().append(location).toFile();
128 sysClip = new Clipboard(null);
133 * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
136 protected void writeImportantState(File target) {
138 Document document = XMLHelper.createEmptyDocument();
140 Element root = (Element) document.appendChild(
141 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
143 BookmarkCollection.getInstance().exportXML(root);
144 SubsetContentProvider.getInstance().exportXML(root);
146 FileWriter writer = new FileWriter(target);
148 XMLHelper.write(writer, document);
152 } catch (ParserConfigurationException e) {
154 } catch (IOException e) {
159 protected void initializeDefaultPluginPreferences() {
160 PluginPreferences.initialize(getPreferenceStore());
162 getPreferenceStore().setDefault(
163 "phpeclipse.sql.select.template",
164 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
166 getPreferenceStore().setDefault(
167 "phpeclipse.sql.insert.template",
168 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
170 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
172 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
174 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
176 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
178 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
180 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
182 getPreferenceStore().setDefault(
183 "phpeclipse.sql.filename.connect",
184 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
186 // Returns the active page
187 public IWorkbenchPage getActivePage()
189 IWorkbench workbench = getWorkbench();
190 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
191 if (window == null) return null;
192 IWorkbenchPage page = window.getActivePage();
196 * returns a view in the active page, creating it if needed
197 * @param view, the name of the view (e.g com.quantum.view.tableview)
198 * @return true if successful, false if not
200 public IViewPart getView(String view)
202 IViewPart tableView = null;
204 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
205 tableView = page.findView(view);
206 if (tableView == null){
207 // showView will give focus to the created view, we don't want that
208 // so we save the active part
209 IWorkbenchPart part = page.getActivePart();
210 tableView = page.showView(view);
211 // and return the focus to it
214 } catch (PartInitException e) {
225 public Clipboard getSysClip() {
228 protected void initializeImageRegistry(ImageRegistry registry) {
229 super.initializeImageRegistry(registry);
231 ImageStore.initialize(this, registry, getIconLocation());
232 } catch (MalformedURLException e) {
233 // this should never happen, but if it does, we don't get images.
239 * @throws MalformedURLException
241 private URL getIconLocation() throws MalformedURLException {
242 URL installURL = getDescriptor().getInstallURL();
243 return new URL(installURL, "icons/");