KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > SelectionManager


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.viewer;
28
29 import java.awt.Color JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.LinkedList JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.Map JavaDoc;
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 //extends AbstractTypeSafeSelectionSupport
49
{
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 JavaDoc<DrawComponent> selectedDrawComponents = null;
63     public List JavaDoc<DrawComponent> getSelectedDrawComponents()
64     {
65         if (selectedDrawComponents == null)
66             selectedDrawComponents = new LinkedList JavaDoc<DrawComponent>();
67         
68         return selectedDrawComponents;
69     }
70
71     protected Collection JavaDoc<ISelectionChangedListener> selectionListener = null;
72     protected Collection JavaDoc<ISelectionChangedListener> getSelectionListener()
73     {
74         if (selectionListener == null)
75             selectionListener = new LinkedList JavaDoc<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 JavaDoc<ISelectionChangedListener> it = getSelectionListener().iterator(); it.hasNext(); )
94         {
95             ISelectionChangedListener l = it.next();
96             l.selectionChanged(evt);
97         }
98     }
99     
100     protected Map JavaDoc<DrawComponent, DrawComponent> dc2SelectionDC = new HashMap JavaDoc<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 JavaDoc<DrawComponent> dcs, boolean repaint)
151     {
152         getSelectedDrawComponents().addAll(dcs);
153         fireSelectionChanged();
154         
155         for (Iterator JavaDoc<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 JavaDoc<DrawComponent> dcs)
167     {
168         addSelectedDrawComponents(dcs, true);
169     }
170     
171     public void removeSelectedDrawComponents(Collection JavaDoc<DrawComponent> dcs, boolean repaint)
172     {
173         getSelectedDrawComponents().removeAll(dcs);
174         fireSelectionChanged();
175         
176         for (Iterator JavaDoc<DrawComponent> it = dcs.iterator(); it.hasNext(); ) {
177             DrawComponent dc = it.next();
178 // dc.setRenderMode(dc.getRenderModeManager().getCurrentRenderMode());
179
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 JavaDoc<DrawComponent> dcs)
192     {
193         removeSelectedDrawComponents(dcs, true);
194     }
195     
196     public void clearSelection(boolean repaint)
197     {
198         for (Iterator JavaDoc<DrawComponent> it = getSelectedDrawComponents().iterator(); it.hasNext(); ) {
199             DrawComponent dc = it.next();
200 // dc.setRenderMode(dc.getRenderModeManager().getCurrentRenderMode());
201
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