Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.geodb.app / org.gvsig.geodb.app.mainplugin / src / main / java / org / gvsig / geodb / vectorialdb / wizard / UserSelectedFieldsPanel.java @ 40557

History | View | Annotate | Download (7.09 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
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.geodb.vectorialdb.wizard;
25

    
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
28
import java.util.ArrayList;
29

    
30
import javax.swing.DefaultListModel;
31
import javax.swing.JPanel;
32
import javax.swing.JScrollPane;
33

    
34
import org.gvsig.andami.PluginServices;
35
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
36
import org.gvsig.gui.beans.swing.JButton;
37

    
38

    
39

    
40
/**
41
 * Utility class that holds a single table field selection controls.
42
 *
43
 * @author jldominguez
44
 *
45
 */
46
public class UserSelectedFieldsPanel extends JPanel implements ActionListener {
47
    private FeatureAttributeDescriptor[] descriptors = null;
48
//    private String[] fieldTypes = null;
49
    private JScrollPane fieldsScrollPane = null;
50
    private AvailableFieldsCheckBoxList fieldsList = null;
51
    private JButton selAllFieldsButton = null;
52
    private JButton deselAllFieldsButton = null;
53
    private WizardDB parent = null;
54

    
55
    public UserSelectedFieldsPanel(FeatureAttributeDescriptor[] descriptors,
56
        boolean empty, WizardDB _p) {
57
        parent = _p;
58
        this.descriptors = descriptors;
59
//        fieldTypes = fTypes;
60
        initialize();
61

    
62
        if (empty) {
63
            enableControls(false);
64
        }
65
        else {
66
            setAllFieldsInTable(descriptors);
67
        }
68
    }
69

    
70
    public void loadValues() {
71
        setAllFieldsInTable(descriptors);
72
    }
73

    
74
    public String[] getUserSelectedFields(String[] idf, String geo) {
75
        String[] resp = getUserSelectedFields();
76
        if (idf != null) {
77
            for (int i=0 ; i<idf.length ; i++){
78
                resp = addBeginningIfNotContained(resp, idf[i]);
79
            }
80
                }
81
        if (geo != null) {
82
                        resp = addBeginningIfNotContained(resp, geo);
83
                }
84

    
85
        return resp;
86
    }
87

    
88
    private String[] addBeginningIfNotContained(String[] arr, String item) {
89
        if (contains(arr, item)) {
90
            return arr;
91
        }
92
        else {
93
            int size = arr.length;
94
            String[] resp = new String[size + 1];
95
            resp[0] = item;
96

    
97
            for (int i = 0; i < size; i++) {
98
                resp[i + 1] = arr[i];
99
            }
100

    
101
            return resp;
102
        }
103
    }
104

    
105
    private String[] removeIfContained(String[] arr, String item) {
106
        if (!contains(arr, item)) {
107
            return arr;
108
        }
109
        else {
110
            int size = arr.length;
111
            ArrayList aux = new ArrayList();
112

    
113
            for (int i = 0; i < size; i++) {
114
                                aux.add(arr[i]);
115
                        }
116

    
117
            aux.remove(item);
118

    
119
            return (String[]) aux.toArray(new String[0]);
120
        }
121
    }
122

    
123
    private boolean contains(String[] arr, String item) {
124
        for (int i = 0; i < arr.length; i++) {
125
            if (arr[i].compareTo(item) == 0) {
126
                return true;
127
            }
128
        }
129

    
130
        return false;
131
    }
132

    
133
    public void enableControls(boolean enable) {
134
        getFieldsList().setEnabled(enable);
135
        getSelAllFieldsButton().setEnabled(enable);
136
        getDeselAllFieldsButton().setEnabled(enable);
137
    }
138

    
139
    private void initialize() {
140
        setLayout(null);
141
        setBounds(new java.awt.Rectangle(255, 55, 251, 166));
142
        setBorder(javax.swing.BorderFactory.createTitledBorder(null,
143
                PluginServices.getText(this, "table_fields"),
144
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
145
                javax.swing.border.TitledBorder.DEFAULT_POSITION, null, null));
146
        add(getFieldsScrollPane(), null);
147

    
148
        add(getSelAllFieldsButton(), null);
149
        add(getDeselAllFieldsButton(), null);
150
    }
151

    
152
    private JScrollPane getFieldsScrollPane() {
153
        if (fieldsScrollPane == null) {
154
            fieldsScrollPane = new JScrollPane();
155
            fieldsScrollPane.setBounds(new java.awt.Rectangle(5, 20,
156
                    101 + (28 * 5), 76 + (7 * 5)));
157
            fieldsScrollPane.setViewportView(getFieldsList());
158
        }
159

    
160
        return fieldsScrollPane;
161
    }
162

    
163
    private AvailableFieldsCheckBoxList getFieldsList() {
164
        if (fieldsList == null) {
165
            fieldsList = new AvailableFieldsCheckBoxList();
166
        }
167

    
168
        return fieldsList;
169
    }
170

    
171
    private void setAllFieldsInTable(FeatureAttributeDescriptor[] descriptors) {
172
        DefaultListModel lmodel = new DefaultListModel();
173

    
174
        for (int i = 0; i < descriptors.length; i++) {
175
            lmodel.addElement(new FieldsListItem(descriptors[i]));
176
        }
177

    
178
        getFieldsList().setModel(lmodel);
179
        getFieldsScrollPane().setViewportView(fieldsList);
180
        getFieldsScrollPane().updateUI();
181
    }
182

    
183
    private JButton getSelAllFieldsButton() {
184
        if (selAllFieldsButton == null) {
185
            selAllFieldsButton = new JButton();
186
            selAllFieldsButton.addActionListener(this);
187
            selAllFieldsButton.setBounds(new java.awt.Rectangle(28 + 5, 135,
188
                    90, 26));
189
            selAllFieldsButton.setText(PluginServices.getText(this, "_All"));
190
        }
191

    
192
        return selAllFieldsButton;
193
    }
194

    
195
    /**
196
     * This method initializes deselAllFieldsButton
197
     *
198
     * @return javax.swing.JButton
199
     */
200
    private JButton getDeselAllFieldsButton() {
201
        if (deselAllFieldsButton == null) {
202
            deselAllFieldsButton = new JButton();
203
            deselAllFieldsButton.addActionListener(this);
204
            deselAllFieldsButton.setBounds(new java.awt.Rectangle(28 + 100,
205
                    135, 90, 26));
206
            deselAllFieldsButton.setText(PluginServices.getText(this, "_None"));
207
        }
208

    
209
        return deselAllFieldsButton;
210
    }
211

    
212
    public void actionPerformed(ActionEvent e) {
213
        Object src = e.getSource();
214

    
215
        if (src == getDeselAllFieldsButton()) {
216
            getFieldsList().checkAll(false);
217
        }
218

    
219
        if (src == getSelAllFieldsButton()) {
220
            getFieldsList().checkAll(true);
221
        }
222
    }
223

    
224
    private String[] getUserSelectedFields() {
225
        Object[] sel = fieldsList.getCheckedItems();
226
        int size = sel.length;
227

    
228
        String[] resp = new String[size];
229

    
230
        for (int i = 0; i < size; i++) {
231
            resp[i] = ((FieldsListItem) sel[i]).getName();
232
        }
233

    
234
        return resp;
235
    }
236

    
237
    public void repaint() {
238
        super.repaint();
239
        getFieldsList().updateUI();
240
        getFieldsScrollPane().updateUI();
241
    }
242
}