1 11 package org.eclipse.pde.internal.ui.editor.feature; 12 13 import java.net.MalformedURLException ; 14 import java.net.URL ; 15 import java.util.Iterator ; 16 17 import org.eclipse.core.runtime.CoreException; 18 import org.eclipse.jface.action.Action; 19 import org.eclipse.jface.action.IMenuManager; 20 import org.eclipse.jface.action.Separator; 21 import org.eclipse.jface.viewers.ISelection; 22 import org.eclipse.jface.viewers.IStructuredContentProvider; 23 import org.eclipse.jface.viewers.IStructuredSelection; 24 import org.eclipse.jface.viewers.LabelProvider; 25 import org.eclipse.jface.viewers.StructuredSelection; 26 import org.eclipse.jface.viewers.TableViewer; 27 import org.eclipse.pde.core.IModel; 28 import org.eclipse.pde.core.IModelChangedEvent; 29 import org.eclipse.pde.internal.core.PDECore; 30 import org.eclipse.pde.internal.core.feature.FeatureURLElement; 31 import org.eclipse.pde.internal.core.ifeature.IFeature; 32 import org.eclipse.pde.internal.core.ifeature.IFeatureModel; 33 import org.eclipse.pde.internal.core.ifeature.IFeatureURL; 34 import org.eclipse.pde.internal.core.ifeature.IFeatureURLElement; 35 import org.eclipse.pde.internal.ui.PDELabelProvider; 36 import org.eclipse.pde.internal.ui.PDEPlugin; 37 import org.eclipse.pde.internal.ui.PDEPluginImages; 38 import org.eclipse.pde.internal.ui.PDEUIMessages; 39 import org.eclipse.pde.internal.ui.editor.ModelDataTransfer; 40 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 41 import org.eclipse.pde.internal.ui.editor.TableSection; 42 import org.eclipse.pde.internal.ui.elements.DefaultContentProvider; 43 import org.eclipse.pde.internal.ui.parts.TablePart; 44 import org.eclipse.swt.SWT; 45 import org.eclipse.swt.custom.BusyIndicator; 46 import org.eclipse.swt.dnd.Clipboard; 47 import org.eclipse.swt.graphics.Image; 48 import org.eclipse.swt.layout.GridLayout; 49 import org.eclipse.swt.widgets.Composite; 50 import org.eclipse.ui.actions.ActionFactory; 51 import org.eclipse.ui.forms.widgets.ExpandableComposite; 52 import org.eclipse.ui.forms.widgets.FormToolkit; 53 import org.eclipse.ui.forms.widgets.Section; 54 55 public class URLSection extends TableSection { 56 private TableViewer fUrlViewer; 57 58 private Action fNewAction; 59 60 private Action fDeleteAction; 61 62 private Image fUrlImage; 63 64 class URLContentProvider extends DefaultContentProvider implements 65 IStructuredContentProvider { 66 public Object [] getElements(Object input) { 67 IFeature feature = (IFeature) input; 68 IFeatureURL featureUrl = feature.getURL(); 69 if (featureUrl != null) { 70 return featureUrl.getDiscoveries(); 71 } 72 return new Object [0]; 73 } 74 } 75 76 class URLLabelProvider extends LabelProvider { 77 78 public Image getImage(Object obj) { 79 if (obj instanceof IFeatureURLElement) { 80 return fUrlImage; 81 } 82 return null; 83 } 84 85 } 86 87 public URLSection(PDEFormPage page, Composite parent) { 88 super(page, parent, Section.DESCRIPTION | ExpandableComposite.NO_TITLE, 89 false, new String [] { PDEUIMessages.FeatureEditor_URLSection_new }); 90 PDELabelProvider provider = PDEPlugin.getDefault().getLabelProvider(); 91 fUrlImage = provider.get(PDEPluginImages.DESC_LINK_OBJ); 92 createClient(getSection(), page.getManagedForm().getToolkit()); 93 94 getSection().setDescription(PDEUIMessages.FeatureEditor_URLSection_desc); 95 } 96 97 public void commit(boolean onSave) { 98 super.commit(onSave); 99 } 100 101 public void createClient(Section section, FormToolkit toolkit) { 102 Composite container = createClientContainer(section, 2, toolkit); 103 GridLayout layout = (GridLayout) container.getLayout(); 104 layout.verticalSpacing = 5; 105 106 createViewerPartControl(container, SWT.SINGLE, 2, toolkit); 107 TablePart tablePart = getTablePart(); 108 fUrlViewer = tablePart.getTableViewer(); 109 fUrlViewer.setContentProvider(new URLContentProvider()); 110 fUrlViewer.setLabelProvider(new URLLabelProvider()); 111 toolkit.paintBordersFor(container); 112 makeActions(); 113 section.setClient(container); 114 initialize(); 115 } 116 117 protected void buttonSelected(int index) { 118 if (index == 0) 119 handleNew(); 120 } 121 122 public void dispose() { 123 IFeatureModel model = (IFeatureModel) getPage().getModel(); 124 if (model != null) 125 model.removeModelChangedListener(this); 126 super.dispose(); 127 } 128 129 protected void fillContextMenu(IMenuManager manager) { 130 IModel model = (IModel) getPage().getModel(); 131 ISelection selection = fUrlViewer.getSelection(); 132 Object object = ((IStructuredSelection) selection).getFirstElement(); 133 134 manager.add(fNewAction); 135 fNewAction.setEnabled(model.isEditable()); 136 137 if (object != null && object instanceof IFeatureURLElement) { 138 manager.add(fDeleteAction); 139 fDeleteAction.setEnabled(model.isEditable()); 140 } 141 142 manager.add(new Separator()); 143 getPage().getPDEEditor().getContributor().contextMenuAboutToShow( 144 manager); 145 } 146 147 private void handleNew() { 148 IFeatureModel model = (IFeatureModel) getPage().getModel(); 149 IFeature feature = model.getFeature(); 150 IFeatureURL url = feature.getURL(); 151 152 if (url == null) { 153 url = model.getFactory().createURL(); 154 try { 155 feature.setURL(url); 156 } catch (CoreException e) { 157 return; 158 } 159 } 160 try { 161 IFeatureURLElement element = model.getFactory().createURLElement( 162 url, IFeatureURLElement.DISCOVERY); 163 element.setLabel(PDEUIMessages.FeatureEditor_URLSection_newDiscoverySite); 164 element.setURL(new URL (PDEUIMessages.FeatureEditor_URLSection_newURL)); 165 url.addDiscovery(element); 166 fUrlViewer.setSelection(new StructuredSelection(element)); 167 168 } catch (CoreException e) { 169 PDEPlugin.logException(e); 170 } catch (MalformedURLException e) { 171 PDEPlugin.logException(e); 172 } 173 } 174 175 private void handleSelectAll() { 176 IStructuredContentProvider provider = (IStructuredContentProvider) fUrlViewer 177 .getContentProvider(); 178 Object [] elements = provider.getElements(fUrlViewer.getInput()); 179 StructuredSelection ssel = new StructuredSelection(elements); 180 fUrlViewer.setSelection(ssel); 181 } 182 183 private void handleDelete() { 184 IStructuredSelection ssel = (IStructuredSelection) fUrlViewer 185 .getSelection(); 186 187 if (ssel.isEmpty()) 188 return; 189 IFeatureModel model = (IFeatureModel) getPage().getModel(); 190 if (!model.isEditable()) { 191 return; 192 } 193 IFeature feature = model.getFeature(); 194 195 IFeatureURL url = feature.getURL(); 196 if (url == null) { 197 return; 198 } 199 for (Iterator iter = ssel.iterator(); iter.hasNext();) { 200 IFeatureURLElement urlElement = (IFeatureURLElement) iter.next(); 201 try { 203 url.removeDiscovery(urlElement); 204 } catch (CoreException e) { 205 PDEPlugin.logException(e); 206 } 207 } 208 } 209 210 public boolean doGlobalAction(String actionId) { 211 if (actionId.equals(ActionFactory.DELETE.getId())) { 212 BusyIndicator.showWhile(fUrlViewer.getTable().getDisplay(), 213 new Runnable () { 214 public void run() { 215 handleDelete(); 216 } 217 }); 218 return true; 219 } 220 if (actionId.equals(ActionFactory.CUT.getId())) { 221 handleDelete(); 224 return false; 225 } 226 if (actionId.equals(ActionFactory.PASTE.getId())) { 227 doPaste(); 228 return true; 229 } 230 if (actionId.equals(ActionFactory.SELECT_ALL.getId())) { 231 BusyIndicator.showWhile(fUrlViewer.getTable().getDisplay(), 232 new Runnable () { 233 public void run() { 234 handleSelectAll(); 235 } 236 }); 237 return true; 238 } 239 return false; 240 } 241 242 protected void selectionChanged(IStructuredSelection selection) { 243 getPage().getPDEEditor().setSelection(selection); 244 } 245 246 public void initialize() { 247 IFeatureModel model = (IFeatureModel) getPage().getModel(); 248 refresh(); 249 getTablePart().setButtonEnabled(0, model.isEditable()); 250 model.addModelChangedListener(this); 251 } 252 253 public void modelChanged(IModelChangedEvent e) { 254 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 255 markStale(); 256 return; 257 } 258 Object obj = e.getChangedObjects()[0]; 259 if (obj instanceof IFeatureURL) { 260 markStale(); 261 return; 262 } 263 if (obj instanceof IFeatureURLElement) { 264 markStale(); 265 return; 266 } 280 } 281 282 private void makeActions() { 283 IModel model = (IModel) getPage().getModel(); 284 fNewAction = new Action() { 285 public void run() { 286 handleNew(); 287 } 288 }; 289 fNewAction.setText(PDEUIMessages.Menus_new_label); 290 fNewAction.setEnabled(model.isEditable()); 291 292 fDeleteAction = new Action() { 293 public void run() { 294 BusyIndicator.showWhile(fUrlViewer.getTable().getDisplay(), 295 new Runnable () { 296 public void run() { 297 handleDelete(); 298 } 299 }); 300 } 301 }; 302 fDeleteAction.setText(PDEUIMessages.Actions_delete_label); 303 fDeleteAction.setEnabled(model.isEditable()); 304 } 305 306 public void setFocus() { 307 if (fUrlViewer != null) 308 fUrlViewer.getTable().setFocus(); 309 } 310 311 public void refresh() { 312 IFeatureModel model = (IFeatureModel) getPage().getModel(); 313 IFeature feature = model.getFeature(); 314 fUrlViewer.setInput(feature); 315 super.refresh(); 316 } 317 318 public boolean canPaste(Clipboard clipboard) { 319 ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance(); 320 Object [] objects = (Object []) clipboard.getContents(modelTransfer); 321 if (objects != null && objects.length > 0) { 322 return canPaste(null, objects); 323 } 324 return false; 325 } 326 327 331 protected boolean canPaste(Object target, Object [] objects) { 332 for (int i = 0; i < objects.length; i++) { 333 if (!(objects[i] instanceof FeatureURLElement)) 334 return false; 335 } 336 return true; 337 } 338 339 protected void doPaste() { 340 Clipboard clipboard = getPage().getPDEEditor().getClipboard(); 341 ModelDataTransfer modelTransfer = ModelDataTransfer.getInstance(); 342 Object [] objects = (Object []) clipboard.getContents(modelTransfer); 343 if (objects != null) { 344 doPaste(null, objects); 345 } 346 } 347 348 352 protected void doPaste(Object target, Object [] objects) { 353 IFeatureModel model = (IFeatureModel) getPage().getModel(); 354 if (!model.isEditable()) { 355 return; 356 } 357 IFeature feature = model.getFeature(); 358 for (int i = 0; i < objects.length; i++) { 359 if (objects[i] instanceof FeatureURLElement) { 360 FeatureURLElement element = (FeatureURLElement) objects[i]; 361 element.setModel(model); 362 element.setParent(feature); 363 try { 364 feature.getURL().addDiscovery(element); 365 } catch (CoreException e) { 366 PDECore.logException(e); 367 } 368 } 369 } 370 } 371 372 void fireSelection() { 373 fUrlViewer.setSelection(fUrlViewer.getSelection()); 374 } 375 } 376 | Popular Tags |