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;
46 * Main class of the quantum plugin, sets defaults, saves and recovers state.
48 public class QuantumPlugin extends AbstractUIPlugin {
50 private static QuantumPlugin plugin;
51 private Clipboard sysClip;
54 public QuantumPlugin(IPluginDescriptor descriptor) {
59 public static QuantumPlugin getDefault() {
63 * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
66 protected void readStateFrom(File target) {
67 String fileName = target.getName();
68 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
70 // It's the 2.0 format for preferences
71 BookmarkCollection.getInstance().load(target);
72 } catch (IOException e) {
76 //It's the 2.1 format for preferences and subsets
77 FileInputStream source = null;
79 source = new FileInputStream(target);
80 } catch (FileNotFoundException e1) {
84 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85 DocumentBuilder parser;
87 parser = factory.newDocumentBuilder();
88 Document doc = parser.parse(source);
90 Element root = doc.getDocumentElement();
91 BookmarkCollection.getInstance().importXML(root);
92 BookmarkCollection.getInstance().setChanged(false);
93 SubsetContentProvider.getInstance().importXML(root);
95 } catch (ParserConfigurationException e) {
97 } catch (SAXException e) {
99 } catch (IOException e) {
106 * @see org.eclipse.core.runtime.Plugin#startup()
108 public void startup() throws CoreException {
110 ISaveParticipant saveParticipant = new QuantumSaveParticipant();
111 ISavedState lastState =
112 ResourcesPlugin.getWorkspace().addSaveParticipant(
115 if (lastState != null) {
116 IPath location = lastState.lookup(new Path(Messages.getString("QuantumPlugin.saveDir"))); //$NON-NLS-1$
117 if (location != null) {
118 // the plugin instance should read any important state from the file.
119 File f = getStateLocation().append(location).toFile();
124 sysClip = new Clipboard(null);
128 * @see org.eclipse.core.runtime.Plugin#shutdown()
130 public void shutdown() throws CoreException {
136 * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
139 protected void writeImportantState(File target) {
141 Document document = XMLHelper.createEmptyDocument();
143 Element root = (Element) document.appendChild(
144 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
146 BookmarkCollection.getInstance().exportXML(root);
147 SubsetContentProvider.getInstance().exportXML(root);
149 FileWriter writer = new FileWriter(target);
151 XMLHelper.createDOMSerializer(writer).serialize(document);
155 } catch (ParserConfigurationException e) {
157 } catch (IOException e) {
163 * Gets an image descriptof from a file in the icons directory
164 * @param name of the file to get
165 * @return ImageDescriptor or null if not found
167 public static ImageDescriptor getImageDescriptor(String name) {
168 ImageDescriptor descriptor = null;
171 QuantumPlugin.getDefault().getDescriptor().getInstallURL();
172 URL url = new URL(installURL, Messages.getString("QuantumPlugin.iconsDir") + name); //$NON-NLS-1$
173 descriptor = ImageDescriptor.createFromURL(url);
174 } catch (Exception e) {
179 public static Image getImage(String name) {
180 ImageDescriptor imageDescriptor = getImageDescriptor(name);
181 return imageDescriptor == null ? null : imageDescriptor.createImage();
184 protected void initializeDefaultPluginPreferences() {
185 RGB BACKGROUND = new RGB(255, 255, 255);
186 RGB COMMENT = new RGB(88, 148, 64);
187 RGB KEYWORD = new RGB(126, 0, 75);
188 RGB STRING = new RGB(0, 0, 255);
189 RGB NUMERIC = new RGB(255, 0, 0);
190 RGB DEFAULT = new RGB(0, 0, 0);
191 IPreferenceStore store = getPreferenceStore();
192 PreferenceConverter.setDefault(store,
193 "quantum.background.color", BACKGROUND); //$NON-NLS-1$
194 PreferenceConverter.setDefault(store,
195 "quantum.text.color", DEFAULT); //$NON-NLS-1$
196 PreferenceConverter.setDefault(store,
197 "quantum.keyword.color", KEYWORD); //$NON-NLS-1$
198 PreferenceConverter.setDefault(store,
199 "quantum.comment.color", COMMENT); //$NON-NLS-1$
200 PreferenceConverter.setDefault(store,
201 "quantum.string.color", STRING); //$NON-NLS-1$
202 PreferenceConverter.setDefault(store,
203 "quantum.numeric.color", NUMERIC); //$NON-NLS-1$
204 getPreferenceStore().setDefault("quantum.text.bold", false); //$NON-NLS-1$
205 getPreferenceStore().setDefault("quantum.keyword.bold", true); //$NON-NLS-1$
206 getPreferenceStore().setDefault("quantum.string.bold", false); //$NON-NLS-1$
207 getPreferenceStore().setDefault("quantum.comment.bold", false); //$NON-NLS-1$
208 getPreferenceStore().setDefault("quantum.numeric.bold", false); //$NON-NLS-1$
209 PreferenceConverter.setDefault(getPreferenceStore(), "quantum.font", (FontData) null); //$NON-NLS-1$
210 getPreferenceStore().setDefault("com.quantum.model.Bookmark.queryHistorySize", 20); //$NON-NLS-1$
212 // Returns the active page
213 public IWorkbenchPage getActivePage()
215 IWorkbench workbench = getWorkbench();
216 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
217 if (window == null) return null;
218 IWorkbenchPage page = window.getActivePage();
222 * returns a view in the active page, creating it if needed
223 * @param view, the name of the view (e.g com.quantum.view.tableview)
224 * @return true if successful, false if not
226 public IViewPart getView(String view)
228 IViewPart tableView = null;
230 IWorkbenchPage page = QuantumPlugin.getDefault().getActivePage();
231 tableView = page.findView(view);
232 if (tableView == null){
233 // showView will give focus to the created view, we don't want that
234 // so we save the active part
235 IWorkbenchPart part = page.getActivePart();
236 tableView = page.showView(view);
237 // and return the focus to it
240 } catch (PartInitException e) {
248 class QuantumSaveParticipant implements ISaveParticipant {
250 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
252 public void doneSaving(ISaveContext context) {
255 * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
257 public void prepareToSave(ISaveContext context) throws CoreException {
261 * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
263 public void rollback(ISaveContext context) {
267 * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
269 public void saving(ISaveContext context) throws CoreException {
270 switch (context.getKind()) {
271 case ISaveContext.FULL_SAVE :
272 QuantumPlugin quantumPluginInstance = QuantumPlugin.getDefault();
273 // save the plug in state
274 if (BookmarkCollection.getInstance().isAnythingChanged()
275 || SubsetContentProvider.getInstance().hasChanged()) {
277 int saveNumber = context.getSaveNumber();
278 String saveFileName = Messages.getString("QuantumPlugin.saveDir") + "-" + Integer.toString(saveNumber) + Messages.getString("QuantumPlugin.saveFileExtension"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
279 File f = quantumPluginInstance.getStateLocation().append(saveFileName).toFile();
281 // if we fail to write, an exception is thrown and we do not update the path
282 quantumPluginInstance.writeImportantState(f);
284 context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
285 context.needSaveNumber();
288 System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
291 case ISaveContext.PROJECT_SAVE :
292 // get the project related to this save operation
293 //IProject project = context.getProject();
294 // save its information, if necessary
296 case ISaveContext.SNAPSHOT :
297 // This operation needs to be really fast because
298 // snapshots can be requested frequently by the
307 public Clipboard getSysClip() {