1 package rero.dck.items; 2 3 import java.awt.*; 4 import java.awt.event.*; 5 6 import javax.swing.*; 7 import javax.swing.event.*; 8 9 import java.io.*; 10 11 import rero.dck.*; 12 import rero.config.*; 13 14 public class FileInput extends SuperInput implements ActionListener 15 { 16 protected JLabel label; 17 protected JTextField text; 18 protected SmallButton button; 19 protected String value; 20 protected JFileChooser chooser; 21 22 protected boolean directory; 23 24 public FileInput(String _variable, String _value, String _label, char mnemonic, boolean _directory, int inset) 25 { 26 text = new JTextField(); 27 button = new SmallButton(text.getBorder(), "Click to open a file chooser"); 28 label = new JLabel(_label); 29 30 button.addActionListener(this); 31 32 setLayout(new BorderLayout(2, 2)); 33 34 36 add(label, BorderLayout.WEST); 37 add(text, BorderLayout.CENTER); 38 39 add(button, BorderLayout.EAST); 40 41 label.setLabelFor(button); 42 label.setDisplayedMnemonic(mnemonic); 43 44 variable = _variable; 45 value = _value; 46 47 directory = _directory; 48 49 setBorder(BorderFactory.createEmptyBorder(0, 0, 0, inset)); 50 } 51 52 public void actionPerformed(ActionEvent ev) 53 { 54 if (chooser == null) 55 { 56 chooser = new JFileChooser(); 57 } 58 59 if (directory) 60 { 61 chooser.setApproveButtonText("Select Directory"); 62 chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 63 } 64 else 65 { 66 chooser.setApproveButtonText("Select File"); 67 } 68 69 if (chooser.showDialog(this, null) == JFileChooser.APPROVE_OPTION) 70 { 71 text.setText(chooser.getSelectedFile().getAbsolutePath()); 72 text.requestFocus(); 73 } 75 76 notifyParent(); 77 } 78 79 public void save() 80 { 81 ClientState.getClientState().setString(getVariable(), text.getText()); 82 } 83 84 public int getEstimatedWidth() 85 { 86 return (int)label.getPreferredSize().getWidth(); 87 } 88 89 public void setAlignWidth(int width) 90 { 91 label.setPreferredSize(new Dimension(width, 0)); 92 revalidate(); 93 } 94 95 public JComponent getComponent() 96 { 97 return this; 98 } 99 100 public void refresh() 101 { 102 text.setText(ClientState.getClientState().getString(getVariable(), value)); 103 } 104 } 105 106 107 | Popular Tags |