KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > viewer > tool > ToolManager


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.tool;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.nightlabs.editor2d.viewer.IViewer;
36 import org.nightlabs.editor2d.viewer.event.MouseEvent;
37 import org.nightlabs.editor2d.viewer.event.MouseListener;
38 import org.nightlabs.editor2d.viewer.event.MouseMoveListener;
39
40 public class ToolManager
41 implements IToolManager
42 {
43
44     public ToolManager(IViewer viewer) {
45         setViewer(viewer);
46     }
47     
48     protected int lastID = 0;
49     protected int nextID() {
50         return ++lastID;
51     }
52     
53     protected Map JavaDoc id2Tool = new HashMap JavaDoc();
54     
55     protected List JavaDoc tools = null;
56     public List JavaDoc getTools()
57     {
58         if (tools == null)
59             tools = new ArrayList JavaDoc();
60         
61         return tools;
62     }
63
64     public void addTool(ITool tool)
65     {
66         tool.setViewer(viewer);
67         int id = nextID();
68         tool.setID(id);
69         id2Tool.put(new Integer JavaDoc(id), tool);
70         getTools().add(tool);
71     }
72
73     public void removeTool(ITool tool) {
74         getTools().remove(tool);
75         id2Tool.remove(tool);
76     }
77
78     protected IViewer viewer = null;
79     public IViewer getViewer() {
80         return viewer;
81     }
82     public void setViewer(IViewer viewer)
83     {
84         if (viewer != null) {
85             viewer.getMouseManager().removeMouseMoveListener(mouseMoveListener);
86             viewer.getMouseManager().removeMouseListener(mouseListener);
87         }
88         
89         this.viewer = viewer;
90         viewer.getMouseManager().addMouseMoveListener(mouseMoveListener);
91         viewer.getMouseManager().addMouseListener(mouseListener);
92         for (Iterator JavaDoc it = getTools().iterator(); it.hasNext(); ) {
93             ITool tool = (ITool) it.next();
94             tool.setViewer(viewer);
95         }
96     }
97
98     public void setActiveTool(int id)
99     {
100         ITool tool = (ITool) id2Tool.get(new Integer JavaDoc(id));
101         if (tool != null)
102             setActiveTool(tool);
103     }
104     
105     protected ITool activeTool = null;
106     public void setActiveTool(ITool tool)
107     {
108         if (activeTool != null)
109             activeTool.deactivate();
110         
111         activeTool = tool;
112         activeTool.activate();
113     }
114     public ITool getActiveTool() {
115         return activeTool;
116     }
117         
118     protected void doMouseMoved(MouseEvent me)
119     {
120         if (activeTool != null) {
121             activeTool.mouseMoved(me);
122         }
123     }
124     
125     protected void doMouseReleased(MouseEvent me)
126     {
127         if (activeTool != null) {
128             activeTool.mouseReleased(me);
129         }
130     }
131
132     protected void doMousePressed(MouseEvent me)
133     {
134         if (activeTool != null) {
135             activeTool.mousePressed(me);
136         }
137     }
138     
139     protected MouseMoveListener mouseMoveListener = new MouseMoveListener()
140     {
141         public void mouseMoved(MouseEvent me) {
142             doMouseMoved(me);
143         }
144     };
145     
146     protected MouseListener mouseListener = new MouseListener()
147     {
148         public void mouseReleased(MouseEvent me) {
149             doMouseReleased(me);
150         }
151         public void mousePressed(MouseEvent me) {
152             doMousePressed(me);
153         }
154     };
155     
156 }
157
Popular Tags