KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.designer.view;
5
6 import java.awt.BorderLayout JavaDoc;
7 import java.awt.Component JavaDoc;
8 import java.awt.Container JavaDoc;
9 import java.awt.Dialog JavaDoc;
10 import java.awt.Dimension JavaDoc;
11 import java.awt.Frame JavaDoc;
12 import java.awt.GridBagConstraints JavaDoc;
13 import java.awt.Insets JavaDoc;
14 import java.awt.Window JavaDoc;
15 import java.awt.event.ActionEvent JavaDoc;
16 import java.awt.event.FocusEvent JavaDoc;
17 import java.awt.event.FocusListener JavaDoc;
18 import java.util.Observable JavaDoc;
19 import java.util.Observer JavaDoc;
20
21 import javax.swing.AbstractAction JavaDoc;
22 import javax.swing.BoxLayout JavaDoc;
23 import javax.swing.JButton JavaDoc;
24 import javax.swing.JPanel JavaDoc;
25 import javax.swing.JScrollPane JavaDoc;
26 import javax.swing.JTextArea JavaDoc;
27 import javax.swing.JTextField JavaDoc;
28 import javax.swing.SwingUtilities JavaDoc;
29
30 import org.oddjob.designer.Looks;
31 import org.oddjob.designer.model.TextInput;
32
33 /**
34  *
35  */

36 public class TextInputView implements ViewProducer {
37         
38     private final TextInput textInput;
39     private final JTextField JavaDoc text;
40     private final JTextArea JavaDoc textArea;
41     private final JButton JavaDoc button;
42     
43     public TextInputView(TextInput ti) {
44         this.textInput = ti;
45         text = new JTextField JavaDoc(Looks.TEXT_FIELD_SIZE);
46         text.addFocusListener(new FocusListener JavaDoc() {
47             public void focusGained(FocusEvent JavaDoc e) {
48             }
49             public void focusLost(FocusEvent JavaDoc e) {
50                 textInput.setText(text.getText());
51             }
52         });
53         
54         textArea = new JTextArea JavaDoc();
55         textArea.addFocusListener(new FocusListener JavaDoc() {
56             public void focusGained(FocusEvent JavaDoc e) {
57             }
58             public void focusLost(FocusEvent JavaDoc e) {
59                 textInput.setText(textArea.getText());
60             }
61         });
62         
63         textInput.addObserver(new Observer JavaDoc() {
64             public void update(Observable JavaDoc o, Object JavaDoc arg) {
65                 updateView();
66             }
67         });
68
69         button = new JButton JavaDoc();
70         button.setAction(new AbstractAction JavaDoc("...") {
71             public void actionPerformed(ActionEvent JavaDoc e) {
72                 Window JavaDoc w = SwingUtilities.windowForComponent(button);
73                 Component JavaDoc form = ViewFactory.create(textInput).dialog();
74     
75                 ValueDialog valueDialog = null;
76                 if (w == null) {
77                     valueDialog = new ValueDialog(form);
78                 }
79                 else {
80                     if (w instanceof Frame JavaDoc) {
81                         valueDialog = new ValueDialog((Frame JavaDoc) w, form);
82                     } else {
83                         valueDialog = new ValueDialog((Dialog JavaDoc) w, form);
84                     }
85                 }
86                 valueDialog.pack();
87                 valueDialog.setVisible(true);
88                 text.setText(textInput.getText());
89             }
90         });
91         updateView();
92     }
93     
94     public Component JavaDoc dialog() {
95         return group();
96     }
97     
98     public Component JavaDoc group() {
99         JScrollPane JavaDoc scroll = new JScrollPane JavaDoc();
100         scroll.setViewportView(textArea);
101
102         JPanel JavaDoc form = new JPanel JavaDoc(new BorderLayout JavaDoc());
103         form.setPreferredSize(new Dimension JavaDoc(
104                 Looks.DETAIL_USABLE_WIDTH - 30, 200));
105         form.setBorder(Looks.groupBorder(textInput.getTitle()));
106         form.add(scroll, BorderLayout.CENTER);
107         return form;
108     }
109
110     /* (non-Javadoc)
111      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
112      */

113     public Component JavaDoc detailEdit() {
114         return ViewHelper.createDetailButton(textInput);
115     }
116     
117     public Component JavaDoc cell() {
118         JPanel JavaDoc panel = new JPanel JavaDoc();
119         panel.setLayout(new BoxLayout JavaDoc(panel, BoxLayout.X_AXIS));
120         
121         button.setMargin(new Insets JavaDoc(0, 0, 0, 0));
122         panel.add(text);
123         panel.add(button);
124         return panel;
125     }
126     
127     private void updateView() {
128         text.setText(textInput.getText());
129         textArea.setText(textInput.getText());
130     }
131     
132     /* (non-Javadoc)
133      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
134      */

135     public int inline(Container JavaDoc container, int row, int column,
136             boolean selectionInGroup) {
137         GridBagConstraints JavaDoc c = new GridBagConstraints JavaDoc();
138
139         c.weightx = 1.0;
140         c.weighty = 0.0;
141         
142         c.fill = GridBagConstraints.HORIZONTAL;
143         c.anchor = GridBagConstraints.NORTHWEST;
144         
145         c.gridx = column;
146         c.gridy = row;
147         
148         c.gridwidth = GridBagConstraints.REMAINDER;
149                 
150         c.insets = new Insets JavaDoc(3, 3, 3, 3);
151
152         container.add(group(), c);
153         
154         return row + 1;
155     }
156
157     /* (non-Javadoc)
158      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
159      */

160     public void setEnabled(boolean enabled) {
161         text.setEnabled(enabled);
162         if (!enabled) {
163             textInput.setText("");
164         }
165     }
166     
167 }
168
Popular Tags