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 / AbstractJDynFormFieldImage.java @ 44473
History | View | Annotate | Download (5.3 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.FocusEvent; |
28 |
import java.awt.event.FocusListener; |
29 |
import java.util.Objects; |
30 |
import javax.swing.JComponent; |
31 |
import javax.swing.JPanel; |
32 |
import org.gvsig.imageviewer.ImageViewer; |
33 |
import org.gvsig.tools.dataTypes.CoercionException; |
34 |
import org.gvsig.tools.dynform.DynFormFieldDefinition; |
35 |
import org.gvsig.tools.dynform.JDynFormField; |
36 |
import org.gvsig.tools.dynform.spi.DynFormSPIManager; |
37 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
38 |
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory; |
39 |
import org.gvsig.tools.dynobject.Tags; |
40 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
41 |
import org.gvsig.tools.swing.api.SimpleImage; |
42 |
import org.gvsig.tools.util.ToolsUtilLocator; |
43 |
|
44 |
public abstract class AbstractJDynFormFieldImage<T> extends AbstractJDynFormField implements JDynFormField, FocusListener { |
45 |
|
46 |
protected T assignedValue = null; |
47 |
protected T currentValue = null; |
48 |
protected boolean readonly = false; |
49 |
private ImageViewer imageViewer;
|
50 |
private int fieldHeight; |
51 |
|
52 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
53 |
public AbstractJDynFormFieldImage(
|
54 |
DynFormSPIManager serviceManager, |
55 |
DynFormSPIManager.ComponentsFactory componentsFactory, |
56 |
JDynFormFieldFactory factory, |
57 |
DynFormFieldDefinition definition, |
58 |
Object value
|
59 |
) { |
60 |
super(serviceManager, componentsFactory, factory, definition, value);
|
61 |
this.assignedValue = (T) value;
|
62 |
} |
63 |
|
64 |
@Override
|
65 |
public void loadDefaultValuesFromTags(Tags tags) { |
66 |
super.loadDefaultValuesFromTags(tags);
|
67 |
this.fieldHeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_HEIGHT,200); |
68 |
} |
69 |
|
70 |
@Override
|
71 |
public void setReadOnly(boolean readonly) { |
72 |
super.setReadOnly(readonly);
|
73 |
if (this.contents != null) { |
74 |
this.contents.setEnabled(!readonly);
|
75 |
} |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public Object getAssignedValue() { |
80 |
return this.assignedValue; |
81 |
} |
82 |
|
83 |
@Override
|
84 |
public void initComponent() { |
85 |
this.initImageViewer();
|
86 |
this.setValue(this.assignedValue); |
87 |
} |
88 |
|
89 |
protected void initImageViewer() { |
90 |
this.contents = new JPanel(); |
91 |
this.contents.setLayout(new BorderLayout(2, 0)); |
92 |
|
93 |
this.imageViewer = ToolsUtilLocator.getImageViewerManager().createImageViewer();
|
94 |
JComponent jimageViewer = this.imageViewer.asJComponent(); |
95 |
|
96 |
jimageViewer.setPreferredSize(new Dimension(300, this.fieldHeight)); |
97 |
jimageViewer.setSize(300,this.fieldHeight); |
98 |
|
99 |
this.contents.add(jimageViewer, BorderLayout.CENTER); |
100 |
this.contents.setVisible(true); |
101 |
} |
102 |
|
103 |
@Override
|
104 |
public boolean hasValidValue() { |
105 |
return true; |
106 |
} |
107 |
|
108 |
protected void fireFieldChangedEventIfChanged(T previous) { |
109 |
if (!Objects.equals(this.currentValue, previous)) { |
110 |
this.fireFieldChangedEvent();
|
111 |
} |
112 |
} |
113 |
|
114 |
@Override
|
115 |
public void focusGained(FocusEvent arg0) { |
116 |
fireFieldEnterEvent(); |
117 |
this.problemIndicator().restore();
|
118 |
} |
119 |
|
120 |
@Override
|
121 |
public void focusLost(FocusEvent arg0) { |
122 |
fireFieldExitEvent(); |
123 |
} |
124 |
|
125 |
@Override
|
126 |
public void clear() { |
127 |
this.currentValue = null; |
128 |
this.updateImage(this.currentValue); |
129 |
} |
130 |
|
131 |
protected void updateImage(Object value) { |
132 |
if (value == null) { |
133 |
this.imageViewer.clean();
|
134 |
return;
|
135 |
} |
136 |
try {
|
137 |
SimpleImage image = ToolsSwingLocator.getToolsSwingManager() |
138 |
.createSimpleImage(value); |
139 |
this.imageViewer.setImage(image.getBufferedImage());
|
140 |
} catch (Exception ex) { |
141 |
this.problemIndicator().set("Can't load image"); |
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
@Override
|
147 |
public double getResizeWeight() { |
148 |
Tags tags = this.getDefinition().getTags();
|
149 |
if( !tags.has(DynFormSPIManager.TAG_DYNFORM_RESIZEWEIGHT) ) {
|
150 |
return 1.0; |
151 |
} |
152 |
int resizeWeight = tags.getInt(DynFormSPIManager.TAG_DYNFORM_RESIZEWEIGHT, 0); |
153 |
if( resizeWeight<1 ) { |
154 |
return 0; |
155 |
} |
156 |
if( resizeWeight>100 ) { |
157 |
return 1; |
158 |
} |
159 |
return resizeWeight / 100.0; |
160 |
} |
161 |
|
162 |
@Override
|
163 |
public abstract Object getValue(); |
164 |
|
165 |
@Override
|
166 |
public abstract void setValue(Object value); |
167 |
} |