Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / UserSelectedFieldsPanel.java @ 24759

History | View | Annotate | Download (7.33 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   Prodevelop Integraci?n de Tecnolog?as SL
34
 *   Conde Salvatierra de ?lava , 34-10
35
 *   46004 Valencia
36
 *   Spain
37
 *
38
 *   +34 963 510 612
39
 *   +34 963 510 968
40
 *   gis@prodevelop.es
41
 *   http://www.prodevelop.es
42
 */
43
package com.prodevelop.cit.gvsig.vectorialdb.wizard;
44

    
45
import java.awt.event.ActionEvent;
46
import java.awt.event.ActionListener;
47
import java.util.ArrayList;
48

    
49
import javax.swing.DefaultListModel;
50
import javax.swing.JPanel;
51
import javax.swing.JScrollPane;
52

    
53
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
54
import org.gvsig.gui.beans.swing.JButton;
55

    
56
import com.iver.andami.PluginServices;
57

    
58

    
59
/**
60
 * Utility class that holds a single table field selection controls.
61
 *
62
 * @author jldominguez
63
 *
64
 */
65
public class UserSelectedFieldsPanel extends JPanel implements ActionListener {
66
    private FeatureAttributeDescriptor[] descriptors = null;
67
//    private String[] fieldTypes = null;
68
    private JScrollPane fieldsScrollPane = null;
69
    private AvailableFieldsCheckBoxList fieldsList = null;
70
    private JButton selAllFieldsButton = null;
71
    private JButton deselAllFieldsButton = null;
72
    private WizardVectorialDB parent = null;
73

    
74
    public UserSelectedFieldsPanel(FeatureAttributeDescriptor[] descriptors,
75
        boolean empty, WizardVectorialDB _p) {
76
        parent = _p;
77
        this.descriptors = descriptors;
78
//        fieldTypes = fTypes;
79
        initialize();
80

    
81
        if (empty) {
82
            enableControls(false);
83
        }
84
        else {
85
            setAllFieldsInTable(descriptors);
86
        }
87
    }
88

    
89
    public void loadValues() {
90
        setAllFieldsInTable(descriptors);
91
    }
92

    
93
    public String[] getUserSelectedFields(String idf, String gef) {
94
        String[] resp = getUserSelectedFields();
95
        resp = addBeginningIfNotContained(resp, idf);
96
//        resp = removeIfContained(resp, gef);
97

    
98
        return resp;
99
    }
100

    
101
    private String[] addBeginningIfNotContained(String[] arr, String item) {
102
        if (contains(arr, item)) {
103
            return arr;
104
        }
105
        else {
106
            int size = arr.length;
107
            String[] resp = new String[size + 1];
108
            resp[0] = item;
109

    
110
            for (int i = 0; i < size; i++) {
111
                resp[i + 1] = arr[i];
112
            }
113

    
114
            return resp;
115
        }
116
    }
117

    
118
    private String[] removeIfContained(String[] arr, String item) {
119
        if (!contains(arr, item)) {
120
            return arr;
121
        }
122
        else {
123
            int size = arr.length;
124
            ArrayList aux = new ArrayList();
125

    
126
            for (int i = 0; i < size; i++) {
127
                                aux.add(arr[i]);
128
                        }
129

    
130
            aux.remove(item);
131

    
132
            return (String[]) aux.toArray(new String[0]);
133
        }
134
    }
135

    
136
    private boolean contains(String[] arr, String item) {
137
        for (int i = 0; i < arr.length; i++) {
138
            if (arr[i].compareTo(item) == 0) {
139
                return true;
140
            }
141
        }
142

    
143
        return false;
144
    }
145

    
146
    public void enableControls(boolean enable) {
147
        getFieldsList().setEnabled(enable);
148
        getSelAllFieldsButton().setEnabled(enable);
149
        getDeselAllFieldsButton().setEnabled(enable);
150
    }
151

    
152
    private void initialize() {
153
        setLayout(null);
154
        setBounds(new java.awt.Rectangle(255, 55, 251, 191));
155
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
156
                PluginServices.getText(this, "table_fields"),
157
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
158
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
159
        add(getFieldsScrollPane(), null);
160

    
161
        add(getSelAllFieldsButton(), null);
162
        add(getDeselAllFieldsButton(), null);
163
    }
164

    
165
    private JScrollPane getFieldsScrollPane() {
166
        if (fieldsScrollPane == null) {
167
            fieldsScrollPane = new JScrollPane();
168
            fieldsScrollPane.setBounds(new java.awt.Rectangle(5, 20,
169
                    101 + (28 * 5), 101 + (7 * 5)));
170
            fieldsScrollPane.setViewportView(getFieldsList());
171
        }
172

    
173
        return fieldsScrollPane;
174
    }
175

    
176
    private AvailableFieldsCheckBoxList getFieldsList() {
177
        if (fieldsList == null) {
178
            fieldsList = new AvailableFieldsCheckBoxList();
179
        }
180

    
181
        return fieldsList;
182
    }
183

    
184
    private void setAllFieldsInTable(FeatureAttributeDescriptor[] descriptors) {
185
        DefaultListModel lmodel = new DefaultListModel();
186

    
187
        for (int i = 0; i < descriptors.length; i++) {
188
            lmodel.addElement(new FieldsListItem(descriptors[i]));
189
        }
190

    
191
        getFieldsList().setModel(lmodel);
192
        getFieldsScrollPane().setViewportView(fieldsList);
193
        getFieldsScrollPane().updateUI();
194
    }
195

    
196
    private JButton getSelAllFieldsButton() {
197
        if (selAllFieldsButton == null) {
198
            selAllFieldsButton = new JButton();
199
            selAllFieldsButton.addActionListener(this);
200
            selAllFieldsButton.setBounds(new java.awt.Rectangle(28 + 5, 160,
201
                    90, 26));
202
            selAllFieldsButton.setText(PluginServices.getText(this, "all"));
203
        }
204

    
205
        return selAllFieldsButton;
206
    }
207

    
208
    /**
209
     * This method initializes deselAllFieldsButton
210
     *
211
     * @return javax.swing.JButton
212
     */
213
    private JButton getDeselAllFieldsButton() {
214
        if (deselAllFieldsButton == null) {
215
            deselAllFieldsButton = new JButton();
216
            deselAllFieldsButton.addActionListener(this);
217
            deselAllFieldsButton.setBounds(new java.awt.Rectangle(28 + 100,
218
                    160, 90, 26));
219
            deselAllFieldsButton.setText(PluginServices.getText(this, "none2"));
220
        }
221

    
222
        return deselAllFieldsButton;
223
    }
224

    
225
    public void actionPerformed(ActionEvent e) {
226
        Object src = e.getSource();
227

    
228
        if (src == getDeselAllFieldsButton()) {
229
            getFieldsList().checkAll(false);
230
        }
231

    
232
        if (src == getSelAllFieldsButton()) {
233
            getFieldsList().checkAll(true);
234
        }
235
    }
236

    
237
    private String[] getUserSelectedFields() {
238
        Object[] sel = fieldsList.getCheckedItems();
239
        int size = sel.length;
240

    
241
        String[] resp = new String[size];
242

    
243
        for (int i = 0; i < size; i++) {
244
            resp[i] = ((FieldsListItem) sel[i]).getName();
245
        }
246

    
247
        return resp;
248
    }
249

    
250
    public void repaint() {
251
        super.repaint();
252
        getFieldsList().updateUI();
253
        getFieldsScrollPane().updateUI();
254
    }
255
}