introduced IConstant.DEBUG flag
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / PHPEclipseSQLPlugin.java
1 package net.sourceforge.phpdt.sql;
2
3 import java.io.File;
4 import java.net.URL;
5 import java.util.ResourceBundle;
6
7 import org.eclipse.core.resources.ISaveContext;
8 import org.eclipse.core.resources.ISaveParticipant;
9 import org.eclipse.core.resources.ISavedState;
10 import org.eclipse.core.resources.ResourcesPlugin;
11 import org.eclipse.core.runtime.CoreException;
12 import org.eclipse.core.runtime.IPath;
13 import org.eclipse.core.runtime.IPluginDescriptor;
14 import org.eclipse.core.runtime.Path;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.preference.PreferenceConverter;
17 import org.eclipse.jface.resource.ImageDescriptor;
18 import org.eclipse.swt.graphics.FontData;
19 import org.eclipse.swt.graphics.Image;
20 import org.eclipse.swt.graphics.RGB;
21 import org.eclipse.ui.plugin.AbstractUIPlugin;
22
23 import net.sourceforge.phpdt.sql.sql.MultiSQLServer;
24 import net.sourceforge.phpdt.sql.view.bookmark.BookmarkContentProvider;
25
26 /**
27  * @author 
28  */
29 public class PHPEclipseSQLPlugin extends AbstractUIPlugin implements IConstants {
30   
31   private static final String BUNDLE_NAME =
32     "net.sourceforge.phpdt.sql.PHPEclipseSQLResources";
33   private static final ResourceBundle RESOURCE_BUNDLE =
34     ResourceBundle.getBundle(BUNDLE_NAME);
35
36   private static PHPEclipseSQLPlugin plugin;
37   public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.sql";
38
39   public PHPEclipseSQLPlugin(IPluginDescriptor descriptor) {
40     super(descriptor);
41     plugin = this;
42   }
43
44   public static PHPEclipseSQLPlugin getDefault() {
45     return plugin;
46   }
47   protected void readStateFrom(File target) {
48     BookmarkContentProvider.getInstance().load(target);
49   }
50
51   public void startup() throws CoreException {
52     super.startup();
53     ISaveParticipant saveParticipant = new QuantumSaveParticipant();
54     ISavedState lastState =
55       ResourcesPlugin.getWorkspace().addSaveParticipant(this, saveParticipant);
56     if (lastState == null)
57       return;
58     IPath location = lastState.lookup(new Path("save"));
59     if (location == null)
60       return;
61     // the plugin instance should read any important state from the file. 
62     File f = getStateLocation().append(location).toFile();
63     readStateFrom(f);
64   }
65   protected void writeImportantState(File target) {
66     BookmarkContentProvider.getInstance().save(target);
67   }
68   public static ImageDescriptor getImageDescriptor(String name) {
69     ImageDescriptor descriptor = null;
70     try {
71       URL installURL =
72         PHPEclipseSQLPlugin.getDefault().getDescriptor().getInstallURL();
73       URL url = new URL(installURL, "icons/" + name);
74       descriptor = ImageDescriptor.createFromURL(url);
75     } catch (Exception e) {
76       e.printStackTrace();
77     }
78     return descriptor;
79   }
80   public static Image getImage(String name) {
81     return getImageDescriptor(name).createImage();
82   }
83
84   public void dispose() throws CoreException {
85     MultiSQLServer.getInstance().shutdown();
86   }
87
88   protected void initializeDefaultPluginPreferences() {
89     RGB BACKGROUND = new RGB(255, 255, 255);
90     RGB COMMENT = new RGB(88, 148, 64);
91     RGB IDENTIFIER = new RGB(0, 0, 0);
92     RGB KEYWORD = new RGB(126, 0, 75);
93     RGB STRING = new RGB(0, 0, 255);
94     RGB NUMERIC = new RGB(255, 0, 0);
95     RGB DEFAULT = new RGB(0, 0, 0);
96     IPreferenceStore store = getPreferenceStore();
97     PreferenceConverter.setDefault(
98       store,
99       "quantum.background.color",
100       BACKGROUND);
101     PreferenceConverter.setDefault(store, "quantum.text.color", DEFAULT);
102     PreferenceConverter.setDefault(store, "quantum.keyword.color", KEYWORD);
103     PreferenceConverter.setDefault(store, "quantum.comment.color", COMMENT);
104     PreferenceConverter.setDefault(store, "quantum.string.color", STRING);
105     PreferenceConverter.setDefault(store, "quantum.numeric.color", NUMERIC);
106     getPreferenceStore().setDefault("quantum.text.bold", false);
107     getPreferenceStore().setDefault("quantum.keyword.bold", true);
108     getPreferenceStore().setDefault("quantum.string.bold", false);
109     getPreferenceStore().setDefault("quantum.comment.bold", false);
110     getPreferenceStore().setDefault("quantum.numeric.bold", false);
111     PreferenceConverter.setDefault(
112       getPreferenceStore(),
113       "quantum.font",
114       (FontData) null);
115   }
116
117 }
118 class QuantumSaveParticipant implements ISaveParticipant, IConstants {
119   /**
120    * @see org.eclipse.core.resources.ISaveParticipant#doneSaving(ISaveContext)
121    */
122   public void doneSaving(ISaveContext context) {
123   }
124
125   /**
126    * @see org.eclipse.core.resources.ISaveParticipant#prepareToSave(ISaveContext)
127    */
128   public void prepareToSave(ISaveContext context) throws CoreException {
129   }
130
131   /**
132    * @see org.eclipse.core.resources.ISaveParticipant#rollback(ISaveContext)
133    */
134   public void rollback(ISaveContext context) {
135   }
136
137   /**
138    * @see org.eclipse.core.resources.ISaveParticipant#saving(ISaveContext)
139    */
140   public void saving(ISaveContext context) throws CoreException {
141     switch (context.getKind()) {
142       case ISaveContext.FULL_SAVE :
143         PHPEclipseSQLPlugin quantumPluginInstance = PHPEclipseSQLPlugin.getDefault();
144         // save the plug in state 
145         if (BookmarkContentProvider.getInstance().hasChanged()) {
146           int saveNumber = context.getSaveNumber();
147           String saveFileName = "save " + Integer.toString(saveNumber);
148           File f =
149             quantumPluginInstance
150               .getStateLocation()
151               .append(saveFileName)
152               .toFile();
153           // if we fail to write, an exception is thrown and we do not update the path 
154           quantumPluginInstance.writeImportantState(f);
155           context.map(new Path("save"), new Path(saveFileName));
156           context.needSaveNumber();
157         } else {
158           if (DEBUG) {
159           System.out.println("Not saving unchanged bookmarks");
160           }
161         }
162         break;
163       case ISaveContext.PROJECT_SAVE :
164         // get the project related to this save operation 
165         //IProject project = context.getProject(); 
166         // save its information, if necessary 
167         break;
168       case ISaveContext.SNAPSHOT :
169         // This operation needs to be really fast because 
170         // snapshots can be requested frequently by the 
171         // workspace. 
172         break;
173     }
174   }
175 }