KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > view > ElementFieldView


1 /*
2  * (c) Rob Gordon 2005.
3  */

4 package org.oddjob.designer.view;
5
6 import java.awt.Component JavaDoc;
7 import java.awt.Container JavaDoc;
8 import java.awt.GridBagConstraints JavaDoc;
9 import java.awt.GridBagLayout JavaDoc;
10 import java.awt.Insets JavaDoc;
11 import java.awt.event.FocusEvent JavaDoc;
12 import java.awt.event.FocusListener JavaDoc;
13 import java.util.Observable JavaDoc;
14 import java.util.Observer JavaDoc;
15
16 import javax.swing.JLabel JavaDoc;
17 import javax.swing.JPanel JavaDoc;
18 import javax.swing.JTextField JavaDoc;
19 import javax.swing.SwingConstants JavaDoc;
20
21 import org.oddjob.designer.Looks;
22 import org.oddjob.designer.arooa.DesignIH;
23 import org.oddjob.designer.model.DesignAttribute;
24 import org.oddjob.designer.model.ElementField;
25
26 /**
27  * A view of an ElementField.
28  * <p>
29  * An ElementField may be a reference to another type, a simple String or
30  * greater detail.
31  * <p>
32  * This view produces a view which is either:
33  * <pre>
34  * Label Title xxxxxxxxxxxxxxxxxxxxx bbbb
35  * </pre>
36  * or
37  * <pre>
38  * Label Title *********************
39  * </pre>
40  *
41  */

42 public class ElementFieldView implements ViewProducer {
43
44     private final DesignAttribute element;
45
46     private final JLabel JavaDoc label;
47     private final JTextField JavaDoc textField;
48     private Component JavaDoc detailButton;
49     
50     /**
51      * Constructor.
52      *
53      * @param elementField The ElementField being modelled.
54      */

55     public ElementFieldView(ElementField elementField) {
56         this.element = elementField.getDesignElement();
57
58         String JavaDoc title = elementField.getTitle();
59         label = new JLabel JavaDoc(ViewHelper.padLabel(title), SwingConstants.LEADING);
60
61         textField = new JTextField JavaDoc(Looks.TEXT_FIELD_SIZE);
62         updateView();
63         
64         textField.addFocusListener(new FocusListener JavaDoc() {
65             public void focusGained(FocusEvent JavaDoc e) {
66             }
67             public void focusLost(FocusEvent JavaDoc e) {
68                 element.attribute(textField.getText());
69             }
70         });
71         
72         ((Observable JavaDoc) element).addObserver(new Observer JavaDoc() {
73             public void update(Observable JavaDoc o, Object JavaDoc arg) {
74                 updateView();
75             }
76         });
77     }
78     
79     /**
80      * Called initially and then after the model has changed.
81      *
82      */

83     private void updateView() {
84         DesignIH dih = DesignIH.getHelper(element.getClass());
85         if (dih.hasDetailData(element)) {
86             textField.setText("(Edit for detail)");
87         }
88         else {
89             textField.setText(element.attribute());
90         }
91     }
92     
93     /*
94      * (non-Javadoc)
95      * @see org.oddjob.designer.view.ViewProducer#dialog()
96      */

97     public Component JavaDoc dialog() {
98         return group();
99     }
100     
101     /*
102      * (non-Javadoc)
103      * @see org.oddjob.designer.view.ViewProducer#group()
104      */

105     public Component JavaDoc group() {
106         JPanel JavaDoc panel = new JPanel JavaDoc(new GridBagLayout JavaDoc());
107         panel.setBorder(Looks.groupBorder("Value"));
108         inline(panel, 0, 0, false);
109         return panel;
110     }
111
112     /*
113      * (non-Javadoc)
114      * @see org.oddjob.designer.view.ViewProducer#detailEdit()
115      */

116     public Component JavaDoc detailEdit() {
117         throw new UnsupportedOperationException JavaDoc("Can't nest Element Fields! - and nothing else uses the detail button!");
118     }
119     
120     /*
121      * (non-Javadoc)
122      * @see org.oddjob.designer.view.ViewProducer#cell()
123      */

124     public Component JavaDoc cell() {
125         throw new UnsupportedOperationException JavaDoc("An element field can't appear in a cell - either use a group or a text field!");
126     }
127
128     /* (non-Javadoc)
129      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
130      */

131     public int inline(Container JavaDoc container, int row, int column,
132             boolean selectionInGroup) {
133         int columnCount = column;
134         
135         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
136
137         c.weightx = 1.0;
138         c.weighty = 0.0;
139         
140         c.fill = GridBagConstraints.HORIZONTAL;
141         c.anchor = GridBagConstraints.NORTHWEST;
142         c.gridx = columnCount++;
143         c.gridy = row;
144         if (selectionInGroup) {
145             c.gridwidth = 2;
146             columnCount++;
147         }
148         
149         c.insets = new Insets JavaDoc(3, 3, 3, 20);
150
151         container.add(label, c);
152         
153         c.fill = GridBagConstraints.NONE;
154         c.anchor = GridBagConstraints.NORTHWEST;
155         c.gridx = columnCount++;
156         c.gridwidth = 1;
157         c.insets = new Insets JavaDoc(3, 0, 3, 0);
158         
159         container.add(textField, c);
160         
161         if (element.hasDetail()) {
162             detailButton = ViewFactory.create(
163                     element.detail()).detailEdit();
164             c.fill = GridBagConstraints.NONE;
165             c.anchor = GridBagConstraints.NORTHWEST;
166             c.gridx = columnCount++;
167             container.add(detailButton, c);
168         }
169         
170         return row + 1;
171     }
172     
173     /*
174      * (non-Javadoc)
175      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
176      */

177     public void setEnabled(boolean enabled) {
178         textField.setEditable(enabled);
179         if (detailButton != null) {
180             detailButton.setEnabled(enabled);
181         }
182         if (!enabled) {
183             element.clear();
184         }
185     }
186         
187 }
188
Popular Tags