source: release-kits/lirk3/bin/ant-installer/src/org/tp23/antinstaller/renderer/swing/FileInputRenderer.java@ 14982

Last change on this file since 14982 was 14982, checked in by oranfry, 16 years ago

initial import of LiRK3

File size: 4.3 KB
Line 
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());
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}
Note: See TracBrowser for help on using the repository browser.