KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)FloatingTextField.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 java.awt.Container JavaDoc;
15 import java.awt.Dimension JavaDoc;
16 import java.awt.Font JavaDoc;
17 import java.awt.Rectangle JavaDoc;
18 import java.awt.event.ActionListener JavaDoc;
19 import java.awt.event.KeyListener JavaDoc;
20 import javax.swing.JTextField JavaDoc;
21
22 /**
23  * A text field overlay that is used to edit a TextFigure.
24  * A FloatingTextField requires a two step initialization:
25  * In a first step the overlay is created and in a
26  * second step it can be positioned.
27  *
28  * @see CH.ifa.draw.figures.TextFigure
29  *
30  * @version <$CURRENT_VERSION$>
31  */

32 public class FloatingTextField {
33
34    private JTextField JavaDoc fEditWidget;
35    private Container JavaDoc fContainer;
36
37    public FloatingTextField() {
38       fEditWidget = new JTextField JavaDoc(20);
39    }
40
41    /**
42     * Creates the overlay for the given Component.
43     */

44    public void createOverlay(Container JavaDoc container) {
45       createOverlay(container, null);
46    }
47
48    /**
49     * Creates the overlay for the given Container using a
50     * specific font.
51     */

52    public void createOverlay(Container JavaDoc container, Font JavaDoc font) {
53       container.add(fEditWidget, 0);
54       if (font != null) {
55          fEditWidget.setFont(font);
56       }
57       fContainer = container;
58    }
59
60    /**
61     * Adds an action listener
62     */

63    public void addActionListener(ActionListener JavaDoc listener) {
64       fEditWidget.addActionListener(listener);
65    }
66
67    /**
68     * Remove an action listener
69     */

70    public void removeActionListener(ActionListener JavaDoc listener) {
71       fEditWidget.removeActionListener(listener);
72    }
73
74    /**
75     * Adds an action listener
76     */

77    public void addKeyListener(KeyListener JavaDoc listener) {
78       fEditWidget.addKeyListener(listener);
79    }
80
81    /**
82     * Remove an action listener
83     */

84    public void removeKeyListener(KeyListener JavaDoc listener) {
85       fEditWidget.removeKeyListener(listener);
86    }
87
88    /**
89     * Positions the overlay.
90     */

91    public void setBounds(Rectangle JavaDoc r, String JavaDoc text) {
92       fEditWidget.setText(text);
93       fEditWidget.setBounds(r.x, r.y, r.width, r.height);
94       fEditWidget.setVisible(true);
95       fEditWidget.selectAll();
96       fEditWidget.requestFocus();
97    }
98
99    /**
100     * Gets the text contents of the overlay.
101     */

102    public String JavaDoc getText() {
103       return fEditWidget.getText();
104    }
105
106    /**
107     * Gets the preferred size of the overlay.
108     */

109    public Dimension JavaDoc getPreferredSize(int cols) {
110       fEditWidget.setColumns(cols);
111       return fEditWidget.getPreferredSize();
112    }
113
114    /**
115     * Removes the overlay.
116     */

117    public void endOverlay() {
118       fContainer.requestFocus();
119       if (fEditWidget != null) {
120          fEditWidget.setVisible(false);
121          fContainer.remove(fEditWidget);
122
123          Rectangle JavaDoc bounds = fEditWidget.getBounds();
124          fContainer.repaint(bounds.x, bounds.y, bounds.width, bounds.height);
125       }
126    }
127 }
128
129
Popular Tags