KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > LaunchShortcutOverviewPage


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Comparator JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtensionRegistry;
20 import org.eclipse.core.runtime.Platform;
21 import org.eclipse.debug.core.ILaunchManager;
22 import org.eclipse.debug.ui.ILaunchShortcut;
23 import org.eclipse.jface.action.IStatusLineManager;
24 import org.eclipse.jface.viewers.StructuredSelection;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.swt.SWTException;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.ui.forms.editor.FormEditor;
29 import org.eclipse.ui.forms.events.HyperlinkEvent;
30 import org.eclipse.ui.forms.events.IHyperlinkListener;
31 import org.eclipse.ui.forms.widgets.ExpandableComposite;
32 import org.eclipse.ui.forms.widgets.FormText;
33 import org.eclipse.ui.forms.widgets.FormToolkit;
34 import org.eclipse.ui.forms.widgets.Section;
35 import org.eclipse.ui.forms.widgets.TableWrapData;
36
37 public abstract class LaunchShortcutOverviewPage extends PDEFormPage implements IHyperlinkListener {
38
39     public LaunchShortcutOverviewPage(FormEditor editor, String JavaDoc id, String JavaDoc title) {
40         super(editor, id, title);
41     }
42     
43     protected final Section createStaticSection(FormToolkit toolkit, Composite parent, String JavaDoc text) {
44         Section section = toolkit.createSection(parent, ExpandableComposite.TITLE_BAR);
45         section.clientVerticalSpacing = FormLayoutFactory.SECTION_HEADER_VERTICAL_SPACING;
46         section.setText(text);
47         section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1));
48         TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB);
49         section.setLayoutData(data);
50         return section;
51     }
52     
53     protected final FormText createClient(Composite section, String JavaDoc content, FormToolkit toolkit) {
54         FormText text = toolkit.createFormText(section, true);
55         try {
56             text.setText(content, true, false);
57         } catch (SWTException e) {
58             text.setText(e.getMessage(), false, false);
59         }
60         text.addHyperlinkListener(this);
61         return text;
62     }
63     
64     /*
65      * (non-Javadoc)
66      *
67      * @see org.eclipse.ui.forms.events.HyperlinkListener#linkActivated(org.eclipse.ui.forms.events.HyperlinkEvent)
68      */

69     public void linkActivated(HyperlinkEvent e) {
70         String JavaDoc href = (String JavaDoc) e.getHref();
71         if (href.startsWith("launchShortcut.")) { //$NON-NLS-1$
72
href = href.substring(15);
73             int index = href.indexOf('.');
74             if (index < 0)
75                 return; // error. Format of href should be launchShortcut.<mode>.<launchShortcutId>
76
String JavaDoc mode = href.substring(0, index);
77             String JavaDoc id = href.substring(index + 1);
78             getEditor().doSave(null);
79             IExtensionRegistry registry = Platform.getExtensionRegistry();
80             IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.debug.ui.launchShortcuts"); //$NON-NLS-1$
81
for (int i = 0; i < elements.length; i++) {
82                 if (id.equals(elements[i].getAttribute("id"))) //$NON-NLS-1$
83
try {
84                         ILaunchShortcut shortcut = (ILaunchShortcut)elements[i].createExecutableExtension("class"); //$NON-NLS-1$
85
preLaunch();
86                         shortcut.launch(new StructuredSelection(getLaunchObject()), mode);
87                     } catch (CoreException e1) {
88                     }
89             }
90         }
91     }
92     
93     // returns the object which will be passed to the launch shortcut in a StructuredSelection
94
protected abstract Object JavaDoc getLaunchObject();
95
96     // allows subclasses to do setup before launching the shortcut
97
protected void preLaunch() {
98     }
99     
100     // returns the indent for each launcher
101
protected abstract short getIndent();
102     
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.eclipse.ui.forms.events.HyperlinkListener#linkEntered(org.eclipse.ui.forms.events.HyperlinkEvent)
107      */

