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.ImageRegistry;
24 import org.eclipse.jface.resource.JFaceResources;
25 import org.eclipse.swt.dnd.Clipboard;
26 import org.eclipse.swt.graphics.RGB;
27 import org.eclipse.ui.IViewPart;
28 import org.eclipse.ui.IWorkbench;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchPart;
31 import org.eclipse.ui.IWorkbenchWindow;
32 import org.eclipse.ui.PartInitException;
33 import org.eclipse.ui.plugin.AbstractUIPlugin;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.xml.sax.SAXException;
38 import com.quantum.model.BookmarkCollection;
39 import com.quantum.util.xml.XMLHelper;
40 import com.quantum.view.subset.SubsetContentProvider;
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) {
160 // public static Image getImage(String name) {
161 // ImageDescriptor imageDescriptor = getImageDescriptor(name);
162 // return imageDescriptor == null ? null : imageDescriptor.createImage();
165 protected void initializeDefaultPluginPreferences() {
166 RGB BACKGROUND = new RGB(255, 255, 255);
167 RGB COMMENT = new RGB(88, 148, 64);
168 RGB KEYWORD = new RGB(126, 0, 75);
169 RGB STRING = new RGB(0, 0, 255);
170 RGB NUMERIC = new RGB(255, 0, 0);
171 RGB DEFAULT = new RGB(0, 0, 0);
172 IPreferenceStore store = getPreferenceStore();
173 PreferenceConverter.setDefault(store,
174 "quantum.background.color", BACKGROUND); //$NON-NLS-1$
175 PreferenceConverter.setDefault(store,
176 "quantum.text.color", DEFAULT); //$NON-NLS-1$
177 PreferenceConverter.setDefault(store,
178 "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
179 PreferenceConverter.setDefault(store,
180 "quantum.comment.color", COMMENT); //$NON-NLS-1$
181 PreferenceConverter.setDefault(store,
182 "quantum.string.color", STRING); //$NON-NLS-1$
183 PreferenceConverter.setDefault(store,
184 "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
185 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
186 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
187 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
188 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
189 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
190 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", //$NON-NLS-1$
191 JFaceResources.getTextFont().getFontData());
192 getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
195 getPreferenceStore().setDefault(
196 "phpeclipse.sql.select.template",
197 "$results = mysql_query(\"SELECT {0} FROM {1} WHERE {2} \");");
199 getPreferenceStore().setDefault(
200 "phpeclipse.sql.insert.template",
201 "$results = mysql_query(\"INSERT INTO {0} ({1}) VALUES {2} \");");
203 getPreferenceStore().setDefault("phpeclipse.sql.update.template", "$results = mysql_query(\"UPDATE {0} SET {1} WHERE {2} \");");
205 getPreferenceStore().setDefault("phpeclipse.sql.delete.template", "$results = mysql_query(\"DELETE FROM {0} WHERE {1} \");");
207 getPreferenceStore().setDefault("phpeclipse.sql.username.connect", "root");
209 getPreferenceStore().setDefault("phpeclipse.sql.connect.connect", "jdbc:mysql://localhost/mysql");
211 getPreferenceStore().setDefault("phpeclipse.sql.driver.connect", "com.mysql.jdbc.Driver");
213 getPreferenceStore().setDefault("phpeclipse.sql.type.connect", "MySQL");
215 getPreferenceStore().setDefault(
216 "phpeclipse.sql.filename.connect",
217 "C:\\wampp2\\mysql\\lib\\mysql-connector.jar");
219 // Returns the active page
220 public IWorkbenchPage getActivePage()
222 IWorkbench workbench = getWorkbench();
223 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
224 if (window == null) return null;
225 IWorkbenchPage page = window.getActivePage();
229 * returns a view in the active page, creating it if needed
230 * @param view, the name of the view (e.g com.quantum.view.tableview)
231 * @return true if successful, false if not
233 public IViewPart getView(String view)
235 IViewPart tableView = null;
237 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
238 tableView = page.findView(view);
239 if (tableView == null){
240 // showView will give focus to the created view, we don't want that
241 // so we save the active part
242 IWorkbenchPart part = page.getActivePart();
243 tableView = page.showView(view);
244 // and return the focus to it
247 } catch (PartInitException e) {
256 public Clipboard getSysClip() {
259 protected void initializeImageRegistry(ImageRegistry registry) {
260 super.initializeImageRegistry(registry);
262 ImageStore.initialize(registry, getIconLocation());
263 } catch (MalformedURLException e) {
264 // this should never happen, but if it does, we don't get images.
270 * @throws MalformedURLException
272 URL getIconLocation() throws MalformedURLException {
273 URL installURL = QuantumPlugin.getDefault().getDescriptor().getInstallURL();
274 return new URL(installURL, "icons/");