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 / base / FormSetButtonBar.java @ 2039

History | View | Annotate | Download (21.1 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.base;
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.awt.event.KeyEvent;
31
import java.net.URL;
32
import java.util.ArrayList;
33
import java.util.HashMap;
34
import java.util.HashSet;
35
import java.util.Iterator;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.Objects;
39
import java.util.Set;
40
import javax.swing.Action;
41
import javax.swing.BorderFactory;
42

    
43
import javax.swing.Icon;
44
import javax.swing.ImageIcon;
45
import javax.swing.JButton;
46
import javax.swing.JComponent;
47
import javax.swing.JOptionPane;
48
import javax.swing.JPanel;
49
import javax.swing.SwingConstants;
50
import org.apache.commons.lang3.StringUtils;
51

    
52
import org.gvsig.tools.ToolsLocator;
53
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CANCEL_NEW;
54
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CLOSE;
55
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE;
56
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_FIRST;
57
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
58
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEXT;
59
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_PREVIOUS;
60
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_LAST;
61
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE;
62
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH;
63
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SET_CURRENT_RECORD;
64
import org.gvsig.tools.i18n.I18nManager;
65
import org.gvsig.tools.swing.api.Component;
66
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
68

    
69
public class FormSetButtonBar implements Component {
70

    
71
    protected static final Logger LOGGER = LoggerFactory
72
            .getLogger(FormSetButtonBar.class);
73

    
74
    public static final int ID_ACTION_FIRST = 1;
75
    public static final int ID_ACTION_PREVIOUS = 2;
76
    public static final int ID_ACTION_NEXT = 3;
77
    public static final int ID_ACTION_LAST = 4;
78
    public static final int ID_ACTION_SAVE = 5;
79
    public static final int ID_ACTION_NEW = 6;
80
    public static final int ID_ACTION_CANCEL_NEW = 7;
81
    public static final int ID_ACTION_DELETE = 8;
82
    public static final int ID_ACTION_SEARCH = 9;
83
    public static final int ID_ACTION_CLOSE = 10;
84
    public static final int ID_ACTION_SET_CURRENT_RECORD = 11;
85

    
86
    private static final int MAX_ACTIONS = 11;
87

    
88
    private static final int COUNTER_HEIGHT = 50;
89
    private static final int COUNTER_WIDTH = 100;
90

    
91
    public interface FormSetListener {
92

    
93
        public boolean doActionFirst();
94

    
95
        public boolean doActionPrevious();
96

    
97
        public boolean doActionNext();
98

    
99
        public boolean doActionLast();
100

    
101
        public boolean doActionSave();
102

    
103
        public boolean doActionNew();
104

    
105
        public boolean doActionCancelNew();
106

    
107
        public boolean doActionDelete();
108

    
109
        public boolean doActionSearch();
110

    
111
        public boolean doActionClose();
112

    
113
        public void doSetCurrentRecord(int current);
114
    }
115

    
116
    private JPanel contents = null;
117
    private Set listeners = null;
118
    private int current = 0;
119
    private String currentLabel = null;
120
    private int numrecords = 0;
121

    
122
    private JPanel othersButtons;
123
    private final List<Action> otherActions;
124
    private final Map<String, JButton> otherButtons;
125
    
126
    private final Map<String,Integer> actionNameToId;
127

    
128
    private boolean readOnly = false;
129

    
130
    private JButton records;
131
    private final JButton buttons[] = new JButton[MAX_ACTIONS];
132
    private final Boolean activatedActions[] = new Boolean[MAX_ACTIONS];
133

    
134

    
135
    public FormSetButtonBar() {
136
        // Activamos todas las acciones por defecto
137
        for (int i = 0; i < this.activatedActions.length; i++) {
138
            this.activatedActions[i] = true;
139
        }
140
        this.actionNameToId = new HashMap();
141
        this.actionNameToId.put(ACTION_FIRST, ID_ACTION_FIRST);
142
        this.actionNameToId.put(ACTION_PREVIOUS, ID_ACTION_PREVIOUS);
143
        this.actionNameToId.put(ACTION_NEXT, ID_ACTION_NEXT);
144
        this.actionNameToId.put(ACTION_LAST, ID_ACTION_LAST);
145
        this.actionNameToId.put(ACTION_SAVE, ID_ACTION_SAVE);
146
        this.actionNameToId.put(ACTION_NEW, ID_ACTION_NEW);
147
        this.actionNameToId.put(ACTION_CANCEL_NEW, ID_ACTION_CANCEL_NEW);
148
        this.actionNameToId.put(ACTION_DELETE, ID_ACTION_DELETE);
149
        this.actionNameToId.put(ACTION_SEARCH, ID_ACTION_SEARCH);
150
        this.actionNameToId.put(ACTION_CLOSE, ID_ACTION_CLOSE);
151
        this.actionNameToId.put(ACTION_SET_CURRENT_RECORD, ID_ACTION_SET_CURRENT_RECORD);
152

    
153
        this.listeners = new HashSet();
154
        this.otherActions = new ArrayList<>();
155
        this.otherButtons = new HashMap<>();
156
    }
157

    
158
    @Override
159
    public JComponent asJComponent() {
160
        if (this.contents == null) {
161
            this.initComponents();
162
        }
163
        return this.contents;
164
    }
165

    
166
    public void addListener(FormSetListener listener) {
167
        this.listeners.add(listener);
168
    }
169

    
170
    public void removeListener(FormSetListener listener) {
171
        this.listeners.remove(listener);
172
    }
173

    
174
    public void fireEvent(String action, Object value) {
175
        
176
        Integer actionid = this.actionNameToId.get(action);
177
        if( actionid==null ) {
178
            JButton button = this.otherButtons.get(action);
179
            if( button != null && button.getAction()!=null ) {
180
                button.getAction().actionPerformed(new ActionEvent(value, 1, action));
181
            }
182
            
183
        } else {
184
            if (this.readOnly) {
185
                Iterator it = this.listeners.iterator();
186
                while (it.hasNext()) {
187
                    FormSetListener listener = (FormSetListener) it.next();
188
                    try {
189
                        switch (actionid) {
190
                            case ID_ACTION_FIRST:
191
                                listener.doActionFirst();
192
                                break;
193
                            case ID_ACTION_PREVIOUS:
194
                                listener.doActionPrevious();
195
                                break;
196
                            case ID_ACTION_NEXT:
197
                                listener.doActionNext();
198
                                break;
199
                            case ID_ACTION_LAST:
200
                                listener.doActionLast();
201
                                break;
202
                            case ID_ACTION_SEARCH:
203
                                listener.doActionSearch();
204
                                break;
205
                            case ID_ACTION_CLOSE:
206
                                listener.doActionClose();
207
                                break;
208
                            case ID_ACTION_SET_CURRENT_RECORD:
209
                                listener.doSetCurrentRecord(((Integer) value));
210
                                break;
211
                        }
212
                    } catch (Exception ex) {
213
                        LOGGER.info("Error calling listener " + listener.toString()
214
                                + "(" + listener.getClass().getName() + ") from "
215
                                + this.toString() + "(" + this.getClass().getName()
216
                                + ").", ex);
217
                    }
218
                }
219
            } else {
220
                Iterator it = this.listeners.iterator();
221
                while (it.hasNext()) {
222
                    FormSetListener listener = (FormSetListener) it.next();
223
                    try {
224
                        switch (actionid) {
225
                            case ID_ACTION_FIRST:
226
                                listener.doActionFirst();
227
                                break;
228
                            case ID_ACTION_PREVIOUS:
229
                                listener.doActionPrevious();
230
                                break;
231
                            case ID_ACTION_NEXT:
232
                                listener.doActionNext();
233
                                break;
234
                            case ID_ACTION_LAST:
235
                                listener.doActionLast();
236
                                break;
237
                            case ID_ACTION_SAVE:
238
                                listener.doActionSave();
239
                                break;
240
                            case ID_ACTION_NEW:
241
                                listener.doActionNew();
242
                                break;
243
                            case ID_ACTION_CANCEL_NEW:
244
                                listener.doActionCancelNew();
245
                                break;
246
                            case ID_ACTION_DELETE:
247
                                listener.doActionDelete();
248
                                break;
249
                            case ID_ACTION_SEARCH:
250
                                listener.doActionSearch();
251
                                break;
252
                            case ID_ACTION_CLOSE:
253
                                listener.doActionClose();
254
                                break;
255
                            case ID_ACTION_SET_CURRENT_RECORD:
256
                                listener.doSetCurrentRecord(((Integer) value));
257
                                break;
258
                        }
259
                    } catch (Exception ex) {
260
                        LOGGER.info("Error calling listener " + listener.toString()
261
                                + "(" + listener.getClass().getName() + ") from "
262
                                + this.toString() + "(" + this.getClass().getName()
263
                                + ").", ex);
264
                    }
265
                }
266
            }
267
        }
268
    }
269

    
270
    private void updateRecords() {
271
        setEnabled(ACTION_NEW, true);
272
        if (numrecords <= 0) {
273
            this.records.setText("0 / 0");
274
            setEnabled(ID_ACTION_FIRST, false);
275
            setEnabled(ID_ACTION_PREVIOUS, false);
276
            setEnabled(ID_ACTION_NEXT, false);
277
            setEnabled(ACTION_LAST, false);
278
            return;
279
        }
280
        if (this.currentLabel == null) {
281
            this.records.setText((current + 1) + " / " + numrecords);
282
        } else {
283
            this.records.setText(this.currentLabel + " / " + numrecords);
284
        }
285
        if (current <= 0) {
286
            current = 0;
287
            setEnabled(ID_ACTION_FIRST, false);
288
            setEnabled(ID_ACTION_PREVIOUS, false);
289
            setEnabled(ID_ACTION_NEXT, true);
290
            setEnabled(ACTION_LAST, true);
291
        } else if (current >= numrecords - 1) {
292
            current = numrecords - 1;
293
            setEnabled(ID_ACTION_NEXT, false);
294
            setEnabled(ACTION_LAST, false);
295
            setEnabled(ID_ACTION_FIRST, true);
296
            setEnabled(ID_ACTION_PREVIOUS, true);
297
        } else {
298
            setEnabled(ID_ACTION_NEXT, true);
299
            setEnabled(ACTION_LAST, true);
300
            setEnabled(ID_ACTION_FIRST, true);
301
            setEnabled(ID_ACTION_PREVIOUS, true);
302
        }
303
    }
304

    
305
    private void initComponents() {
306
        this.contents = new JPanel();
307
        this.contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
308

    
309
        this.contents.add(getStandardButtonBar());
310

    
311
        this.othersButtons = new JPanel();
312
        this.othersButtons.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
313
        this.contents.add(this.othersButtons);
314

    
315
        for (Action action : this.otherActions) {
316
            this.othersButtons.add(new JButton(action));
317
        }
318

    
319
        this.contents.add(createButton("Close", "close.png", ACTION_CLOSE, KeyEvent.VK_UNDEFINED));
320
        this.initButtons();
321
        this.updateRecords();
322
    }
323

    
324
    private JPanel getStandardButtonBar() {
325
        I18nManager i18nManager = ToolsLocator.getI18nManager();
326
        JPanel contents = new JPanel();
327
        contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
328

    
329
        JComponent firstButton = createButton(i18nManager.getTranslation("_first"), "first.png", ACTION_FIRST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_UP);
330
        contents.add(firstButton);
331
        contents.add(createButton(i18nManager.getTranslation("_back"), "previous.png", ACTION_PREVIOUS, KeyEvent.VK_PAGE_UP));
332

    
333
        this.records = new JButton();
334
        this.records.setCursor(new Cursor(Cursor.HAND_CURSOR));
335
        this.records.setBorder(BorderFactory.createEmptyBorder());
336
        this.records.setBorderPainted(false);
337
        this.records.setFocusPainted(false);
338
        this.records.setContentAreaFilled(false);
339
        this.records.addActionListener(new ActionListener() {
340

    
341
            @Override
342
            public void actionPerformed(ActionEvent ae) {
343
                doGoRecord();
344
            }
345
        });
346
        int height = this.records.getFont().getSize();
347
        if (height < 16) {
348
            height = 16;
349
        }
350
        this.records.setPreferredSize(new Dimension(COUNTER_WIDTH, height));
351
        this.records.setHorizontalAlignment(SwingConstants.CENTER);
352
        contents.add(this.records);
353

    
354
        contents.add(createButton(i18nManager.getTranslation("_next"), "next.png", ACTION_NEXT, KeyEvent.VK_PAGE_DOWN));
355
        contents.add(createButton(i18nManager.getTranslation("_last"), "last.png", ACTION_LAST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_DOWN));
356

    
357
        contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ACTION_SAVE, KeyEvent.CTRL_MASK + KeyEvent.VK_S));
358
        contents.add(createButton(i18nManager.getTranslation("nuevo"), "new.png", ACTION_NEW, KeyEvent.VK_F6));
359
        contents.add(createButton(i18nManager.getTranslation("cancelar nuevo"), "cancelnew.png", ACTION_CANCEL_NEW, KeyEvent.VK_F12));
360
        contents.add(createButton(i18nManager.getTranslation("borrar"), "delete.png", ACTION_DELETE, KeyEvent.SHIFT_MASK + KeyEvent.VK_F2));
361
        contents.add(createButton(i18nManager.getTranslation("search"), "search.png", ACTION_SEARCH, KeyEvent.CTRL_MASK + KeyEvent.VK_F));
362

    
363
        return contents;
364
    }
