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 @ 2537

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

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

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

    
68
@SuppressWarnings("UseSpecificCatch")
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
        I18nManager i18n = ToolsLocator.getI18nManager();
319
        this.contents.add(createButton(i18n.getTranslation("_Close"), "form-buttonbar-close", ACTION_CLOSE, KeyEvent.VK_UNDEFINED));
320
        this.initButtons();
321
        this.updateRecords();
322
    }
323

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

    
329
        JComponent firstButton = createButton(i18nManager.getTranslation("_first"), "form-buttonbar-first", ACTION_FIRST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_UP);
330
        theContents.add(firstButton);
331
        theContents.add(createButton(i18nManager.getTranslation("_back"), "form-buttonbar-previous", 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((ActionEvent ae) -> { doGoRecord(); });
340
        int height = this.records.getFont().getSize();
341
        if (height < 16) {
342
            height = 16;
343
        }
344
        this.records.setPreferredSize(new Dimension(COUNTER_WIDTH, height));
345
        this.records.setHorizontalAlignment(SwingConstants.CENTER);
346
        theContents.add(this.records);
347

    
348
        theContents.add(createButton(i18nManager.getTranslation("_next"), "form-buttonbar-next", ACTION_NEXT, KeyEvent.VK_PAGE_DOWN));
349
        theContents.add(createButton(i18nManager.getTranslation("_last"), "form-buttonbar-last", ACTION_LAST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_DOWN));
350

    
351
        theContents.add(createButton(i18nManager.getTranslation("guardar"), "form-buttonbar-save", ACTION_SAVE, KeyEvent.CTRL_MASK + KeyEvent.VK_S));
352
        theContents.add(createButton(i18nManager.getTranslation("nuevo"), "form-buttonbar-new", ACTION_NEW, KeyEvent.VK_F6));
353
        theContents.add(createButton(i18nManager.getTranslation("cancelar nuevo"), "form-buttonbar-cancelnew", ACTION_CANCEL_NEW, KeyEvent.VK_F12));
354
        theContents.add(createButton(i18nManager.getTranslation("borrar"), "form-buttonbar-delete", ACTION_DELETE, KeyEvent.SHIFT_MASK + KeyEvent.VK_F2));
355
        theContents.add(createButton(i18nManager.getTranslation("search"), "form-buttonbar-search", ACTION_SEARCH, KeyEvent.CTRL_MASK + KeyEvent.VK_F));
356

    
357
        return theContents;
358
    }
359

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

    
386
    private void initButtons() {
387
        setActionActive(ID_ACTION_SAVE, isActionActive(ID_ACTION_SAVE));
388
        setActionActive(ID_ACTION_NEW, isActionActive(ID_ACTION_NEW));
389
        setActionActive(ID_ACTION_SEARCH, isActionActive(ID_ACTION_SEARCH));
390
        setActionActive(ID_ACTION_DELETE, isActionActive(ID_ACTION_DELETE));
391
        setActionActive(ID_ACTION_CLOSE, isActionActive(ID_ACTION_CLOSE));
392
    }
393

    
394
    private JComponent createButton(final String tip, final String image, final String action, int mnemonic) {
395
        int actionid = this.actionNameToId.get(action);
396

    
397
        IconTheme iconTheme = ToolsSwingLocator.getIconThemeManager().getCurrent();
398
        ImageIcon icon = iconTheme.get(image);
399
//                
400
//        URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/images/" + image+".png");
401
//        Icon icon = new ImageIcon(url);
402
        JButton button = new JButton(icon);
403
        if (mnemonic != KeyEvent.VK_UNDEFINED) {
404
            button.setMnemonic(mnemonic);
405
        }
406
        button.setBorder(BorderFactory.createEmptyBorder());
407
        button.setBorderPainted(false);
408
        button.setFocusPainted(false);
409
        button.setContentAreaFilled(false);
410
        button.setToolTipText(tip);
411
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
412
        button.addActionListener((ActionEvent ae) -> { fireEvent(action, null); });
413
        this.buttons[actionid] = button;
414
        return button;
415
    }
416

    
417
    public void setEnabled(int action, boolean enabled) {
418
        JButton button = this.buttons[action];
419
        if (enabled) {
420
            button.setEnabled(true);
421
        } else {
422
            button.setEnabled(false);
423
        }
424
    }
425

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

    
435
    public void setVisible(String id, boolean visible) {
436
        Integer x = this.actionNameToId.get(id);
437
        if( x!=null ) {
438
            this.buttons[x].setVisible(visible);
439
            return;
440
        }
441
        JButton button = this.otherButtons.get(id);
442
        if (button == null) {
443
            return;
444
        }
445
        button.setVisible(visible);
446
    }
447

    
448
    public void setEnabled(String id, boolean enabled) {
449
        Integer x = this.actionNameToId.get(id);
450
        if( x!=null ) { // Ojo, si quito el intValue a veces falla
451
            this.buttons[x].setEnabled(enabled);
452
            return;
453
        }
454
        JButton button = this.otherButtons.get(id);
455
        if (button == null) {
456
            return;
457
        }
458
        button.setEnabled(enabled);
459
    }
460

    
461
    public boolean isEnabled(String id) {
462
        Integer x = this.actionNameToId.get(id);
463
        if( x!=null ) {
464
            return this.buttons[x].isEnabled();
465
        }
466
        JButton button = this.otherButtons.get(id);
467
        if (button == null) {
468
            throw new IllegalArgumentException("Don't exists action '" + id + "'.");
469
        }
470
        return button.isEnabled();
471
    }
472

    
473
    public JComponent getActionButton(String id) {
474
        Integer x = this.actionNameToId.get(id);
475
        if( x!=null ) {
476
            return this.buttons[x];
477
        }
478
        JButton button = this.otherButtons.get(id);
479
        return button;
480
    }
481

    
482
    public boolean isVisible(String id) {
483
        Integer x = this.actionNameToId.get(id);
484
        if( x!=null ) {
485
            return this.buttons[x].isVisible();
486
        }
487
        JButton button = this.otherButtons.get(id);
488
        if (button == null) {
489
            throw new IllegalArgumentException("Don't exists action '" + id + "'.");
490
        }
491
        return button.isVisible();
492
    }
493

    
494
    public void setCurrent(int current) {
495
        this.current = current;
496
        updateRecords();
497
    }
498

    
499
    public void setCurrentLabel(String label) {
500
        this.currentLabel = label;
501
        updateRecords();
502
    }
503

    
504
    public void setNumrecords(int numrecords) {
505
        this.numrecords = numrecords;
506
        updateRecords();
507
    }
508

    
509
    public void setActionActive(int action, boolean active) {
510
        if (action == ID_ACTION_SAVE || action == ID_ACTION_DELETE
511
                || action == ID_ACTION_NEW || action == ID_ACTION_SEARCH
512
                || action == ID_ACTION_CLOSE) {
513
            if (this.contents == null) {
514
                this.initComponents();
515
            }
516
            this.activatedActions[action] = active;
517
            this.buttons[action].setVisible(active);
518
        }
519
    }
520

    
521
    public boolean isActionActive(int action) {
522
        return this.activatedActions[action];
523
    }
524

    
525
    public void setReadOnly(boolean readOnly) {
526
        this.readOnly = readOnly;
527

    
528
        this.setEnabled(ID_ACTION_SAVE, !readOnly);
529
        this.setEnabled(ACTION_NEW, !readOnly);
530
        this.setEnabled(ID_ACTION_CANCEL_NEW, !readOnly);
531
        this.setEnabled(ACTION_DELETE, !readOnly);
532
        this.setEnabled(ID_ACTION_SEARCH, true);
533
        this.setEnabled(ACTION_CLOSE, true);
534

    
535
        updateRecords();
536
    }
537

    
538
    public void addAction(Action action) {
539
        this.otherActions.add(action);
540
        if (this.contents == null) {
541
            return;
542
        }
543
        JButton button = new JButton(action);
544
        button.setBorder(BorderFactory.createEmptyBorder());
545
        button.setBorderPainted(false);
546
        button.setFocusPainted(false);
547
        button.setContentAreaFilled(false);
548
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
549
        String id = Objects.toString(action.getValue(Action.ACTION_COMMAND_KEY), null);
550
        if (!StringUtils.isEmpty(id)) {
551
            this.otherButtons.put(id, button);
552
        }
553
        this.othersButtons.add(button);
554
    }
555
}