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.FlowLayout;
|
---|
20 | import javax.swing.BoxLayout;
|
---|
21 | import java.awt.Dimension;
|
---|
22 | import java.awt.event.ActionEvent;
|
---|
23 | import java.awt.event.ActionListener;
|
---|
24 | import java.awt.Insets;
|
---|
25 | import java.io.File;
|
---|
26 | import java.util.ResourceBundle;
|
---|
27 |
|
---|
28 | import javax.swing.JFileChooser;
|
---|
29 | import javax.swing.JPanel;
|
---|
30 | import javax.swing.JLabel;
|
---|
31 |
|
---|
32 | import org.tp23.antinstaller.input.DirectoryInput;
|
---|
33 | import org.tp23.antinstaller.input.OutputField;
|
---|
34 | import org.tp23.gui.GBCF;
|
---|
35 | import org.tp23.gui.widget.DefaultingDirectoryChooser;
|
---|
36 |
|
---|
37 | public class DirectoryInputRenderer
|
---|
38 | extends SwingOutputFieldRenderer {
|
---|
39 |
|
---|
40 | private static final ResourceBundle res = ResourceBundle.getBundle("resources.LanguagePack");
|
---|
41 |
|
---|
42 | private static final String EMPTY_STRING = "";
|
---|
43 | protected DirectoryInput inputField;
|
---|
44 | private boolean createMode;
|
---|
45 | private DefaultingDirectoryChooser chooser = null;
|
---|
46 |
|
---|
47 | protected JLabel fieldLabel = new AILabel();
|
---|
48 | protected AIShortTextField jTextField = new AIShortTextField();
|
---|
49 | protected AIButton browseButton = new AIButton();
|
---|
50 | protected JPanel browsePanel = new JPanel();
|
---|
51 | private JPanel parent;
|
---|
52 |
|
---|
53 | public DirectoryInputRenderer() {
|
---|
54 | }
|
---|
55 | public void initComponent(JPanel parent){
|
---|
56 | this.parent = parent;
|
---|
57 | try {
|
---|
58 | jbInit();
|
---|
59 | }
|
---|
60 | catch(Exception e) {
|
---|
61 | e.printStackTrace();
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public void setOutputField(OutputField inputField) {
|
---|
66 | this.inputField = (DirectoryInput)inputField;
|
---|
67 | this.inputField.setValue(this.inputField.getDefaultValue(true));
|
---|
68 | this.createMode = OutputField.isTrue(this.inputField.getCreate());
|
---|
69 |
|
---|
70 | }
|
---|
71 | public void updateInputField(){
|
---|
72 | if( !inputField.getDefaultValue(true).equals(jTextField.getText()) ){
|
---|
73 | inputField.setEditted(true);
|
---|
74 | }
|
---|
75 | inputField.setValue(jTextField.getText());
|
---|
76 | }
|
---|
77 | public void updateDefaultValue(){
|
---|
78 | if(!inputField.isEditted())jTextField.setText(inputField.getDefaultValue(true));
|
---|
79 | }
|
---|
80 |
|
---|
81 | private void jbInit() throws Exception {
|
---|
82 | BorderLayout bl = new BorderLayout();
|
---|
83 | //bl.setHgap(3);
|
---|
84 | browsePanel.setLayout(bl);
|
---|
85 | fieldLabel.setText(inputField.getDisplayText());
|
---|
86 | jTextField.setText(inputField.getDefaultValue(true));
|
---|
87 | browsePanel.add(jTextField, BorderLayout.CENTER);
|
---|
88 | browsePanel.add(browseButton, BorderLayout.EAST);
|
---|
89 | browseButton.addActionListener(new ActionListener() {
|
---|
90 | public void actionPerformed(ActionEvent e) {
|
---|
91 | File selectedFile = null;
|
---|
92 | if(chooser==null){
|
---|
93 | chooser = new DefaultingDirectoryChooser(createMode);
|
---|
94 | chooser.setFileHidingEnabled(false);
|
---|
95 | }
|
---|
96 |
|
---|
97 | //PR 1468823 - if input is cleared, dialogue won't be displayed
|
---|
98 | String dirPath = jTextField.getText();
|
---|
99 | if( dirPath == null ) {
|
---|
100 | dirPath = EMPTY_STRING;
|
---|
101 | }
|
---|
102 | dirPath = dirPath.trim();
|
---|
103 |
|
---|
104 | if( dirPath.length() == 0 ) {
|
---|
105 | dirPath = inputField.getDefaultValue(true);
|
---|
106 | jTextField.setText(dirPath);
|
---|
107 | }
|
---|
108 | chooser.setDefaultDirectory(new File(dirPath));
|
---|
109 |
|
---|
110 | int returnVal = chooser.showDialog(parent, e.getActionCommand());
|
---|
111 | if (returnVal == JFileChooser.APPROVE_OPTION) {
|
---|
112 | selectedFile = chooser.getSelectedFile();
|
---|
113 | }
|
---|
114 | if (selectedFile != null) {
|
---|
115 | jTextField.setText(selectedFile.getAbsolutePath());
|
---|
116 | inputField.setValue(selectedFile.getAbsolutePath());
|
---|
117 | inputField.setEditted(true);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | });
|
---|
121 | browseButton.setText(res.getString("selectFolder"));
|
---|
122 | browseButton.setPreferredSize(new Dimension(150, SizeConstants.FIELD_HEIGHT));
|
---|
123 |
|
---|
124 | jTextField.addActionListener(new ActionListener() {
|
---|
125 | public void actionPerformed(ActionEvent e) {
|
---|
126 | updateInputField();
|
---|
127 | }
|
---|
128 | });
|
---|
129 |
|
---|
130 | }
|
---|
131 | public int addSelf(JPanel content,GBCF cf, int row, boolean overflow) {
|
---|
132 | FlowLayout l = new FlowLayout(FlowLayout.LEFT);
|
---|
133 | JPanel p = new JPanel(l);
|
---|
134 | p.add(fieldLabel);
|
---|
135 | content.add(p);
|
---|
136 | content.add(browsePanel);
|
---|
137 | if(overflow){
|
---|
138 | jTextField.setOverflow(SizeConstants.OVERFLOW_SHORT_FIELD_SIZE);
|
---|
139 | }
|
---|
140 | return ++row;
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * renderError
|
---|
146 | */
|
---|
147 | public void renderError() {
|
---|
148 | jTextField.requestFocus();
|
---|
149 | }
|
---|
150 | }
|
---|