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