3c24502fc130b2e093e3ce1518af51e431d6dfa0
[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 = "__configurations";
47
48         public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
49                         + "<configurations>"
50                         + "<config name=\"PHP Online Manual\" type-id=\"HTTP Query\" url=\"http://www.php.net/manual/en/function.$php.selection.php\"/>"
51                         + "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
52                         + "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
53                         + "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
54                         + "</configurations>";
55
56         public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
57
58         /**
59          * The id of the PHP plugin (value
60          * <code>"net.sourceforge.phpeclipse.phphelp"</code>).
61          */
62         public static final String PLUGIN_ID = "net.sourceforge.phpeclipse.phphelp"; //$NON-NLS-1$
63
64         // The shared instance.
65         private static PHPHelpPlugin plugin;
66
67         private static ConfigurationManager manager;
68         /**
69          * The constructor.
70          */
71         public PHPHelpPlugin() {
72                 super();
73                 plugin = this;
74         }
75
76         /**
77          * Returns the shared instance.
78          */
79         public static PHPHelpPlugin getDefault() {
80                 return plugin;
81         }
82
83         /**
84          * Returns the workspace instance.
85          */
86         public static IWorkspace getWorkspace() {
87                 return ResourcesPlugin.getWorkspace();
88         }
89
90         public static IWorkbenchPage getActivePage() {
91                 return getDefault().internalGetActivePage();
92         }
93
94         private IWorkbenchPage internalGetActivePage() {
95                 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
96                 if (window != null)
97                         return window.getActivePage();
98                 return null;
99         }
100
101         public static IWorkbenchWindow getActiveWorkbenchWindow() {
102                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
103         }
104
105         public static Shell getActiveWorkbenchShell() {
106                 return getActiveWorkbenchWindow().getShell();
107         }
108
109         public static void log(IStatus status) {
110                 getDefault().getLog().log(status);
111         }
112
113         public static void log(int severity, String message) {
114                 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
115                 log(status);
116         }
117
118         public static void log(Throwable e) {
119                 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
120         }
121
122         public static boolean isDebug() {
123                 return getDefault().isDebugging();
124         }
125
126         protected void initializeDefaultPreferences(IPreferenceStore store) {
127                 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
128                 // windows preferences:
129                 String windowsSystem = Platform.getWS();
130
131                 if (windowsSystem.equals(Platform.WS_WIN32)) {
132                         store.setDefault(PHP_CHM_ENABLED, "false");
133                         store.setDefault(PHP_CHM_FILE, "c:\\wampp2\\php\\php_manual_en.chm");
134                         store.setDefault(PHP_CHM_COMMAND, "hh.exe \"mk:@MSITStore:{0}::/en/function.{1}.html\"");
135                 } else {
136                         store.setDefault(PHP_CHM_ENABLED, "false");
137                         store.setDefault(PHP_CHM_FILE, "");
138                         store.setDefault(PHP_CHM_COMMAND, "");
139                 }
140
141         }
142
143         /**
144          * Returns the standard display to be used. The method first checks, if the
145          * thread calling this method has an associated display. If so, this display
146          * is returned. Otherwise the method returns the default display.
147          */
148         public static Display getStandardDisplay() {
149                 Display display = Display.getCurrent();
150                 if (display == null) {
151                         display = Display.getDefault();
152                 }
153                 return display;
154         }
155
156         // public void startup() throws CoreException {
157         // super.startup();
158         // IAdapterManager manager = Platform.getAdapterManager();
159         // manager.registerAdapters(new PHPElementAdapterFactory(),
160         // PHPElement.class);
161         // manager.registerAdapters(new ResourceAdapterFactory(), IResource.class);
162         // // externalTools.startUp();
163         // getStandardDisplay().asyncExec(new Runnable() {
164         // public void run() {
165         // //initialize the variable context manager
166         // VariableContextManager.getDefault();
167         // }
168         // });
169         // }
170
171         // /**
172         // * @see org.eclipse.core.runtime.Plugin#shutdown()
173         // */
174         // public void shutdown() throws CoreException {
175         // // externalTools.shutDown();
176         // ColorManager.getDefault().dispose();
177         // }
178
179         public void start(BundleContext context) throws Exception {
180                 super.start(context);
181
182                 manager = ConfigurationManager.getInstance();
183                 // IAdapterManager manager = Platform.getAdapterManager();
184                 // manager.registerAdapters(new PHPElementAdapterFactory(),
185                 // PHPElement.class);
186                 // manager.registerAdapters(new ResourceAdapterFactory(),
187                 // IResource.class);
188                 // // externalTools.startUp();
189                 // getStandardDisplay().asyncExec(new Runnable() {
190                 // public void run() {
191                 // //initialize the variable context manager
192                 // VariableContextManager.getDefault();
193                 // }
194                 // });
195         }
196
197         public void stop(BundleContext context) throws Exception {
198                 // ColorManager.getDefault().dispose();
199                 super.stop(context);
200         }
201
202         /**
203          * Returns the translated String found with the given key.
204          *
205          * @return java.lang.String
206          * @param key
207          *          java.lang.String
208          */
209         public static String getResource(String key) {
210                 try {
211                         return Platform.getResourceString(getDefault().getBundle(), key);
212                 } catch (Exception e) {
213                         return key;
214                 }
215         }
216
217         /**
218          * Return a list of all the existing configurations.
219          *
220          * @return java.util.List
221          */
222         public static List getConfigurations() {
223                 return manager.getConfigurations();
224         }
225
226         /**
227          * Create a new monitor.
228          *
229          * @return working copy
230          */
231         public static IConfigurationWorkingCopy createConfiguration() {
232                 return manager.createConfiguration();
233         }
234
235         public static ArrayList getTypes() {
236                 return CONFIGURATION_TYPES;
237         }
238
239         public static void addType(String type) {
240                 CONFIGURATION_TYPES.add(type);
241         }
242 }