1 17 package org.eclipse.emf.mapping.presentation; 18 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.eclipse.jface.viewers.ISelection; 25 import org.eclipse.jface.viewers.IStructuredSelection; 26 import org.eclipse.jface.viewers.StructuredSelection; 27 28 29 public class ComposedSelection implements IStructuredSelection, IComposedSelection 30 { 31 protected ISelection [] selections; 32 protected ISelection primarySelection; 33 34 public ComposedSelection(ISelection primarySelection, ISelection [] selections) 35 { 36 this.primarySelection = primarySelection; 37 this.selections = selections; 38 } 39 40 public boolean isEmpty() 41 { 42 return primarySelection == null || primarySelection.isEmpty(); 43 } 44 45 48 public Iterator getElements() 49 { 50 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).iterator() : null; 51 } 52 53 public Iterator iterator() 54 { 55 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).iterator() : null; 56 } 57 58 public Object [] toArray() 59 { 60 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).toArray() : null; 61 } 62 63 public List toList() 64 { 65 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).toList() : null; 66 } 67 68 public Object getFirstElement() 69 { 70 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).getFirstElement() : null; 71 } 72 73 public Object getPrimaryItem() 74 { 75 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).getFirstElement() : null; 76 } 77 78 public int size() 79 { 80 return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).size() : 0; 81 } 82 83 public ISelection getSelection() 84 { 85 return primarySelection; 86 } 87 88 public ISelection [] getSelections() 89 { 90 return selections; 91 } 92 93 public IStructuredSelection getCombinedSelection() 94 { 95 List result = new ArrayList (); 96 for (int i = 0; i < selections.length; ++i) 97 { 98 ISelection selection = selections[i]; 99 if (selection instanceof IStructuredSelection) 100 { 101 for (Iterator subselections = ((IStructuredSelection)selection).iterator(); subselections.hasNext(); ) 102 { 103 result.add(subselections.next()); 104 } 105 } 106 } 107 108 return new StructuredSelection(result); 109 } 110 111 public boolean equals(Object that) 112 { 113 if (this == that) 114 { 115 return true; 116 } 117 else if (!(that instanceof IComposedSelection)) 118 { 119 return false; 120 } 121 else 122 { 123 IComposedSelection thatComposedSelection = (IComposedSelection)that; 124 ISelection thatPrimarySelection = thatComposedSelection.getSelection(); 125 ISelection [] thatSelections = thatComposedSelection.getSelections(); 126 127 if (selections.length == thatSelections.length) 128 { 129 for (int i = 0; i < selections.length; ++i) 130 { 131 if (selections[i] == primarySelection) 132 { 133 if (thatSelections[i] != thatPrimarySelection) 134 { 135 return false; 136 } 137 } 138 139 if (!selections[i].equals(thatSelections[i])) 140 { 141 return false; 142 } 143 } 144 145 return true; 146 } 147 else 148 { 149 return false; 150 } 151 } 152 } 153 154 public String toString() 155 { 156 StringBuffer result = new StringBuffer (); 157 result.append("ComposedSection("); 158 if (selections != null) 159 { 160 for (int i = 0; i < selections.length; ++i) 161 { 162 if (selections[i] == primarySelection) 163 { 164 result.append('('); 165 result.append(selections[i]); 166 result.append(')'); 167 } 168 else 169 { 170 result.append(selections[i]); 171 } 172 } 173 } 174 result.append(')'); 175 return result.toString(); 176 } 177 } 178 | Popular Tags |