svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.swing / org.gvsig.fmap.dal.swing.impl / src / main / java / org / gvsig / featureform / swing / impl / dynformfield / ImageFile / AbstractJDynFormFieldImageFile.java @ 44644
History | View | Annotate | Download (8.68 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.featureform.swing.impl.dynformfield.ImageFile; |
24 |
|
25 |
import java.awt.BorderLayout; |
26 |
import java.awt.Dimension; |
27 |
import java.awt.event.ActionEvent; |
28 |
import java.awt.event.ActionListener; |
29 |
import java.awt.event.FocusListener; |
30 |
import java.io.File; |
31 |
import javax.swing.JButton; |
32 |
import javax.swing.JComponent; |
33 |
import javax.swing.JFileChooser; |
34 |
import javax.swing.JPanel; |
35 |
import javax.swing.JTextField; |
36 |
import javax.swing.filechooser.FileFilter; |
37 |
import javax.swing.filechooser.FileNameExtensionFilter; |
38 |
import org.gvsig.imageviewer.ImageViewer; |
39 |
import org.gvsig.tools.ToolsLocator; |
40 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
41 |
import org.gvsig.tools.dynform.JDynFormField; |
42 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
43 |
import org.gvsig.tools.dynform.spi.dynformfield.JCustomTextField; |
44 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
45 |
import org.gvsig.tools.dynobject.Tags; |
46 |
import org.gvsig.tools.i18n.I18nManager; |
47 |
import org.gvsig.tools.swing.api.SimpleImage; |
48 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
49 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
50 |
import org.gvsig.tools.swing.icontheme.IconTheme; |
51 |
import org.gvsig.tools.util.ToolsUtilLocator; |
52 |
|
53 |
public abstract class AbstractJDynFormFieldImageFile<T> extends AbstractJDynFormFieldImage<T> implements JDynFormField, FocusListener { |
54 |
|
55 |
protected JTextField jtext = null; |
56 |
protected JButton jbutton = null; |
57 |
protected JButton buttonOpenImage; |
58 |
|
59 |
public AbstractJDynFormFieldImageFile(
|
60 |
DynFormSPIManager serviceManager, |
61 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
62 |
JDynFormFieldFactory factory, |
63 |
DynFormFieldDefinition definition, |
64 |
Object value
|
65 |
) { |
66 |
super(serviceManager, componentsFactory, factory, definition, value);
|
67 |
} |
68 |
|
69 |
@Override
|
70 |
public void setReadOnly(boolean readonly) { |
71 |
super.setReadOnly(readonly);
|
72 |
if (this.contents != null) { |
73 |
if (readonly) {
|
74 |
this.jtext.setEditable(false); |
75 |
this.jbutton.setEnabled(false); |
76 |
} else {
|
77 |
this.jtext.setEditable(true); |
78 |
this.jbutton.setEnabled(true); |
79 |
} |
80 |
} |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public void initComponent() { |
85 |
this.initImageViewer();
|
86 |
|
87 |
this.jtext = new JCustomTextField(this.getLabel()); |
88 |
this.jtext.addFocusListener(this); |
89 |
this.jbutton = new JButton("..."); |
90 |
this.jbutton.addActionListener(new ActionListener() { |
91 |
@Override
|
92 |
public void actionPerformed(ActionEvent e) { |
93 |
onClickBrowse(); |
94 |
} |
95 |
}); |
96 |
IconTheme theme = ToolsSwingLocator.getIconThemeManager().getCurrent(); |
97 |
if( theme.exists("form-open-image") ) { |
98 |
this.buttonOpenImage = new JButton(theme.get("form-open-image")); |
99 |
} else {
|
100 |
this.buttonOpenImage = new JButton(" "); |
101 |
} |
102 |
this.buttonOpenImage.addActionListener(new ActionListener() { |
103 |
@Override
|
104 |
public void actionPerformed(ActionEvent e) { |
105 |
onClickViewer(); |
106 |
} |
107 |
}); |
108 |
JPanel buttonsPanel = new JPanel(); |
109 |
buttonsPanel.setLayout(new BorderLayout(0, 2)); |
110 |
buttonsPanel.add(jtext, BorderLayout.CENTER);
|
111 |
buttonsPanel.add(jbutton, BorderLayout.LINE_END);
|
112 |
|
113 |
this.contents.add(buttonsPanel, BorderLayout.NORTH); |
114 |
|
115 |
this.jbutton.setEnabled(!this.isReadOnly()); |
116 |
this.jtext.setEditable(!this.isReadOnly()); |
117 |
} |
118 |
|
119 |
protected void onClickViewer() { |
120 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
121 |
WindowManager wmanager = ToolsSwingLocator.getWindowManager(); |
122 |
Object value = this.getValue(); |
123 |
value = this.getPreparedValue(value);
|
124 |
if (value == null) { |
125 |
return;
|
126 |
} |
127 |
try {
|
128 |
ImageViewer viewer = ToolsUtilLocator.getImageViewerManager() |
129 |
.createImageViewer(); |
130 |
SimpleImage image = ToolsSwingLocator.getToolsSwingManager() |
131 |
.createSimpleImage(value); |
132 |
viewer.setImage(image.getBufferedImage()); |
133 |
JComponent jimageViewer = viewer.asJComponent();
|
134 |
jimageViewer.setPreferredSize(new Dimension(650, 600)); |
135 |
wmanager.showWindow( |
136 |
jimageViewer, |
137 |
i18n.getTranslation("_Image_viewer"),
|
138 |
WindowManager.MODE.WINDOW |
139 |
); |
140 |
} catch (Exception ex) { |
141 |
|
142 |
} |
143 |
} |
144 |
|
145 |
protected void onClickBrowse() { |
146 |
fireFieldEnterEvent(); |
147 |
T previous = this.currentValue;
|
148 |
|
149 |
this.problemIndicator().restore();
|
150 |
|
151 |
File initialPath = null; |
152 |
FileFilter filter = null; |
153 |
Tags tags = this.getDefinition().getTags();
|
154 |
if (tags.has("path")) { |
155 |
initialPath = new File((String) tags.get("path")); |
156 |
} |
157 |
if (tags.has("filter")) { |
158 |
try {
|
159 |
String extensions = (String) tags.get("filter"); |
160 |
String filterDescription = extensions;
|
161 |
if (tags.has("filterDescription")) { |
162 |
filterDescription = (String) tags.get("filterDescription"); |
163 |
} |
164 |
filter = new FileNameExtensionFilter(filterDescription, extensions.split("[|]")); |
165 |
} catch (Exception ex) { |
166 |
// Ignore
|
167 |
} |
168 |
} |
169 |
|
170 |
if (this.getCurrentFile() != null) { |
171 |
initialPath = this.getCurrentFile().getParentFile();
|
172 |
} |
173 |
File[] x = showOpenFileDialog(this.getLabel(), initialPath, filter); |
174 |
if (x == null) { |
175 |
return;
|
176 |
} |
177 |
this.setValue(x[0]); |
178 |
} |
179 |
|
180 |
protected File[] showOpenFileDialog(String title, File initialPath, FileFilter filter) { |
181 |
return showChooserDialog(title, JFileChooser.OPEN_DIALOG, JFileChooser.FILES_ONLY, false, initialPath, filter, true); |
182 |
} |
183 |
|
184 |
protected File[] showChooserDialog( |
185 |
final String title, |
186 |
final int type, // SAVE_DIALOG / OPEN_DIALOG |
187 |
final int selectionMode, // JFileChooser.FILES_ONLY, JFileChooser.DIRECTORIES_ONLY, JFileChooser.FILES_AND_DIRECTORIES |
188 |
final boolean multiselection, |
189 |
final File initialPath, |
190 |
final FileFilter filter, |
191 |
final boolean fileHidingEnabled |
192 |
) { |
193 |
File[] returnValue; |
194 |
|
195 |
JFileChooser fc = new JFileChooser(); |
196 |
fc.setDialogTitle(title); |
197 |
fc.setDialogType(type); |
198 |
fc.setFileSelectionMode(selectionMode); |
199 |
fc.setMultiSelectionEnabled(multiselection); |
200 |
fc.setCurrentDirectory(initialPath); |
201 |
fc.setFileFilter(filter); |
202 |
fc.setFileHidingEnabled(fileHidingEnabled); |
203 |
int r;
|
204 |
switch (type) {
|
205 |
case JFileChooser.SAVE_DIALOG: |
206 |
r = fc.showSaveDialog(this.contents);
|
207 |
break;
|
208 |
case JFileChooser.OPEN_DIALOG: |
209 |
default:
|
210 |
r = fc.showOpenDialog(this.contents);
|
211 |
break;
|
212 |
} |
213 |
if (r != JFileChooser.APPROVE_OPTION) { |
214 |
returnValue = null;
|
215 |
} else {
|
216 |
if (fc.isMultiSelectionEnabled()) {
|
217 |
returnValue = fc.getSelectedFiles(); |
218 |
} else {
|
219 |
returnValue = new File[]{fc.getSelectedFile()}; |
220 |
} |
221 |
} |
222 |
return returnValue;
|
223 |
} |
224 |
|
225 |
@Override
|
226 |
public void clear() { |
227 |
Object value = this.getDefinition().getDefaultValue(); |
228 |
if (value != null) { |
229 |
value = value.toString(); |
230 |
} else {
|
231 |
value = "";
|
232 |
} |
233 |
this.jtext.setText((String) value); |
234 |
this.currentValue = null; |
235 |
this.updateImage(this.currentValue); |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public abstract Object getValue(); |
240 |
|
241 |
@Override
|
242 |
public abstract void setValue(Object value); |
243 |
|
244 |
protected abstract File getCurrentFile(); |
245 |
} |