KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionEvent JavaDoc;
12 import java.awt.event.FocusEvent JavaDoc;
13 import java.awt.event.FocusListener JavaDoc;
14 import java.io.File JavaDoc;
15 import java.util.Observable JavaDoc;
16 import java.util.Observer JavaDoc;
17
18 import javax.swing.AbstractAction JavaDoc;
19 import javax.swing.BoxLayout JavaDoc;
20 import javax.swing.JButton JavaDoc;
21 import javax.swing.JFileChooser JavaDoc;
22 import javax.swing.JLabel JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JTextField JavaDoc;
25 import javax.swing.SwingConstants JavaDoc;
26
27 import org.oddjob.designer.Looks;
28 import org.oddjob.designer.model.FileSelection;
29
30 /**
31  * Produces views for FileSelection.
32  */

33 public class FileSelectionView implements ViewProducer {
34         
35     private final FileSelection fileSelection;
36     private final JLabel JavaDoc label;
37     private final JTextField JavaDoc textField;
38     private JButton JavaDoc detailButton;
39     
40     public FileSelectionView(FileSelection fs) {
41         this.fileSelection = fs;
42
43         String JavaDoc title = fileSelection.getTitle();
44         StringBuffer JavaDoc paddedTitle = new StringBuffer JavaDoc();
45         paddedTitle.append(title);
46         for (int i = title.length(); i < Looks.LABEL_SIZE; ++i) {
47             paddedTitle.append(' ');
48         }
49         label = new JLabel JavaDoc(paddedTitle.toString(), SwingConstants.LEADING);
50
51         textField = new JTextField JavaDoc(Looks.TEXT_FIELD_SIZE);
52         updateView();
53         
54         textField.addFocusListener(new FocusListener JavaDoc() {
55             public void focusGained(FocusEvent JavaDoc e) {
56             }
57             public void focusLost(FocusEvent JavaDoc e) {
58                 fileSelection.setFile(new File JavaDoc(textField.getText()));
59             }
60         });
61         
62         fileSelection.addObserver(new Observer JavaDoc() {
63             public void update(Observable JavaDoc o, Object JavaDoc arg) {
64                 updateView();
65             }
66         });
67         
68         detailButton = new JButton JavaDoc();
69         detailButton.setAction(new AbstractAction JavaDoc("...") {
70             public void actionPerformed(ActionEvent JavaDoc e) {
71                 JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
72                 if (fileSelection.getFile() != null) {
73                     chooser.setCurrentDirectory(fileSelection.getFile().getParentFile());
74                 }
75                 
76                 int option = chooser.showOpenDialog(detailButton);
77                 
78                 if (option == JFileChooser.APPROVE_OPTION) {
79                     File JavaDoc chosen = chooser.getSelectedFile();
80                     fileSelection.setFile(chosen);
81                 }
82             }
83         });
84         
85     }
86     
87     public Component JavaDoc dialog() {
88         JPanel JavaDoc form = new JPanel JavaDoc();
89         form.setLayout(new GridBagLayout JavaDoc());
90         form.setBorder(Looks.groupBorder(fileSelection.getTitle()));
91         
92         inline(form, 0, 0, false);
93         
94         return form;
95     }
96     
97     public Component JavaDoc group() {
98         throw new UnsupportedOperationException JavaDoc("Only available inline - use a group.");
99     }
100
101     /* (non-Javadoc)
102      * @see org.oddjob.designer.view.ViewProducer#detailEdit(org.oddjob.designer.view.ActionWrapper)
103      */

104     public Component JavaDoc detailEdit() {
105         throw new UnsupportedOperationException JavaDoc("Can't be wrapped by a FieldElement.");
106     }
107     
108     // cell and inline have a lot in common.
109
private void updateView() {
110         File JavaDoc file = fileSelection.getFile();
111         if (file == null) {
112             textField.setText("");
113         }
114         else {
115             textField.setText(file.getPath());
116         }
117         
118     }
119     
120     public Component JavaDoc cell() {
121         JPanel JavaDoc panel = new JPanel JavaDoc();
122         panel.setLayout(new BoxLayout JavaDoc(panel, BoxLayout.X_AXIS));
123         
124         detailButton.setMargin(new Insets JavaDoc(0, 0, 0, 0));
125         panel.add(textField);
126         panel.add(detailButton);
127         return panel;
128     }
129     
130     /* (non-Javadoc)
131      * @see org.oddjob.designer.view.ViewProducer#inline(java.awt.Container, int, int, boolean)
132      */

133     public int inline(Container JavaDoc container, int row, int column,
134             boolean selectionInGroup) {
135         int columnCount = column;
136         
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         c.gridx = columnCount++;
145         c.gridy = row;
146         if (selectionInGroup) {
147             c.gridwidth = 2;
148             columnCount++;
149         }
150         
151         c.insets = new Insets JavaDoc(3, 3, 3, 20);
152
153         container.add(label, c);
154         
155         c.fill = GridBagConstraints.NONE;
156         c.anchor = GridBagConstraints.NORTHEAST;
157         c.gridx = columnCount++;
158         c.gridwidth = 1;
159         c.insets = new Insets JavaDoc(3, 0, 3, 0);
160         
161         container.add(textField, c);
162         
163         if (detailButton != null) {
164             c.fill = GridBagConstraints.NONE;
165             c.anchor = GridBagConstraints.NORTHWEST;
166             c.gridx = columnCount++;
167             
168             container.add(detailButton, c);
169         }
170         return row + 1;
171     }
172
173     /* (non-Javadoc)
174      * @see org.oddjob.designer.view.ViewProducer#setEnabled(boolean)
175      */

176     public void setEnabled(boolean enabled) {
177         textField.setEditable(enabled);
178         detailButton.setEnabled(enabled);
179         if (!enabled) {
180             fileSelection.setFile(null);
181         }
182     }
183     
184 }
185
Popular Tags