108     public void linkEntered(HyperlinkEvent e) {
109         IStatusLineManager mng = getEditor().getEditorSite().getActionBars()
110                 .getStatusLineManager();
111         mng.setMessage(e.getLabel());
112     }
113     
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.eclipse.ui.forms.events.HyperlinkListener#linkExited(org.eclipse.ui.forms.events.HyperlinkEvent)
118      */

119     public void linkExited(HyperlinkEvent e) {
120         IStatusLineManager mng = getEditor().getEditorSite().getActionBars()
121                 .getStatusLineManager();
122         mng.setMessage(null);
123     }
124     
125     protected final String JavaDoc getLauncherText(boolean osgi, String JavaDoc message) {
126         IConfigurationElement[] elements = getLaunchers(osgi);
127         
128         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
129         String JavaDoc indent = Short.toString(getIndent());
130         
131         for (int i = 0; i < elements.length; i++) {
132             String JavaDoc mode = elements[i].getAttribute("mode"); //$NON-NLS-1$
133
buffer.append("<li style=\"image\" value=\""); //$NON-NLS-1$
134
buffer.append(mode);
135             buffer.append("\" bindent=\"" + indent + "\"><a HREF=\"launchShortcut."); //$NON-NLS-1$ //$NON-NLS-2$
136
buffer.append(mode);
137             buffer.append('.');
138             buffer.append(elements[i].getAttribute("id")); //$NON-NLS-1$
139
buffer.append("\">"); //$NON-NLS-1$
140
buffer.append(elements[i].getAttribute("label")); //$NON-NLS-1$
141
buffer.append("</a></li>"); //$NON-NLS-1$
142
}
143         return NLS.bind(message, buffer.toString());
144     }
145     
146     private IConfigurationElement[] getLaunchers(boolean osgi) {
147         IExtensionRegistry registry = Platform.getExtensionRegistry();
148         IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.pde.ui.launchShortcuts"); //$NON-NLS-1$
149
// validate elements
150
ArrayList JavaDoc list = new ArrayList JavaDoc();
151         for (int i = 0; i < elements.length; i++) {
152             String JavaDoc mode = elements[i].getAttribute("mode"); //$NON-NLS-1$
153
if (mode != null && (mode.equals(ILaunchManager.RUN_MODE) || mode.equals(ILaunchManager.DEBUG_MODE) || mode.equals(ILaunchManager.PROFILE_MODE))
154                     && elements[i].getAttribute("label") != null && elements[i].getAttribute("id") != null && //$NON-NLS-1$ //$NON-NLS-2$
155
osgi == "true".equals(elements[i].getAttribute("osgi"))) //$NON-NLS-1$ //$NON-NLS-2$
156
list.add(elements[i]);
157         }
158         
159         // sort elements based on criteria specified in bug 172703
160
elements = (IConfigurationElement[])list.toArray(new IConfigurationElement[list.size()]);
161         Arrays.sort(elements, new Comparator JavaDoc() {
162
163             public int compare(Object JavaDoc arg0, Object JavaDoc arg1) {
164                 int mode1 = getModeValue(((IConfigurationElement)arg0).getAttribute("mode")); //$NON-NLS-1$
165
int mode2 = getModeValue(((IConfigurationElement)arg1).getAttribute("mode")); //$NON-NLS-1$
166
if (mode1 != mode2)
167                     return mode1 - mode2;
168                 String JavaDoc label1 = ((IConfigurationElement)arg0).getAttribute("label"); //$NON-NLS-1$
169
String JavaDoc label2 = ((IConfigurationElement)arg1).getAttribute("label"); //$NON-NLS-1$
170
return label1.compareTo(label2);
171             }
172             
173             private int getModeValue(String JavaDoc value) {
174                 if (value.equals(ILaunchManager.RUN_MODE))
175                     return 0;
176                 else if (value.equals(ILaunchManager.DEBUG_MODE))
177                     return 1;
178                 return 2; // has to be ILaunchManager.PROFILE_MODE
179
}
180             
181         });
182         return elements;
183     }
184
185 }
186
Popular Tags