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.preference.IPreferenceStore;
22 import org.eclipse.jface.preference.PreferenceConverter;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.resource.ImageRegistry;
25 import org.eclipse.swt.dnd.Clipboard;
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.IWorkbenchPart;
33 import org.eclipse.ui.IWorkbenchWindow;
34 import org.eclipse.ui.PartInitException;
35 import org.eclipse.ui.plugin.AbstractUIPlugin;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.xml.sax.SAXException;
40 import com.quantum.model.BookmarkCollection;
41 import com.quantum.util.xml.XMLHelper;
42 import com.quantum.view.subset.SubsetContentProvider;
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 ISavedState lastState =
118 ResourcesPlugin.getWorkspace().addSaveParticipant(
120 new QuantumSaveParticipant());
121 if (lastState != null) {
122 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
123 if (location != null) {
124 // the plugin instance should read any important state from the file.
125 File f = getStateLocation().append(location).toFile();
130 sysClip = new Clipboard(null);
134 * @see org.eclipse.core.runtime.Plugin#shutdown()
136 public void shutdown() throws CoreException {
137 if (!sysClip.isDisposed()) {
144 * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
147 protected void writeImportantState(File target) {
149 Document document = XMLHelper.createEmptyDocument();
151 Element root = (Element) document.appendChild(
152 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
154 BookmarkCollection.getInstance().exportXML(root);
155 SubsetContentProvider.getInstance().exportXML(root);
157 FileWriter writer = new FileWriter(target);
159 XMLHelper.write(writer, document);
163 } catch (ParserConfigurationException e) {
165 } catch (IOException e) {
171 // public static Image getImage(String name) {
172 // ImageDescriptor imageDescriptor = getImageDescriptor(name);
173 // return imageDescriptor == null ? null : imageDescriptor.createImage();
176 protected void initializeDefaultPluginPreferences() {
177 RGB BACKGROUND = new RGB(255, 255, 255);
178 RGB COMMENT = new RGB(88, 148, 64);
179 RGB KEYWORD = new RGB(126, 0, 75);
180 RGB STRING = new RGB(0, 0, 255);
181 RGB NUMERIC = new RGB(255, 0, 0);
182 RGB DEFAULT = new RGB(0, 0, 0);
183 IPreferenceStore store = getPreferenceStore();
184 PreferenceConverter.setDefault(store,
185 "quantum.background.color", BACKGROUND); //$NON-NLS-1$
186 PreferenceConverter.setDefault(store,
187 "quantum.text.color", DEFAULT); //$NON-NLS-1$
188 PreferenceConverter.setDefault(store,
189 "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
190 PreferenceConverter.setDefault(store,
191 "quantum.comment.color", COMMENT); //$NON-NLS-1$
192 PreferenceConverter.setDefault(store,
193 "quantum.string.color", STRING); //$NON-NLS-1$
194 PreferenceConverter.setDefault(store,
195 "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
196 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
197 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
198 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
199 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
200 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
201 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
202 getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
205 getPreferenceStore().setDefault(
206 "phpeclipse.sql.select.template",
207 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
209 getPreferenceStore().setDefault(
210 "phpeclipse.sql.insert.template",
211 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
213 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
215 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
217 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
219 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
221 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
223 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
225 getPreferenceStore().setDefault(
226 "phpeclipse.sql.filename.connect",
227 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
229 // Returns the active page
230 public IWorkbenchPage getActivePage()
232 IWorkbench workbench = getWorkbench();
233 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
234 if (window == null) return null;
235 IWorkbenchPage page = window.getActivePage();
239 * returns a view in the active page, creating it if needed
240 * @param view, the name of the view (e.g com.quantum.view.tableview)
241 * @return true if successful, false if not
243 public IViewPart getView(String view)
245 IViewPart tableView = null;
247 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
248 tableView = page.findView(view);
249 if (tableView == null){
250 // showView will give focus to the created view, we don't want that
251 // so we save the active part
252 IWorkbenchPart part = page.getActivePart();
253 tableView = page.showView(view);
254 // and return the focus to it
257 } catch (PartInitException e) {
266 public Clipboard getSysClip() {
269 protected void initializeImageRegistry(ImageRegistry registry) {
270 super.initializeImageRegistry(registry);
272 ImageStore.initialize(registry, getIconLocation());
273 } catch (MalformedURLException e) {
274 // this should never happen, but if it does, we don't get images.
280 * @throws MalformedURLException
282 URL getIconLocation() throws MalformedURLException {
283 URL installURL = QuantumPlugin.getDefault().getDescriptor().getInstallURL();
284 return new URL(installURL, "icons/");