Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformfield / Bytearray / JDynFormFieldBytearray.java @ 2548

History | View | Annotate | Download (5.47 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.tools.dynform.services.dynformfield.Bytearray;
24

    
25
import java.awt.BorderLayout;
26
import java.awt.FlowLayout;
27
import java.awt.event.FocusEvent;
28
import java.awt.event.FocusListener;
29
import java.util.Objects;
30
import javax.swing.JButton;
31
import javax.swing.JPanel;
32
import javax.swing.JTextField;
33
import javax.swing.event.ChangeEvent;
34
import javax.swing.event.ChangeListener;
35
import javax.swing.text.JTextComponent;
36
import org.gvsig.tools.dynform.DynFormFieldDefinition;
37
import org.gvsig.tools.dynform.JDynFormField;
38
import org.gvsig.tools.dynform.spi.DynFormSPIManager;
39
import org.gvsig.tools.dynform.spi.dynformfield.JDynFormFieldFactory;
40
import org.gvsig.tools.dynform.spi.dynformfield.AbstractJDynFormField;
41
import org.gvsig.tools.swing.api.ToolsSwingLocator;
42
import org.gvsig.tools.swing.api.ToolsSwingManager;
43
import org.gvsig.tools.swing.api.pickercontroller.PickerController;
44

    
45
public class JDynFormFieldBytearray extends AbstractJDynFormField implements JDynFormField, FocusListener {
46

    
47
    protected byte[] assignedValue = null;
48
    protected byte[] currentValue = null;
49
    protected JTextComponent jtext = null;
50
    protected JButton jbuttonUpload = null;
51
    protected JButton jbuttonDownload = null;
52
    private PickerController<byte[]> picker;
53

    
54
    public JDynFormFieldBytearray(
55
            DynFormSPIManager serviceManager,
56
            DynFormSPIManager.ComponentsFactory componentsFactory,
57
            JDynFormFieldFactory factory,
58
            DynFormFieldDefinition definition,
59
            Object value
60
    ) {
61
        super(serviceManager, componentsFactory, factory, definition, value);
62
        this.assignedValue = (byte[]) value;
63
    }
64

    
65
    @Override
66
    public void setReadOnly(boolean readonly) {
67
        super.setReadOnly(readonly);
68
        if (this.contents != null) {
69
            this.contents.setEnabled(true);
70
        }
71
        if (this.picker != null) {
72
            this.picker.setEnabled(!this.isReadOnly());
73
        }
74
    }
75

    
76
    @Override
77
    public Object getAssignedValue() {
78
        return this.assignedValue;
79
    }
80

    
81
    @Override
82
    public void initComponent() {
83
        this.contents = new JPanel();
84
        this.contents.setLayout(new BorderLayout());
85

    
86
        DynFormSPIManager.ComponentsFactory components = this.getComponentsFactory();
87
        ToolsSwingManager toolsSwingManager = ToolsSwingLocator.getToolsSwingManager();
88

    
89
        this.jtext = components.getJTextField(this.getDefinition(), null);
90
        this.jbuttonDownload = components.getJButton(this.getDefinition(), "Download");
91
        this.jbuttonUpload = components.getJButton(this.getDefinition(), "Upload");
92

    
93
        this.picker = toolsSwingManager.createByteArrayPickerController(
94
                (JTextField) jtext,
95
                jbuttonUpload,
96
                jbuttonDownload,
97
                this.getForm().getDefinition().getName() + ".RAWDATA",
98
                null
99
        );
100
        this.picker.addChangeListener(new ChangeListener() {
101
            @Override
102
            public void stateChanged(ChangeEvent e) {
103
                fireFieldChangedEvent();
104
            }
105
        });
106
        if (!components.containsComponents(this.getDefinition())) {
107
            toolsSwingManager.removeBorder(this.jbuttonDownload);
108
            toolsSwingManager.removeBorder(this.jbuttonUpload);
109
            JPanel buttons = new JPanel(new FlowLayout(FlowLayout.TRAILING, 4, 2));
110
            buttons.add(this.jbuttonUpload);
111
            buttons.add(this.jbuttonDownload);
112
            this.contents.add(jtext, BorderLayout.CENTER);
113
            this.contents.add(buttons, BorderLayout.LINE_END);
114
            this.contents.setVisible(true);
115
        }
116

    
117
        this.picker.setEnabled(!this.isReadOnly());
118
        this.setValue(this.assignedValue);
119
    }
120

    
121
    @Override
122
    public void setValue(Object value) {
123
        this.picker.set((byte[]) value);
124
        this.assignedValue = (byte[]) value;
125
    }
126

    
127
    @Override
128
    public Object getValue() {
129
        return this.picker.get();
130
    }
131

    
132
    @Override
133
    public boolean hasValidValue() {
134
        return true;
135
    }
136

    
137
    @Override
138
    public void focusGained(FocusEvent arg0) {
139
        fireFieldEnterEvent();
140
        this.problemIndicator().restore();
141
    }
142

    
143
    @Override
144
    public void focusLost(FocusEvent arg0) {
145
        fireFieldExitEvent();
146
    }
147

    
148
    @Override
149
    public void clear() {
150
        this.picker.set(null);
151
    }
152
    
153
    @Override
154
    public boolean isModified() {
155
        try {
156
            byte[] value = this.picker.get();
157
            return !Objects.equals(value, this.getAssignedValue());
158
        } catch (Exception ex) {
159
            return false;
160
        }
161
    }
162
    
163

    
164
}