1 27 28 package org.nightlabs.editor2d.actions; 29 30 import java.util.ArrayList ; 31 import java.util.Collection ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 36 import org.eclipse.gef.EditPart; 37 import org.eclipse.gef.GraphicalViewer; 38 import org.eclipse.gef.RootEditPart; 39 import org.eclipse.gef.ui.actions.SelectionAction; 40 import org.eclipse.jface.viewers.IStructuredSelection; 41 import org.eclipse.jface.viewers.StructuredSelection; 42 import org.eclipse.swt.widgets.Shell; 43 44 import org.nightlabs.editor2d.AbstractEditor; 45 import org.nightlabs.editor2d.DrawComponent; 46 import org.nightlabs.editor2d.Layer; 47 import org.nightlabs.editor2d.MultiLayerDrawComponent; 48 import org.nightlabs.editor2d.edit.LayerEditPart; 49 50 public abstract class AbstractEditorSelectionAction 51 extends SelectionAction 52 { 53 54 public AbstractEditorSelectionAction(AbstractEditor editor, int style) { 55 super(editor, style); 56 } 57 58 public AbstractEditorSelectionAction(AbstractEditor editor) { 59 super(editor); 60 } 61 62 protected abstract boolean calculateEnabled(); 63 64 public boolean isActiveEditor() 65 { 66 if (getWorkbenchPart().getSite().getWorkbenchWindow().getActivePage().getActiveEditor().equals(getEditor())) { 67 return true; 68 } 69 return false; 70 } 71 72 77 public AbstractEditor getEditor() { 78 return (AbstractEditor) getWorkbenchPart(); 79 } 80 81 86 public MultiLayerDrawComponent getMultiLayerDrawComponent() 87 { 88 return getEditor().getMultiLayerDrawComponent(); 89 } 90 91 101 public boolean selectionContains(Class clazz, int amount, boolean model) 102 { 103 if (!getSelectedObjects().isEmpty()) 104 { 105 int counter = 0; 106 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) 107 { 108 EditPart editPart = (EditPart) it.next(); 109 Class c = null; 110 if (!model) 111 c = editPart.getClass(); 112 else 113 c = editPart.getModel().getClass(); 114 115 if (clazz.isAssignableFrom(c)) { 116 counter++; 117 if (amount == counter) 118 return true; 119 } 120 } 121 } 122 return false; 123 } 124 125 129 public boolean selectionContains(Class clazz, boolean model) { 130 return selectionContains(clazz, 1, model); 131 } 132 133 protected static List EMPTY_LIST = new LinkedList (); 134 135 143 public List getSelection(Class clazz, boolean model) 144 { 145 if (!getSelectedObjects().isEmpty()) 146 { 147 List selection = new ArrayList (); 148 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) 149 { 150 EditPart editPart = (EditPart) it.next(); 151 Class c = null; 152 if (!model) 153 c = editPart.getClass(); 154 else 155 c = editPart.getModel().getClass(); 156 157 if (clazz.isAssignableFrom(c)) 158 { 159 if (!model) 160 selection.add(clazz.cast(editPart)); 161 else 162 selection.add(clazz.cast(editPart.getModel())); 163 } 164 } 165 return selection; 166 } 167 return EMPTY_LIST; 168 } 169 170 180 public List getSelection(Collection excludeClasses, boolean model) 181 { 182 if (!getSelectedObjects().isEmpty()) 183 { 184 List selection = new ArrayList (); 185 for (Iterator it = getSelectedObjects().iterator(); it.hasNext(); ) 186 { 187 EditPart editPart = (EditPart) it.next(); 188 Class c = null; 189 if (!model) 190 c = editPart.getClass(); 191 else 192 c = editPart.getModel().getClass(); 193 194 for (Iterator itExclude = excludeClasses.iterator(); itExclude.hasNext(); ) 195 { 196 Class clazz = (Class ) itExclude.next(); 197 198 if (clazz.isAssignableFrom(c)) 199 { 200 if (!model) { 201 if (selection.contains(editPart)) 202 selection.remove(editPart); 203 } 204 else { 205 if (selection.contains(editPart.getModel())) 206 selection.remove(editPart.getModel()); 207 } 208 } 209 else { 210 if (!model) 211 selection.add(editPart); 212 else 213 selection.add(editPart.getModel()); 214 } 215 } 216 } 217 return selection; 218 } 219 return EMPTY_LIST; 220 } 221 222 protected Collection defaultEditPartExcludeList = null; 223 protected Collection defaultModelExcludeList = null; 224 public Collection getDefaultExcludeList(boolean model) 225 { 226 if (!model) { 227 if (defaultEditPartExcludeList == null) { 228 defaultEditPartExcludeList = new LinkedList (); 229 defaultEditPartExcludeList.add(RootEditPart.class); 230 defaultEditPartExcludeList.add(LayerEditPart.class); 231 } 232 return defaultEditPartExcludeList; 233 } 234 else { 235 if (defaultModelExcludeList == null) { 236 defaultModelExcludeList = new LinkedList (); 237 defaultModelExcludeList.add(MultiLayerDrawComponent.class); 238 defaultModelExcludeList.add(Layer.class); 239 } 240 return defaultModelExcludeList; 241 } 242 } 243 244 public List getDefaultSelection(boolean model) 245 { 246 return getSelection(getDefaultExcludeList(model), model); 247 } 248 249 public IStructuredSelection getStructuredSelection() { 250 return (IStructuredSelection) getSelection(); 251 } 252 253 public EditPart getPrimarySelected() { 254 return (EditPart) getStructuredSelection().getFirstElement(); 255 } 256 257 262 public GraphicalViewer getGraphicalViewer() { 263 return getEditor().getOutlineGraphicalViewer(); 264 } 265 266 272 public EditPart getEditPart(DrawComponent model) { 273 return (EditPart) getGraphicalViewer().getEditPartRegistry().get(model); 274 } 275 276 281 public List getEditParts(List drawComponents) 282 { 283 List editParts = new ArrayList (); 284 for (Iterator it = drawComponents.iterator(); it.hasNext(); ) 285 { 286 DrawComponent dc = (DrawComponent) it.next(); 287 EditPart ep = getEditPart(dc); 288 editParts.add(ep); 289 } 290 return editParts; 291 } 292 293 298 public void selectEditPart(List drawComponents) 299 { 300 List editParts = getEditParts(drawComponents); 301 getGraphicalViewer().setSelection(new StructuredSelection(editParts)); 302 } 303 304 308 public Shell getShell() { 309 return getEditor().getSite().getShell(); 310 } 311 312 public void dispose() 313 { 314 super.dispose(); 315 } 316 317 } 318 | Popular Tags |