make sure that the VariableHover for the debugPlugin is the first to show
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.jtidy / src / net / sourceforge / phpdt / tidy / JtidyPlugin.java
1 package net.sourceforge.phpdt.tidy;
2
3 import java.io.File;
4 import java.util.MissingResourceException;
5 import java.util.ResourceBundle;
6
7 import net.sourceforge.phpdt.tidy.preferences.IPreferenceConstants;
8 import net.sourceforge.phpdt.tidy.w3c.Configuration;
9 import net.sourceforge.phpdt.tidy.w3c.Tidy;
10
11 import org.eclipse.core.resources.IResource;
12 import org.eclipse.core.resources.IWorkspace;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.core.runtime.IPluginDescriptor;
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.util.IPropertyChangeListener;
17 import org.eclipse.jface.util.PropertyChangeEvent;
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
19
20 /**
21  * The main plugin class to be used in the desktop.
22  */
23 public class JtidyPlugin extends AbstractUIPlugin implements IPreferenceConstants {
24   //The shared instance.
25   private static JtidyPlugin fPlugin;
26   //Resource bundle.
27   private ResourceBundle fResourceBundle;
28
29   private Tidy fTidy;
30   private boolean fUseConfigurationFile;
31   public static String MARKER_NAME = "net.sourceforge.phpdt.tidy.MarkerName";
32
33   /**
34    * The constructor.
35    */
36   public JtidyPlugin(IPluginDescriptor descriptor) {
37     super(descriptor);
38     fPlugin = this;
39     try {
40       fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpdt.tidy.JtidyPluginResources");
41     } catch (MissingResourceException x) {
42       fResourceBundle = null;
43     }
44     initTidy();
45   }
46
47   /* (non-Javadoc)
48    * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
49    */
50   protected void initializeDefaultPreferences(IPreferenceStore store) {
51     store.setDefault(GENERAL_CONFIG_FILE, ""); //$NON-NLS-1$
52     store.setDefault(GENERAL_USE_CONFIG_FILE, "false"); //$NON-NLS-1$
53
54     store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
55     store.setDefault(GENERAL_SHOW_WARNINGS, "true"); //$NON-NLS-1$
56     store.setDefault(GENERAL_QUIET, "false"); //$NON-NLS-1$
57     store.setDefault(GENERAL_EMACS, "false"); //$NON-NLS-1$
58     store.setDefault(GENERAL_KEEP_FILE_TIMES, "true"); //$NON-NLS-1$    
59
60     store.setDefault(WRAP_ATT_VALS, "false"); //$NON-NLS-1$
61     store.setDefault(WRAP_SCRIPTLETS, "false"); //$NON-NLS-1$
62     store.setDefault(WRAP_SECTION, "true"); //$NON-NLS-1$
63     store.setDefault(WRAP_ASP, "true"); //$NON-NLS-1$
64     store.setDefault(WRAP_JSTE, "true"); //$NON-NLS-1$
65     store.setDefault(WRAP_PHP, "true"); //$NON-NLS-1$
66     store.setDefault(INDENT_ATTRIBUTES, "false"); //$NON-NLS-1$
67     store.setDefault(LITERAL_ATTRIBS, "false"); //$NON-NLS-1$
68
69     //    store.setDefault(TYPE_TREAD_AS_XML, "false"); //$NON-NLS-1$
70     //    store.setDefault(TYPE_DOCTYPE, "false"); //$NON-NLS-1$
71
72     store.setDefault(OUTPUT_MAKE_CLEAR, "false"); //$NON-NLS-1$
73     store.setDefault(OUTPUT_STRIP_WORD, "false"); //$NON-NLS-1$
74     store.setDefault(OUTPUT_ENCLOSE_BODY_TEXT, "false"); //$NON-NLS-1$
75     store.setDefault(OUTPUT_ENCLOSE_BLOCK_TEXT, "false"); //$NON-NLS-1$
76
77     store.setDefault(OUT_AS_RAW, "false"); //$NON-NLS-1$
78     store.setDefault(OUT_UPPER_TAGS, "false"); //$NON-NLS-1$
79     store.setDefault(OUT_UPPER_ATTR, "false"); //$NON-NLS-1$
80     store.setDefault(OUT_BREAK_BR, "false"); //$NON-NLS-1$
81     store.setDefault(OUT_WRAP_ATTR_VALUES, "false"); //$NON-NLS-1$
82     store.setDefault(OUT_WRAP_SCRIPS, "false"); //$NON-NLS-1$
83
84     store.setDefault(GENERAL_TIDY_MARK, "true"); //$NON-NLS-1$
85   }
86
87   /**
88    * Initialises the Tidy Instance and registers the Preference Listener.
89    */
90   private void initTidy() {
91     fTidy = new Tidy();
92     String rawConfigFileName = getPreferenceStore().getString(GENERAL_CONFIG_FILE);
93     updateTidyConfig(rawConfigFileName);
94     IPropertyChangeListener listener = new IPropertyChangeListener() {
95       public void propertyChange(PropertyChangeEvent event) {
96         String propName = event.getProperty();
97         IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
98         Configuration configuration = fTidy.getConfiguration();
99         Object value = event.getNewValue();
100
101         if (value instanceof Boolean) {
102           boolean enabled = ((Boolean) value).booleanValue();
103           if (propName.equals(GENERAL_USE_CONFIG_FILE)) {
104             fUseConfigurationFile = enabled;
105             initConfiguration();
106             return;
107           }
108           fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
109           if (!fUseConfigurationFile) {
110             if (propName.equals(GENERAL_TIDY_MARK)) {
111               configuration.TidyMark = enabled;
112             }
113             if (propName.equals(GENERAL_SHOW_WARNINGS)) {
114               configuration.ShowWarnings = enabled;
115             }
116             if (propName.equals(GENERAL_QUIET)) {
117               configuration.Quiet = enabled;
118             }
119             if (propName.equals(GENERAL_EMACS)) {
120               configuration.Emacs = enabled;
121             }
122             if (propName.equals(GENERAL_KEEP_FILE_TIMES)) {
123               configuration.KeepFileTimes = enabled;
124             }
125             // wrap / indent
126             if (propName.equals(WRAP_ATT_VALS)) {
127               configuration.WrapAttVals = enabled;
128             }
129             if (propName.equals(WRAP_SCRIPTLETS)) {
130               configuration.WrapScriptlets = enabled;
131             }
132             if (propName.equals(WRAP_SECTION)) {
133               configuration.WrapSection = enabled;
134             }
135             if (propName.equals(WRAP_ASP)) {
136               configuration.WrapAsp = enabled;
137             }
138             if (propName.equals(WRAP_JSTE)) {
139               configuration.WrapJste = enabled;
140             }
141             if (propName.equals(WRAP_PHP)) {
142               configuration.WrapPhp = enabled;
143             }
144             if (propName.equals(INDENT_ATTRIBUTES)) {
145               configuration.IndentAttributes = enabled;
146             }
147             if (propName.equals(LITERAL_ATTRIBS)) {
148               configuration.LiteralAttribs = enabled;
149             }
150
151             //            if (propName.equals(TYPE_TREAD_AS_XML)) {
152             //              configuration.XmlTags = enabled;
153             //            }
154             //            if (propName.equals(TYPE_DOCTYPE)) {
155             //              configuration.XmlPi = enabled;
156             //            }                
157
158             if (propName.equals(OUTPUT_MAKE_CLEAR)) {
159               configuration.MakeClean = enabled;
160             }
161
162             if (propName.equals(OUTPUT_ENCLOSE_BODY_TEXT)) {
163               configuration.EncloseBodyText = enabled;
164             }
165             if (propName.equals(OUTPUT_ENCLOSE_BLOCK_TEXT)) {
166               configuration.EncloseBlockText = enabled;
167             }
168             if (propName.equals(OUTPUT_STRIP_WORD)) {
169               configuration.Word2000 = enabled;
170             }
171             //            if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
172             //              configuration. = enabled;
173             //            } 
174
175             if (propName.equals(OUT_AS_RAW)) {
176               configuration.RawOut = enabled;
177             }
178             if (propName.equals(OUT_UPPER_TAGS)) {
179               configuration.UpperCaseTags = enabled;
180             }
181             if (propName.equals(OUT_UPPER_ATTR)) {
182               configuration.UpperCaseAttrs = enabled;
183             }
184
185           }
186         } else if (value instanceof String) {
187           if (fUseConfigurationFile) {
188             if (propName.equals(GENERAL_CONFIG_FILE)) {
189               updateTidyConfig((String) value);
190             }
191           } else {
192             if (propName.equals(OUTPUT_DEFAULT_ALT_TEXT)) {
193               configuration.altText = (String) value;
194             }
195             if (propName.equals(INPUT_NEW_EMPTY_TAGS)) {
196               configuration.parseEmptyTagNames((String) value, null);
197             }
198             if (propName.equals(INPUT_NEW_INLINE_TAGS)) {
199               configuration.parseInlineTagNames((String) value, null);
200             }
201             if (propName.equals(INPUT_NEW_BLOCKLEVEL_TAGS)) {
202               configuration.parseBlockTagNames((String) value, null);
203             }
204             if (propName.equals(INPUT_NEW_PRE_TAGS)) {
205               configuration.parsePreTagNames((String) value, null);
206             }
207           }
208         }
209       }
210     };
211
212     getPreferenceStore().addPropertyChangeListener(listener);
213     initConfiguration();
214   }
215
216   private void initConfiguration() {
217     IPreferenceStore store = JtidyPlugin.getDefault().getPreferenceStore();
218
219     fUseConfigurationFile = store.getBoolean(GENERAL_USE_CONFIG_FILE);
220     Configuration configuration = fTidy.getConfiguration();
221
222     String value;
223     if (fUseConfigurationFile) {
224       if ((value = store.getString(GENERAL_CONFIG_FILE)) != null) {
225         updateTidyConfig((String) value);
226       }
227     } else {
228
229       configuration.TidyMark = store.getBoolean(GENERAL_TIDY_MARK);
230       configuration.ShowWarnings = store.getBoolean(GENERAL_SHOW_WARNINGS);
231       configuration.Quiet = store.getBoolean(GENERAL_QUIET);
232       configuration.Emacs = store.getBoolean(GENERAL_EMACS);
233       configuration.KeepFileTimes = store.getBoolean(GENERAL_KEEP_FILE_TIMES);
234
235       configuration.WrapAttVals = store.getBoolean(WRAP_ATT_VALS);
236       configuration.WrapScriptlets = store.getBoolean(WRAP_SCRIPTLETS);
237       configuration.WrapSection = store.getBoolean(WRAP_SECTION);
238       configuration.WrapAsp = store.getBoolean(WRAP_ASP);
239       configuration.WrapJste = store.getBoolean(WRAP_JSTE);
240       configuration.WrapPhp = store.getBoolean(WRAP_PHP);
241       configuration.IndentAttributes = store.getBoolean(INDENT_ATTRIBUTES);
242       configuration.LiteralAttribs = store.getBoolean(LITERAL_ATTRIBS);
243
244       configuration.MakeClean = store.getBoolean(OUTPUT_MAKE_CLEAR);
245       configuration.EncloseBodyText = store.getBoolean(OUTPUT_ENCLOSE_BODY_TEXT);
246       configuration.EncloseBlockText = store.getBoolean(OUTPUT_ENCLOSE_BLOCK_TEXT);
247       configuration.Word2000 = store.getBoolean(OUTPUT_STRIP_WORD);
248       if ((value = store.getString(OUTPUT_DEFAULT_ALT_TEXT)) != null) {
249         configuration.altText = value;
250       }
251       
252       configuration.RawOut = store.getBoolean(OUT_AS_RAW);
253       configuration.UpperCaseTags = store.getBoolean(OUT_UPPER_TAGS);
254       configuration.UpperCaseAttrs = store.getBoolean(OUT_UPPER_ATTR);
255       
256       if ((value = store.getString(INPUT_NEW_EMPTY_TAGS)) != null) {
257         configuration.parseEmptyTagNames(value, null);
258       }
259       if ((value = store.getString(INPUT_NEW_INLINE_TAGS)) != null) {
260         configuration.parseInlineTagNames(value, null);
261       }
262       if ((value = store.getString(INPUT_NEW_BLOCKLEVEL_TAGS)) != null) {
263         configuration.parseBlockTagNames(value, null);
264       }
265       if ((value = store.getString(INPUT_NEW_PRE_TAGS)) != null) {
266         configuration.parsePreTagNames(value, null);
267       }
268     }
269   }
270
271   /**
272    * Updates the configuration of the tidy instance with content of the given
273    * file, if ths file exists. Returns silently on error.
274    * 
275    * @param rawConfigFileName
276    */
277   private void updateTidyConfig(String rawConfigFileName) {
278     File config = new File(rawConfigFileName);
279     if (config.exists()) {
280       fTidy.setConfigurationFromFile(config.getAbsolutePath());
281     }
282   }
283
284   /**
285    * Returns the shared instance.
286    */
287   public static JtidyPlugin getDefault() {
288     return fPlugin;
289   }
290
291   /**
292    * Returns the workspace instance.
293    */
294   public static IWorkspace getWorkspace() {
295     return ResourcesPlugin.getWorkspace();
296   }
297
298   /**
299    * Returns the string from the plugin's resource bundle,
300    * or 'key' if not found.
301    */
302   public static String getResourceString(String key) {
303     ResourceBundle bundle = JtidyPlugin.getDefault().getResourceBundle();
304     if (bundle == null) {
305       return key;
306     }
307     try {
308       return bundle.getString(key);
309     } catch (MissingResourceException e) {
310       return key;
311     }
312
313   }
314
315   /**
316    * Returns the plugin's resource bundle,
317    */
318   public ResourceBundle getResourceBundle() {
319     return fResourceBundle;
320   }
321
322   /**
323    * Returns the Tidy instance for this resource. Curently the resource can be
324    * null, I just copied a similar conzept from a differnt Project, which uses
325    * a per Project configuration.
326    * @param resource
327    * @return Tidy
328    */
329
330   public static Tidy getTidyInstance(IResource resource) {
331     //IProject project = resource.getProject();
332     //TODO: bind the instance to the resource...
333     if (getDefault().fTidy == null) {
334       getDefault().initTidy();
335     }
336     return getDefault().fTidy;
337   }
338
339 }