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 @ 44189
History | View | Annotate | Download (4.12 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.dynform.JDynFormField; |
34 |
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField; |
35 |
import org.gvsig.tools.dynobject.DynObject; |
36 |
import org.gvsig.tools.service.spi.ServiceManager; |
37 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
38 |
import org.gvsig.tools.swing.api.SimpleImage; |
39 |
import org.gvsig.tools.util.ToolsUtilLocator; |
40 |
|
41 |
public abstract class AbstractJDynFormFieldImage<T> extends AbstractJDynFormField implements JDynFormField, FocusListener { |
42 |
|
43 |
protected T assignedValue = null; |
44 |
protected T currentValue = null; |
45 |
protected boolean readonly = false; |
46 |
private ImageViewer imageViewer;
|
47 |
|
48 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
49 |
public AbstractJDynFormFieldImage(DynObject parameters,
|
50 |
ServiceManager serviceManager) { |
51 |
super(parameters, serviceManager);
|
52 |
this.assignedValue = (T) this.getParameterValue(); |
53 |
} |
54 |
|
55 |
public void setReadOnly(boolean readonly) { |
56 |
super.setReadOnly(readonly);
|
57 |
if (this.contents != null) { |
58 |
this.contents.setEnabled(!readonly);
|
59 |
} |
60 |
} |
61 |
|
62 |
public Object getAssignedValue() { |
63 |
return this.assignedValue; |
64 |
} |
65 |
|
66 |
public void initComponent() { |
67 |
this.initImageViewer();
|
68 |
this.setValue(this.assignedValue); |
69 |
} |
70 |
|
71 |
protected void initImageViewer() { |
72 |
this.contents = new JPanel(); |
73 |
this.contents.setLayout(new BorderLayout(2, 0)); |
74 |
|
75 |
this.imageViewer = ToolsUtilLocator.getImageViewerManager().createImageViewer();
|
76 |
JComponent jimageViewer = this.imageViewer.asJComponent(); |
77 |
|
78 |
jimageViewer.setPreferredSize(new Dimension(300, 200)); |
79 |
jimageViewer.setSize(300,200); |
80 |
|
81 |
this.contents.add(jimageViewer, BorderLayout.CENTER); |
82 |
this.contents.setVisible(true); |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public boolean hasValidValue() { |
87 |
return true; |
88 |
} |
89 |
|
90 |
protected void fireFieldChangedEventIfChanged(T previous) { |
91 |
if (!Objects.equals(this.currentValue, previous)) { |
92 |
this.fireFieldChangedEvent();
|
93 |
} |
94 |
} |
95 |
|
96 |
public void focusGained(FocusEvent arg0) { |
97 |
fireFieldEnterEvent(); |
98 |
this.problemIndicator().restore();
|
99 |
} |
100 |
|
101 |
public void focusLost(FocusEvent arg0) { |
102 |
fireFieldExitEvent(); |
103 |
} |
104 |
|
105 |
public void clear() { |
106 |
this.currentValue = null; |
107 |
this.updateImage(this.currentValue); |
108 |
} |
109 |
|
110 |
protected void updateImage(Object value) { |
111 |
if (value == null) { |
112 |
this.imageViewer.clean();
|
113 |
return;
|
114 |
} |
115 |
try {
|
116 |
SimpleImage image = ToolsSwingLocator.getToolsSwingManager() |
117 |
.createSimpleImage(value); |
118 |
this.imageViewer.setImage(image.getBufferedImage());
|
119 |
} catch (Exception ex) { |
120 |
this.problemIndicator().set("Can't load image"); |
121 |
} |
122 |
} |
123 |
|
124 |
public abstract Object getValue(); |
125 |
|
126 |
public abstract void setValue(Object value); |
127 |
} |