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 / JDynFormFieldImageFile.java @ 44472
History | View | Annotate | Download (4.02 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.event.FocusListener; |
26 |
import java.io.File; |
27 |
import java.util.Objects; |
28 |
import org.gvsig.tools.ToolsLocator; |
29 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
30 |
import org.gvsig.tools.dynform.JDynFormField; |
31 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
32 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
33 |
import org.gvsig.tools.dynobject.Tags; |
34 |
|
35 |
public class JDynFormFieldImageFile extends AbstractJDynFormFieldImageFile<File> implements JDynFormField, FocusListener { |
36 |
|
37 |
public JDynFormFieldImageFile(
|
38 |
DynFormSPIManager serviceManager, |
39 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
40 |
JDynFormFieldFactory factory, |
41 |
DynFormFieldDefinition definition, |
42 |
Object value
|
43 |
) { |
44 |
super(serviceManager, componentsFactory, factory, definition, value);
|
45 |
} |
46 |
|
47 |
@Override
|
48 |
protected File getCurrentFile() { |
49 |
return this.currentValue; |
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void setValue(Object value) { |
54 |
File previous = this.currentValue; |
55 |
if (value == null) { |
56 |
this.jtext.setText(""); |
57 |
} else {
|
58 |
if (!(value instanceof File)) { |
59 |
LOGGER.info("setValue invoked with non File value (" + value.toString() + ")."); |
60 |
return;
|
61 |
} |
62 |
this.jtext.setText(((File) value).getPath()); |
63 |
} |
64 |
this.assignedValue = (File) value; |
65 |
this.currentValue = this.assignedValue; |
66 |
this.updateImage(this.currentValue); |
67 |
this.fireFieldChangedEventIfChanged(previous);
|
68 |
} |
69 |
|
70 |
@Override
|
71 |
public Object getValue() { |
72 |
File value;
|
73 |
String s;
|
74 |
s = this.jtext.getText();
|
75 |
if (s.trim().length() == 0) { |
76 |
Object x = this.getDefinition().getDefaultValue(); |
77 |
if ((x != null && x instanceof File)) { |
78 |
value = new File(x.toString()); |
79 |
} else {
|
80 |
value = null;
|
81 |
} |
82 |
} else {
|
83 |
value = new File(s); |
84 |
} |
85 |
return value;
|
86 |
} |
87 |
|
88 |
@Override
|
89 |
protected void updateImage(Object value) { |
90 |
if (value != null) { |
91 |
File f = (File) value; |
92 |
if (!f.isAbsolute()) {
|
93 |
Tags tags = this.getDefinition().getTags();
|
94 |
if (tags.has("path")) { |
95 |
File folder;
|
96 |
if( tags.get("path") instanceof File ) { |
97 |
folder = (File) tags.get("path"); |
98 |
} else {
|
99 |
folder = new File(Objects.toString(tags.get("path"), null)); |
100 |
} |
101 |
File f2 = new File(folder, f.getPath()); |
102 |
if (f2.exists()) {
|
103 |
value = f2; |
104 |
} else {
|
105 |
folder = ToolsLocator.getFoldersManager().get("Project");
|
106 |
if (folder != null) { |
107 |
value = new File(folder, f.getPath()); |
108 |
} |
109 |
} |
110 |
} |
111 |
} |
112 |
} |
113 |
super.updateImage(value);
|
114 |
} |
115 |
|
116 |
} |