Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / UserSelectedFieldsPanel.java @ 10954

History | View | Annotate | Download (7.22 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.iver.cit.gvsig.sde.gui.sdewizard2;
44

    
45
import com.iver.andami.PluginServices;
46

    
47
import org.gvsig.gui.beans.swing.JButton;
48

    
49
import java.awt.event.ActionEvent;
50
import java.awt.event.ActionListener;
51

    
52
import java.util.ArrayList;
53

    
54
import javax.swing.DefaultListModel;
55
import javax.swing.JPanel;
56
import javax.swing.JScrollPane;
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 String[] fieldNames = 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 WizardSDE parent = null;
73

    
74
    public UserSelectedFieldsPanel(String[] fNames, String[] fTypes,
75
        boolean empty, WizardSDE _p) {
76
        parent = _p;
77
        fieldNames = fNames;
78
        fieldTypes = fTypes;
79
        initialize();
80

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

    
89
    public void loadValues() {
90
        setAllFieldsInTable(fieldNames, fieldTypes);
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
            aux.remove(item);
130

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

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

    
142
        return false;
143
    }
144

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

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

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

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

    
172
        return fieldsScrollPane;
173
    }
174

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

    
180
        return fieldsList;
181
    }
182

    
183
    private void setAllFieldsInTable(String[] all_f, String[] all_t) {
184
        DefaultListModel lmodel = new DefaultListModel();
185

    
186
        for (int i = 0; i < all_f.length; i++) {
187
            lmodel.addElement(new FieldsListItem(all_f[i], all_t[i]));
188
        }
189

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

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

    
204
        return selAllFieldsButton;
205
    }
206

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

    
221
        return deselAllFieldsButton;
222
    }
223

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

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

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

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

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

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

    
246
        return resp;
247
    }
248

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