1 11 package org.eclipse.pde.internal.ui.editor.plugin; 12 import java.util.ArrayList ; 13 import java.util.List ; 14 15 import org.eclipse.jface.action.Action; 16 import org.eclipse.jface.action.IMenuManager; 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.IStructuredContentProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.viewers.ITableLabelProvider; 21 import org.eclipse.jface.viewers.LabelProvider; 22 import org.eclipse.jface.viewers.TableViewer; 23 import org.eclipse.jface.window.Window; 24 import org.eclipse.pde.core.IModelChangedEvent; 25 import org.eclipse.pde.core.plugin.IPluginModelBase; 26 import org.eclipse.pde.core.plugin.PluginRegistry; 27 import org.eclipse.pde.internal.core.ibundle.IBundleModel; 28 import org.eclipse.pde.internal.core.text.bundle.ExportPackageObject; 29 import org.eclipse.pde.internal.core.text.bundle.PackageFriend; 30 import org.eclipse.pde.internal.ui.PDEPlugin; 31 import org.eclipse.pde.internal.ui.PDEPluginImages; 32 import org.eclipse.pde.internal.ui.PDEUIMessages; 33 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 34 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 35 import org.eclipse.pde.internal.ui.editor.TableSection; 36 import org.eclipse.pde.internal.ui.editor.context.InputContextManager; 37 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 38 import org.eclipse.pde.internal.ui.parts.EditableTablePart; 39 import org.eclipse.pde.internal.ui.wizards.PluginSelectionDialog; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.graphics.Image; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.layout.GridLayout; 45 import org.eclipse.swt.widgets.Button; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.ui.actions.ActionFactory; 48 import org.eclipse.ui.forms.IFormPart; 49 import org.eclipse.ui.forms.IPartSelectionListener; 50 import org.eclipse.ui.forms.widgets.FormToolkit; 51 import org.eclipse.ui.forms.widgets.Section; 52 import org.osgi.framework.Constants; 53 54 public class ExportPackageVisibilitySection extends TableSection 55 implements IPartSelectionListener { 56 57 private static int ADD_INDEX = 0; 58 private static int REMOVE_INDEX = 1; 59 60 private TableViewer fFriendViewer; 61 private Action fAddAction; 62 private Action fRemoveAction; 63 private Button fInternalButton; 64 private boolean fBlockChanges; 65 private ExportPackageObject[] fSelectedObjects; 66 private Image fImage; 67 private Button fVisibleButton; 68 69 class TableContentProvider extends DefaultContentProvider 70 implements IStructuredContentProvider { 71 public Object [] getElements(Object parent) { 72 ExportPackageObject object = (ExportPackageObject)parent; 73 if (!object.isInternal()) return new Object [0]; 74 return (object != null) ? object.getFriends() : new Object [0]; 75 } 76 } 77 class TableLabelProvider extends LabelProvider 78 implements ITableLabelProvider { 79 80 public String getColumnText(Object obj, int index) { 81 return obj.toString(); 82 } 83 public Image getColumnImage(Object obj, int index) { 84 return fImage; 85 } 86 } 87 88 public ExportPackageVisibilitySection(PDEFormPage formPage, Composite parent) { 89 super(formPage, parent, Section.DESCRIPTION, new String []{ 90 PDEUIMessages.ManifestEditor_ExportSection_add, 91 PDEUIMessages.ManifestEditor_ExportSection_remove}); 92 fHandleDefaultButton = false; 93 } 94 95 public void createClient(Section section, FormToolkit toolkit) { 96 section.setText(PDEUIMessages.ExportPackageVisibilitySection_title); 97 section.setDescription(PDEUIMessages.ExportPackageVisibilitySection_default); 98 Composite comp = toolkit.createComposite(section); 99 comp.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 1)); 100 101 fVisibleButton = toolkit.createButton(comp, PDEUIMessages.ExportPackageVisibilitySection_unconditional, SWT.RADIO); 102 103 fInternalButton = toolkit.createButton(comp, PDEUIMessages.ExportPackageVisibilitySection_hideAll, SWT.RADIO); 104 fInternalButton.addSelectionListener(new SelectionAdapter() { 105 public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { 106 if (!fBlockChanges) { 107 for (int i = 0; i < fSelectedObjects.length; i++) { 108 fSelectedObjects[i].setInternal(fInternalButton.getSelection()); 109 } 110 getTablePart().setButtonEnabled(ADD_INDEX, fInternalButton.getSelection()); 111 getTablePart().setButtonEnabled(REMOVE_INDEX, fInternalButton.getSelection()); 112 fFriendViewer.refresh(); 113 } 114 } 115 }); 116 117 Composite container = toolkit.createComposite(comp); 118 GridLayout layout = new GridLayout(); 119 layout.marginWidth = 1; 120 layout.marginHeight = 1; 121 layout.numColumns = 2; 122 container.setLayout(layout); 123 container.setLayoutData(new GridData(GridData.FILL_BOTH)); 124 125 EditableTablePart tablePart = getTablePart(); 126 tablePart.setEditable(getPage().getModel().isEditable()); 127 createViewerPartControl(container, SWT.MULTI, 2, toolkit); 128 fFriendViewer = tablePart.getTableViewer(); 129 fFriendViewer.setContentProvider(new TableContentProvider()); 130 fFriendViewer.setLabelProvider(new TableLabelProvider()); 131 toolkit.paintBordersFor(container); 132 133 makeActions(); 134 fImage = PDEPluginImages.DESC_PLUGIN_OBJ.createImage(); 135 update(null); 136 getBundleModel().addModelChangedListener(this); 137 138 section.setLayout(FormLayoutFactory.createClearGridLayout(false, 1)); 139 section.setLayoutData(new GridData(GridData.FILL_BOTH)); 140 section.setClient(comp); 141 } 142 143 private void makeActions() { 144 fAddAction = new Action(PDEUIMessages.ManifestEditor_ExportSection_add) { 145 public void run() { 146 handleAdd(); 147 } 148 }; 149 fAddAction.setEnabled(isEditable()); 150 151 fRemoveAction = new Action(PDEUIMessages.ManifestEditor_ExportSection_remove) { 152 public void run() { 153 handleRemove(); 154 } 155 }; 156 fRemoveAction.setEnabled(isEditable()); 157 } 158 159 protected void selectionChanged(IStructuredSelection selection) { 160 getPage().getPDEEditor().setSelection(selection); 162 163 Object item = selection.getFirstElement(); 164 getTablePart().setButtonEnabled(1, item != null); 165 } 166 167 protected void buttonSelected(int index) { 168 if (index == ADD_INDEX) 169 handleAdd(); 170 else if (index == REMOVE_INDEX) 171 handleRemove(); 172 } 173 174 177 public boolean doGlobalAction(String actionId) { 178 179 if (!isEditable()) { return false; } 180 181 if (actionId.equals(ActionFactory.DELETE.getId())) { 182 handleRemove(); 183 return true; 184 } 185 if (actionId.equals(ActionFactory.CUT.getId())) { 186 handleRemove(); 189 return false; 190 } 191 if (actionId.equals(ActionFactory.PASTE.getId())) { 192 doPaste(); 193 return true; 194 } 195 return false; 196 } 197 198 201 protected boolean canPaste(Object targetObject, Object [] sourceObjects) { 202 if (isOneObjectSelected() == false) { 204 return false; 205 } 206 for (int i = 0; i < sourceObjects.length; i++) { 209 if ((sourceObjects[i] instanceof PackageFriend) == false) { 211 return false; 212 } 213 PackageFriend friend = (PackageFriend)sourceObjects[i]; 215 if (fSelectedObjects[0].hasFriend(friend.getName())) { 216 return false; 217 } 218 } 219 return true; 220 } 221 222 225 private boolean isOneObjectSelected() { 226 if ((fSelectedObjects == null) || 227 (fSelectedObjects.length != 1)) { 228 return false; 229 } 230 return true; 231 } 232 233 236 protected void doPaste(Object targetObject, Object [] sourceObjects) { 237 for (int i = 0; i < sourceObjects.length; i++) { 239 Object sourceObject = sourceObjects[i]; 240 if ((sourceObject instanceof PackageFriend) && 241 isOneObjectSelected()) { 242 PackageFriend friend = (PackageFriend)sourceObject; 244 friend.reconnect(fSelectedObjects[0]); 247 fSelectedObjects[0].addFriend(friend); 249 } 250 } 251 } 252 253 public void dispose() { 254 IBundleModel model = getBundleModel(); 255 if (model != null) 256 model.removeModelChangedListener(this); 257 if (fImage != null) 258 fImage.dispose(); 259 super.dispose(); 260 } 261 262 protected void fillContextMenu(IMenuManager manager) { 263 getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); 264 } 265 266 private void handleAdd() { 267 PluginSelectionDialog dialog = new PluginSelectionDialog(PDEPlugin.getActiveWorkbenchShell(), getModels(), true); 268 dialog.create(); 269 if (dialog.open() == Window.OK) { 270 Object [] selected = dialog.getResult(); 271 for (int i = 0; i < selected.length; i++) { 272 IPluginModelBase model = (IPluginModelBase)selected[i]; 273 for (int j = 0; j < fSelectedObjects.length; j++) { 274 fSelectedObjects[j].addFriend(new PackageFriend(fSelectedObjects[j], model.getPluginBase().getId())); 275 } 276 } 277 } 278 } 279 280 private IPluginModelBase[] getModels() { 281 ArrayList list = new ArrayList (); 282 IPluginModelBase[] models = PluginRegistry.getActiveModels(true); 283 for (int i = 0; i < models.length; i++) { 284 String id = models[i].getPluginBase().getId(); 285 if (!fSelectedObjects[0].hasFriend(id)) 286 list.add(models[i]); 287 } 288 return (IPluginModelBase[])list.toArray(new IPluginModelBase[list.size()]); 289 } 290 291 private void handleRemove() { 292 Object [] removed = ((IStructuredSelection) fFriendViewer.getSelection()).toArray(); 293 for (int i = 0; i < removed.length; i++) { 294 for (int j = 0; j < fSelectedObjects.length; j++) { 295 fSelectedObjects[j].removeFriend((PackageFriend) removed[i]); 296 } 297 } 298 } 299 300 public void modelChanged(IModelChangedEvent event) { 301 if (event.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 302 markStale(); 303 return; 304 } 305 306 if (Constants.EXPORT_PACKAGE.equals(event.getChangedProperty())) { 307 refresh(); 308 return; 309 } 310 int index = fFriendViewer.getTable().getSelectionIndex(); 311 fFriendViewer.refresh(); 312 fFriendViewer.getTable().setSelection(Math.min(index, fFriendViewer.getTable().getItemCount() - 1)); 313 } 314 315 public void refresh() { 316 update(null); 317 super.refresh(); 318 } 319 320 public void selectionChanged(IFormPart source, ISelection selection) { 321 List list = ((IStructuredSelection)selection).toList(); 322 if (list.size() > 0) { 323 Object [] objects = list.toArray(); 324 ExportPackageObject first = null; 325 for (int i = 0; i < objects.length; i++) { 326 if (!(objects[i] instanceof ExportPackageObject)) { 327 update(null); 328 return; 329 } 330 if (first == null) { 331 first = (ExportPackageObject)objects[i]; 332 continue; 333 } 334 if (!first.hasSameVisibility((ExportPackageObject)objects[i])) { 335 update(null); 336 return; 337 } 338 } 339 update((ExportPackageObject[])list.toArray(new ExportPackageObject[list.size()])); 340 } else { 341 update(null); 342 } 343 } 344 345 private void update(ExportPackageObject[] objects) { 346 fBlockChanges = true; 347 fSelectedObjects = objects; 348 349 ExportPackageObject object = objects == null ? null : objects[0]; 350 fVisibleButton.setEnabled(object != null && isEditable()); 351 fVisibleButton.setSelection(objects != null && !object.isInternal()); 352 353 fInternalButton.setEnabled(object != null && isEditable()); 354 fInternalButton.setSelection(objects != null && object.isInternal()); 355 356 getTablePart().setButtonEnabled(0, fInternalButton.getSelection() && isEditable()); 357 getTablePart().setButtonEnabled(1, fInternalButton.getSelection()&& isEditable()); 358 fFriendViewer.setInput(object); 359 fBlockChanges = false; 360 } 361 362 private BundleInputContext getBundleContext() { 363 InputContextManager manager = getPage().getPDEEditor().getContextManager(); 364 return (BundleInputContext) manager.findContext(BundleInputContext.CONTEXT_ID); 365 } 366 367 private IBundleModel getBundleModel() { 368 BundleInputContext context = getBundleContext(); 369 return (context != null) ? (IBundleModel)context.getModel() : null; 370 } 371 372 } 373 | Popular Tags |