KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > BoundsWin


1 package jimm.datavision.gui;
2 import jimm.datavision.*;
3 import jimm.datavision.field.Field;
4 import jimm.datavision.gui.cmd.BoundsCommand;
5 import jimm.util.I18N;
6 import java.awt.BorderLayout JavaDoc;
7 import javax.swing.*;
8
9 /**
10  * A field bounds (position and size) editing dialog box.
11  *
12  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
13  */

14 public class BoundsWin extends EditWin implements FieldWalker {
15
16 protected static final int TEXT_FIELD_COLS = 8;
17
18 protected Field field;
19 protected jimm.datavision.field.Rectangle origBounds;
20 protected jimm.datavision.field.Rectangle fieldBounds;
21 protected JTextField x_text;
22 protected JTextField y_text;
23 protected JTextField w_text;
24 protected JTextField h_text;
25
26 /**
27  * Constructor.
28  *
29  * @param designer the window to which this dialog belongs
30  * @param f a field from which we will take the bounds
31  */

32 public BoundsWin(Designer designer, Field f) {
33     super(designer, I18N.get("BoundsWin.title"), "BoundsCommand.name");
34
35     field = f;
36     origBounds = field.getBounds();
37     fieldBounds = new jimm.datavision.field.Rectangle(origBounds);
38
39     buildWindow();
40     pack();
41     setVisible(true);
42 }
43
44 /**
45  * Builds the window contents.
46  */

47 protected void buildWindow() {
48     JPanel editorPanel = buildBoundsEditor();
49
50     // OK, Apply, Revert, and Cancel Buttons
51
JPanel buttonPanel = closeButtonPanel();
52
53     // Add values and buttons to window
54
getContentPane().add(editorPanel, BorderLayout.CENTER);
55     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
56 }
57
58 protected JPanel buildBoundsEditor() {
59     JPanel panel = new JPanel();
60     panel.setLayout(new BorderLayout JavaDoc());
61
62     x_text = addCoord(panel, I18N.get("BoundsWin.x"), fieldBounds.x,
63               BorderLayout.WEST);
64     y_text = addCoord(panel, I18N.get("BoundsWin.y"), fieldBounds.y,
65               BorderLayout.NORTH);
66     w_text = addCoord(panel, I18N.get("BoundsWin.width"), fieldBounds.width,
67               BorderLayout.EAST);
68     h_text = addCoord(panel, I18N.get("BoundsWin.height"), fieldBounds.height,
69               BorderLayout.SOUTH);
70
71     return panel;
72 }
73
74 protected JTextField addCoord(JPanel parent, String JavaDoc label, double value,
75                   String JavaDoc compassPoint)
76 {
77     JPanel coordPanel = new JPanel();
78     coordPanel.add(new JLabel(label));
79     JTextField text = new JTextField("" + value, TEXT_FIELD_COLS);
80     coordPanel.add(text);
81     parent.add(coordPanel, compassPoint);
82     return text;
83 }
84
85 protected void fillCoords(jimm.datavision.field.Rectangle bounds) {
86     x_text.setText("" + bounds.x);
87     y_text.setText("" + bounds.y);
88     w_text.setText("" + bounds.width);
89     h_text.setText("" + bounds.height);
90 }
91
92 protected void doSave() {
93     fieldBounds.setBounds(Double.parseDouble(x_text.getText()),
94               Double.parseDouble(y_text.getText()),
95               Double.parseDouble(w_text.getText()),
96               Double.parseDouble(h_text.getText()));
97
98     if (designer.countSelectedFields() == 0
99     || field == field.getReport().getDefaultField()) // "==", not "equals"
100
step(field);
101     else // Call step() for all selected fields
102
designer.withSelectedFieldsDo(this);
103 }
104
105 /**
106  * Creates and performs a command that gives the bounds to the specified
107  * field.
108  *
109  * @param f the field
110  */

111 public void step(Field f) {
112     BoundsCommand command = new BoundsCommand(f, fieldBounds);
113     command.perform();
114     commands.add(command);
115 }
116
117 protected void doRevert() {
118     fillCoords(origBounds);
119 }
120
121 }
122
Popular Tags