1 26 27 package org.nightlabs.editor2d.viewer; 28 29 import java.awt.Color ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.LinkedList ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 import org.apache.log4j.Logger; 38 import org.nightlabs.editor2d.DrawComponent; 39 import org.nightlabs.editor2d.RectangleDrawComponent; 40 import org.nightlabs.editor2d.ShapeDrawComponent; 41 import org.nightlabs.editor2d.impl.RectangleDrawComponentImpl; 42 import org.nightlabs.editor2d.impl.ShapeDrawComponentImpl; 43 import org.nightlabs.editor2d.j2d.GeneralShape; 44 import org.nightlabs.editor2d.viewer.event.ISelectionChangedListener; 45 import org.nightlabs.editor2d.viewer.event.SelectionEvent; 46 47 public class SelectionManager 48 { 50 public static final Logger LOGGER = Logger.getLogger(SelectionManager.class); 51 52 public SelectionManager(IViewer viewer) 53 { 54 this.viewer = viewer; 55 } 56 57 protected IViewer viewer = null; 58 public IViewer getViewer() { 59 return viewer; 60 } 61 62 protected List <DrawComponent> selectedDrawComponents = null; 63 public List <DrawComponent> getSelectedDrawComponents() 64 { 65 if (selectedDrawComponents == null) 66 selectedDrawComponents = new LinkedList <DrawComponent>(); 67 68 return selectedDrawComponents; 69 } 70 71 protected Collection <ISelectionChangedListener> selectionListener = null; 72 protected Collection <ISelectionChangedListener> getSelectionListener() 73 { 74 if (selectionListener == null) 75 selectionListener = new LinkedList <ISelectionChangedListener>(); 76 77 return selectionListener; 78 } 79 80 public void addSelectionChangedListener(ISelectionChangedListener l) 81 { 82 getSelectionListener().add(l); 83 } 84 85 public void removeSelectionChangedListener(ISelectionChangedListener l) 86 { 87 getSelectionListener().remove(l); 88 } 89 90 protected void fireSelectionChanged() 91 { 92 SelectionEvent evt = new SelectionEvent(getSelectedDrawComponents()); 93 for (Iterator <ISelectionChangedListener> it = getSelectionListener().iterator(); it.hasNext(); ) 94 { 95 ISelectionChangedListener l = it.next(); 96 l.selectionChanged(evt); 97 } 98 } 99 100 protected Map <DrawComponent, DrawComponent> dc2SelectionDC = new HashMap <DrawComponent, DrawComponent>(); 101 public void addSelectedDrawComponent(DrawComponent dc, boolean repaint) 102 { 103 if (!getSelectedDrawComponents().contains(dc)) 104 { 105 getSelectedDrawComponents().add(dc); 106 fireSelectionChanged(); 107 108 DrawComponent selectionDC = createSelectionDrawComponent(dc); 109 dc2SelectionDC.put(dc, selectionDC); 110 addToTempContent(selectionDC); 111 112 if (repaint) 113 getViewer().getBufferedCanvas().repaint(); 114 115 LOGGER.debug("dc "+dc.getName()+" selected"); 116 } 117 } 118 119 public void addSelectedDrawComponent(DrawComponent dc) 120 { 121 addSelectedDrawComponent(dc, true); 122 } 123 124 public void removeSelectedDrawComponent(DrawComponent dc, boolean repaint) 125 { 126 if (getSelectedDrawComponents().contains(dc)) 127 { 128 getSelectedDrawComponents().remove(dc); 129 fireSelectionChanged(); 130 131 DrawComponent selectionDC = dc2SelectionDC.get(dc); 132 if (selectionDC != null) { 133 removeTempContent(selectionDC); 134 } else { 135 LOGGER.debug("selectionDC for "+dc.getName()+" not in dc2SelectionDC"); 136 } 137 138 if (repaint) 139 getViewer().getBufferedCanvas().repaint(); 140 141 LOGGER.debug("dc "+dc.getName()+" deselected"); 142 } 143 } 144 145 public void removeSelectedDrawComponent(DrawComponent dc) 146 { 147 removeSelectedDrawComponent(dc, true); 148 } 149 150 public void addSelectedDrawComponents(Collection <DrawComponent> dcs, boolean repaint) 151 { 152 getSelectedDrawComponents().addAll(dcs); 153 fireSelectionChanged(); 154 155 for (Iterator <DrawComponent> it = dcs.iterator(); it.hasNext(); ) { 156 DrawComponent dc = it.next(); 157 DrawComponent selectionDC = createSelectionDrawComponent(dc); 158 dc2SelectionDC.put(dc, selectionDC); 159 addToTempContent(selectionDC); 160 } 161 162 if (repaint) 163 getViewer().getBufferedCanvas().repaint(); 164 } 165 166 public void addSelectedDrawComponents(Collection <DrawComponent> dcs) 167 { 168 addSelectedDrawComponents(dcs, true); 169 } 170 171 public void removeSelectedDrawComponents(Collection <DrawComponent> dcs, boolean repaint) 172 { 173 getSelectedDrawComponents().removeAll(dcs); 174 fireSelectionChanged(); 175 176 for (Iterator <DrawComponent> it = dcs.iterator(); it.hasNext(); ) { 177 DrawComponent dc = it.next(); 178 DrawComponent selectionDC = dc2SelectionDC.get(dc); 180 if (selectionDC != null) { 181 removeTempContent(selectionDC); 182 } else { 183 LOGGER.debug("selectionDC for "+dc.getName()+" not in dc2SelectionDC"); 184 } 185 } 186 187 if (repaint) 188 getViewer().getBufferedCanvas().repaint(); 189 } 190 191 public void removeSelectedDrawComponents(Collection <DrawComponent> dcs) 192 { 193 removeSelectedDrawComponents(dcs, true); 194 } 195 196 public void clearSelection(boolean repaint) 197 { 198 for (Iterator <DrawComponent> it = getSelectedDrawComponents().iterator(); it.hasNext(); ) { 199 DrawComponent dc = it.next(); 200 DrawComponent selectionDC = dc2SelectionDC.get(dc); 202 if (selectionDC != null) { 203 removeTempContent(selectionDC); 204 } else { 205 LOGGER.debug("selectionDC for "+dc.getName()+" not in dc2SelectionDC"); 206 } 207 } 208 209 getSelectedDrawComponents().clear(); 210 fireSelectionChanged(); 211 212 if (repaint) 213 getViewer().getBufferedCanvas().repaint(); 214 } 215 216 public void clearSelection() 217 { 218 clearSelection(true); 219 } 220 221 public boolean contains(DrawComponent dc) 222 { 223 return getSelectedDrawComponents().contains(dc); 224 } 225 226 protected DrawComponent createSelectionDrawComponent(DrawComponent dc) 227 { 228 if (dc instanceof ShapeDrawComponent) { 229 ShapeDrawComponent original = (ShapeDrawComponent) dc; 230 ShapeDrawComponent sdc = new ShapeDrawComponentImpl(); 231 sdc.setGeneralShape((GeneralShape)original.getGeneralShape().clone()); 232 sdc.setLineWidth(3); 233 sdc.setFill(false); 234 sdc.setLineColor(Color.YELLOW); 235 return sdc; 236 } 237 else { 238 RectangleDrawComponent rect = new RectangleDrawComponentImpl(); 239 rect.setGeneralShape(new GeneralShape(dc.getBounds())); 240 rect.setLineWidth(3); 241 rect.setFill(false); 242 rect.setLineColor(Color.YELLOW); 243 return rect; 244 } 245 } 246 247 protected void addToTempContent(DrawComponent dc) 248 { 249 getViewer().getBufferedCanvas().getTempContentManager().addToTempContent(dc); 250 } 251 252 protected void removeTempContent(DrawComponent dc) 253 { 254 getViewer().getBufferedCanvas().getTempContentManager().removeFromTempContent(dc); 255 } 256 } 257 | Popular Tags |