1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.renderer.swing;
17
18import java.awt.BorderLayout;
19import java.awt.Color;
20import java.awt.Dimension;
21import java.awt.event.ActionEvent;
22import java.awt.event.ActionListener;
23import java.awt.event.FocusAdapter;
24import java.awt.event.FocusEvent;
25import java.io.File;
26import java.util.ResourceBundle;
27
28import javax.swing.JFileChooser;
29import javax.swing.JPanel;
30
31import org.tp23.antinstaller.input.FileInput;
32import org.tp23.antinstaller.input.OutputField;
33import org.tp23.antinstaller.renderer.MessageRenderer;
34import org.tp23.gui.GBCF;
35
36public class FileInputRenderer
37    extends SwingOutputFieldRenderer {
38
39    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
40
41    protected FileInput inputField;
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 Color origFore = jTextField.getForeground();
48    private JPanel parent;
49
50    public FileInputRenderer() {
51    }
52    public void initComponent(JPanel parent){
53        this.parent=parent;
54        try {
55            jbInit();
56        }
57        catch(Exception e) {
58            e.printStackTrace();
59        }
60    }
61
62    public void setOutputField(OutputField inputField) {
63        this.inputField = (FileInput)inputField;
64        this.inputField.setValue(this.inputField.getDefaultValue(true));
65    }
66    public void updateInputField(){
67        if( !inputField.getDefaultValue(true).equals(jTextField.getText()) ){
68            inputField.setEditted(true);            
69        }
70        inputField.setValue(jTextField.getText());
71    }
72    public void updateDefaultValue(){
73        if(!inputField.isEditted())jTextField.setText(inputField.getDefaultValue(true));
74    }
75
76    private void jbInit() throws Exception {
77        BorderLayout bl = new BorderLayout();
78        //bl.setHgap(3);
79        browsePanel.setLayout(bl);
80        fieldLabel.setText(inputField.getDisplayText());
81        jTextField.setText(inputField.getDefaultValue(true));
82        browsePanel.add(jTextField, BorderLayout.CENTER);
83        browsePanel.add(browseButton, BorderLayout.EAST);
84        browseButton.addActionListener(new ActionListener() {
85            public void actionPerformed(ActionEvent e) {
86                File selectedFile = null;
87
88                JFileChooser chooser = new JFileChooser();
89                chooser.setFileHidingEnabled(false);
90                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
91                if (jTextField.getText() != null) {
92                    chooser.setCurrentDirectory(new File(jTextField.getText()).getParentFile());
93                }
94                int returnVal = chooser.showDialog(parent, e.getActionCommand());
95                if (returnVal == JFileChooser.APPROVE_OPTION) {
96                    selectedFile = chooser.getSelectedFile();
97                }
98                if (selectedFile != null) {
99                    jTextField.setText(selectedFile.getAbsolutePath());
00                    inputField.setValue(selectedFile.getAbsolutePath());
01                    inputField.setEditted(true);
02                }
03            }
04        });
05        browseButton.setText(res.getString("selectFile"));
06        browseButton.setPreferredSize(new Dimension(150, SizeConstants.FIELD_HEIGHT));
07
08        jTextField.addActionListener(new ActionListener() {
09            public void actionPerformed(ActionEvent e) {
10                updateInputField();
11            }
12        });
13        jTextField.addFocusListener(new FocusAdapter() {
14            public void focusLost(FocusEvent fe){
15                jTextField.setForeground(origFore);
16            }
17        });
18
19    }
20    public int addSelf(JPanel content,GBCF cf,  int row,boolean overflow) {
21        content.add(fieldLabel,cf.getCell(row,0));
22        content.add(browsePanel,cf.getCell(row,1));
23        if(overflow){
24            jTextField.setOverflow(SizeConstants.OVERFLOW_SHORT_FIELD_SIZE);
25        }
26        return ++row;
27    }
28
29
30    /**
31     * renderError
32     */
33    public void renderError() {
34        MessageRenderer mr = ctx.getMessageRenderer();
35        mr.printMessage(res.getString("fileNotExist"));
36        this.jTextField.requestFocus();
37        this.jTextField.setForeground(Color.red);
38    }
39}
40