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 | */
|
---|
16 | package org.tp23.antinstaller.renderer.swing;
|
---|
17 |
|
---|
18 | import java.awt.BorderLayout;
|
---|
19 | import java.awt.Color;
|
---|
20 | import java.awt.Dimension;
|
---|
21 | import java.awt.event.ActionEvent;
|
---|
22 | import java.awt.event.ActionListener;
|
---|
23 | import java.awt.event.FocusAdapter;
|
---|
24 | import java.awt.event.FocusEvent;
|
---|
25 | import java.io.File;
|
---|
26 | import java.util.ResourceBundle;
|
---|
27 |
|
---|
28 | import javax.swing.JFileChooser;
|
---|
29 | import javax.swing.JPanel;
|
---|
30 |
|
---|
31 | import org.tp23.antinstaller.input.FileInput;
|
---|
32 | import org.tp23.antinstaller.input.OutputField;
|
---|
33 | import org.tp23.antinstaller.renderer.MessageRenderer;
|
---|
34 | import org.tp23.gui.GBCF;
|
---|
35 |
|
---|
36 | public class FileInputRenderer
|
---|
37 | extends SwingOutputFieldRenderer {
|
---|
38 |
|
---|
39 | private static final ResourceBundle res = ResourceBundle.getBundle("resources.LanguagePack");
|
---|
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());
|
---|
100 | inputField.setValue(selectedFile.getAbsolutePath());
|
---|
101 | inputField.setEditted(true);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | });
|
---|
105 | browseButton.setText(res.getString("selectFile"));
|
---|
106 | browseButton.setPreferredSize(new Dimension(150, SizeConstants.FIELD_HEIGHT));
|
---|
107 |
|
---|
108 | jTextField.addActionListener(new ActionListener() {
|
---|
109 | public void actionPerformed(ActionEvent e) {
|
---|
110 | updateInputField();
|
---|
111 | }
|
---|
112 | });
|
---|
113 | jTextField.addFocusListener(new FocusAdapter() {
|
---|
114 | public void focusLost(FocusEvent fe){
|
---|
115 | jTextField.setForeground(origFore);
|
---|
116 | }
|
---|
117 | });
|
---|
118 |
|
---|
119 | }
|
---|
120 | public int addSelf(JPanel content,GBCF cf, int row,boolean overflow) {
|
---|
121 | content.add(fieldLabel,cf.getCell(row,0));
|
---|
122 | content.add(browsePanel,cf.getCell(row,1));
|
---|
123 | if(overflow){
|
---|
124 | jTextField.setOverflow(SizeConstants.OVERFLOW_SHORT_FIELD_SIZE);
|
---|
125 | }
|
---|
126 | return ++row;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * renderError
|
---|
132 | */
|
---|
133 | public void renderError() {
|
---|
134 | MessageRenderer mr = ctx.getMessageRenderer();
|
---|
135 | mr.printMessage(res.getString("fileNotExist"));
|
---|
136 | this.jTextField.requestFocus();
|
---|
137 | this.jTextField.setForeground(Color.red);
|
---|
138 | }
|
---|
139 | }
|
---|