misc changes
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / QuantumPlugin.java
1 package com.quantum;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileNotFoundException;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.net.URL;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import com.quantum.model.BookmarkCollection;
15 import com.quantum.util.xml.XMLHelper;
16 import com.quantum.view.subset.SubsetContentProvider;
17
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;
43
44 /**
45  * @author root
46  * Main class of the quantum plugin, sets defaults, saves and recovers state.
47  */
48 public class QuantumPlugin extends AbstractUIPlugin {
49      
50         private static QuantumPlugin plugin;
51         private Clipboard sysClip;
52
53         
54         public QuantumPlugin(IPluginDescriptor descriptor) {
55                 super(descriptor);
56                 plugin = this;
57         }
58
59         public static QuantumPlugin getDefault() {
60                 return plugin;
61         }
62         /**
63          * Reads the Quantum Plugin state from a file. The file has been created with writeImportantState
64          * @param target
65          */
66         protected void readStateFrom(File target) {
67                 String fileName = target.getName();
68                 if (!fileName.endsWith(Messages.getString("QuantumPlugin.saveFileExtension"))){ //$NON-NLS-1$
69             try {
70                         // It's the 2.0 format for preferences
71                         BookmarkCollection.getInstance().load(target);
72             } catch (IOException e) {
73                 e.printStackTrace();
74             }
75                 } else {
76                         //It's the 2.1 format for preferences and subsets
77                         FileInputStream source = null;
78                         try {
79                                 source = new FileInputStream(target);
80                         } catch (FileNotFoundException e1) {
81                                 e1.printStackTrace();
82                                 return;
83                         }
84                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
85                         DocumentBuilder parser;
86                         try {
87                                 parser = factory.newDocumentBuilder();
88                 Document doc = parser.parse(source);
89
90                 Element root = doc.getDocumentElement();
91                 BookmarkCollection.getInstance().importXML(root);
92                 BookmarkCollection.getInstance().setChanged(false);
93                 SubsetContentProvider.getInstance().importXML(root);
94
95                         } catch (ParserConfigurationException e) {
96                                 e.printStackTrace();
97                         } catch (SAXException e) {
98                                 e.printStackTrace();
99                         } catch (IOException e) {
100                                 e.printStackTrace();
101                         }
102                 }               
103         }
104
105         /* (non-Javadoc)
106          * @see org.eclipse.core.runtime.Plugin#startup()
107          */
108         public void startup() throws CoreException {
109                 super.startup();
110                 ISaveParticipant saveParticipant = new QuantumSaveParticipant();
111                 ISavedState lastState =
112                         ResourcesPlugin.getWorkspace().addSaveParticipant(
113                                 this,
114                                 saveParticipant);
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();
120                         readStateFrom(f);
121                         
122             }
123         }
124         sysClip = new Clipboard(null);
125         }
126
127         /* (non-Javadoc)
128          * @see org.eclipse.core.runtime.Plugin#shutdown()
129          */
130         public void shutdown() throws CoreException {
131                 super.shutdown();
132                 sysClip.dispose();
133         }
134
135         /**
136          * Write the bookmarks and subsets to a file, saving them for next use of the quantum plugin
137          * @param target
138          */
139         protected void writeImportantState(File target) {
140         try {
141             Document document = XMLHelper.createEmptyDocument();
142             
143                 Element root = (Element) document.appendChild(
144                 document.createElement(Messages.getString("ExportXMLAction.SavedData"))); //$NON-NLS-1$
145                 
146                 BookmarkCollection.getInstance().exportXML(root);
147                 SubsetContentProvider.getInstance().exportXML(root);
148
149             FileWriter writer =  new FileWriter(target);
150             try {
151                 XMLHelper.createDOMSerializer(writer).serialize(document);
152                 } finally {
153                         writer.close();
154                 }
155         } catch (ParserConfigurationException e) {
156             e.printStackTrace();
157         } catch (IOException e) {
158             e.printStackTrace();
159         }
160         }
161         
162         /**
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
166          */
167         public static ImageDescriptor getImageDescriptor(String name) {
168                 ImageDescriptor descriptor = null;
169                 try {
170                         URL installURL =
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) {
175                         e.printStackTrace();
176                 }
177                 return descriptor;
178         }
179         public static Image getImage(String name) {
180         ImageDescriptor imageDescriptor = getImageDescriptor(name);
181                 return imageDescriptor == null ? null : imageDescriptor.createImage();
182         }
183
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$
211         }
212         // Returns the active page
213         public IWorkbenchPage getActivePage()
214         {
215                 IWorkbench workbench = getWorkbench();
216                 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
217                 if (window == null)     return null;
218                 IWorkbenchPage page = window.getActivePage();
219                 return page;
220         }
221         /**
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
225          */
226         public IViewPart getView(String view)
227         {
228                 IViewPart tableView = null;
229                 try {
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
238                                 page.activate(part);
239                         }
240                 } catch (PartInitException e) {
241                         e.printStackTrace();
242                 }
243                 return tableView;
244         }
245
246
247
248         class QuantumSaveParticipant implements ISaveParticipant {
249                 /**
250                 * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
251                 */
252                 public void doneSaving(ISaveContext context) {
253                 }
254                 /**
255                  * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
256                  */
257                 public void prepareToSave(ISaveContext context) throws CoreException {
258                 }
259
260                 /**
261                  * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
262                  */
263                 public void rollback(ISaveContext context) {
264                 }
265
266                 /**
267                  * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
268                  */
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()) {
276
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();
280
281                                                 // if we fail to write, an exception is thrown and we do not update the path 
282                                                 quantumPluginInstance.writeImportantState(f);
283
284                                                 context.map(new Path(Messages.getString("QuantumPlugin.saveDir")), new Path(saveFileName)); //$NON-NLS-1$
285                                                 context.needSaveNumber();
286
287                                         } else {
288                                                 System.out.println("Not saving unchanged bookmarks"); //$NON-NLS-1$
289                                         }
290                                         break;
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 
295                                         break;
296                                 case ISaveContext.SNAPSHOT :
297                                         // This operation needs to be really fast because 
298                                         // snapshots can be requested frequently by the 
299                                         // workspace. 
300                                         break;
301                         }
302                 }
303         }
304         /**
305          * @return
306          */
307         public Clipboard getSysClip() {
308                 return sysClip;
309         }
310 }