1 4 package org.oddjob.designer.view; 5 6 import java.awt.Component ; 7 import java.awt.Container ; 8 import java.awt.GridBagConstraints ; 9 import java.awt.GridBagLayout ; 10 import java.awt.Insets ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.FocusEvent ; 13 import java.awt.event.FocusListener ; 14 import java.io.File ; 15 import java.util.Observable ; 16 import java.util.Observer ; 17 18 import javax.swing.AbstractAction ; 19 import javax.swing.BoxLayout ; 20 import javax.swing.JButton ; 21 import javax.swing.JFileChooser ; 22 import javax.swing.JLabel ; 23 import javax.swing.JPanel ; 24 import javax.swing.JTextField ; 25 import javax.swing.SwingConstants ; 26 27 import org.oddjob.designer.Looks; 28 import org.oddjob.designer.model.FileSelection; 29 30 33 public class FileSelectionView implements ViewProducer { 34 35 private final FileSelection fileSelection; 36 private final JLabel label; 37 private final JTextField textField; 38 private JButton detailButton; 39 40 public FileSelectionView(FileSelection fs) { 41 this.fileSelection = fs; 42 43 String title = fileSelection.getTitle(); 44 StringBuffer paddedTitle = new StringBuffer (); 45 paddedTitle.append(title); 46 for (int i = title.length(); i < Looks.LABEL_SIZE; ++i) { 47 paddedTitle.append(' '); 48 } 49 label = new JLabel (paddedTitle.toString(), SwingConstants.LEADING); 50 51 textField = new JTextField (Looks.TEXT_FIELD_SIZE); 52 updateView(); 53 54 textField.addFocusListener(new FocusListener () { 55 public void focusGained(FocusEvent e) { 56 } 57 public void focusLost(FocusEvent e) { 58 fileSelection.setFile(new File (textField.getText())); 59 } 60 }); 61 62 fileSelection.addObserver(new Observer () { 63 public void update(Observable o, Object arg) { 64 updateView(); 65 } 66 }); 67 68 detailButton = new JButton (); 69 detailButton.setAction(new AbstractAction ("...") { 70 public void actionPerformed(ActionEvent e) { 71 JFileChooser chooser = new JFileChooser (); 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 chosen = chooser.getSelectedFile(); 80 fileSelection.setFile(chosen); 81 } 82 } 83 }); 84 85 } 86 87 public Component dialog() { 88 JPanel form = new JPanel (); 89 form.setLayout(new GridBagLayout ()); 90 form.setBorder(Looks.groupBorder(fileSelection.getTitle())); 91 92 inline(form, 0, 0, false); 93 94 return form; 95 } 96 97 public Component group() { 98 throw new UnsupportedOperationException ("Only available inline - use a group."); 99 } 100 101 104 public Component detailEdit() { 105 throw new UnsupportedOperationException ("Can't be wrapped by a FieldElement."); 106 } 107 108 private void updateView() { 110 File 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 cell() { 121 JPanel panel = new JPanel (); 122 panel.setLayout(new BoxLayout (panel, BoxLayout.X_AXIS)); 123 124 detailButton.setMargin(new Insets (0, 0, 0, 0)); 125 panel.add(textField); 126 panel.add(detailButton); 127 return panel; 128 } 129 130 133 public int inline(Container container, int row, int column, 134 boolean selectionInGroup) { 135 int columnCount = column; 136 137 GridBagConstraints c = new GridBagConstraints (); 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 (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 (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 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 |