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