Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.dynform / org.gvsig.tools.dynform.services / src / main / java / org / gvsig / tools / dynform / services / dynformset / subform / FormSetButtonBar.java @ 1405

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

    
25
import java.awt.Cursor;
26
import java.awt.Dimension;
27
import java.awt.FlowLayout;
28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
30
import java.net.URL;
31
import java.util.ArrayList;
32
import java.util.HashSet;
33
import java.util.Iterator;
34
import java.util.List;
35
import java.util.Set;
36
import javax.swing.Action;
37
import javax.swing.BorderFactory;
38

    
39
import javax.swing.Icon;
40
import javax.swing.ImageIcon;
41
import javax.swing.JButton;
42
import javax.swing.JComponent;
43
import javax.swing.JOptionPane;
44
import javax.swing.JPanel;
45
import javax.swing.SwingConstants;
46
import org.apache.commons.lang3.StringUtils;
47

    
48
import org.gvsig.tools.ToolsLocator;
49
import org.gvsig.tools.i18n.I18nManager;
50
import org.gvsig.tools.swing.api.Component;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
public class FormSetButtonBar implements Component {
55

    
56
    protected static final Logger logger = LoggerFactory
57
            .getLogger(FormSetButtonBar.class);
58

    
59
    public static final int ActionFirst = 1;
60
    public static final int ActionPrevious = 2;
61
    public static final int ActionNext = 3;
62
    public static final int ActionLast = 4;
63
    public static final int ActionSave = 5;
64
    public static final int ActionNew = 6;
65
    public static final int ActionCancelNew = 7;
66
    public static final int ActionDelete = 8;
67
    public static final int ActionSearch = 9;
68
    public static final int ActionClose = 10;
69
    public static final int ActionSetCurrentRecord = 11;
70

    
71
    private static final int MAX_ACTIONS = 11;
72

    
73
    public Boolean activatedActions[] = new Boolean[MAX_ACTIONS];
74

    
75
    private static final int COUNTER_HEIGHT = 50;
76
    private static final int COUNTER_WIDTH = 100;
77

    
78
    public interface FormSetListener {
79

    
80
        public boolean doActionFirst();
81

    
82
        public boolean doActionPrevious();
83

    
84
        public boolean doActionNext();
85

    
86
        public boolean doActionLast();
87

    
88
        public boolean doActionSave();
89

    
90
        public boolean doActionNew();
91

    
92
        public boolean doActionCancelNew();
93

    
94
        public boolean doActionDelete();
95

    
96
        public boolean doActionSearch();
97

    
98
        public boolean doActionClose();
99

    
100
        public void doSetCurrentRecord(int current);
101
    }
102

    
103
    private JPanel contents = null;
104
    private Set listeners = null;
105
    private int current = 0;
106
    private String currentLabel = null;
107
    private int numrecords = 0;
108

    
109
    private JPanel othersButtons;
110
    private final List<Action> otherActions;
111

    
112
    private boolean readOnly = false;
113

    
114
    private JButton records;
115
    private final JButton buttons[] = new JButton[MAX_ACTIONS];
116

    
117
    public FormSetButtonBar() {
118
        // Activamos todas las acciones por defecto
119
        for (int i = 0; i < this.activatedActions.length; i++) {
120
            this.activatedActions[i] = true;
121
        }
122
        this.listeners = new HashSet();
123
        this.otherActions = new ArrayList<>();
124
    }
125

    
126
    @Override
127
    public JComponent asJComponent() {
128
        if (this.contents == null) {
129
            this.initComponents();
130
        }
131
        return this.contents;
132
    }
133

    
134
    public void addListener(FormSetListener listener) {
135
        this.listeners.add(listener);
136
    }
137

    
138
    public void removeListener(FormSetListener listener) {
139
        this.listeners.remove(listener);
140
    }
141

    
142
    protected void fireEvent(int eventid, Object value) {
143
        if (this.readOnly) {
144
            Iterator it = this.listeners.iterator();
145
            while (it.hasNext()) {
146
                FormSetListener listener = (FormSetListener) it.next();
147
                try {
148
                    switch (eventid) {
149
                        case ActionFirst:
150
                            listener.doActionFirst();
151
                            break;
152
                        case ActionPrevious:
153
                            listener.doActionPrevious();
154
                            break;
155
                        case ActionNext:
156
                            listener.doActionNext();
157
                            break;
158
                        case ActionLast:
159
                            listener.doActionLast();
160
                            break;
161
                        case ActionSearch:
162
                            listener.doActionSearch();
163
                            break;
164
                        case ActionClose:
165
                            listener.doActionClose();
166
                            break;
167
                        case ActionSetCurrentRecord:
168
                            listener.doSetCurrentRecord(((Integer) value));
169
                            break;
170
                    }
171
                } catch (Exception ex) {
172
                    logger.info("Error calling listener " + listener.toString()
173
                            + "(" + listener.getClass().getName() + ") from "
174
                            + this.toString() + "(" + this.getClass().getName()
175
                            + ").", ex);
176
                }
177
            }
178
        } else {
179
            Iterator it = this.listeners.iterator();
180
            while (it.hasNext()) {
181
                FormSetListener listener = (FormSetListener) it.next();
182
                try {
183
                    switch (eventid) {
184
                        case ActionFirst:
185
                            listener.doActionFirst();
186
                            break;
187
                        case ActionPrevious:
188
                            listener.doActionPrevious();
189
                            break;
190
                        case ActionNext:
191
                            listener.doActionNext();
192
                            break;
193
                        case ActionLast:
194
                            listener.doActionLast();
195
                            break;
196
                        case ActionSave:
197
                            listener.doActionSave();
198
                            break;
199
                        case ActionNew:
200
                            listener.doActionNew();
201
                            break;
202
                        case ActionCancelNew:
203
                            listener.doActionCancelNew();
204
                            break;
205
                        case ActionDelete:
206
                            listener.doActionDelete();
207
                            break;
208
                        case ActionSearch:
209
                            listener.doActionSearch();
210
                            break;
211
                        case ActionClose:
212
                            listener.doActionClose();
213
                            break;
214
                        case ActionSetCurrentRecord:
215
                            listener.doSetCurrentRecord(((Integer) value));
216
                            break;
217
                    }
218
                } catch (Exception ex) {
219
                    logger.info("Error calling listener " + listener.toString()
220
                            + "(" + listener.getClass().getName() + ") from "
221
                            + this.toString() + "(" + this.getClass().getName()
222
                            + ").", ex);
223
                }
224
            }
225
        }
226
    }
227

    
228
    private void updateRecords() {
229
        setEnabled(ActionNew, true);
230
        if (numrecords <= 0) {
231
            this.records.setText("0 / 0");
232
            setEnabled(ActionFirst, false);
233
            setEnabled(ActionPrevious, false);
234
            setEnabled(ActionNext, false);
235
            setEnabled(ActionLast, false);
236
            return;
237
        }
238
        if( this.currentLabel==null ) {
239
            this.records.setText((current + 1) + " / " + numrecords);
240
        } else {
241
            this.records.setText(this.currentLabel + " / " + numrecords);
242
        }
243
        if (current <= 0) {
244
            current = 0;
245
            setEnabled(ActionFirst, false);
246
            setEnabled(ActionPrevious, false);
247
            setEnabled(ActionNext, true);
248
            setEnabled(ActionLast, true);
249
        } else if (current >= numrecords - 1) {
250
            current = numrecords - 1;
251
            setEnabled(ActionNext, false);
252
            setEnabled(ActionLast, false);
253
            setEnabled(ActionFirst, true);
254
            setEnabled(ActionPrevious, true);
255
        } else {
256
            setEnabled(ActionNext, true);
257
            setEnabled(ActionLast, true);
258
            setEnabled(ActionFirst, true);
259
            setEnabled(ActionPrevious, true);
260
        }
261
    }
262

    
263
    private void initComponents() {
264
        this.contents = new JPanel();
265
        this.contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
266

    
267
        this.contents.add(getStandardButtonBar());
268

    
269
        this.othersButtons = new JPanel();
270
        this.othersButtons.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
271
        this.contents.add(this.othersButtons);
272

    
273
        for (Action action : this.otherActions) {
274
            this.othersButtons.add(new JButton(action));
275
        }
276

    
277
        this.contents.add(createButton("Close", "close.png", ActionClose));
278
        this.initButtons();
279
        this.updateRecords();
280
    }
281

    
282
    private JPanel getStandardButtonBar() {
283
        I18nManager i18nManager = ToolsLocator.getI18nManager();
284
        JPanel contents = new JPanel();
285
        contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
286

    
287
        JComponent firstButton = createButton(i18nManager.getTranslation("_first"), "first.png", ActionFirst);
288
        contents.add(firstButton);
289
        contents.add(createButton(i18nManager.getTranslation("_back"), "previous.png", ActionPrevious));
290

    
291
        this.records = new JButton();
292
        this.records.setCursor(new Cursor(Cursor.HAND_CURSOR));
293
        this.records.setBorder(BorderFactory.createEmptyBorder());
294
        this.records.setBorderPainted(false);
295
        this.records.setFocusPainted(false);
296
        this.records.setContentAreaFilled(false);
297
        this.records.addActionListener(new ActionListener() {
298

    
299
            @Override
300
            public void actionPerformed(ActionEvent ae) {
301
                doGoRecord();
302
            }
303
        });
304
        int height = this.records.getFont().getSize();
305
        if (height < 16) {
306
            height = 16;
307
        }
308
        this.records.setPreferredSize(new Dimension(COUNTER_WIDTH, height));
309
        this.records.setHorizontalAlignment(SwingConstants.CENTER);
310
        contents.add(this.records);
311

    
312
        contents.add(createButton(i18nManager.getTranslation("_next"), "next.png", ActionNext));
313
        contents.add(createButton(i18nManager.getTranslation("_last"), "last.png", ActionLast));
314

    
315
        contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ActionSave));
