Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extGeoDB / src / com / prodevelop / cit / gvsig / vectorialdb / wizard / UserSelectedFieldsPanel.java @ 29199

History | View | Annotate | Download (7.37 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 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
                        resp = addBeginningIfNotContained(resp, idf);
97
                }
98
        if (geo != null) {
99
                        resp = addBeginningIfNotContained(resp, geo);
100
                }
101

    
102
        return resp;
103
    }
104

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

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

    
118
            return resp;
119
        }
120
    }
121

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

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

    
134
            aux.remove(item);
135

    
136
            return (String[]) aux.toArray(new String[0]);
137
        }
138
    }
139

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

    
147
        return false;
148
    }
149

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

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

    
165
        add(getSelAllFieldsButton(), null);
166
        add(getDeselAllFieldsButton(), null);
167
    }
168

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

    
177
        return fieldsScrollPane;
178
    }
179

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

    
185
        return fieldsList;
186
    }
187

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

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

    
195
        getFieldsList().setModel(lmodel);
196
        getFieldsScrollPane().setViewportView(fieldsList);
197
        getFieldsScrollPane().updateUI();
198
    }
199

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

    
209
        return selAllFieldsButton;
210
    }
211

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

    
226
        return deselAllFieldsButton;
227
    }
228

    
229
    public void actionPerformed(ActionEvent e) {
230
        Object src = e.getSource();
231

    
232
        if (src == getDeselAllFieldsButton()) {
233
            getFieldsList().checkAll(false);
234
        }
235

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

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

    
245
        String[] resp = new String[size];
246

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

    
251
        return resp;
252
    }
253

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