1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import java.util.*; 13 14 import org.eclipse.core.resources.*; 15 import org.eclipse.core.runtime.*; 16 import org.eclipse.jdt.core.*; 17 import org.eclipse.jdt.ui.*; 18 import org.eclipse.jface.action.*; 19 import org.eclipse.jface.viewers.*; 20 import org.eclipse.osgi.util.*; 21 import org.eclipse.pde.core.*; 22 import org.eclipse.pde.core.plugin.*; 23 import org.eclipse.pde.internal.core.*; 24 import org.eclipse.pde.internal.core.ibundle.*; 25 import org.eclipse.pde.internal.ui.*; 26 import org.eclipse.pde.internal.ui.editor.*; 27 import org.eclipse.pde.internal.ui.editor.context.*; 28 import org.eclipse.pde.internal.ui.elements.*; 29 import org.eclipse.swt.*; 30 import org.eclipse.swt.events.*; 31 import org.eclipse.swt.graphics.*; 32 import org.eclipse.swt.layout.*; 33 import org.eclipse.swt.widgets.*; 34 import org.eclipse.ui.actions.*; 35 import org.eclipse.ui.forms.events.*; 36 import org.eclipse.ui.forms.widgets.*; 37 import org.eclipse.ui.part.PageBook; 38 import org.osgi.framework.*; 39 40 public class PluginActivationSection extends TableSection 41 implements 42 IModelChangedListener, IInputContextListener { 43 private PageBook topBook; 44 private Composite topContainer; 45 private Composite blankContainer; 46 private TableViewer fExceptionsTableViewer; 47 private Button fDoActivateButton; 48 private Button fDoNotActivateButton; 49 private Font fBoldFont; 50 private static final String ECLIPSE_AUTOSTART = PDEPlugin.getResourceString("PluginActivationSection.autostart"); 52 class TableContentProvider extends DefaultContentProvider 53 implements 54 IStructuredContentProvider { 55 public Object [] getElements(Object parent) { 56 return getExceptions(); 57 } 58 } 59 class TableLabelProvider extends LabelProvider 60 implements 61 ITableLabelProvider { 62 public String getColumnText(Object obj, int index) { 63 return obj.toString(); 64 } 65 public Image getColumnImage(Object obj, int index) { 66 return JavaUI.getSharedImages().getImage( 67 ISharedImages.IMG_OBJS_PACKAGE); 68 } 69 } 70 public PluginActivationSection(PDEFormPage page, Composite parent) { 71 super( 72 page, 73 parent, 74 Section.DESCRIPTION, 75 new String []{ 76 PDEPlugin 77 .getResourceString("ManifestEditor.OSGiSection.add"), PDEPlugin 79 .getResourceString("ManifestEditor.OSGiSection.remove")}); getSection().setText(PDEPlugin.getResourceString("PluginActivationSection.title")); getSection() 82 .setDescription( 83 PDEPlugin.getResourceString("PluginActivationSection.desc")); } 85 private void update() { 86 fDoActivateButton.setEnabled(isEditable()); 87 fDoActivateButton.setSelection(isAutoStart()); 88 fDoNotActivateButton.setSelection(!isAutoStart()); 89 fDoNotActivateButton.setEnabled(isEditable()); 90 enableButtons(); 91 } 92 93 private boolean isAutoStart() { 94 ManifestElement element = getManifestElement(); 95 return (element == null) ? true : !"false".equals(element.getValue()); } 97 98 private String [] getExceptions() { 99 ManifestElement element = getManifestElement(); 100 if (element == null) 101 return new String [0]; 102 103 String exceptions = element.getAttribute("exceptions"); if (exceptions == null) 105 return new String [0]; 106 107 ArrayList tokens = new ArrayList(); 108 StringTokenizer tok = new StringTokenizer(exceptions, ","); while (tok.hasMoreTokens()) 110 tokens.add(tok.nextToken().trim()); 111 return (String []) tokens.toArray(new String [tokens.size()]); 112 } 113 114 private ManifestElement getManifestElement() { 115 IBundleModel model = getBundleModel(); 116 if (model != null) { 117 String value = model.getBundle().getHeader(ECLIPSE_AUTOSTART); 118 if (value != null) { 119 try { 120 ManifestElement[] elements = ManifestElement.parseHeader( 121 ECLIPSE_AUTOSTART, value); 122 if (elements != null && elements.length > 0) 123 return elements[0]; 124 } catch (BundleException e) { 125 } 126 } 127 } 128 return null; 129 } 130 135 public boolean isEditable() { 136 return getPage().getModel().isEditable() 137 && getBundleModel() != null; 138 } 139 144 public void dispose() { 145 InputContextManager contextManager = getPage().getPDEEditor().getContextManager(); 146 if (contextManager!=null) 147 contextManager.removeInputContextListener(this); 148 IBundleModel model = getBundleModel(); 149 if (model != null) 150 model.removeModelChangedListener(this); 151 fBoldFont.dispose(); 152 super.dispose(); 153 } 154 155 public boolean doGlobalAction(String actionId) { 156 if (actionId.equals(ActionFactory.DELETE.getId())) { 157 handleRemove(); 158 return true; 159 } 160 return false; 161 } 162 163 protected void fillContextMenu(IMenuManager manager) { 164 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 165 manager); 166 } 167 168 173 public void modelChanged(IModelChangedEvent event) { 174 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 175 markStale(); 176 return; 177 } 178 if (event.getChangedProperty().equals(ECLIPSE_AUTOSTART)) { 179 refresh(); 180 } 181 } 182 183 188 public void refresh() { 189 fExceptionsTableViewer.refresh(); 190 fDoActivateButton.setSelection(isAutoStart()); 191 fDoNotActivateButton.setSelection(!isAutoStart()); 192 super.refresh(); 193 } 194 195 private void initializeFonts() { 196 FontData[] fontData = getSection().getFont().getFontData(); 197 FontData data; 198 if (fontData.length > 0) 199 data = fontData[0]; 200 else 201 data = new FontData(); 202 data.setStyle(SWT.BOLD); 203 fBoldFont = new Font(getSection().getDisplay(), fontData); 204 } 205 211 protected void createClient(Section section, FormToolkit toolkit) { 212 initializeFonts(); 213 214 Composite mainContainer = toolkit.createComposite(section); 215 GridLayout layout = new GridLayout(); 216 layout.marginHeight = layout.marginWidth = 0; 217 layout.verticalSpacing = 5; 218 mainContainer.setLayout(layout); 219 mainContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 220 221 224 if (getPage().getPDEEditor().getAggregateModel().isEditable()) { 225 topBook = new PageBook(mainContainer, SWT.NULL); 226 blankContainer = toolkit.createComposite(topBook); 229 blankContainer.setLayout(new GridLayout()); 230 Label label = toolkit.createLabel(blankContainer, null); 231 GridData gd = new GridData(); 232 gd.heightHint = 1; 233 gd.widthHint = 1; 234 label.setLayoutData(gd); 235 236 topContainer = toolkit.createComposite(topBook); 237 layout = new GridLayout(); 238 layout.marginHeight = layout.marginWidth = 2; 239 layout.numColumns = 2; 240 layout.makeColumnsEqualWidth = false; 241 topContainer.setLayout(layout); 242 toolkit 243 .createLabel( 244 topContainer, 245 PDEPlugin.getResourceString("PluginActivationSection.manifestRequired")); Hyperlink manifestLink = toolkit.createHyperlink(topContainer, 247 PDEPlugin.getResourceString("PluginActivationSection.createManifest"), SWT.NULL); manifestLink.addHyperlinkListener(new IHyperlinkListener() { 249 public void linkActivated(HyperlinkEvent e) { 250 try { 251 getPage().getEditor().doSave(null); 252 IPluginModelBase model = (IPluginModelBase) getPage() 253 .getPDEEditor().getAggregateModel(); 254 PDEPluginConverter.convertToOSGIFormat(model 255 .getUnderlyingResource().getProject(), model 256 .isFragmentModel() 257 ? "fragment.xml" : "plugin.xml", new NullProgressMonitor()); } catch (CoreException e1) { 260 } 261 } 262 public void linkExited(HyperlinkEvent e) { 263 } 264 public void linkEntered(HyperlinkEvent e) { 265 } 266 }); 267 bundleModeChanged(getContextId().equals(BundleInputContext.CONTEXT_ID)); 268 } 269 270 273 Composite bottomContainer = toolkit.createComposite(mainContainer); 274 layout = new GridLayout(); 275 layout.makeColumnsEqualWidth = true; 276 layout.marginHeight = layout.marginWidth = 0; 277 layout.numColumns = 2; 278 bottomContainer.setLayout(layout); 279 bottomContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 280 283 Composite ruleContainer = toolkit.createComposite(bottomContainer); 284 layout = new GridLayout(); 285 layout.marginHeight = layout.marginWidth = 2; 286 ruleContainer.setLayout(layout); 287 ruleContainer.setLayoutData(new GridData( 288 GridData.VERTICAL_ALIGN_BEGINNING)); 289 290 Label activateLabel = toolkit.createLabel(ruleContainer, 291 PDEPlugin.getResourceString("PluginActivationSection.rule")); activateLabel.setFont(fBoldFont); 293 294 fDoActivateButton = toolkit.createButton(ruleContainer, 295 PDEPlugin.getResourceString("PluginActivationSection.activate"), SWT.RADIO); 297 GridData gd = new GridData(); 298 gd.horizontalIndent = 5; 299 fDoActivateButton.setLayoutData(gd); 300 fDoActivateButton.addSelectionListener(new SelectionAdapter() { 301 public void widgetSelected(SelectionEvent e) { 302 writeHeader(); 303 } 304 }); 305 309 fDoNotActivateButton = toolkit.createButton(ruleContainer, 310 PDEPlugin.getResourceString("PluginActivationSection.noActivate"), SWT.RADIO); gd = new GridData(); 312 gd.horizontalIndent = 5; 313 fDoNotActivateButton.setLayoutData(gd); 314 fDoNotActivateButton.addSelectionListener(new SelectionAdapter() { 315 public void widgetSelected(SelectionEvent e) { 316 writeHeader(); 317 } 318 }); 319 322 Composite exceptionsContainer = toolkit 323 .createComposite(bottomContainer); 324 layout = new GridLayout(); 325 layout.marginWidth = layout.marginHeight = 2; 326 layout.verticalSpacing = 3; 327 exceptionsContainer.setLayout(layout); 328 exceptionsContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 329 330 Label exceptionLabel = toolkit.createLabel(exceptionsContainer, 331 PDEPlugin.getResourceString("PluginActivationSection.exception.title")); exceptionLabel.setFont(fBoldFont); 333 exceptionLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 334 Label label = toolkit 335 .createLabel( 336 exceptionsContainer, 337 PDEPlugin.getResourceString("PluginActivationSection.exception.desc"), SWT.WRAP); 339 gd = new GridData(GridData.FILL_HORIZONTAL); 340 gd.widthHint = 225; 341 label.setLayoutData(gd); 342 343 Composite exceptionsPkgContainer = toolkit 344 .createComposite(exceptionsContainer); 345 layout = new GridLayout(); 346 layout.marginWidth = layout.marginHeight = 0; 347 layout.numColumns = 2; 348 exceptionsPkgContainer.setLayout(layout); 349 exceptionsPkgContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 350 351 createViewerPartControl(exceptionsPkgContainer, SWT.FULL_SELECTION, 2, 352 toolkit); 353 fExceptionsTableViewer = getTablePart().getTableViewer(); 354 fExceptionsTableViewer.setContentProvider(new TableContentProvider()); 355 fExceptionsTableViewer.setLabelProvider(new TableLabelProvider()); 356 fExceptionsTableViewer.setInput(getBundleModel()); 357 358 toolkit.paintBordersFor(exceptionsContainer); 359 section.setClient(mainContainer); 360 IBundleModel model = getBundleModel(); 361 if (model != null) 362 model.addModelChangedListener(this); 363 InputContextManager contextManager = getPage().getPDEEditor().getContextManager(); 364 if (contextManager!=null) 365 contextManager.addInputContextListener(this); 366 update(); 367 } 368 protected void enableButtons() { 369 getTablePart().setButtonEnabled(0, isEditable()); 370 getTablePart().setButtonEnabled(1, false); 371 } 372 protected void buttonSelected(int index) { 373 if (index == 0) 374 handleAdd(); 375 else if (index == 1) 376 handleRemove(); 377 } 378 private void handleAdd() { 379 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 380 IProject project = model.getUnderlyingResource().getProject(); 381 try { 382 if (project.hasNature(JavaCore.NATURE_ID)) { 383 TableItem[] existingPackages = fExceptionsTableViewer 384 .getTable().getItems(); 385 Vector existing = new Vector(); 386 for (int i = 0; i < existingPackages.length; i++) { 387 existing.add(existingPackages[i].getText()); 388 } 389 ILabelProvider labelProvider = new JavaElementLabelProvider(); 390 PackageSelectionDialog dialog = new PackageSelectionDialog( 391 fExceptionsTableViewer.getTable().getShell(), 392 labelProvider, JavaCore.create(project), existing); 393 if (dialog.open() == PackageSelectionDialog.OK) { 394 Object [] elements = dialog.getResult(); 395 for (int i = 0; i < elements.length; i++) { 396 fExceptionsTableViewer 397 .add(((IPackageFragment) elements[i]) 398 .getElementName()); 399 } 400 writeHeader(); 401 } 402 labelProvider.dispose(); 403 } 404 } catch (CoreException e) { 405 } 406 } 407 408 private void writeHeader() { 409 StringBuffer buffer = new StringBuffer (); 410 buffer.append(fDoActivateButton.getSelection() ? "true" : "false"); TableItem[] items = fExceptionsTableViewer.getTable().getItems(); 412 if (items.length > 0) 413 buffer.append(";exceptions=\""); for (int i = 0; i < items.length; i++) { 415 if (i > 0) 416 buffer.append(" "); buffer.append(items[i].getData().toString()); 418 if (i < items.length - 1) 419 buffer.append("," + System.getProperty("line.separator")); } 421 if (items.length > 0) 422 buffer.append("\""); getBundleModel().getBundle().setHeader(ECLIPSE_AUTOSTART, 424 buffer.toString()); 425 } 426 427 private void handleRemove() { 428 IStructuredSelection ssel = (IStructuredSelection) fExceptionsTableViewer 429 .getSelection(); 430 Object [] items = ssel.toArray(); 431 for (int i = 0; i < items.length; i++) { 432 fExceptionsTableViewer.remove(items[i]); 433 } 434 writeHeader(); 435 } 436 441 protected void selectionChanged(IStructuredSelection selection) { 442 getTablePart().setButtonEnabled(1, 443 selection != null && !selection.isEmpty()); 444 } 445 446 public String getContextId() { 447 if (getPluginBase() instanceof IBundlePluginBase) 448 return BundleInputContext.CONTEXT_ID; 449 return PluginInputContext.CONTEXT_ID; 450 } 451 private IPluginBase getPluginBase() { 452 IBaseModel model = getPage().getPDEEditor().getAggregateModel(); 453 return ((IPluginModelBase) model).getPluginBase(); 454 } 455 456 private IBundleModel getBundleModel() { 457 InputContext context = getPage().getPDEEditor().getContextManager() 458 .findContext(BundleInputContext.CONTEXT_ID); 459 return context != null ? (IBundleModel) context.getModel() : null; 460 } 461 462 465 public void contextAdded(InputContext context) { 466 if (!context.getId().equals(BundleInputContext.CONTEXT_ID)) 467 return; 468 bundleModeChanged(true); 471 } 472 475 public void contextRemoved(InputContext context) { 476 if (!context.getId().equals(BundleInputContext.CONTEXT_ID)) 477 return; 478 bundleModeChanged(false); 481 } 482 private void bundleModeChanged(boolean added) { 483 if (added && getPage().getModel().isEditable()) { 484 topBook.showPage(blankContainer); 485 } 486 else { 487 topBook.showPage(topContainer); 488 } 489 if (fDoActivateButton!=null) { 490 update(); 491 topBook.getParent().layout(); 492 getManagedForm().reflow(true); 493 fExceptionsTableViewer.setInput(getBundleModel()); 494 } 495 } 496 499 public void monitoredFileAdded(IFile monitoredFile) { 500 } 501 504 public boolean monitoredFileRemoved(IFile monitoredFile) { 505 return false; 506 } 507 } 508 | Popular Tags |