1 11 package org.eclipse.pde.internal.ui.editor.build; 12 13 import org.eclipse.core.resources.IFile; 14 import org.eclipse.core.resources.IProject; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Status; 20 import org.eclipse.jface.action.Action; 21 import org.eclipse.jface.action.IMenuManager; 22 import org.eclipse.jface.action.Separator; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.IStructuredContentProvider; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.ITableLabelProvider; 27 import org.eclipse.jface.viewers.LabelProvider; 28 import org.eclipse.jface.viewers.StructuredSelection; 29 import org.eclipse.jface.viewers.TableViewer; 30 import org.eclipse.jface.viewers.Viewer; 31 import org.eclipse.jface.viewers.ViewerFilter; 32 import org.eclipse.jface.window.Window; 33 import org.eclipse.pde.core.IModelChangedEvent; 34 import org.eclipse.pde.core.IModelChangedListener; 35 import org.eclipse.pde.core.build.IBuild; 36 import org.eclipse.pde.core.build.IBuildEntry; 37 import org.eclipse.pde.core.build.IBuildModel; 38 import org.eclipse.pde.core.plugin.IPluginModelBase; 39 import org.eclipse.pde.core.plugin.PluginRegistry; 40 import org.eclipse.pde.internal.build.IBuildPropertiesConstants; 41 import org.eclipse.pde.internal.core.natures.PDE; 42 import org.eclipse.pde.internal.ui.PDEPlugin; 43 import org.eclipse.pde.internal.ui.PDEUIMessages; 44 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 45 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 46 import org.eclipse.pde.internal.ui.editor.TableSection; 47 import org.eclipse.pde.internal.ui.editor.context.InputContext; 48 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 49 import org.eclipse.pde.internal.ui.parts.EditableTablePart; 50 import org.eclipse.swt.SWT; 51 import org.eclipse.swt.graphics.Image; 52 import org.eclipse.swt.layout.GridData; 53 import org.eclipse.swt.widgets.Composite; 54 import org.eclipse.swt.widgets.Table; 55 import org.eclipse.ui.ISharedImages; 56 import org.eclipse.ui.PlatformUI; 57 import org.eclipse.ui.actions.ActionFactory; 58 import org.eclipse.ui.dialogs.ElementTreeSelectionDialog; 59 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 60 import org.eclipse.ui.forms.widgets.ExpandableComposite; 61 import org.eclipse.ui.forms.widgets.FormToolkit; 62 import org.eclipse.ui.forms.widgets.Section; 63 import org.eclipse.ui.model.WorkbenchContentProvider; 64 import org.eclipse.ui.model.WorkbenchLabelProvider; 65 import org.eclipse.ui.views.navigator.ResourceComparator; 66 67 public class BuildClasspathSection extends TableSection implements IModelChangedListener { 68 69 private TableViewer fTableViewer; 70 private boolean fEnabled = true; 71 72 73 78 class ElementSelectionValidator implements ISelectionStatusValidator { 79 80 private Class [] fAcceptedTypes; 81 private boolean fAllowMultipleSelection; 82 83 84 89 public ElementSelectionValidator(Class [] acceptedTypes, boolean allowMultipleSelection) { 90 Assert.isNotNull(acceptedTypes); 91 fAcceptedTypes= acceptedTypes; 92 fAllowMultipleSelection= allowMultipleSelection; 93 } 94 95 96 99 public IStatus validate(Object [] elements) { 100 if (isValid(elements)) { 101 return new Status( 102 IStatus.OK, 103 PDEPlugin.getPluginId(), 104 IStatus.OK, 105 "", null); 107 } 108 return new Status( 109 IStatus.ERROR, 110 PDEPlugin.getPluginId(), 111 IStatus.ERROR, 112 "", null); 114 } 115 116 private boolean isOfAcceptedType(Object o) { 117 for (int i= 0; i < fAcceptedTypes.length; i++) { 118 if (fAcceptedTypes[i].isInstance(o)) { 119 return true; 120 } 121 } 122 return false; 123 } 124 125 private boolean isValid(Object [] selection) { 126 if (selection.length == 0) { 127 return false; 128 } 129 130 if (!fAllowMultipleSelection && selection.length != 1) { 131 return false; 132 } 133 134 for (int i= 0; i < selection.length; i++) { 135 Object o= selection[i]; 136 if (!isOfAcceptedType(o)) { 137 return false; 138 } 139 } 140 return true; 141 } 142 } 143 144 class TableContentProvider 145 extends DefaultContentProvider 146 implements IStructuredContentProvider { 147 public Object [] getElements(Object parent) { 148 if (parent instanceof IBuildModel) { 149 IBuild build = ((IBuildModel)parent).getBuild(); 150 IBuildEntry entry = build.getEntry(IBuildPropertiesConstants.PROPERTY_JAR_EXTRA_CLASSPATH); 151 if (entry != null) { 152 return entry.getTokens(); 153 } 154 } 155 return new Object [0]; 156 } 157 } 158 159 class TableLabelProvider 160 extends LabelProvider 161 implements ITableLabelProvider { 162 public String getColumnText(Object obj, int index) { 163 return obj.toString(); 164 } 165 public Image getColumnImage(Object obj, int index) { 166 ISharedImages sharedImages = PlatformUI.getWorkbench().getSharedImages(); 167 return sharedImages.getImage(ISharedImages.IMG_OBJ_FILE); 168 } 169 } 170 public BuildClasspathSection(PDEFormPage page, Composite parent) { 171 super( 172 page, 173 parent, 174 Section.DESCRIPTION | ExpandableComposite.TWISTIE, 175 new String [] { 176 PDEUIMessages.BuildEditor_ClasspathSection_add, 177 PDEUIMessages.BuildEditor_ClasspathSection_remove, 178 null, 179 null }); 180 getSection().setText(PDEUIMessages.BuildEditor_ClasspathSection_title); 181 getSection().setDescription(PDEUIMessages.BuildEditor_ClasspathSection_desc); 182 initialize(); 183 184 } 185 186 private IBuildModel getBuildModel() { 187 InputContext context = getPage().getPDEEditor().getContextManager() 188 .findContext(BuildInputContext.CONTEXT_ID); 189 if (context==null) return null; 190 return (IBuildModel) context.getModel(); 191 } 192 193 public void initialize(){ 194 getBuildModel().addModelChangedListener(this); 195 IBuildEntry entry = getBuildModel().getBuild().getEntry(IBuildPropertiesConstants.PROPERTY_JAR_EXTRA_CLASSPATH); 196 getSection().setExpanded(entry!=null && entry.getTokens().length>0); 197 } 198 199 public void createClient( 200 Section section, 201 FormToolkit toolkit) { 202 Composite container = createClientContainer(section, 2, toolkit); 203 createViewerPartControl(container, SWT.FULL_SELECTION, 2, toolkit); 204 205 EditableTablePart tablePart = getTablePart(); 206 tablePart.setEditable(true); 207 fTableViewer = tablePart.getTableViewer(); 208 209 fTableViewer.setContentProvider(new TableContentProvider()); 210 fTableViewer.setLabelProvider(new TableLabelProvider()); 211 fTableViewer.setInput(getBuildModel()); 212 213 toolkit.paintBordersFor(container); 214 enableSection(true); 215 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 216 GridData data = new GridData(GridData.FILL_HORIZONTAL); 217 section.setLayoutData(data); 218 data.horizontalSpan = 2; 219 section.setClient(container); 220 } 221 222 protected void fillContextMenu(IMenuManager manager) { 223 ISelection selection = fTableViewer.getSelection(); 224 225 Action action = 227 new Action(PDEUIMessages.BuildEditor_ClasspathSection_add) { 228 public void run() { 229 handleNew(); 230 } 231 }; 232 action.setEnabled(fEnabled); 233 manager.add(action); 234 235 manager.add(new Separator()); 236 237 action = 239 new Action(PDEUIMessages.BuildEditor_ClasspathSection_remove) { 240 public void run() { 241 handleDelete(); 242 } 243 }; 244 action.setEnabled(!selection.isEmpty() && fEnabled); 245 manager.add(action); 246 247 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 248 manager, false); 249 } 250 251 public void dispose() { 252 IBuildModel model = getBuildModel(); 253 if (model!=null) 254 model.removeModelChangedListener(this); 255 super.dispose(); 256 } 257 258 public void refresh() { 259 fTableViewer.refresh(); 260 } 261 262 public boolean doGlobalAction(String actionId) { 263 if (actionId.equals(ActionFactory.DELETE.getId())) { 264 if (fEnabled) { 265 handleDelete(); 266 } 267 return true; 268 } 269 return false; 270 } 271 272 public void enableSection(boolean enable){ 273 fEnabled = enable; 274 EditableTablePart tablePart = getTablePart(); 275 tablePart.setButtonEnabled(1, enable && !fTableViewer.getSelection().isEmpty()); 276 tablePart.setButtonEnabled(0, enable); 277 } 278 279 protected void selectionChanged(IStructuredSelection selection) { 280 getPage().getPDEEditor().setSelection(selection); 281 getTablePart().setButtonEnabled(1, selection != null && selection.size() > 0 && fEnabled); 282 } 283 284 private void handleDelete() { 285 Object selection = 286 ((IStructuredSelection) fTableViewer.getSelection()).getFirstElement(); 287 if (selection != null && selection instanceof String ) { 288 IBuild build = getBuildModel().getBuild(); 289 IBuildEntry entry = build.getEntry(IBuildPropertiesConstants.PROPERTY_JAR_EXTRA_CLASSPATH); 290 if (entry != null) { 291 try { 292 entry.removeToken(selection.toString()); 293 294 String [] tokens = entry.getTokens(); 295 if (tokens.length == 0) 296 build.remove(entry); 297 298 } catch (CoreException e) { 299 PDEPlugin.logException(e); 300 } 301 } 302 } 303 } 304 305 private void initializeDialogSettings(ElementTreeSelectionDialog dialog){ 306 Class [] acceptedClasses = new Class [] { IFile.class }; 307 dialog.setValidator(new ElementSelectionValidator(acceptedClasses, true)); 308 dialog.setTitle(PDEUIMessages.BuildEditor_ClasspathSection_jarsTitle); 309 dialog.setMessage(PDEUIMessages.BuildEditor_ClasspathSection_jarsDesc); 310 dialog.addFilter(new JARFileFilter()); 311 dialog.addFilter(new ViewerFilter() { 312 public boolean select(Viewer viewer, Object parentElement, Object element) { 313 if (element instanceof IProject) { 314 try { 315 return ((IProject)element).hasNature(PDE.PLUGIN_NATURE); 316 } catch (CoreException e) { 317 } 318 return false; 319 } else if (element instanceof IResource) { 320 IBuildModel model = getBuildModel(); 321 IBuildEntry entry = model.getBuild().getEntry(IBuildPropertiesConstants.PROPERTY_JAR_EXTRA_CLASSPATH); 322 if (entry != null) 323 return !entry.contains(getRelativePathTokenName((IResource)element)); 324 } 325 return true; 326 } 327 }); 328 dialog.setInput(PDEPlugin.getWorkspace().getRoot()); 329 dialog.setComparator(new ResourceComparator(ResourceComparator.NAME)); 330 dialog.setInitialSelection(getBuildModel().getUnderlyingResource().getProject()); 331 332 } 333 private void handleNew() { 334 ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog( 335 getSection().getShell(), 336 new WorkbenchLabelProvider(), 337 new WorkbenchContentProvider()); 338 339 initializeDialogSettings(dialog); 340 if (dialog.open() == Window.OK) { 341 Object [] elements = dialog.getResult(); 342 for (int i = 0; i < elements.length; i++) { 343 IResource elem = (IResource) elements[i]; 344 String tokenName = getRelativePathTokenName(elem); 345 if (tokenName == null) 346 continue; 347 addClasspathToken(tokenName); 348 } 349 } 350 } 351 352 private void addClasspathToken(String tokenName){ 353 IBuildModel model = getBuildModel(); 354 IBuildEntry entry = model.getBuild().getEntry(IBuildPropertiesConstants.PROPERTY_JAR_EXTRA_CLASSPATH); 355 try { 356 if (entry == null){ 357 entry = model.getFactory().createEntry(IBuildPropertiesConstants.PROPERTY_JAR_EXTRA_CLASSPATH); 358 model.getBuild().add(entry); 359 } 360 if (!entry.contains(tokenName)) 361 entry.addToken(tokenName); 362 } catch (CoreException e) { 363 PDEPlugin.logException(e); 364 } 365 } 366 367 private String getRelativePathTokenName(IResource elem) { 368 IProject thisProject = getBuildModel().getUnderlyingResource().getProject(); 369 IProject elemProject = elem.getProject(); 370 String projectRelative = elem.getProjectRelativePath().toString(); 371 if (thisProject == elemProject) 372 return projectRelative; 373 374 IPluginModelBase model = PluginRegistry.findModel(elemProject); 375 if (model != null) 376 return "platform:/plugin/" + model.getPluginBase().getId() + '/' + projectRelative; return null; 378 } 379 380 protected void buttonSelected(int index) { 381 switch (index) { 382 case 0 : 383 handleNew(); 384 break; 385 case 1 : 386 handleDelete(); 387 break; 388 default : 389 break; 390 } 391 } 392 public void modelChanged(IModelChangedEvent event) { 393 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) 394 markStale(); 395 else if (event.getChangeType() == IModelChangedEvent.CHANGE) { 396 Object changeObject = event.getChangedObjects()[0]; 397 398 if (changeObject instanceof IBuildEntry && ((IBuildEntry)changeObject).getName().equals(IBuildEntry.JARS_EXTRA_CLASSPATH)){ 399 Table table = fTableViewer.getTable(); 400 int index = table.getSelectionIndex(); 401 fTableViewer.refresh(); 402 int count = table.getItemCount(); 403 if (index == -1 || index >= count || event.getOldValue() == null) 404 index = count - 1; 405 if (count == 0) 406 fTableViewer.setSelection(null); 407 else 408 fTableViewer.setSelection( 409 new StructuredSelection(table.getItem(index).getData())); 410 table.setFocus(); 411 } 412 } 413 } 414 } 415 416 | Popular Tags |