1 17 package org.eclipse.emf.mapping.presentation; 18 19 20 import java.util.Arrays ; 21 import java.util.Collection ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 25 import org.eclipse.jface.action.Action; 26 import org.eclipse.jface.action.IMenuManager; 27 import org.eclipse.jface.action.IStatusLineManager; 28 import org.eclipse.jface.action.IToolBarManager; 29 import org.eclipse.jface.action.Separator; 30 import org.eclipse.jface.resource.ImageDescriptor; 31 import org.eclipse.jface.viewers.ISelection; 32 import org.eclipse.jface.viewers.ISelectionChangedListener; 33 import org.eclipse.jface.viewers.IStructuredSelection; 34 import org.eclipse.jface.viewers.SelectionChangedEvent; 35 import org.eclipse.jface.viewers.StructuredSelection; 36 import org.eclipse.jface.viewers.TreeViewer; 37 import org.eclipse.swt.events.MouseAdapter; 38 import org.eclipse.swt.events.MouseEvent; 39 import org.eclipse.swt.widgets.Tree; 40 import org.eclipse.swt.widgets.Widget; 41 42 import org.eclipse.emf.common.notify.AdapterFactory; 43 import org.eclipse.emf.common.util.TreeIterator; 44 import org.eclipse.emf.edit.provider.AdapterFactoryTreeIterator; 45 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; 46 import org.eclipse.emf.mapping.Mapping; 47 import org.eclipse.emf.mapping.domain.MappingDomain; 48 49 50 class SimpleMappedObjectViewer extends TreeViewer 51 { 52 protected SimpleMappedObjectViewer otherViewer; 53 protected MappingDomain mappingDomain; 54 protected AdapterFactory adapterFactory; 55 protected boolean isTop; 56 57 protected class SelectObjectAction extends Action 58 { 59 protected Object objectToSelect; 60 61 public SelectObjectAction(String text, ImageDescriptor imageDescriptor) 62 { 63 super(text, imageDescriptor); 64 setEnabled(false); 65 } 66 67 public void setObjectToSelect(Object objectToSelect) 68 { 69 this.objectToSelect = objectToSelect; 70 setEnabled(objectToSelect != null); 71 } 72 73 public void run() 74 { 75 setSelection(new StructuredSelection(new Object [] { objectToSelect }), true); 76 } 77 } 78 79 protected SelectObjectAction selectNextMappedObject; 80 protected SelectObjectAction selectPreviousMappedObject; 81 protected SelectObjectAction selectNextUnmappedObject; 82 protected SelectObjectAction selectPreviousUnmappedObject; 83 protected Action filterMappedObjects; 84 protected Action selectOtherMappedObjects; 85 86 public SimpleMappedObjectViewer(MappingDomain domain, Tree tree, boolean isTop) 87 { 88 super(tree); 89 this.mappingDomain = domain; 90 this.isTop = isTop; 91 tree.addMouseListener 92 (new MouseAdapter() 93 { 94 public void mouseDoubleClick(MouseEvent event) 95 { 96 selectOtherMappedObjects(); 97 } 98 }); 99 100 addSelectionChangedListener 101 (new ISelectionChangedListener() 102 { 103 public void selectionChanged(SelectionChangedEvent event) 104 { 105 updateActions(); 106 } 107 }); 108 } 109 110 public void setAdapterFactory(AdapterFactory adapterFactory) 111 { 112 this.adapterFactory = adapterFactory; 113 } 114 115 public Action getFilterMappedObjectsAction() 116 { 117 return filterMappedObjects; 118 } 119 120 public void setOtherViewer(SimpleMappedObjectViewer otherViewer) 121 { 122 this.otherViewer = otherViewer; 123 } 124 125 protected ISelection doGetSelection() 126 { 127 return super.getSelection(); 128 } 129 130 public void updateActions() 131 { 132 Collection selection = ((IStructuredSelection)doGetSelection()).toList(); 133 boolean isReady = false; 134 Object previousMapped = null; 135 Object previousUnmapped = null; 136 Object nextMapped = null; 137 Object nextUnmapped = null; 138 boolean selectionHasMappings = false; 139 LOOP: for (Iterator i = ((IStructuredItemContentProvider)getInput()).getElements(getInput()).iterator(); i.hasNext(); ) 140 { 141 Object object = i.next(); 142 for (TreeIterator treeIterator = new AdapterFactoryTreeIterator(adapterFactory, object); treeIterator.hasNext(); ) 143 { 144 Object child = treeIterator.next(); 145 if (selection.contains(child)) 146 { 147 isReady = true; 148 if (!selectionHasMappings) 149 { 150 for (Iterator mappings = mappingDomain.getMappingRoot().getMappings(child).iterator(); mappings.hasNext(); ) 151 { 152 Mapping mapping = (Mapping)mappings.next(); 153 if (mapping.getInputs().contains(child) && !mapping.getOutputs().isEmpty() || 154 mapping.getOutputs().contains(child) && !mapping.getInputs().isEmpty()) 155 { 156 selectionHasMappings = true; 157 break; 158 } 159 } 160 } 161 } 162 else if (mappingDomain.getMappingRoot().getMappings(child).isEmpty()) 163 { 164 if (isReady) 165 { 166 if (nextUnmapped == null) 167 { 168 nextUnmapped = child; 169 if (nextMapped != null) 170 { 171 break LOOP; 172 } 173 } 174 } 175 else 176 { 177 previousUnmapped = child; 178 } 179 } 180 else 181 { 182 if (isReady) 183 { 184 if (nextMapped == null) 185 { 186 nextMapped = child; 187 if (nextUnmapped != null) 188 { 189 break LOOP; 190 } 191 } 192 } 193 else 194 { 195 previousMapped = child; 196 } 197 } 198 } 199 } 200 selectNextMappedObject.setObjectToSelect(nextMapped); 201 selectPreviousMappedObject.setObjectToSelect(previousMapped); 202 selectNextUnmappedObject.setObjectToSelect(nextUnmapped); 203 selectPreviousUnmappedObject.setObjectToSelect(previousUnmapped); 204 selectOtherMappedObjects.setEnabled(selectionHasMappings); 205 } 206 207 public void selectOtherMappedObjects() 208 { 209 Collection result = new HashSet (); 210 for (Iterator i = ((IStructuredSelection)doGetSelection()).iterator(); i.hasNext(); ) 211 { 212 Object object = i.next(); 213 Collection mappings = mappingDomain.getMappingRoot().getMappings(object); 214 if (mappingDomain.getMappingRoot().isInputObject(object)) 215 { 216 for (Iterator j = mappings.iterator(); j.hasNext(); ) 217 { 218 Mapping mapping = (Mapping)j.next(); 219 result.addAll(mapping.getOutputs()); 220 } 221 } 222 else if (mappingDomain.getMappingRoot().isOutputObject(object)) 223 { 224 for (Iterator j = mappings.iterator(); j.hasNext(); ) 225 { 226 Mapping mapping = (Mapping)j.next(); 227 result.addAll(mapping.getInputs()); 228 } 229 } 230 } 231 otherViewer.setSelection(new StructuredSelection(result.toArray()), true); 232 } 233 234 public ISelection getSelection() 235 { 236 ISelection basicSelection = doGetSelection(); 237 if (otherViewer == null) 238 { 239 return basicSelection; 240 } 241 else 242 { 243 return new ComposedSelection(basicSelection, new ISelection [] {basicSelection, otherViewer.doGetSelection()}); 244 } 245 } 246 247 void makeContributions(IMenuManager menuManager, IToolBarManager toolBarManager, IStatusLineManager statusLineManager) 248 { 249 String label = isTop ? "Top" : "Bottom"; 250 String oppositeLabel = isTop ? "Bottom" : "Top"; 251 252 selectNextMappedObject = 253 new SelectObjectAction 254 (MappingUIPlugin.getPlugin().getString("_UI_NextMappedObject_menu_item"), 255 MappingUIPlugin.getPlugin().getImageDescriptor("full/elcl16/SelectNext" + label + "MappedObject")); 256 selectNextMappedObject.setToolTipText(MappingUIPlugin.getPlugin().getString("_UI_NextMappedObject_description")); 257 selectNextMappedObject.setHoverImageDescriptor 258 (MappingUIPlugin.getPlugin().getImageDescriptor("full/clcl16/SelectNext" + label + "MappedObject")); 259 selectNextMappedObject.setDisabledImageDescriptor 260 (MappingUIPlugin.getPlugin().getImageDescriptor("full/dlcl16/SelectNext" + label + "MappedObject")); 261 toolBarManager.add(selectNextMappedObject); 262 menuManager.add(selectNextMappedObject); 263 264 selectPreviousMappedObject = 265 new SelectObjectAction 266 (MappingUIPlugin.getPlugin().getString("_UI_PreviousMappedObject_menu_item"), 267 MappingUIPlugin.getPlugin().getImageDescriptor("full/elcl16/SelectPrevious" + label + "MappedObject")); 268 selectPreviousMappedObject.setToolTipText(MappingUIPlugin.getPlugin().getString("_UI_PreviousMappedObject_description")); 269 selectPreviousMappedObject.setHoverImageDescriptor 270 (MappingUIPlugin.getPlugin().getImageDescriptor("full/clcl16/SelectPrevious" + label + "MappedObject")); 271 selectPreviousMappedObject.setDisabledImageDescriptor 272 (MappingUIPlugin.getPlugin().getImageDescriptor("full/dlcl16/SelectPrevious" + label + "MappedObject")); 273 toolBarManager.add(selectPreviousMappedObject); 274 menuManager.add(selectPreviousMappedObject); 275 276 menuManager.add(new Separator()); 277 278 selectNextUnmappedObject = 279 new SelectObjectAction 280 (MappingUIPlugin.getPlugin().getString("_UI_NextUnmappedObject_menu_item"), 281 MappingUIPlugin.getPlugin().getImageDescriptor("full/elcl16/SelectNext" + label + "UnmappedObject")); 282 selectNextUnmappedObject.setToolTipText(MappingUIPlugin.getPlugin().getString("_UI_NextUnmappedObject_description")); 283 selectNextUnmappedObject.setHoverImageDescriptor 284 (MappingUIPlugin.getPlugin().getImageDescriptor("full/clcl16/SelectNext" + label + "UnmappedObject")); 285 selectNextUnmappedObject.setDisabledImageDescriptor 286 (MappingUIPlugin.getPlugin().getImageDescriptor("full/dlcl16/SelectNext" + label + "UnmappedObject")); 287 toolBarManager.add(selectNextUnmappedObject); 288 menuManager.add(selectNextUnmappedObject); 289 290 selectPreviousUnmappedObject = 291 new SelectObjectAction 292 (MappingUIPlugin.getPlugin().getString("_UI_PreviousUnmappedObject_menu_item"), 293 MappingUIPlugin.getPlugin().getImageDescriptor("full/elcl16/SelectPrevious" + label + "UnmappedObject")); 294 selectPreviousUnmappedObject.setToolTipText(MappingUIPlugin.getPlugin().getString("_UI_PreviousUnmappedObject_description")); 295 selectPreviousUnmappedObject.setHoverImageDescriptor 296 (MappingUIPlugin.getPlugin().getImageDescriptor("full/clcl16/SelectPrevious" + label + "UnmappedObject")); 297 selectPreviousUnmappedObject.setDisabledImageDescriptor 298 (MappingUIPlugin.getPlugin().getImageDescriptor("full/dlcl16/SelectPrevious" + label + "UnmappedObject")); 299 toolBarManager.add(selectPreviousUnmappedObject); 300 menuManager.add(selectPreviousUnmappedObject); 301 302 menuManager.add(new Separator()); 303 304 filterMappedObjects = 305 new Action 306 (MappingUIPlugin.getPlugin().getString("_UI_FilterMappedObjects_menu_item"), 307 MappingUIPlugin.getPlugin().getImageDescriptor("full/elcl16/HideAllMappedObjects")) 308 { 309 public void run() 310 { 311 preserveState(); 312 Object input = getInput(); 313 setInput(input); 314 restoreState(); 315 316 updateActions(); 317 } 318 public void setChecked(boolean checked) 319 { 320 super.setChecked(checked); 321 setToolTipText 322 (MappingUIPlugin.getPlugin().getString 323 (checked ? "_UI_FilterMappedObjects_checked_description" : "_UI_FilterMappedObjects_unchecked_description")); 324 } 325 }; 326 filterMappedObjects.setChecked(false); 327 filterMappedObjects.setHoverImageDescriptor 328 (MappingUIPlugin.getPlugin().getImageDescriptor("full/clcl16/HideAllMappedObjects")); 329 filterMappedObjects.setDisabledImageDescriptor 330 (MappingUIPlugin.getPlugin().getImageDescriptor("full/dlcl16/HideAllMappedObjects")); 331 toolBarManager.add(filterMappedObjects); 332 menuManager.add(filterMappedObjects); 333 334 menuManager.add(new Separator()); 335 336 selectOtherMappedObjects = 337 new Action 338 (MappingUIPlugin.getPlugin().getString("_UI_SelectOtherMappedObjects_menu_item"), 339 MappingUIPlugin.getPlugin().getImageDescriptor("full/elcl16/Select" + oppositeLabel + "MappedObjects")) 340 { 341 public void run() 342 { 343 selectOtherMappedObjects(); 344 } 345 }; 346 selectOtherMappedObjects.setEnabled(false); 347 selectOtherMappedObjects.setToolTipText(MappingUIPlugin.getPlugin().getString("_UI_SelectOtherMappedObjects_description")); 348 selectOtherMappedObjects.setHoverImageDescriptor 349 (MappingUIPlugin.getPlugin().getImageDescriptor("full/clcl16/Select" + oppositeLabel + "MappedObjects")); 350 selectOtherMappedObjects.setDisabledImageDescriptor 351 (MappingUIPlugin.getPlugin().getImageDescriptor("full/dlcl16/Select" + oppositeLabel + "MappedObjects")); 352 toolBarManager.add(selectOtherMappedObjects); 353 menuManager.add(selectOtherMappedObjects); 354 355 toolBarManager.update(true); 356 menuManager.update(true); 357 } 358 359 protected Collection expandedObjects = new HashSet (); 360 protected Collection selectedObjects = new HashSet (); 361 362 public void preserveState() 363 { 364 Collection oldExpandedObjects = expandedObjects; 365 expandedObjects = new HashSet (Arrays.asList(getExpandedElements())); 366 oldExpandedObjects.removeAll(expandedObjects); 367 for (Iterator i = oldExpandedObjects.iterator(); i.hasNext(); ) 368 { 369 Object oldExpandedObject = i.next(); 370 Widget item = findItem(oldExpandedObject); 371 if (item == null) 372 { 373 expandedObjects.add(oldExpandedObject); 374 } 375 } 376 377 Collection oldSelectedObjects = selectedObjects; 378 selectedObjects = new HashSet (((IStructuredSelection)getSelection()).toList()); 379 if (selectedObjects.isEmpty()) 380 { 381 selectedObjects = oldSelectedObjects; 382 } 383 } 384 385 public void restoreState() 386 { 387 setExpandedElements(expandedObjects.toArray()); 388 setSelection(new StructuredSelection(selectedObjects.toArray()), true); 389 } 390 } 391 | Popular Tags |