1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import java.util.*; 13 import java.util.Vector ; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.jdt.core.*; 18 import org.eclipse.jdt.ui.*; 19 import org.eclipse.jface.action.IMenuManager; 20 import org.eclipse.jface.viewers.*; 21 import org.eclipse.osgi.util.ManifestElement; 22 import org.eclipse.pde.core.*; 23 import org.eclipse.pde.core.plugin.*; 24 import org.eclipse.pde.internal.core.ibundle.*; 25 import org.eclipse.pde.internal.ui.PDEPlugin; 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.DefaultContentProvider; 29 import org.eclipse.pde.internal.ui.parts.EditableTablePart; 30 import org.eclipse.swt.SWT; 31 import org.eclipse.swt.events.*; 32 import org.eclipse.swt.graphics.Image; 33 import org.eclipse.swt.layout.*; 34 import org.eclipse.swt.widgets.*; 35 import org.eclipse.ui.actions.ActionFactory; 36 import org.eclipse.ui.forms.*; 37 import org.eclipse.ui.forms.widgets.*; 38 import org.osgi.framework.*; 39 42 public class ExportSection extends TableSection 43 implements 44 IPartSelectionListener, IInputContextListener { 45 public static final String SECTION_TITLE = "ManifestEditor.ExportSection.title"; public static final String SECTION_DESC = "ManifestEditor.ExportSection.desc"; public static final String KEY_NO_EXPORT = "ManifestEditor.ExportSection.noExport"; public static final String KEY_NEW_FILTER = "ManifestEditor.ExportSection.newFilter"; public static final String KEY_FULL_EXPORT = "ManifestEditor.ExportSection.fullExport"; public static final String KEY_SELECTED_EXPORT = "ManifestEditor.ExportSection.selectedExport"; public static final String KEY_ADD = "ManifestEditor.ExportSection.add"; public static final String KEY_REMOVE = "ManifestEditor.ExportSection.remove"; public static final String SECTION_ADD_TITLE = "ManifestEditor.ExportSection.addTitle"; private Button fFullExportButton; 55 private Button fSelectedExportButton; 56 private IPluginLibrary fCurrentLibrary; 57 private Composite fPackageExportContainer; 58 private TableViewer fPackageExportViewer; 59 60 class TableContentProvider extends DefaultContentProvider 61 implements 62 IStructuredContentProvider { 63 public Object [] getElements(Object parent) { 64 if (parent instanceof IModel) { 65 return getProvidedPackages(); 66 } 67 else if (parent instanceof IPluginLibrary) { 68 String [] filters = ((IPluginLibrary) parent) 69 .getContentFilters(); 70 return filters == null ? new Object [0] : filters; 71 } 72 return new Object [0]; 73 } 74 } 75 class TableLabelProvider extends LabelProvider 76 implements 77 ITableLabelProvider { 78 public String getColumnText(Object obj, int index) { 79 return obj.toString(); 80 } 81 public Image getColumnImage(Object obj, int index) { 82 return JavaUI.getSharedImages().getImage( 83 ISharedImages.IMG_OBJS_PACKAGE); 84 } 85 } 86 public ExportSection(PDEFormPage formPage, Composite parent) { 87 super(formPage, parent, Section.DESCRIPTION, new String []{ 88 PDEPlugin.getResourceString(KEY_ADD), 89 PDEPlugin.getResourceString(KEY_REMOVE)}); 90 getSection().setText(PDEPlugin.getResourceString(SECTION_TITLE)); 91 getSection().setDescription(PDEPlugin.getResourceString(SECTION_DESC)); 92 handleDefaultButton = false; 93 } 94 public void createClient(Section section, FormToolkit toolkit) { 95 Composite container = toolkit.createComposite(section); 96 container.setLayout(new GridLayout()); 97 String label = PDEPlugin.getResourceString(KEY_FULL_EXPORT); 98 fFullExportButton = toolkit.createButton(container, label, SWT.RADIO); 99 fFullExportButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 100 fFullExportButton.addSelectionListener(new SelectionAdapter() { 101 public void widgetSelected(SelectionEvent e) { 102 try { 103 if (fCurrentLibrary != null) 104 fCurrentLibrary.setExported(fFullExportButton 105 .getSelection()); 106 getTablePart().setButtonEnabled(0, 107 !fFullExportButton.getSelection()); 108 getTablePart().setButtonEnabled(1, false); 109 } catch (CoreException e1) { 110 } 111 } 112 }); 113 label = PDEPlugin.getResourceString(KEY_SELECTED_EXPORT); 114 fSelectedExportButton = toolkit.createButton(container, label, 115 SWT.RADIO); 116 fSelectedExportButton.setLayoutData(new GridData( 117 GridData.FILL_HORIZONTAL)); 118 fPackageExportContainer = toolkit.createComposite(container); 119 fPackageExportContainer.setLayoutData(new GridData(GridData.FILL_BOTH)); 120 GridLayout layout = new GridLayout(); 121 layout.marginWidth = 2; 122 layout.marginHeight = 2; 123 layout.numColumns = 2; 124 fPackageExportContainer.setLayout(layout); 125 createNameTable(fPackageExportContainer, toolkit); 126 update(null, isBundleMode()); 127 initialize(); 128 section.setClient(container); 129 } 130 private void createNameTable(Composite parent, FormToolkit toolkit) { 131 EditableTablePart tablePart = getTablePart(); 132 tablePart.setEditable(getPage().getModel().isEditable()); 133 createViewerPartControl(parent, SWT.FULL_SELECTION, 2, toolkit); 134 fPackageExportViewer = tablePart.getTableViewer(); 135 fPackageExportViewer.setContentProvider(new TableContentProvider()); 136 fPackageExportViewer.setLabelProvider(new TableLabelProvider()); 137 fPackageExportViewer.setSorter(new ViewerSorter()); 138 toolkit.paintBordersFor(parent); 139 } 140 protected void selectionChanged(IStructuredSelection selection) { 141 Object item = selection.getFirstElement(); 142 getTablePart().setButtonEnabled(1, item != null); 143 } 144 protected void buttonSelected(int index) { 145 if (index == 0) 146 handleAdd(); 147 else if (index == 1) 148 handleDelete(); 149 } 150 public boolean doGlobalAction(String actionId) { 151 if (actionId.equals(ActionFactory.DELETE.getId())) { 152 handleDelete(); 153 return true; 154 } 155 return false; 156 } 157 public void dispose() { 158 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 159 if (model!=null) 160 model.removeModelChangedListener(this); 161 InputContextManager contextManager = getPage().getPDEEditor().getContextManager(); 162 if (contextManager!=null) 163 contextManager.removeInputContextListener(this); 164 super.dispose(); 165 } 166 protected void fillContextMenu(IMenuManager manager) { 167 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 168 manager); 169 } 170 private void handleAdd() { 171 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 172 IProject project = model.getUnderlyingResource().getProject(); 173 try { 174 if (project.hasNature(JavaCore.NATURE_ID)) { 175 String [] names; 176 if (isBundleMode()) 177 names = getProvidedPackages(); 178 else 179 names = fCurrentLibrary.getContentFilters(); 180 Vector existing = new Vector (); 181 if (names != null) { 182 for (int i = 0; i < names.length; i++) { 183 existing.add(names[i]); 184 } 185 } 186 ILabelProvider labelProvider = new JavaElementLabelProvider(); 187 PackageSelectionDialog dialog = new PackageSelectionDialog( 188 fPackageExportViewer.getTable().getShell(), 189 labelProvider, JavaCore.create(project), existing); 190 if (dialog.open() == PackageSelectionDialog.OK) { 191 Object [] elements = dialog.getResult(); 192 if (isBundleMode()) 193 addProvidedPackages(elements); 194 else { 195 for (int i = 0; i < elements.length; i++) { 196 IPackageFragment fragment = (IPackageFragment) elements[i]; 197 fCurrentLibrary.addContentFilter(fragment 198 .getElementName()); 199 } 200 } 201 } 202 labelProvider.dispose(); 203 } 204 } catch (CoreException e) { 205 } 206 } 207 private void handleDelete() { 208 IStructuredSelection ssel = (IStructuredSelection) fPackageExportViewer 209 .getSelection(); 210 Object [] items = ssel.toArray(); 211 try { 212 if (isBundleMode()) 213 removeProvidedPackages(items); 214 else { 215 for (int i = 0; i < items.length; i++) { 216 fCurrentLibrary.removeContentFilter(items[i].toString()); 217 } 218 } 219 } catch (CoreException e) { 220 } 221 } 222 public void initialize() { 223 IPluginModelBase model = (IPluginModelBase) getPage().getModel(); 224 model.addModelChangedListener(this); 225 InputContextManager contextManager = getPage().getPDEEditor().getContextManager(); 226 if (contextManager!=null) 227 contextManager.addInputContextListener(this); 228 if (isBundleMode()) 229 getBundleModel().addModelChangedListener(this); 230 } 231 public void modelChanged(IModelChangedEvent e) { 232 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 233 if (fCurrentLibrary!=null) 234 update(null, isBundleMode()); 235 markStale(); 236 return; 237 } 238 refresh(); 239 } 240 241 public void refresh() { 242 update(fCurrentLibrary, isBundleMode()); 243 super.refresh(); 244 } 245 246 public void selectionChanged(IFormPart source, ISelection selection) { 247 if (selection == null || selection.isEmpty()) 248 update(null, isBundleMode()); 249 IStructuredSelection ssel = (IStructuredSelection) selection; 250 if (ssel.getFirstElement() instanceof IPluginLibrary) 251 update((IPluginLibrary) ssel.getFirstElement(), isBundleMode()); 252 } 253 private boolean isReadOnly() { 254 IBaseModel model = getPage().getModel(); 255 if (model instanceof IEditable) 256 return !((IEditable) model).isEditable(); 257 return true; 258 } 259 private boolean isBundleMode() { 260 return getPage().getModel() instanceof IBundlePluginModelBase; 261 } 262 private void update(IPluginLibrary library, boolean bundleMode) { 263 fCurrentLibrary = library; 264 if (bundleMode) { 266 updateInBundleMode(); 267 return; 268 } 269 if (library == null) { 270 fFullExportButton.setEnabled(false); 271 fFullExportButton.setSelection(false); 272 fSelectedExportButton.setEnabled(false); 273 fSelectedExportButton.setSelection(false); 274 fPackageExportViewer.setInput(new Object [0]); 275 getTablePart().setButtonEnabled(0, false); 276 getTablePart().setButtonEnabled(1, false); 277 return; 278 } 279 fFullExportButton.setEnabled(!isReadOnly()); 280 fSelectedExportButton.setEnabled(!isReadOnly()); 281 fFullExportButton.setSelection(library.isFullyExported()); 282 fSelectedExportButton.setSelection(!library.isFullyExported()); 283 fPackageExportViewer.setInput(library); 284 getTablePart().setButtonEnabled(1, false); 285 getTablePart() 286 .setButtonEnabled(0, fSelectedExportButton.getSelection()); 287 } 288 289 private void updateInBundleMode() { 290 getTablePart().setButtonEnabled(1, false); 291 getTablePart().setButtonEnabled(0, true); 292 fFullExportButton.setEnabled(false); 293 fFullExportButton.setSelection(false); 294 fSelectedExportButton.setEnabled(false); 295 fSelectedExportButton.setSelection(true); 296 fPackageExportViewer.setInput(getPage().getModel()); 297 } 298 299 private String [] getProvidedPackages() { 300 IBundleModel model = getBundleModel(); 301 IBundle bundle = model.getBundle(); 302 if (bundle == null) 303 return new String [0]; 304 String value = bundle.getHeader(Constants.PROVIDE_PACKAGE); 305 if (value == null) 306 return new String [0]; 307 try { 308 ManifestElement [] result = ManifestElement.parseHeader(Constants.PROVIDE_PACKAGE, value); 309 String [] names = new String [result.length]; 310 for (int i=0; i<result.length; i++) { 311 names[i] = result[i].getValue(); 312 } 313 return names; 314 } catch (BundleException e) { 315 } 316 return new String [0]; 317 } 318 private void addProvidedPackages(Object [] names) { 319 String [] current = getProvidedPackages(); 320 Object [] newNames; 321 if (current.length==0) 322 newNames = names; 323 else 324 newNames = new Object [current.length+names.length]; 325 System.arraycopy(current, 0, newNames, 0, current.length); 326 System.arraycopy(names, 0, newNames, current.length, names.length); 327 setProvidedPackages(newNames); 328 } 329 private void removeProvidedPackages(Object [] removed) { 330 String [] current = getProvidedPackages(); 331 ArrayList result = new ArrayList(); 332 for (int i=0; i<current.length; i++) { 333 String name = current[i]; 334 boolean skip=false; 335 for (int j=0; j<removed.length; j++) { 336 if (name.equals(removed[j])) { 337 skip=true; 338 break; 339 } 340 } 341 if (skip) continue; 342 result.add(name); 343 } 344 setProvidedPackages(result.toArray()); 345 } 346 347 IBundleModel getBundleModel() { 348 InputContextManager contextManager = getPage().getPDEEditor().getContextManager(); 349 if (contextManager==null) return null; 350 InputContext context = contextManager.findContext(BundleInputContext.CONTEXT_ID); 351 if (context!=null) 352 return (IBundleModel)context.getModel(); 353 return null; 354 } 355 356 private void setProvidedPackages(Object [] names) { 357 StringBuffer buf = new StringBuffer (); 358 for (int i=0; i<names.length; i++) { 359 String name; 360 if (names[i] instanceof IPackageFragment) 361 name = ((IPackageFragment)names[i]).getElementName(); 362 else 363 name = names[i].toString(); 364 365 buf.append(name); 366 if (i < names.length - 1) 367 buf.append("," + System.getProperty("line.separator") + " "); } 369 IBundleModel model = getBundleModel(); 370 IBundle bundle = model.getBundle(); 371 if (bundle == null) return; 372 bundle.setHeader(Constants.PROVIDE_PACKAGE, buf.toString()); 373 } 374 377 public void contextAdded(InputContext context) { 378 if (context.getId().equals(BundleInputContext.CONTEXT_ID)) 379 bundleModeChanged((IBundleModel)context.getModel(), true); 380 } 381 384 public void contextRemoved(InputContext context) { 385 if (context.getId().equals(BundleInputContext.CONTEXT_ID)) 386 bundleModeChanged((IBundleModel)context.getModel(), false); 387 } 388 private void bundleModeChanged(IBundleModel model, boolean added) { 389 if (added) { 390 update(fCurrentLibrary, true); 391 model.addModelChangedListener(this); 392 } 393 else { 394 model.removeModelChangedListener(this); 395 update(fCurrentLibrary, false); 396 } 397 } 398 401 public void monitoredFileAdded(IFile monitoredFile) { 402 } 403 406 public boolean monitoredFileRemoved(IFile monitoredFile) { 407 return false; 408 } 409 } 410 | Popular Tags |