365

    
366
    private void doGoRecord() {
367
        I18nManager i18nManager = ToolsLocator.getI18nManager();
368
        String value = JOptionPane.showInputDialog(
369
                this.records,
370
                i18nManager.getTranslation("_Enter_the_registration_number_you_want_to_go"),
371
                i18nManager.getTranslation("_Go_to_record"),
372
                JOptionPane.QUESTION_MESSAGE
373
        );
374
        if (StringUtils.isEmpty(value)) {
375
            return;
376
        }
377
        int record;
378
        try {
379
            record = Integer.parseInt(value);
380
        } catch (Exception ex) {
381
            JOptionPane.showMessageDialog(
382
                    this.records,
383
                    i18nManager.getTranslation("_invalid_record_number"),
384
                    i18nManager.getTranslation("_Go_to_record"),
385
                    JOptionPane.WARNING_MESSAGE
386
            );
387
            return;
388
        }
389
        fireEvent(ACTION_SET_CURRENT_RECORD, record);
390
    }
391

    
392
    private void initButtons() {
393
        setActionActive(ID_ACTION_SAVE, isActionActive(ID_ACTION_SAVE));
394
        setActionActive(ID_ACTION_NEW, isActionActive(ID_ACTION_NEW));
395
        setActionActive(ID_ACTION_SEARCH, isActionActive(ID_ACTION_SEARCH));
396
        setActionActive(ID_ACTION_DELETE, isActionActive(ID_ACTION_DELETE));
397
        setActionActive(ID_ACTION_CLOSE, isActionActive(ID_ACTION_CLOSE));
398
    }
