1 11 package org.eclipse.pde.internal.ui.editor; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Comparator ; 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 id, String title) { 40 super(editor, id, title); 41 } 42 43 protected final Section createStaticSection(FormToolkit toolkit, Composite parent, String 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 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 69 public void linkActivated(HyperlinkEvent e) { 70 String href = (String ) e.getHref(); 71 if (href.startsWith("launchShortcut.")) { href = href.substring(15); 73 int index = href.indexOf('.'); 74 if (index < 0) 75 return; String mode = href.substring(0, index); 77 String id = href.substring(index + 1); 78 getEditor().doSave(null); 79 IExtensionRegistry registry = Platform.getExtensionRegistry(); 80 IConfigurationElement[] elements = registry.getConfigurationElementsFor("org.eclipse.debug.ui.launchShortcuts"); for (int i = 0; i < elements.length; i++) { 82 if (id.equals(elements[i].getAttribute("id"))) try { 84 ILaunchShortcut shortcut = (ILaunchShortcut)elements[i].createExecutableExtension("class"); preLaunch(); 86 shortcut.launch(new StructuredSelection(getLaunchObject()), mode); 87 } catch (CoreException e1) { 88 } 89 } 90 } 91 } 92 93 protected abstract Object getLaunchObject(); 95 96 protected void preLaunch() { 98 } 99 100 protected abstract short getIndent(); 102 103 108 public void linkEntered(HyperlinkEvent e) { 109 IStatusLineManager mng = getEditor().getEditorSite().getActionBars() 110 .getStatusLineManager(); 111 mng.setMessage(e.getLabel()); 112 } 113 114 119 public void linkExited(HyperlinkEvent e) { 120 IStatusLineManager mng = getEditor().getEditorSite().getActionBars() 121 .getStatusLineManager(); 122 mng.setMessage(null); 123 } 124 125 protected final String getLauncherText(boolean osgi, String message) { 126 IConfigurationElement[] elements = getLaunchers(osgi); 127 128 StringBuffer buffer = new StringBuffer (); 129 String indent = Short.toString(getIndent()); 130 131 for (int i = 0; i < elements.length; i++) { 132 String mode = elements[i].getAttribute("mode"); buffer.append("<li style=\"image\" value=\""); buffer.append(mode); 135 buffer.append("\" bindent=\"" + indent + "\"><a HREF=\"launchShortcut."); buffer.append(mode); 137 buffer.append('.'); 138 buffer.append(elements[i].getAttribute("id")); buffer.append("\">"); buffer.append(elements[i].getAttribute("label")); buffer.append("</a></li>"); } 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"); ArrayList list = new ArrayList (); 151 for (int i = 0; i < elements.length; i++) { 152 String mode = elements[i].getAttribute("mode"); 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 && osgi == "true".equals(elements[i].getAttribute("osgi"))) list.add(elements[i]); 157 } 158 159 elements = (IConfigurationElement[])list.toArray(new IConfigurationElement[list.size()]); 161 Arrays.sort(elements, new Comparator () { 162 163 public int compare(Object arg0, Object arg1) { 164 int mode1 = getModeValue(((IConfigurationElement)arg0).getAttribute("mode")); int mode2 = getModeValue(((IConfigurationElement)arg1).getAttribute("mode")); if (mode1 != mode2) 167 return mode1 - mode2; 168 String label1 = ((IConfigurationElement)arg0).getAttribute("label"); String label2 = ((IConfigurationElement)arg1).getAttribute("label"); return label1.compareTo(label2); 171 } 172 173 private int getModeValue(String 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; } 180 181 }); 182 return elements; 183 } 184 185 } 186 | Popular Tags |