A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / phphelp / PHPHelpPlugin.java
1 /**********************************************************************
2  Copyright (c) 2000, 2002 IBM Corp. and others.
3  All rights reserved. This program and the accompanying materials
4  are made available under the terms of the Common Public License v1.0
5  which accompanies this distribution, and is available at
6  http://www.eclipse.org/legal/cpl-v10.html
7
8  Contributors:
9  IBM Corporation - Initial implementation
10  www.phpeclipse.de
11  **********************************************************************/
12 package net.sourceforge.phpdt.phphelp;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import net.sourceforge.phpdt.httpquery.config.ConfigurationManager;
18 import net.sourceforge.phpdt.httpquery.config.IConfigurationWorkingCopy;
19
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Platform;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.preference.IPreferenceStore;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.PlatformUI;
31 import org.eclipse.ui.plugin.AbstractUIPlugin;
32 import org.osgi.framework.BundleContext;
33
34 /**
35  * The main plugin class to be used in the desktop.
36  */
37 public class PHPHelpPlugin extends AbstractUIPlugin {
38         public static final String PHP_CHM_ENABLED = "_php_chm_enabled";
39
40         public static final String PHP_CHM_FILE = "_php_chm_file";
41
42         public static final String PHP_CHM_COMMAND = "_php_chm_command";
43
44         public static final String HTTP_QUERY = "HTTP Query";
45
46         public final static String PREF_STRING_CONFIGURATIONS = "__configurations1";
47
48         public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
49                         + "<configurations>"
50                         + "<config name=\"PHP Manual\" type-id=\"HTTP Query\" url=\"http://www.php.net/manual/en/function.$php.selection.php\"/>"
51                         + "<config name=\"Google.com\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
52                         + "<config name=\"Koders.com\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
53                         + "<config name=\"Leo.org Deutsch/English\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
54                         + "<config name=\"Localhost\" type-id=\"HTTP Query\" url=\"http://localhost\"/>"
55                         + "</configurations>";
56
57         public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
58
59         /**
60          * The id of the PHP plugin (value
61          * <code>"net.sourceforge.phpeclipse.phphelp"</code>).
62          */
63         public static final String PLUGIN_ID = "net.sourceforge.phpeclipse.phphelp"; //$NON-NLS-1$
64
65         // The shared instance.
66         private static PHPHelpPlugin plugin;
67
68         private static ConfigurationManager manager;
69
70         /**
71          * The constructor.
72          */
73         public PHPHelpPlugin() {
74                 super();
75                 plugin = this;
76         }
77
78         /**
79          * Returns the shared instance.
80          */
81         public static PHPHelpPlugin getDefault() {
82                 return plugin;
83         }
84
85         /**
86          * Returns the workspace instance.
87          */
88         public static IWorkspace getWorkspace() {
89                 return ResourcesPlugin.getWorkspace();
90         }
91
92         public static IWorkbenchPage getActivePage() {
93                 return getDefault().internalGetActivePage();
94         }
95
96         private IWorkbenchPage internalGetActivePage() {
97                 IWorkbenchWindow window = PlatformUI.getWorkbench()
98                                 .getActiveWorkbenchWindow();
99                 if (window != null)
100                         return window.getActivePage();
101                 return null;
102         }
103
104         public static IWorkbenchWindow getActiveWorkbenchWindow() {
105                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
106         }
107
108         public static Shell getActiveWorkbenchShell() {
109                 return getActiveWorkbenchWindow().getShell();
110         }
111
112         public static void log(IStatus status) {
113                 getDefault().getLog().log(status);
114         }
115
116         public static void log(int severity, String message) {
117                 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message,
118                                 null);
119                 log(status);
120         }
121
122         public static void log(Throwable e) {
123                 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR,
124                                 "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
125         }
126
127         public static boolean isDebug() {
128                 return getDefault().isDebugging();
129         }
130
131         protected void initializeDefaultPreferences(IPreferenceStore store) {
132                 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
133                 addType(HTTP_QUERY);
134                 // windows preferences:
135                 String windowsSystem = Platform.getWS();
136
137                 if (windowsSystem.equals(Platform.WS_WIN32)) {
138                         store.setDefault(PHP_CHM_ENABLED, "false");
139                         store
140                                         .setDefault(PHP_CHM_FILE,
141                                                         "c:\\wampp2\\php\\php_manual_en.chm");
142                         store.setDefault(PHP_CHM_COMMAND,
143                                         "hh.exe \"mk:@MSITStore:{0}::/en/function.{1}.html\"");
144                 } else {
145                         store.setDefault(PHP_CHM_ENABLED, "false");
146                         store.setDefault(PHP_CHM_FILE, "");
147                         store.setDefault(PHP_CHM_COMMAND, "");
148                 }
149
150         }
151
152         /**
153          * Returns the standard display to be used. The method first checks, if the
154          * thread calling this method has an associated display. If so, this display
155          * is returned. Otherwise the method returns the default display.
156          */
157         public static Display getStandardDisplay() {
158                 Display display = Display.getCurrent();
159                 if (display == null) {
160                         display = Display.getDefault();
161                 }
162                 return display;
163         }
164
165         // public void startup() throws CoreException {
166         // super.startup();
167         // IAdapterManager manager = Platform.getAdapterManager();
168         // manager.registerAdapters(new PHPElementAdapterFactory(),
169         // PHPElement.class);
170         // manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
171         // // externalTools.startUp();
172         // getStandardDisplay().asyncExec(new Runnable() {
173         // public void run() {
174         // //initialize the variable context manager
175         // VariableContextManager.getDefault();
176         // }
177         // });
178         // }
179
180         // /**
181         // * @see org.eclipse.core.runtime.Plugin#shutdown()
182         // */
183         // public void shutdown() throws CoreException {
184         // // externalTools.shutDown();
185         // ColorManager.getDefault().dispose();
186         // }
187
188         public void start(BundleContext context) throws Exception {
189                 super.start(context);
190
191                 manager = ConfigurationManager.getInstance();
192                 // IAdapterManager manager = Platform.getAdapterManager();
193                 // manager.registerAdapters(new PHPElementAdapterFactory(),
194                 // PHPElement.class);
195                 // manager.registerAdapters(new ResourceAdapterFactory(),
196                 // IResource.class);
197                 // // externalTools.startUp();
198                 // getStandardDisplay().asyncExec(new Runnable() {
199                 // public void run() {
200                 // //initialize the variable context manager
201                 // VariableContextManager.getDefault();
202                 // }
203                 // });
204         }
205
206         public void stop(BundleContext context) throws Exception {
207                 // ColorManager.getDefault().dispose();
208                 super.stop(context);
209         }
210
211         /**
212          * Returns the translated String found with the given key.
213          * 
214          * @return java.lang.String
215          * @param key
216          *            java.lang.String
217          */
218         public static String getResource(String key) {
219                 try {
220                         return Platform.getResourceString(getDefault().getBundle(), key);
221                 } catch (Exception e) {
222                         return key;
223                 }
224         }
225
226         /**
227          * Return a list of all the existing configurations.
228          * 
229          * @return java.util.List
230          */
231         public static List getConfigurations() {
232                 return manager.getConfigurations();
233         }
234
235         /**
236          * Create a new monitor.
237          * 
238          * @return working copy
239          */
240         public static IConfigurationWorkingCopy createConfiguration() {
241                 return manager.createConfiguration();
242         }
243
244         public static ArrayList getTypes() {
245                 return CONFIGURATION_TYPES;
246         }
247
248         public static void addType(String type) {
249                 CONFIGURATION_TYPES.add(type);
250         }
251 }