399

    
400
    private JComponent createButton(final String tip, final String image, final String action, int mnemonic) {
401
        int actionid = this.actionNameToId.get(action);
402
        
403
        URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/" + image);
404
        Icon icon = new ImageIcon(url);
405
        JButton button = new JButton(icon);
406
        if (mnemonic != KeyEvent.VK_UNDEFINED) {
407
            button.setMnemonic(mnemonic);
408
        }
409
        button.setBorder(BorderFactory.createEmptyBorder());
410
        button.setBorderPainted(false);
411
        button.setFocusPainted(false);
412
        button.setContentAreaFilled(false);
413
        button.setToolTipText(tip);
414
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
415
        button.addActionListener(new ActionListener() {
416

    
417
            @Override
418
            public void actionPerformed(ActionEvent ae) {
419
                fireEvent(action, null);
420
            }
421
        });
422
        this.buttons[actionid] = button;
423
        return button;
424
    }
425

    
426
    public void setEnabled(int action, boolean enabled) {
427
        JButton button = this.buttons[action];
428
        if (enabled) {
429
            button.setEnabled(true);
430
        } else {
431
            button.setEnabled(false);
432
        }
433
    }
434

    
435
    public void setVisible(int action, boolean visible) {
436
        JButton button = this.buttons[action];
437
        if (visible) {
438
            button.setVisible(true);
439
        } else {
440
            button.setVisible(false);
441
        }
442
    }
