1 11 package org.eclipse.pde.internal.ui.editor.target; 12 13 import org.eclipse.core.runtime.Path; 14 import org.eclipse.jface.action.Action; 15 import org.eclipse.jface.action.IMenuManager; 16 import org.eclipse.jface.action.Separator; 17 import org.eclipse.jface.viewers.DoubleClickEvent; 18 import org.eclipse.jface.viewers.IDoubleClickListener; 19 import org.eclipse.jface.viewers.ISelectionChangedListener; 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 import org.eclipse.jface.viewers.SelectionChangedEvent; 22 import org.eclipse.jface.viewers.StructuredSelection; 23 import org.eclipse.jface.viewers.TableViewer; 24 import org.eclipse.jface.viewers.Viewer; 25 import org.eclipse.jface.viewers.ViewerComparator; 26 import org.eclipse.pde.core.IModelChangedEvent; 27 import org.eclipse.pde.core.plugin.TargetPlatform; 28 import org.eclipse.pde.internal.core.itarget.IAdditionalLocation; 29 import org.eclipse.pde.internal.core.itarget.ILocationInfo; 30 import org.eclipse.pde.internal.core.itarget.ITarget; 31 import org.eclipse.pde.internal.core.itarget.ITargetModel; 32 import org.eclipse.pde.internal.ui.PDEPlugin; 33 import org.eclipse.pde.internal.ui.PDEUIMessages; 34 import org.eclipse.pde.internal.ui.editor.FormLayoutFactory; 35 import org.eclipse.pde.internal.ui.editor.PDEFormPage; 36 import org.eclipse.pde.internal.ui.editor.TableSection; 37 import org.eclipse.pde.internal.ui.elements.DefaultTableProvider; 38 import org.eclipse.pde.internal.ui.parts.TablePart; 39 import org.eclipse.pde.internal.ui.util.SWTUtil; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.custom.BusyIndicator; 42 import org.eclipse.swt.layout.GridData; 43 import org.eclipse.swt.widgets.Composite; 44 import org.eclipse.ui.actions.ActionFactory; 45 import org.eclipse.ui.forms.widgets.FormToolkit; 46 import org.eclipse.ui.forms.widgets.Section; 47 import org.eclipse.ui.forms.widgets.TableWrapData; 48 49 public class LocationsSection extends TableSection { 50 51 private TableViewer fContentViewer; 52 53 class ContentProvider extends DefaultTableProvider { 54 public Object [] getElements(Object parent) { 55 ITarget target = getTarget(); 56 return target.getAdditionalDirectories(); 57 } 58 } 59 60 public LocationsSection(PDEFormPage page, Composite parent) { 61 super(page, parent, Section.DESCRIPTION, new String [] {PDEUIMessages.LocationsSection_add,PDEUIMessages.LocationsSection_edit, PDEUIMessages.LocationsSection_remove}); 62 } 63 64 protected void createClient(Section section, FormToolkit toolkit) { 65 section.setLayout(FormLayoutFactory.createClearTableWrapLayout(false, 1)); 66 Composite client = toolkit.createComposite(section); 67 client.setLayout(FormLayoutFactory.createSectionClientGridLayout(false, 2)); 68 client.setLayoutData(new GridData(GridData.FILL_BOTH | GridData.GRAB_VERTICAL)); 69 70 createViewerPartControl(client, SWT.MULTI, 2, toolkit); 71 72 TablePart tablePart = getTablePart(); 73 GridData data = (GridData) tablePart.getControl().getLayoutData(); 74 data.grabExcessVerticalSpace = true; 75 data.grabExcessHorizontalSpace = true; 76 fContentViewer = tablePart.getTableViewer(); 77 fContentViewer.setContentProvider(new ContentProvider()); 78 fContentViewer.setLabelProvider(PDEPlugin.getDefault().getLabelProvider()); 79 fContentViewer.setInput(getTarget()); 80 fContentViewer.addSelectionChangedListener(new ISelectionChangedListener() { 81 public void selectionChanged(SelectionChangedEvent event) { 82 updateButtons(); 83 } 84 }); 85 fContentViewer.setComparator(new ViewerComparator() { 86 public int compare(Viewer viewer, Object e1, Object e2) { 87 IAdditionalLocation loc1 = (IAdditionalLocation)e1; 88 return loc1.getPath().compareToIgnoreCase(((IAdditionalLocation)e2).getPath()); 89 } 90 }); 91 fContentViewer.addDoubleClickListener(new IDoubleClickListener() { 92 public void doubleClick(DoubleClickEvent event) { 93 if (((StructuredSelection)fContentViewer.getSelection()).toArray().length == 1) 95 handleEdit(); 96 } 97 }); 98 99 toolkit.paintBordersFor(client); 100 section.setClient(client); 101 section.setText(PDEUIMessages.LocationsSection_title); 102 section.setDescription(PDEUIMessages.LocationsSection_description); 103 TableWrapData tdata = new TableWrapData(TableWrapData.FILL_GRAB); 104 tdata.colspan = 2; 105 tdata.grabVertical = true; 106 section.setLayoutData(tdata); 107 updateButtons(); 108 109 getModel().addModelChangedListener(this); 110 } 111 112 private ITarget getTarget() { 113 return getModel().getTarget(); 114 } 115 116 private ITargetModel getModel() { 117 return (ITargetModel)getPage().getPDEEditor().getAggregateModel(); 118 } 119 120 protected void updateButtons() { 121 int selectionNum = ((StructuredSelection)fContentViewer.getSelection()).toArray().length; 122 TablePart table = getTablePart(); 123 table.setButtonEnabled(1, selectionNum == 1); 124 table.setButtonEnabled(2, selectionNum > 0); 125 } 126 127 protected void buttonSelected(int index) { 128 switch (index) { 129 case 0: 130 handleAdd(); 131 break; 132 case 1: 133 handleEdit(); 134 break; 135 case 2: 136 handleDelete(); 137 } 138 } 139 140 protected void handleAdd() { 141 showDialog(null); 142 } 143 144 protected void handleEdit() { 145 showDialog((IAdditionalLocation)((StructuredSelection)fContentViewer.getSelection()).iterator().next()); 146 } 147 148 protected void handleDelete() { 149 IStructuredSelection ssel = (IStructuredSelection)fContentViewer.getSelection(); 150 if (ssel.size() > 0) { 151 Object [] objects = ssel.toArray(); 152 ITarget target = getTarget(); 153 IAdditionalLocation[] dirs = new IAdditionalLocation[objects.length]; 154 System.arraycopy(objects, 0, dirs, 0, objects.length); 155 target.removeAdditionalDirectories(dirs); 156 } 157 } 158 159 private void showDialog(final IAdditionalLocation location) { 160 final ITarget model = getTarget(); 161 BusyIndicator.showWhile(fContentViewer.getTable().getDisplay(), new Runnable () { 162 public void run() { 163 LocationDialog dialog = new LocationDialog(fContentViewer.getTable() 164 .getShell(), model, location); 165 dialog.create(); 166 SWTUtil.setDialogSize(dialog, 500, -1); 167 dialog.open(); 168 } 169 }); 170 } 171 172 175 public void modelChanged(IModelChangedEvent e) { 176 if (e.getChangeType() == IModelChangedEvent.WORLD_CHANGED) { 177 handleModelEventWorldChanged(e); 178 return; 179 } 180 Object [] objects = e.getChangedObjects(); 181 if (e.getChangeType() == IModelChangedEvent.INSERT) { 182 for (int i = 0; i < objects.length; i++) { 183 if (objects[i] instanceof IAdditionalLocation) { 184 fContentViewer.add(objects[i]); 185 } 186 } 187 } else if (e.getChangeType() == IModelChangedEvent.REMOVE) { 188 for (int i = 0; i < objects.length; i++) { 189 if (objects[i] instanceof IAdditionalLocation) { 190 fContentViewer.remove(objects[i]); 191 } 192 } 193 } 194 if (e.getChangedProperty() == IAdditionalLocation.P_PATH) { 195 fContentViewer.refresh(); 196 } 197 } 198 199 202 private void handleModelEventWorldChanged(IModelChangedEvent event) { 203 fContentViewer.setInput(getTarget()); 205 refresh(); 207 } 208 209 public boolean doGlobalAction(String actionId) { 210 if (actionId.equals(ActionFactory.DELETE.getId())) { 211 handleDelete(); 212 return true; 213 } 214 if (actionId.equals(ActionFactory.CUT.getId())) { 215 handleDelete(); 216 return false; 217 } 218 if (actionId.equals(ActionFactory.PASTE.getId())) { 219 doPaste(); 220 return true; 221 } 222 return false; 223 } 224 225 protected boolean canPaste(Object target, Object [] objects) { 226 for (int i = 0; i < objects.length; i++) { 227 if (objects[i] instanceof IAdditionalLocation) 228 return true; 229 } 230 return false; 231 } 232 233 protected void doPaste(Object target, Object [] objects) { 234 for (int i = 0; i < objects.length; i++) { 235 if (objects[i] instanceof IAdditionalLocation && 236 !hasPath(((IAdditionalLocation)objects[i]).getPath())) { 237 IAdditionalLocation loc = (IAdditionalLocation)objects[i]; 238 loc.setModel(getModel()); 239 getTarget().addAdditionalDirectories(new IAdditionalLocation[] {loc}); 240 } 241 } 242 } 243 244 protected boolean hasPath(String path) { 245 Path checkPath = new Path(path); 246 IAdditionalLocation[] locs = getModel().getTarget().getAdditionalDirectories(); 247 for (int i = 0; i < locs.length; i++) { 248 if (new Path(locs[i].getPath()).equals(checkPath)) 249 return true; 250 } 251 return isTargetLocation(checkPath); 252 } 253 254 private boolean isTargetLocation(Path path) { 255 ILocationInfo info = getModel().getTarget().getLocationInfo(); 256 if (info.useDefault()) { 257 Path home = new Path(TargetPlatform.getDefaultLocation()); 258 return home.equals(path); 259 } 260 return new Path(info.getPath()).equals(path); 261 } 262 263 public void dispose() { 264 ITargetModel model = getModel(); 265 if (model != null) 266 model.removeModelChangedListener(this); 267 super.dispose(); 268 } 269 270 273 public void refresh() { 274 fContentViewer.refresh(); 275 updateButtons(); 276 super.refresh(); 277 } 278 279 protected void fillContextMenu(IMenuManager manager) { 280 IStructuredSelection ssel = (IStructuredSelection)fContentViewer.getSelection(); 281 if (ssel == null) 282 return; 283 284 Action removeAction = new Action(PDEUIMessages.ContentSection_remove) { 285 public void run() { 286 handleDelete(); 287 } 288 }; 289 removeAction.setEnabled(isEditable() && ssel.size() > 0); 290 manager.add(removeAction); 291 292 manager.add(new Separator()); 293 294 getPage().getPDEEditor().getContributor().contextMenuAboutToShow(manager); 295 } 296 297 protected void selectionChanged(IStructuredSelection selection) { 298 getPage().getPDEEditor().setSelection(selection); 299 } 300 } 301 | Popular Tags |