1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import java.util.ArrayList ; 13 import java.util.Iterator ; 14 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IncrementalProjectBuilder; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.CoreException; 19 import org.eclipse.core.runtime.IProgressMonitor; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Status; 22 import org.eclipse.core.runtime.jobs.Job; 23 import org.eclipse.jdt.core.JavaCore; 24 import org.eclipse.jdt.launching.JavaRuntime; 25 import org.eclipse.jdt.launching.environments.IExecutionEnvironment; 26 import org.eclipse.jface.action.Action; 27 import org.eclipse.jface.action.IMenuManager; 28 import org.eclipse.jface.action.Separator; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.LabelProvider; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.viewers.TableViewer; 33 import org.eclipse.jface.window.Window; 34 import org.eclipse.pde.core.IModelChangedEvent; 35 import org.eclipse.pde.core.plugin.IPluginModelBase; 36 import org.eclipse.pde.core.plugin.PluginRegistry; 37 import org.eclipse.pde.internal.core.ibundle.IBundle; 38 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 39 import org.eclipse.pde.internal.core.ibundle.IManifestHeader; 40 import org.eclipse.pde.internal.core.text.bundle.ExecutionEnvironment; 41 import org.eclipse.pde.internal.core.text.bundle.RequiredExecutionEnvironmentHeader; 42 import org.eclipse.pde.internal.ui.PDEPlugin; 43 import org.eclipse.pde.internal.ui.PDEPluginImages; 44 import org.eclipse.pde.internal.ui.PDEUIMessages; 45 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 46 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 47 import org.eclipse.pde.internal.ui.editor.TableSection; 48 import org.eclipse.pde.internal.ui.editor.context.InputContextManager; 49 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 50 import org.eclipse.pde.internal.ui.parts.EditableTablePart; 51 import org.eclipse.pde.internal.ui.parts.TablePart; 52 import org.eclipse.pde.internal.ui.preferences.PDEPreferencesUtil; 53 import org.eclipse.pde.internal.ui.wizards.plugin.ClasspathComputer; 54 import org.eclipse.swt.SWT; 55 import org.eclipse.swt.graphics.Image; 56 import org.eclipse.swt.layout.GridData; 57 import org.eclipse.swt.widgets.Composite; 58 import org.eclipse.swt.widgets.Table; 59 import org.eclipse.ui.actions.ActionFactory; 60 import org.eclipse.ui.dialogs.ElementListSelectionDialog; 61 import org.eclipse.ui.forms.IFormColors; 62 import org.eclipse.ui.forms.events.HyperlinkEvent; 63 import org.eclipse.ui.forms.events.IHyperlinkListener; 64 import org.eclipse.ui.forms.widgets.FormToolkit; 65 import org.eclipse.ui.forms.widgets.Hyperlink; 66 import org.eclipse.ui.forms.widgets.Section; 67 import org.eclipse.ui.forms.widgets.TableWrapData; 68 import org.osgi.framework.Constants; 69 70 public class ExecutionEnvironmentSection extends TableSection { 71 72 private TableViewer fEETable; 73 private Action fRemoveAction; 74 private Action fAddAction; 75 76 class EELabelProvider extends LabelProvider { 77 78 private Image fImage; 79 80 public EELabelProvider() { 81 fImage = PDEPluginImages.DESC_JAVA_LIB_OBJ.createImage(); 82 } 83 84 public Image getImage(Object element) { 85 return fImage; 86 } 87 88 public String getText(Object element) { 89 if (element instanceof IExecutionEnvironment) 90 return ((IExecutionEnvironment)element).getId(); 91 return super.getText(element); 92 } 93 94 public void dispose() { 95 if (fImage != null) 96 fImage.dispose(); 97 super.dispose(); 98 } 99 } 100 101 class ContentProvider extends DefaultTableProvider { 102 public Object [] getElements(Object inputElement) { 103 if (inputElement instanceof IBundleModel) { 104 IBundleModel model = (IBundleModel)inputElement; 105 IBundle bundle = model.getBundle(); 106 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); 107 if (header instanceof RequiredExecutionEnvironmentHeader) { 108 return ((RequiredExecutionEnvironmentHeader)header).getEnvironments(); 109 } 110 } 111 return new Object [0]; 112 } 113 } 114 115 public ExecutionEnvironmentSection(PDEFormPage page, Composite parent) { 116 super(page, parent, Section.DESCRIPTION, new String [] {PDEUIMessages.RequiredExecutionEnvironmentSection_add, PDEUIMessages.RequiredExecutionEnvironmentSection_remove, PDEUIMessages.RequiredExecutionEnvironmentSection_up, PDEUIMessages.RequiredExecutionEnvironmentSection_down}); 117 createClient(getSection(), page.getEditor().getToolkit()); 118 } 119 120 protected void createClient(Section section, FormToolkit toolkit) { 121 section.setText(PDEUIMessages.RequiredExecutionEnvironmentSection_title); 122 if (isFragment()) 123 section.setDescription(PDEUIMessages.RequiredExecutionEnvironmentSection_fragmentDesc); 124 else 125 section.setDescription(PDEUIMessages.RequiredExecutionEnvironmentSection_pluginDesc); 126 127 section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1)); 128 129 TableWrapData data = new TableWrapData(TableWrapData.FILL_GRAB); 130 section.setLayoutData(data); 131 132 Composite container = createClientContainer(section, 2, toolkit); 133 EditableTablePart tablePart = getTablePart(); 134 tablePart.setEditable(isEditable()); 135 136 createViewerPartControl(container, SWT.FULL_SELECTION|SWT.MULTI, 2, toolkit); 137 fEETable = tablePart.getTableViewer(); 138 fEETable.setContentProvider(new ContentProvider()); 139 fEETable.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 140 141 Hyperlink link = toolkit.createHyperlink(container, PDEUIMessages.BuildExecutionEnvironmentSection_configure, SWT.NONE); 142 link.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 143 link.addHyperlinkListener(new IHyperlinkListener() { 144 public void linkEntered(HyperlinkEvent e) { 145 } 146 public void linkExited(HyperlinkEvent e) { 147 } 148 public void linkActivated(HyperlinkEvent e) { 149 PDEPreferencesUtil.showPreferencePage(new String [] {"org.eclipse.jdt.debug.ui.jreProfiles"}, PDEPlugin.getActiveWorkbenchShell()); } 151 }); 152 GridData gd = new GridData(); 153 gd.horizontalSpan = 2; 154 link.setLayoutData(gd); 155 156 final IProject project = getPage().getPDEEditor().getCommonProject(); 157 try { 158 if (project != null && project.hasNature(JavaCore.NATURE_ID)) { 159 link = toolkit.createHyperlink(container, PDEUIMessages.ExecutionEnvironmentSection_updateClasspath, SWT.NONE); 160 link.setForeground(toolkit.getColors().getColor(IFormColors.TITLE)); 161 link.addHyperlinkListener(new IHyperlinkListener() { 162 public void linkEntered(HyperlinkEvent e) { 163 } 164 public void linkExited(HyperlinkEvent e) { 165 } 166 public void linkActivated(HyperlinkEvent e) { 167 try { 168 getPage().getEditor().doSave(null); 169 IPluginModelBase model = PluginRegistry.findModel(project); 170 if (model != null) { 171 ClasspathComputer.setClasspath(project, model); 172 if (PDEPlugin.getWorkspace().isAutoBuilding()) { 173 doFullBuild(project); 174 } 175 } 176 } catch (CoreException e1) { 177 } 178 } 179 }); 180 gd = new GridData(); 181 gd.horizontalSpan = 2; 182 link.setLayoutData(gd); 183 } 184 } catch (CoreException e1) { 185 } 186 187 makeActions(); 188 189 IBundleModel model = getBundleModel(); 190 if (model != null) { 191 fEETable.setInput(model); 192 model.addModelChangedListener(this); 193 } 194 toolkit.paintBordersFor(container); 195 section.setClient(container); 196 } 197 198 public void dispose() { 199 IBundleModel model = getBundleModel(); 200 if (model != null) 201 model.removeModelChangedListener(this); 202 } 203 204 public void refresh() { 205 fEETable.refresh(); 206 updateButtons(); 207 } 208 209 protected void buttonSelected(int index) { 210 switch (index) { 211 case 0 : 212 handleAdd(); 213 break; 214 case 1: 215 handleRemove(); 216 break; 217 case 2 : 218 handleUp(); 219 break; 220 case 3 : 221 handleDown(); 222 break; 223 } 224 } 225 226 protected void fillContextMenu(IMenuManager manager) { 227 manager.add(fAddAction); 228 if (!fEETable.getSelection().isEmpty()) { 229 manager.add(new Separator()); 230 manager.add(fRemoveAction); 231 } 232 getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); 233 } 234 235 private void makeActions() { 236 fAddAction = new Action(PDEUIMessages.RequiredExecutionEnvironmentSection_add) { 237 public void run() { 238 handleAdd(); 239 } 240 }; 241 fAddAction.setEnabled(isEditable()); 242 243 fRemoveAction = new Action(PDEUIMessages.NewManifestEditor_LibrarySection_remove) { 244 public void run() { 245 handleRemove(); 246 } 247 }; 248 fRemoveAction.setEnabled(isEditable()); 249 } 250 251 private void updateButtons() { 252 Table table = fEETable.getTable(); 253 int count = table.getItemCount(); 254 boolean canMoveUp = count > 0 && table.getSelection().length == 1 && table.getSelectionIndex() > 0; 255 boolean canMoveDown = count > 0 && table.getSelection().length == 1 && table.getSelectionIndex() < count - 1; 256 257 TablePart tablePart = getTablePart(); 258 tablePart.setButtonEnabled(0, isEditable()); 259 tablePart.setButtonEnabled(1, isEditable() && table.getSelection().length > 0); 260 tablePart.setButtonEnabled(2, isEditable() && canMoveUp); 261 tablePart.setButtonEnabled(3, isEditable() && canMoveDown); 262 } 263 264 private void handleDown() { 265 int selection = fEETable.getTable().getSelectionIndex(); 266 swap(selection, selection + 1); 267 } 268 269 private void handleUp() { 270 int selection = fEETable.getTable().getSelectionIndex(); 271 swap(selection, selection - 1); 272 } 273 274 public void swap(int index1, int index2) { 275 RequiredExecutionEnvironmentHeader header = getHeader(); 276 header.swap(index1, index2); 277 } 278 279 private void handleRemove() { 280 IStructuredSelection ssel = (IStructuredSelection)fEETable.getSelection(); 281 if (ssel.size() > 0) { 282 Iterator iter = ssel.iterator(); 283 while (iter.hasNext()) { 284 Object object = iter.next(); 285 if (object instanceof ExecutionEnvironment) { 286 getHeader().removeExecutionEnvironment((ExecutionEnvironment)object); 287 } 288 } 289 } 290 } 291 292 private void handleAdd() { 293 ElementListSelectionDialog dialog = new ElementListSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), new EELabelProvider()); 294 dialog.setElements(getEnvironments()); 295 dialog.setAllowDuplicates(false); 296 dialog.setMultipleSelection(true); 297 dialog.setTitle(PDEUIMessages.RequiredExecutionEnvironmentSection_dialog_title); 298 dialog.setMessage(PDEUIMessages.RequiredExecutionEnvironmentSection_dialogMessage); 299 if (dialog.open() == Window.OK) { 300 addExecutionEnvironments(dialog.getResult()); 301 } 302 } 303 304 private void addExecutionEnvironments(Object [] result) { 305 IManifestHeader header = getHeader(); 306 if (header == null) { 307 StringBuffer buffer = new StringBuffer (); 308 for (int i = 0; i < result.length; i++) { 309 String id = null; 310 if (result[i] instanceof IExecutionEnvironment) 311 id = ((IExecutionEnvironment)result[i]).getId(); 312 else if (result[i] instanceof ExecutionEnvironment) 313 id = ((ExecutionEnvironment)result[i]).getName(); 314 else 315 continue; 316 if (buffer.length() > 0) { 317 buffer.append(","); buffer.append(getLineDelimiter()); 319 buffer.append(" "); } 321 buffer.append(id); 322 } 323 getBundle().setHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT, buffer.toString()); 324 } else { 325 RequiredExecutionEnvironmentHeader ee = (RequiredExecutionEnvironmentHeader)header; 326 ee.addExecutionEnvironments(result); 327 } 328 } 329 330 private String getLineDelimiter() { 331 BundleInputContext inputContext = getBundleContext(); 332 if (inputContext != null) { 333 return inputContext.getLineDelimiter(); 334 } 335 return System.getProperty("line.separator"); } 337 338 private Object [] getEnvironments() { 339 RequiredExecutionEnvironmentHeader header = getHeader(); 340 IExecutionEnvironment[] envs = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments(); 341 if (header == null) 342 return envs; 343 ArrayList list = new ArrayList (); 344 for (int i = 0; i < envs.length; i++) { 345 if (!header.hasExecutionEnvironment(envs[i])) 346 list.add(envs[i]); 347 } 348 return list.toArray(); 349 } 350 351 public void modelChanged(IModelChangedEvent e) { 352 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 353 markStale(); 354 } else if (e.getChangeType() == IModelChangedEvent.REMOVE) { 355 Object [] objects = e.getChangedObjects(); 356 for (int i = 0; i < objects.length; i++) { 357 Table table = fEETable.getTable(); 358 if (objects[i] instanceof ExecutionEnvironment) { 359 int index = table.getSelectionIndex(); 360 fEETable.remove(objects[i]); 361 table.setSelection(index < table.getItemCount() ? index : table.getItemCount() -1); 362 } 363 } 364 updateButtons(); 365 } else if (e.getChangeType() == IModelChangedEvent.INSERT) { 366 Object [] objects = e.getChangedObjects(); 367 for (int i = 0; i < objects.length; i++) { 368 if (objects[i] instanceof ExecutionEnvironment) { 369 fEETable.add(objects[i]); 370 } 371 } 372 if (objects.length > 0) 373 fEETable.setSelection(new StructuredSelection(objects[objects.length - 1])); 374 updateButtons(); 375 } else if (Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT.equals(e.getChangedProperty())){ 376 refresh(); 377 } 378 } 379 380 private BundleInputContext getBundleContext() { 381 InputContextManager manager = getPage().getPDEEditor().getContextManager(); 382 return (BundleInputContext) manager.findContext(BundleInputContext.CONTEXT_ID); 383 } 384 385 private IBundle getBundle() { 386 IBundleModel model = getBundleModel(); 387 return model == null ? null : model.getBundle(); 388 } 389 390 private IBundleModel getBundleModel() { 391 BundleInputContext context = getBundleContext(); 392 return context == null ? null : (IBundleModel)context.getModel(); 393 } 394 395 protected RequiredExecutionEnvironmentHeader getHeader() { 396 IBundle bundle = getBundle(); 397 if (bundle == null) return null; 398 IManifestHeader header = bundle.getManifestHeader(Constants.BUNDLE_REQUIREDEXECUTIONENVIRONMENT); 399 if (header instanceof RequiredExecutionEnvironmentHeader) 400 return (RequiredExecutionEnvironmentHeader) header; 401 return null; 402 } 403 404 protected boolean isFragment() { 405 InputContextManager manager = getPage().getPDEEditor().getContextManager(); 406 IPluginModelBase model = (IPluginModelBase) manager.getAggregateModel(); 407 return model.isFragmentModel(); 408 } 409 410 public boolean doGlobalAction(String actionId) { 411 if (!isEditable()) { return false; } 412 413 if (actionId.equals(ActionFactory.DELETE.getId())) { 414 handleRemove(); 415 return true; 416 } 417 418 if (actionId.equals(ActionFactory.CUT.getId())) { 419 handleRemove(); 422 return false; 423 } 424 425 if (actionId.equals(ActionFactory.PASTE.getId())) { 426 doPaste(); 427 return true; 428 } 429 430 return false; 431 } 432 433 protected boolean canPaste(Object target, Object [] objects) { 434 RequiredExecutionEnvironmentHeader header = getHeader(); 435 for (int i = 0; i < objects.length; i++) { 436 if (objects[i] instanceof ExecutionEnvironment) { 437 String env = ((ExecutionEnvironment)objects[i]).getName(); 438 if (header == null || !header.hasElement(env)) 439 return true; 440 } 441 } 442 return false; 443 } 444 445 protected void selectionChanged(IStructuredSelection selection) { 446 getPage().getPDEEditor().setSelection(selection); 447 if (getPage().getModel().isEditable()) 448 updateButtons(); 449 } 450 451 protected void doPaste(Object target, Object [] objects) { 452 addExecutionEnvironments(objects); 453 } 454 455 private void doFullBuild(final IProject project) { 456 Job buildJob = new Job(PDEUIMessages.CompilersConfigurationBlock_building) { 457 public boolean belongsTo(Object family) { 458 return ResourcesPlugin.FAMILY_MANUAL_BUILD == family; 459 } 460 protected IStatus run(IProgressMonitor monitor) { 461 try { 462 project.build(IncrementalProjectBuilder.FULL_BUILD, 463 JavaCore.BUILDER_ID, 464 null, 465 monitor); 466 } catch (CoreException e) { 467 } 468 return Status.OK_STATUS; 469 } 470 }; 471 buildJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().buildRule()); 472 buildJob.schedule(); 473 } 474 475 } 476 | Popular Tags |