443

    
444
    public void setVisible(String id, boolean visible) {
445
        Integer x = this.actionNameToId.get(id);
446
        if( x!=null ) {
447
            this.buttons[x].setVisible(visible);
448
            return;
449
        }
450
        JButton button = this.otherButtons.get(id);
451
        if (button == null) {
452
            return;
453
        }
454
        button.setVisible(visible);
455
    }
456

    
457
    public void setEnabled(String id, boolean enabled) {
458
        Integer x = this.actionNameToId.get(id);
459
        if( x!=null ) { // Ojo, si quito el intValue a veces falla
460
            this.buttons[x.intValue()].setEnabled(enabled);
461
            return;
462
        }
463
        JButton button = this.otherButtons.get(id);
464
        if (button == null) {
465
            return;
466
        }
467
        button.setEnabled(enabled);
468
    }
469

    
470
    public boolean isEnabled(String id) {
471
        Integer x = this.actionNameToId.get(id);
472
        if( x!=null ) {
473
            return this.buttons[x].isEnabled();
474
        }
475
        JButton button = this.otherButtons.get(id);
476
        if (button == null) {
477
            throw new IllegalArgumentException("Don't exists action '" + id + "'.");
478
        }
479
        return button.isEnabled();
480
    }
481

    
482
    public JComponent getActionButton(String id) {
483
        Integer x = this.actionNameToId.get(id);
484
        if( x!=null ) {
485
            return this.buttons[x];
486
        }
487
        JButton button = this.otherButtons.get(id);
488
        return button;
489
    }