316
        contents.add(createButton(i18nManager.getTranslation("nuevo"), "new.png", ActionNew));
317
        contents.add(createButton(i18nManager.getTranslation("cancelar nuevo"), "cancelnew.png", ActionCancelNew));
318
        contents.add(createButton(i18nManager.getTranslation("borrar"), "delete.png", ActionDelete));
319
        contents.add(createButton(i18nManager.getTranslation("search"), "search.png", ActionSearch));
320

    
321
        return contents;
322
    }
323

    
324
    private void doGoRecord() {
325
        I18nManager i18nManager = ToolsLocator.getI18nManager();
326
        String value = JOptionPane.showInputDialog(
327
                this.records,
328
                i18nManager.getTranslation("_Enter_the_registration_number_you_want_to_go"),
329
                i18nManager.getTranslation("_Go_to_record"),
330
                JOptionPane.QUESTION_MESSAGE
331
        );
332
        if (StringUtils.isEmpty(value)) {
333
            return;
334
        }
335
        int record;
336
        try {
337
            record = Integer.parseInt(value);
338
        } catch (Exception ex) {
339
            JOptionPane.showMessageDialog(
340
                    this.records,
341
                    i18nManager.getTranslation("_invalid_record_number"),
342
                    i18nManager.getTranslation("_Go_to_record"),
343
                    JOptionPane.WARNING_MESSAGE
344
            );
345
            return;
346
        }
347
        fireEvent(ActionSetCurrentRecord, record);
348
    }
