KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > ide > diagrams > TextTool


1 /*
2  * @(#)TextTool.java
3  *
4  * Project: JHotdraw - a GUI framework for technical drawings
5  * http://www.jhotdraw.org
6  * http://jhotdraw.sourceforge.net
7  * Copyright: © by the original author(s) and all contributors
8  * License: Lesser GNU Public License (LGPL)
9  * http://www.opensource.org/licenses/lgpl-license.html
10  */

11
12 package org.objectweb.jac.ide.diagrams;
13
14 import CH.ifa.draw.framework.DrawingEditor;
15 import CH.ifa.draw.framework.Figure;
16 import CH.ifa.draw.standard.TextHolder;
17 import org.objectweb.jac.util.Log;
18 import java.awt.Container JavaDoc;
19 import java.awt.Dimension JavaDoc;
20 import java.awt.Rectangle JavaDoc;
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.awt.event.KeyListener JavaDoc;
25 import java.awt.event.MouseEvent JavaDoc;
26
27 /**
28  * Tool to create new or edit existing text figures.
29  * The editing behavior is implemented by overlaying the
30  * Figure providing the text with a FloatingTextField.<p>
31  * A tool interaction is done once a Figure that is not
32  * a TextHolder is clicked.
33  *
34  * @see TextHolder
35  * @see FloatingTextField
36  *
37  * @version <$CURRENT_VERSION$>
38  */

39 public class TextTool extends CreationTool
40    implements ActionListener JavaDoc, KeyListener JavaDoc
41 {
42
43    private FloatingTextField fTextField;
44    private TextHolder fTypingTarget;
45
46    public TextTool(DrawingEditor newDrawingEditor, Figure prototype) {
47       super(newDrawingEditor, prototype);
48    }
49
50    /**
51     * If the pressed figure is a TextHolder it can be edited otherwise
52     * a new text figure is created.
53     */

54    public void mouseDown(MouseEvent JavaDoc e, int x, int y)
55    {
56       TextHolder textHolder = null;
57       Figure pressedFigure = drawing().findFigureInside(x, y);
58       if (pressedFigure instanceof TextHolder) {
59          textHolder = (TextHolder) pressedFigure;
60          if (!textHolder.acceptsTyping())
61             textHolder = null;
62       }
63       if (textHolder != null) {
64          beginEdit(textHolder);
65          return;
66       }
67       if (getTypingTarget() != null) {
68          endEdit();
69          editor().toolDone();
70       } else {
71          super.mouseDown(e, x, y);
72          // update view so the created figure is drawn before the floating text
73
// figure is overlaid. (Note, fDamage should be null in StandardDrawingView
74
// when the overlay figure is drawn because a JTextField cannot be scrolled)
75
view().checkDamage();
76          textHolder = (TextHolder)getCreatedFigure();
77          beginEdit(textHolder);
78       }
79    }
80
81    public void mouseDrag(MouseEvent JavaDoc e, int x, int y) {
82    }
83
84    public void mouseUp(MouseEvent JavaDoc e, int x, int y) {
85    }
86
87    /**
88     * Terminates the editing of a text figure.
89     */

90    public void deactivate() {
91       endEdit();
92       super.deactivate();
93    }
94
95    /**
96     * Sets the text cursor.
97     */

98    public void activate() {
99       super.activate();
100       view().clearSelection();
101       // JDK1.1 TEXT_CURSOR has an incorrect hot spot
102
//view().setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
103
}
104
105    /**
106     * Test whether the text tool is currently activated and is displaying
107     * a overlay TextFigure for accepting input.
108     *
109     * @return true, if the text tool has a accepting target TextFigure for its input, false otherwise
110     */

111    public boolean isActivated() {
112       return getTypingTarget() != null;
113    }
114     
115    // ActionListener
116
public void actionPerformed(ActionEvent JavaDoc e) {
117       endEdit();
118    }
119
120    // KeyListener
121
public void keyPressed(KeyEvent JavaDoc e) {
122       int code = e.getKeyCode();
123       if (code==KeyEvent.VK_ESCAPE) {
124          abortEdit();
125       }
126    }
127    public void keyReleased(KeyEvent JavaDoc e) {}
128    public void keyTyped(KeyEvent JavaDoc e) {}
129
130    protected void beginEdit(TextHolder figure) {
131       if (fTextField == null) {
132          fTextField = new FloatingTextField();
133       }
134            
135       if (figure != getTypingTarget() && getTypingTarget() != null) {
136          endEdit();
137       }
138
139       fTextField.addActionListener(this);
140       fTextField.addKeyListener(this);
141       fTextField.createOverlay((Container JavaDoc)view(), figure.getFont());
142       fTextField.setBounds(fieldBounds(figure), figure.getText());
143
144       setTypingTarget(figure);
145    }
146
147    protected void endEdit() {
148       Log.trace("diagram","End edit");
149       if (getTypingTarget() != null) {
150          if (fTextField.getText().length() > 0) {
151             getTypingTarget().setText(fTextField.getText());
152          } else {
153             drawing().orphan((Figure)getAddedFigure());
154          }
155               
156          setTypingTarget(null);
157          fTextField.endOverlay();
158          fTextField.removeActionListener(this);
159          fTextField.removeKeyListener(this);
160          // view().checkDamage();
161
}
162    }
163
164    protected void abortEdit() {
165       Log.trace("diagram","Abort edit");
166       if (getTypingTarget() != null) {
167          setTypingTarget(null);
168          fTextField.endOverlay();
169          fTextField.removeActionListener(this);
170          fTextField.removeKeyListener(this);
171          // view().checkDamage();
172
}
173    }
174
175    private Rectangle JavaDoc fieldBounds(TextHolder figure) {
176       Rectangle JavaDoc box = figure.textDisplayBox();
177       int nChars = figure.overlayColumns();
178       Dimension JavaDoc d = fTextField.getPreferredSize(nChars);
179       return new Rectangle JavaDoc(box.x, box.y, d.width, d.height);
180    }
181     
182    protected void setTypingTarget(TextHolder newTypingTarget) {
183       fTypingTarget = newTypingTarget;
184    }
185     
186    protected TextHolder getTypingTarget() {
187       return fTypingTarget;
188    }
189
190 }
191
192
Popular Tags