KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.log4j.Logger;
30 import org.eclipse.swt.graphics.Color;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.nightlabs.editor2d.DrawComponent;
34 import org.nightlabs.editor2d.render.RenderModeManager;
35
36 public abstract class AbstractCanvasComposite
37 extends Composite
38 implements IViewer
39 {
40     public static final Logger LOGGER = Logger.getLogger(AbstractCanvasComposite.class.getName());
41     
42     public AbstractCanvasComposite(Composite parent, int style, DrawComponent dc) {
43         this(parent, style, dc, true);
44     }
45
46     public AbstractCanvasComposite(Composite parent, int style, DrawComponent dc,
47             boolean autoScroll)
48     {
49         super(parent, style);
50         this.autoScroll = autoScroll;
51         this.drawComponent = dc;
52         GridLayout layout = new GridLayout(1, true);
53         setLayout(layout);
54         init(this);
55     }
56     
57     protected boolean autoScroll = true;
58     protected IAutoScrollSupport autoScrollSupport = null;
59     
60     protected void init(Composite parent)
61     {
62         // TODO: uncomment if used as plugin, renderMode Registration now done by TestDialog
63
// RenderModeManager renderMan = RendererRegistry.sharedInstance().getRenderModeManager();
64
// drawComponent.setRenderModeManager(renderMan);
65

66         getHitTestManager();
67         canvas = createCanvas(this);
68         mouseManager = initMouseManager(this);
69         getZoomSupport().addZoomListener(zoomListener);
70         updateCanvas();
71         
72         if (autoScroll) {
73             autoScrollSupport = initAutoScrollSupport();
74         }
75     }
76         
77     protected DrawComponent drawComponent;
78         
79     /**
80      *
81      * @return the DrawComponent to draw
82      */

83     public DrawComponent getDrawComponent() {
84         return drawComponent;
85     }
86     
87     /**
88      *
89      * @param drawComponent the DrawComponent to draw
90      */

91     public void setDrawComponent(DrawComponent drawComponent) {
92         this.drawComponent = drawComponent;
93         hitTestManager = new HitTestManager(drawComponent);
94     }
95                 
96     /**
97      * updates the Canvas
98      *
99      */

100     public void updateCanvas()
101     {
102         if (canvas != null)
103             canvas.repaint();
104     }
105     
106     /**
107      * zooms the viewer to the given zoomFactor
108      * @param zoomFactor the zoomFactor to zoom (1.0 = 100%)
109      */

110     public void setZoom(double zoomFactor) {
111         setZoom(zoomFactor, false);
112     }
113
114     protected ICanvas canvas;
115     public ICanvas getCanvas() {
116         return canvas;
117     }
118     
119     protected void setZoom(double zoomFactor, boolean internal)
120     {
121         if (internal) {
122             getZoomSupport().setZoom(zoomFactor);
123         }
124         double zoom = getZoom();
125         if (canvas != null) {
126             canvas.setScale(zoom);
127             canvas.repaint();
128         }
129     }
130         
131     /**
132      *
133      * @return the current zoomFactor as double (100% = 1.0)
134      */

135     public double getZoom() {
136         return getZoomSupport().getZoom();
137     }
138             
139     public static Color defaultBgColor = new Color(null, 255, 255, 255);
140     protected Color bgColor = defaultBgColor;
141     
142     /**
143      *
144      * @return the Background Color of the Viewer
145      */

146     public Color getBgColor() {
147         return bgColor;
148     }
149     
150     /**
151      *
152      * @param bgColor the Background Color of the Viewer
153      */

154     public void setBgColor(Color bgColor) {
155         this.bgColor = bgColor;
156         canvas.setBackground(bgColor.getRed(), bgColor.getGreen(), bgColor.getBlue());
157     }
158
159 // protected ISelectionSupport selectionSupport = new SelectionSupport();
160
protected SelectionManager selectionManager = new SelectionManager(this);
161     
162     /**
163      *
164      * @see org.nightlabs.editor2d.viewer.IViewer#getSelectionManager()
165      */

166     public SelectionManager getSelectionManager() {
167         return selectionManager;
168     }
169
170     protected IZoomSupport zoomSupport = null;
171     public IZoomSupport getZoomSupport()
172     {
173         if (zoomSupport == null)
174             zoomSupport = new ZoomSupport(getViewport());
175         
176         return zoomSupport;
177     }
178
179     /**
180      *
181      * @see org.nightlabs.editor2d.viewer.IViewer#getRenderModeManager()
182      */

183     public RenderModeManager getRenderModeManager() {
184         return getDrawComponent().getRenderModeManager();
185     }
186     
187     protected IZoomListener zoomListener = new IZoomListener()
188     {
189         public void zoomChanged(double zoom) {
190             setZoom(zoom, false);
191         }
192     };
193     
194     protected IMouseManager mouseManager = null;
195     public IMouseManager getMouseManager() {
196         return mouseManager;
197     }
198
199 // public DrawComponent findObjectAt(int x, int y)
200
// {
201
// return ViewerUtil.findObjectAt(getDrawComponent(), x, y, conditional, excludeList);
202
// }
203
//
204
// protected Collection excludeList = null;
205
// public void setExcludeList(Collection c) {
206
// excludeList = c;
207
// }
208
//
209
// protected IDrawComponentConditional conditional = null;
210
// public void setConditional(IDrawComponentConditional conditional) {
211
// this.conditional = conditional;
212
// }
213

214     protected HitTestManager hitTestManager = null;
215     public HitTestManager getHitTestManager()
216     {
217         if (hitTestManager == null)
218             hitTestManager = new HitTestManager(getDrawComponent());
219         
220         return hitTestManager;
221     }
222     
223     protected abstract IMouseManager initMouseManager(IViewer viewer);
224     protected abstract ICanvas createCanvas(Composite parent);
225     protected abstract IAutoScrollSupport initAutoScrollSupport();
226         
227 }
228
Popular Tags