490

    
491
    public boolean isVisible(String id) {
492
        Integer x = this.actionNameToId.get(id);
493
        if( x!=null ) {
494
            return this.buttons[x].isVisible();
495
        }
496
        JButton button = this.otherButtons.get(id);
497
        if (button == null) {
498
            throw new IllegalArgumentException("Don't exists action '" + id + "'.");
499
        }
500
        return button.isVisible();
501
    }
502

    
503
    public void setCurrent(int current) {
504
        this.current = current;
505
        updateRecords();
506
    }
507

    
508
    public void setCurrentLabel(String label) {
509
        this.currentLabel = label;
510
        updateRecords();
511
    }
512

    
513
    public void setNumrecords(int numrecords) {
514
        this.numrecords = numrecords;
515
        updateRecords();
516
    }
517

    
518
    public void setActionActive(int action, boolean active) {
519
        if (action == ID_ACTION_SAVE || action == ID_ACTION_DELETE
520
                || action == ID_ACTION_NEW || action == ID_ACTION_SEARCH
521
                || action == ID_ACTION_CLOSE) {
522
            if (this.contents == null) {
523
                this.initComponents();
524
            }
525
            this.activatedActions[action] = active;
526
            this.buttons[action].setVisible(active);
527
        }
528
    }
529

    
530
    public boolean isActionActive(int action) {
531
        return this.activatedActions[action];
532
    }
533

    
534
    public void setReadOnly(boolean readOnly) {
535
        this.readOnly = readOnly;
536

    
537
        this.setEnabled(ID_ACTION_SAVE, !readOnly);
538
        this.setEnabled(ACTION_NEW, !readOnly);
539
        this.setEnabled(ID_ACTION_CANCEL_NEW, !readOnly);
540
        this.setEnabled(ACTION_DELETE, !readOnly);
541
        this.setEnabled(ID_ACTION_SEARCH, true);
542
        this.setEnabled(ACTION_CLOSE, true);
543

    
544
        updateRecords();
545
    }
546

    
547
    public void addAction(Action action) {
548
        this.otherActions.add(action);
549
        if (this.contents == null) {
550
            return;
551
        }
552
        JButton button = new JButton(action);
553
        button.setBorder(BorderFactory.createEmptyBorder());
554
        button.setBorderPainted(false);
555
        button.setFocusPainted(false);
556
        button.setContentAreaFilled(false);
557
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
558
        String id = Objects.toString(action.getValue(Action.ACTION_COMMAND_KEY), null);
559
        if (!StringUtils.isEmpty(id)) {
560
            this.otherButtons.put(id, button);
561
        }
562
        this.othersButtons.add(button);
563
    }
564
}