Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / dynform / src / org / gvsig / tools / dynform / impl / dynformfield / file / JDynFormFieldFile.java @ 846

History | View | Annotate | Download (4.03 KB)

1
package org.gvsig.tools.dynform.impl.dynformfield.file;
2

    
3
import java.awt.BorderLayout;
4
import java.awt.event.ActionEvent;
5
import java.awt.event.ActionListener;
6
import java.awt.event.FocusEvent;
7
import java.awt.event.FocusListener;
8
import java.io.File;
9

    
10
import javax.swing.JButton;
11
import javax.swing.JFileChooser;
12
import javax.swing.JPanel;
13
import javax.swing.JTextField;
14
import javax.swing.filechooser.FileFilter;
15

    
16
import org.gvsig.tools.dynform.api.JDynFormField;
17
import org.gvsig.tools.dynform.impl.dynformfield.AbstractJDynFormField;
18
import org.gvsig.tools.dynobject.DynObject;
19
import org.gvsig.tools.service.spi.ServiceManager;
20

    
21
public class JDynFormFieldFile extends AbstractJDynFormField implements JDynFormField, FocusListener {
22
        
23
        private File assignedValue  = null;
24
        private File currentValue = null;
25
        private JTextField jtext = null;
26
        private JButton jbutton = null;
27

    
28
        public JDynFormFieldFile(DynObject parameters,
29
                        ServiceManager serviceManager) {
30
                super(parameters, serviceManager);
31
                this.assignedValue = (File) this.getParameterValue();
32
        }
33

    
34
        public Object getAssignedValue() {
35
                return this.assignedValue;
36
        }
37
        
38
        public void initComponent() {
39
                this.contents = new JPanel();
40
                this.contents.setLayout(new BorderLayout());
41
                
42
                this.jtext = new JTextField();
43
                this.jtext.addFocusListener(this);
44
                this.jtext.setEditable(false);
45
                this.jbutton = new JButton("Browse...");
46
                this.jbutton.addActionListener(new ActionListener() {
47
                        public void actionPerformed(ActionEvent e) {
48
                                onClickBrowse();
49
                        }
50
                });
51

    
52
                this.contents.add(jtext,BorderLayout.CENTER);
53
                this.contents.add(jbutton,BorderLayout.LINE_END);
54
                this.contents.setVisible(true);
55
                this.setValue(this.assignedValue);
56
        }
57
        
58
        public void onClickBrowse() {
59
                fireFieldEnterEvent();
60
                this.problemIndicator().restore();
61
                
62
                File initialPath = null;
63
                if( this.currentValue!=null ) {
64
                        initialPath = this.currentValue.getParentFile();
65
                }
66
                File[] x = showOpenFileDialog(this.getLabel(), initialPath);
67
                if( x==null ) {
68
                        return;
69
                }
70
                this.currentValue = x[0];
71
                this.jtext.setText(x[0].getPath());
72
        }
73
        
74
        public void setValue(Object value) {
75
                if( value == null ) {
76
                        this.jtext.setText("");
77
                } else {
78
                        if( !(value instanceof File) ) {
79
                                logger.info("setValue invoked with non File value ("+value.toString()+").");
80
                                return;
81
                        }
82
                        this.jtext.setText(((File)value).getPath());
83
                }
84
                this.assignedValue = (File) value;
85
                this.currentValue = this.assignedValue;
86
        }
87
        
88
        public Object getValue() {
89
                return this.currentValue;
90
        }
91
        
92
        public boolean hasValidValue() {
93
                return true;
94
        }
95

    
96
        public File[] showOpenFileDialog(String title, File initialPath) {
97
                return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, null, false);
98
        }
99
        
100
        public File[] showChooserDialog(
101
                        final String title,
102
                        final int type, // SAVE_DIALOG / OPEN_DIALOG
103
                        final int selectionMode, //    JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES
104
                        final boolean multiselection, 
105
                        final File initialPath,
106
                        final FileFilter filter,
107
                        final boolean fileHidingEnabled
108
                        ) {
109
                File[] returnValue = null;
110
                
111
                JFileChooser fc = new JFileChooser();
112
                fc.setDialogTitle(title);
113
                fc.setDialogType(type);
114
                fc.setFileSelectionMode(selectionMode);
115
                fc.setMultiSelectionEnabled(multiselection);
116
                fc.setCurrentDirectory(initialPath);
117
                fc.setFileFilter(filter);
118
                fc.setFileHidingEnabled(fileHidingEnabled);
119
                int r = JFileChooser.CANCEL_OPTION;
120
                switch(type) {
121
                case JFileChooser.SAVE_DIALOG:
122
                        r = fc.showSaveDialog(this.contents);
123
                        break;
124
                case JFileChooser.OPEN_DIALOG:
125
                default:
126
                        r = fc.showOpenDialog(this.contents);
127
                        break;
128
                }
129
                if( r != JFileChooser.APPROVE_OPTION ) {
130
                        returnValue = null;
131
                } else {
132
                        if( fc.isMultiSelectionEnabled() ) {
133
                                returnValue = fc.getSelectedFiles();
134
                        } else {
135
                                returnValue = new File[] { fc.getSelectedFile() };
136
                        }
137
                }
138
                return returnValue;
139
        }
140

    
141
        public void focusGained(FocusEvent arg0) {
142
                fireFieldEnterEvent();
143
                this.problemIndicator().restore();
144
        }
145

    
146
        public void focusLost(FocusEvent arg0) {
147
                fireFieldExitEvent();
148
        }}