1
16package org.tp23.antinstaller.renderer.swing;
17
18import java.awt.BorderLayout;
19import java.awt.Dimension;
20import java.awt.event.ActionEvent;
21import java.awt.event.ActionListener;
22import java.io.File;
23import java.util.ResourceBundle;
24
25import javax.swing.JFileChooser;
26import javax.swing.JPanel;
27
28import org.tp23.antinstaller.input.DirectoryInput;
29import org.tp23.antinstaller.input.OutputField;
30import org.tp23.gui.GBCF;
31import org.tp23.gui.widget.DefaultingDirectoryChooser;
32
33public class DirectoryInputRenderer
34 extends SwingOutputFieldRenderer {
35
36 private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
37
38 private static final String EMPTY_STRING = "";
39 protected DirectoryInput inputField;
40 private boolean createMode;
41 private DefaultingDirectoryChooser chooser = null;
42
43 protected AILabel fieldLabel = new AILabel();
44 protected AIShortTextField jTextField = new AIShortTextField();
45 protected AIButton browseButton = new AIButton();
46 protected JPanel browsePanel = new JPanel();
47 private JPanel parent;
48
49 public DirectoryInputRenderer() {
50 }
51 public void initComponent(JPanel parent){
52 this.parent = parent;
53 try {
54 jbInit();
55 }
56 catch(Exception e) {
57 e.printStackTrace();
58 }
59 }
60
61 public void setOutputField(OutputField inputField) {
62 this.inputField = (DirectoryInput)inputField;
63 this.inputField.setValue(this.inputField.getDefaultValue(true));
64 this.createMode = OutputField.isTrue(this.inputField.getCreate());
65
66 }
67 public void updateInputField(){
68 if( !inputField.getDefaultValue(true).equals(jTextField.getText()) ){
69 inputField.setEditted(true);
70 }
71 inputField.setValue(jTextField.getText());
72 }
73 public void updateDefaultValue(){
74 if(!inputField.isEditted())jTextField.setText(inputField.getDefaultValue(true));
75 }
76
77 private void jbInit() throws Exception {
78 BorderLayout bl = new BorderLayout();
79 browsePanel.setLayout(bl);
81 fieldLabel.setText(inputField.getDisplayText());
82 jTextField.setText(inputField.getDefaultValue(true));
83 browsePanel.add(jTextField, BorderLayout.CENTER);
84 browsePanel.add(browseButton, BorderLayout.EAST);
85 browseButton.addActionListener(new ActionListener() {
86 public void actionPerformed(ActionEvent e) {
87 File selectedFile = null;
88 if(chooser==null){
89 chooser = new DefaultingDirectoryChooser(createMode);
90 chooser.setFileHidingEnabled(false);
91 }
92
93 String dirPath = jTextField.getText();
95 if( dirPath == null ) {
96 dirPath = EMPTY_STRING;
97 }
98 dirPath = dirPath.trim();
99
00 if( dirPath.length() == 0 ) {
01 dirPath = inputField.getDefaultValue(true);
02 jTextField.setText(dirPath);
03 }
04 chooser.setDefaultDirectory(new File(dirPath));
05
06 int returnVal = chooser.showDialog(parent, e.getActionCommand());
07 if (returnVal == JFileChooser.APPROVE_OPTION) {
08 selectedFile = chooser.getSelectedFile();
09 }
10 if (selectedFile != null) {
11 jTextField.setText(selectedFile.getAbsolutePath());
12 inputField.setValue(selectedFile.getAbsolutePath());
13 inputField.setEditted(true);
14 }
15 }
16 });
17 browseButton.setText(res.getString("selectFolder"));
18 browseButton.setPreferredSize(new Dimension(150, SizeConstants.FIELD_HEIGHT));
19
20 jTextField.addActionListener(new ActionListener() {
21 public void actionPerformed(ActionEvent e) {
22 updateInputField();
23 }
24 });
25
26 }
27 public int addSelf(JPanel content,GBCF cf, int row,boolean overflow) {
28 content.add(fieldLabel,cf.getCell(row,0));
29 content.add(browsePanel,cf.getCell(row,1));
30 if(overflow){
31 jTextField.setOverflow(SizeConstants.OVERFLOW_SHORT_FIELD_SIZE);
32 }
33 return ++row;
34 }
35
36
37
40 public void renderError() {
41 jTextField.requestFocus();
42 }
43}
44