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 org.eclipse.core.resources.ISavedState;
16 import org.eclipse.core.resources.ResourcesPlugin;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IPath;
19 import org.eclipse.core.runtime.IPluginDescriptor;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.resource.ImageRegistry;
22 import org.eclipse.swt.dnd.Clipboard;
23 import org.eclipse.ui.IViewPart;
24 import org.eclipse.ui.IWorkbench;
25 import org.eclipse.ui.IWorkbenchPage;
26 import org.eclipse.ui.IWorkbenchPart;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.plugin.AbstractUIPlugin;
30 import org.osgi.framework.BundleContext;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
33 import org.xml.sax.SAXException;
35 import com.quantum.model.BookmarkCollection;
36 import com.quantum.util.xml.XMLHelper;
37 import com.quantum.view.subset.SubsetContentProvider;
40 * Main class of the quantum plugin, sets defaults, saves and recovers state.
43 public class QuantumPlugin extends AbstractUIPlugin {
44 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.quantum.sql";
45 private static QuantumPlugin plugin;
46 private Clipboard sysClip;
50 * TODO: BCH - this constructor has changed in Eclipse 3.0. This
51 * old version of the constructor is still necessary for running under
56 public QuantumPlugin(IPluginDescriptor descriptor) {
60 * This version is recommended for eclipse3.0 and above
62 public QuantumPlugin(){
67 public static QuantumPlugin getDefault() {
71 * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
74 protected void readStateFrom(File target) {
75 String fileName = target.getName();
76 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
78 // It's the 2.0 format for preferences
79 BookmarkCollection.getInstance().load(target);
80 } catch (IOException e) {
84 //It's the 2.1 format for preferences and subsets
85 FileInputStream source = null;
87 source = new FileInputStream(target);
88 } catch (FileNotFoundException e1) {
92 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
93 DocumentBuilder parser;
95 parser = factory.newDocumentBuilder();
96 Document doc = parser.parse(source);
98 Element root = doc.getDocumentElement();
99 BookmarkCollection.getInstance().importXML(root);
100 BookmarkCollection.getInstance().setChanged(false);
101 SubsetContentProvider.getInstance().importXML(root);
103 } catch (ParserConfigurationException e) {
105 } catch (SAXException e) {
107 } catch (IOException e) {
114 * @see org.eclipse.core.runtime.Plugin#startup()
115 * This method is deprecated in Eclipse3.0 we must use start(BundleContext context):
116 * Migration completed.
118 public void startup() throws CoreException {
120 startupMigrationTempMethod(); // To be removed later
124 * Used during startup Eclipse3.0 compatible
126 public void start(BundleContext bundleContext) throws Exception {
127 super.start(bundleContext);
128 startupMigrationTempMethod(); // To be removed later
130 private void startupMigrationTempMethod() throws CoreException {
131 // the contents of this.startup() is moved to here to avoid code duplication - see this.start(BundleContext)
132 ISavedState lastState =
133 ResourcesPlugin.getWorkspace().addSaveParticipant(
135 new QuantumSaveParticipant());
136 if (lastState != null) {
137 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
138 if (location != null) {
139 // the plugin instance should read any important state from the file.
140 File f = getStateLocation().append(location).toFile();
145 sysClip = new Clipboard(null);
150 * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
153 protected void writeImportantState(File target) {
155 Document document = XMLHelper.createEmptyDocument();
157 Element root = (Element) document.appendChild(
158 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
160 BookmarkCollection.getInstance().exportXML(root);
161 SubsetContentProvider.getInstance().exportXML(root);
163 FileWriter writer = new FileWriter(target);
165 XMLHelper.write(writer, document);
169 } catch (ParserConfigurationException e) {
171 } catch (IOException e) {
176 protected void initializeDefaultPluginPreferences() {
177 PluginPreferences.initialize(getPreferenceStore());
179 getPreferenceStore().setDefault(
180 "phpeclipse.sql.select.template",
181 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
183 getPreferenceStore().setDefault(
184 "phpeclipse.sql.insert.template",
185 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
187 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
189 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
191 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
193 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
195 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
197 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
199 getPreferenceStore().setDefault(
200 "phpeclipse.sql.filename.connect",
201 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
203 // Returns the active page
204 public IWorkbenchPage getActivePage()
206 IWorkbench workbench = getWorkbench();
207 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
208 if (window == null) return null;
209 IWorkbenchPage page = window.getActivePage();
213 * returns a view in the active page, creating it if needed
214 * @param view, the name of the view (e.g com.quantum.view.tableview)
215 * @return true if successful, false if not
217 public IViewPart getView(String view)
219 IViewPart tableView = null;
221 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
222 tableView = page.findView(view);
223 if (tableView == null){
224 // showView will give focus to the created view, we don't want that
225 // so we save the active part
226 IWorkbenchPart part = page.getActivePart();
227 tableView = page.showView(view);
228 // and return the focus to it
231 } catch (PartInitException e) {
242 public Clipboard getSysClip() {
245 protected void initializeImageRegistry(ImageRegistry registry) {
246 super.initializeImageRegistry(registry);
248 ImageStore.initialize(this, registry, getIconLocation());
249 } catch (MalformedURLException e) {
250 // this should never happen, but if it does, we don't get images.
256 * @throws MalformedURLException
258 private URL getIconLocation() throws MalformedURLException {
259 // URL installURL = getDescriptor().getInstallURL();
260 URL installURL = getBundle().getEntry("/");
261 return new URL(installURL, "icons/");