Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / org / gvsig / geodb / vectorialdb / wizard / UserSelectedFieldsPanel.java @ 38608

History | View | Annotate | Download (7.44 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 org.gvsig.geodb.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.andami.PluginServices;
54
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
55
import org.gvsig.gui.beans.swing.JButton;
56

    
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 WizardDB parent = null;
73

    
74
    public UserSelectedFieldsPanel(FeatureAttributeDescriptor[] descriptors,
75
        boolean empty, WizardDB _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 geo) {
94
        String[] resp = getUserSelectedFields();
95
        if (idf != null) {
96
            for (int i=0 ; i<idf.length ; i++){
97
                resp = addBeginningIfNotContained(resp, idf[i]);
98
            }
99
                }
100
        if (geo != null) {
101
                        resp = addBeginningIfNotContained(resp, geo);
102
                }
103

    
104
        return resp;
105
    }
106

    
107
    private String[] addBeginningIfNotContained(String[] arr, String item) {
108
        if (contains(arr, item)) {
109
            return arr;
110
        }
111
        else {
112
            int size = arr.length;
113
            String[] resp = new String[size + 1];
114
            resp[0] = item;
115

    
116
            for (int i = 0; i < size; i++) {
117
                resp[i + 1] = arr[i];
118
            }
119

    
120
            return resp;
121
        }
122
    }
123

    
124
    private String[] removeIfContained(String[] arr, String item) {
125
        if (!contains(arr, item)) {
126
            return arr;
127
        }
128
        else {
129
            int size = arr.length;
130
            ArrayList aux = new ArrayList();
131

    
132
            for (int i = 0; i < size; i++) {
133
                                aux.add(arr[i]);
134
                        }
135

    
136
            aux.remove(item);
137

    
138
            return (String[]) aux.toArray(new String[0]);
139
        }
140
    }
141

    
142
    private boolean contains(String[] arr, String item) {
143
        for (int i = 0; i < arr.length; i++) {
144
            if (arr[i].compareTo(item) == 0) {
145
                return true;
146
            }
147
        }
148

    
149
        return false;
150
    }
151

    
152
    public void enableControls(boolean enable) {
153
        getFieldsList().setEnabled(enable);
154
        getSelAllFieldsButton().setEnabled(enable);
155
        getDeselAllFieldsButton().setEnabled(enable);
156
    }
157

    
158
    private void initialize() {
159
        setLayout(null);
160
        setBounds(new java.awt.Rectangle(255, 55, 251, 166));
161
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
162
                PluginServices.getText(this, "table_fields"),
163
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
164
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
165
        add(getFieldsScrollPane(), null);
166

    
167
        add(getSelAllFieldsButton(), null);
168
        add(getDeselAllFieldsButton(), null);
169
    }
170

    
171
    private JScrollPane getFieldsScrollPane() {
172
        if (fieldsScrollPane == null) {
173
            fieldsScrollPane = new JScrollPane();
174
            fieldsScrollPane.setBounds(new java.awt.Rectangle(5, 20,
175
                    101 + (28 * 5), 76 + (7 * 5)));
176
            fieldsScrollPane.setViewportView(getFieldsList());
177
        }
178

    
179
        return fieldsScrollPane;
180
    }
181

    
182
    private AvailableFieldsCheckBoxList getFieldsList() {
183
        if (fieldsList == null) {
184
            fieldsList = new AvailableFieldsCheckBoxList();
185
        }
186

    
187
        return fieldsList;
188
    }
189

    
190
    private void setAllFieldsInTable(FeatureAttributeDescriptor[] descriptors) {
191
        DefaultListModel lmodel = new DefaultListModel();
192

    
193
        for (int i = 0; i < descriptors.length; i++) {
194
            lmodel.addElement(new FieldsListItem(descriptors[i]));
195
        }
196

    
197
        getFieldsList().setModel(lmodel);
198
        getFieldsScrollPane().setViewportView(fieldsList);
199
        getFieldsScrollPane().updateUI();
200
    }
201

    
202
    private JButton getSelAllFieldsButton() {
203
        if (selAllFieldsButton == null) {
204
            selAllFieldsButton = new JButton();
205
            selAllFieldsButton.addActionListener(this);
206
            selAllFieldsButton.setBounds(new java.awt.Rectangle(28 + 5, 135,
207
                    90, 26));
208
            selAllFieldsButton.setText(PluginServices.getText(this, "all"));
209
        }
210

    
211
        return selAllFieldsButton;
212
    }
213

    
214
    /**
215
     * This method initializes deselAllFieldsButton
216
     *
217
     * @return javax.swing.JButton
218
     */
219
    private JButton getDeselAllFieldsButton() {
220
        if (deselAllFieldsButton == null) {
221
            deselAllFieldsButton = new JButton();
222
            deselAllFieldsButton.addActionListener(this);
223
            deselAllFieldsButton.setBounds(new java.awt.Rectangle(28 + 100,
224
                    135, 90, 26));
225
            deselAllFieldsButton.setText(PluginServices.getText(this, "none2"));
226
        }
227

    
228
        return deselAllFieldsButton;
229
    }
230

    
231
    public void actionPerformed(ActionEvent e) {
232
        Object src = e.getSource();
233

    
234
        if (src == getDeselAllFieldsButton()) {
235
            getFieldsList().checkAll(false);
236
        }
237

    
238
        if (src == getSelAllFieldsButton()) {
239
            getFieldsList().checkAll(true);
240
        }
241
    }
242

    
243
    private String[] getUserSelectedFields() {
244
        Object[] sel = fieldsList.getCheckedItems();
245
        int size = sel.length;
246

    
247
        String[] resp = new String[size];
248

    
249
        for (int i = 0; i < size; i++) {
250
            resp[i] = ((FieldsListItem) sel[i]).getName();
251
        }
252

    
253
        return resp;
254
    }
255

    
256
    public void repaint() {
257
        super.repaint();
258
        getFieldsList().updateUI();
259
        getFieldsScrollPane().updateUI();
260
    }
261
}