349

    
350
    private void initButtons() {
351
        setActionActive(ActionSave, isActionActive(ActionSave));
352
        setActionActive(ActionNew, isActionActive(ActionNew));
353
        setActionActive(ActionSearch, isActionActive(ActionSearch));
354
        setActionActive(ActionDelete, isActionActive(ActionDelete));
355
        setActionActive(ActionClose, isActionActive(ActionClose));
356
    }
357

    
358
    private JComponent createButton(final String tip, final String image, final int action) {
359
        URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/" + image);
360
        Icon icon = new ImageIcon(url);
361
        JButton button = new JButton(icon);
362
        button.setBorder(BorderFactory.createEmptyBorder());
363
        button.setBorderPainted(false);
364
        button.setFocusPainted(false);
365
        button.setContentAreaFilled(false);
366
        button.setToolTipText(tip);
367
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
368
        button.addActionListener(new ActionListener() {
369

    
370
            @Override
371
            public void actionPerformed(ActionEvent ae) {
372
                fireEvent(action, null);
373
            }
374
        });
375
        this.buttons[action] = button;
376
        return button;
377
    }
378

    
379
    public void setEnabled(int action, boolean enabled) {
380
        JButton button = this.buttons[action];
381
        if (enabled) {
382
            button.setEnabled(true);
383
        } else {
384
            button.setEnabled(false);
385
        }
386
    }
387

    
388
    public void setVisible(int action, boolean visible) {
389
        JButton button = this.buttons[action];
390
        if (visible) {
391
            button.setVisible(true);
392
        } else {
393
            button.setVisible(false);
394
        }
395
    }
396

    
397
    public void setCurrent(int current) {
398
        this.current = current;
399
        updateRecords();
400
    }
401

    
402
    public void setCurrentLabel(String label) {
403
        this.currentLabel = label;
404
        updateRecords();
405
    }
406

    
407
    public void setNumrecords(int numrecords) {
408
        this.numrecords = numrecords;
409
        updateRecords();
410
    }
411

    
412
    public void setActionActive(int action, boolean active) {
413
        if (action == ActionSave || action == ActionDelete
414
                || action == ActionNew || action == ActionSearch
415
                || action == ActionClose) {
416
            if (this.contents == null) {
417
                this.initComponents();
418
            }
419
            this.activatedActions[action] = active;
420
            this.buttons[action].setVisible(active);
421
        }
422
    }
423

    
424
    public boolean isActionActive(int action) {
425
        return this.activatedActions[action];
426
    }
427

    
428
    public void setReadOnly(boolean readOnly) {
429
        this.readOnly = readOnly;
430
        for (int i = 0; i < buttons.length; i++) {
431
            if (isActionActive(i) && this.buttons[i] != null) {
432
                this.buttons[i].setEnabled(!readOnly);
433
            }
434
            if (!readOnly) {
435
                updateRecords();
436
            }
437
        }
438
    }
439

    
440
    public void addAction(Action action) {
441
        this.otherActions.add(action);
442
        if (this.contents == null) {
443
            return;
444
        }
445
        JButton button = new JButton(action);
446
        button.setBorder(BorderFactory.createEmptyBorder());
447
        button.setBorderPainted(false);
448
        button.setFocusPainted(false);
449
        button.setContentAreaFilled(false);
450
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
451
        this.othersButtons.add(button);
452
    }
453

    
454
}