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

History | View | Annotate | Download (18.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_LAST;
58
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
59
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEXT;
60
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_PREVIOUS;
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

    
75
    private static final int MAX_ACTIONS = 11;
76

    
77
    public Boolean activatedActions[] = new Boolean[MAX_ACTIONS];
78

    
79
    private static final int COUNTER_HEIGHT = 50;
80
    private static final int COUNTER_WIDTH = 100;
81

    
82
    public interface FormSetListener {
83

    
84
        public boolean doActionFirst();
85

    
86
        public boolean doActionPrevious();
87

    
88
        public boolean doActionNext();
89

    
90
        public boolean doActionLast();
91

    
92
        public boolean doActionSave();
93

    
94
        public boolean doActionNew();
95

    
96
        public boolean doActionCancelNew();
97

    
98
        public boolean doActionDelete();
99

    
100
        public boolean doActionSearch();
101

    
102
        public boolean doActionClose();
103

    
104
        public void doSetCurrentRecord(int current);
105
    }
106

    
107
    private JPanel contents = null;
108
    private Set listeners = null;
109
    private int current = 0;
110
    private String currentLabel = null;
111
    private int numrecords = 0;
112

    
113
    private JPanel othersButtons;
114
    private final List<Action> otherActions;
115
    private final Map<String,JButton> otherButtons;
116

    
117
    private boolean readOnly = false;
118

    
119
    private JButton records;
120
    private final JButton buttons[] = new JButton[MAX_ACTIONS];
121

    
122
    public FormSetButtonBar() {
123
        // Activamos todas las acciones por defecto
124
        for (int i = 0; i < this.activatedActions.length; i++) {
125
            this.activatedActions[i] = true;
126
        }
127
        this.listeners = new HashSet();
128
        this.otherActions = new ArrayList<>();
129
        this.otherButtons = new HashMap<>();
130
    }
131

    
132
    @Override
133
    public JComponent asJComponent() {
134
        if (this.contents == null) {
135
            this.initComponents();
136
        }
137
        return this.contents;
138
    }
139

    
140
    public void addListener(FormSetListener listener) {
141
        this.listeners.add(listener);
142
    }
143

    
144
    public void removeListener(FormSetListener listener) {
145
        this.listeners.remove(listener);
146
    }
147

    
148
    public void fireEvent(int eventid, Object value) {
149
        if (this.readOnly) {
150
            Iterator it = this.listeners.iterator();
151
            while (it.hasNext()) {
152
                FormSetListener listener = (FormSetListener) it.next();
153
                try {
154
                    switch (eventid) {
155
                        case ACTION_FIRST:
156
                            listener.doActionFirst();
157
                            break;
158
                        case ACTION_PREVIOUS:
159
                            listener.doActionPrevious();
160
                            break;
161
                        case ACTION_NEXT:
162
                            listener.doActionNext();
163
                            break;
164
                        case ACTION_LAST:
165
                            listener.doActionLast();
166
                            break;
167
                        case ACTION_SEARCH:
168
                            listener.doActionSearch();
169
                            break;
170
                        case ACTION_CLOSE:
171
                            listener.doActionClose();
172
                            break;
173
                        case ACTION_SET_CURRENT_RECORD:
174
                            listener.doSetCurrentRecord(((Integer) value));
175
                            break;
176
                    }
177
                } catch (Exception ex) {
178
                    logger.info("Error calling listener " + listener.toString()
179
                            + "(" + listener.getClass().getName() + ") from "
180
                            + this.toString() + "(" + this.getClass().getName()
181
                            + ").", ex);
182
                }
183
            }
184
        } else {
185
            Iterator it = this.listeners.iterator();
186
            while (it.hasNext()) {
187
                FormSetListener listener = (FormSetListener) it.next();
188
                try {
189
                    switch (eventid) {
190
                        case ACTION_FIRST:
191
                            listener.doActionFirst();
192
                            break;
193
                        case ACTION_PREVIOUS:
194
                            listener.doActionPrevious();
195
                            break;
196
                        case ACTION_NEXT:
197
                            listener.doActionNext();
198
                            break;
199
                        case ACTION_LAST:
200
                            listener.doActionLast();
201
                            break;
202
                        case ACTION_SAVE:
203
                            listener.doActionSave();
204
                            break;
205
                        case ACTION_NEW:
206
                            listener.doActionNew();
207
                            break;
208
                        case ACTION_CANCEL_NEW:
209
                            listener.doActionCancelNew();
210
                            break;
211
                        case ACTION_DELETE:
212
                            listener.doActionDelete();
213
                            break;
214
                        case ACTION_SEARCH:
215
                            listener.doActionSearch();
216
                            break;
217
                        case ACTION_CLOSE:
218
                            listener.doActionClose();
219
                            break;
220
                        case ACTION_SET_CURRENT_RECORD:
221
                            listener.doSetCurrentRecord(((Integer) value));
222
                            break;
223
                    }
224
                } catch (Exception ex) {
225
                    logger.info("Error calling listener " + listener.toString()
226
                            + "(" + listener.getClass().getName() + ") from "
227
                            + this.toString() + "(" + this.getClass().getName()
228
                            + ").", ex);
229
                }
230
            }
231
        }
232
    }
233

    
234
    private void updateRecords() {
235
        setEnabled(ACTION_NEW, true);
236
        if (numrecords <= 0) {
237
            this.records.setText("0 / 0");
238
            setEnabled(ACTION_FIRST, false);
239
            setEnabled(ACTION_PREVIOUS, false);
240
            setEnabled(ACTION_NEXT, false);
241
            setEnabled(ACTION_LAST, false);
242
            return;
243
        }
244
        if( this.currentLabel==null ) {
245
            this.records.setText((current + 1) + " / " + numrecords);
246
        } else {
247
            this.records.setText(this.currentLabel + " / " + numrecords);
248
        }
249
        if (current <= 0) {
250
            current = 0;
251
            setEnabled(ACTION_FIRST, false);
252
            setEnabled(ACTION_PREVIOUS, false);
253
            setEnabled(ACTION_NEXT, true);
254
            setEnabled(ACTION_LAST, true);
255
        } else if (current >= numrecords - 1) {
256
            current = numrecords - 1;
257
            setEnabled(ACTION_NEXT, false);
258
            setEnabled(ACTION_LAST, false);
259
            setEnabled(ACTION_FIRST, true);
260
            setEnabled(ACTION_PREVIOUS, true);
261
        } else {
262
            setEnabled(ACTION_NEXT, true);
263
            setEnabled(ACTION_LAST, true);
264
            setEnabled(ACTION_FIRST, true);
265
            setEnabled(ACTION_PREVIOUS, true);
266
        }
267
    }
268

    
269
    private void initComponents() {
270
        this.contents = new JPanel();
271
        this.contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
272

    
273
        this.contents.add(getStandardButtonBar());
274

    
275
        this.othersButtons = new JPanel();
276
        this.othersButtons.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
277
        this.contents.add(this.othersButtons);
278

    
279
        for (Action action : this.otherActions) {
280
            this.othersButtons.add(new JButton(action));
281
        }
282

    
283
        this.contents.add(createButton("Close", "close.png", ACTION_CLOSE, KeyEvent.VK_UNDEFINED));
284
        this.initButtons();
285
        this.updateRecords();
286
    }
287

    
288
    private JPanel getStandardButtonBar() {
289
        I18nManager i18nManager = ToolsLocator.getI18nManager();
290
        JPanel contents = new JPanel();
291
        contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
292

    
293
        JComponent firstButton = createButton(i18nManager.getTranslation("_first"), "first.png", ACTION_FIRST, KeyEvent.CTRL_MASK+KeyEvent.VK_PAGE_UP);
294
        contents.add(firstButton);
295
        contents.add(createButton(i18nManager.getTranslation("_back"), "previous.png", ACTION_PREVIOUS, KeyEvent.VK_PAGE_UP));
296

    
297
        this.records = new JButton();
298
        this.records.setCursor(new Cursor(Cursor.HAND_CURSOR));
299
        this.records.setBorder(BorderFactory.createEmptyBorder());
300
        this.records.setBorderPainted(false);
301
        this.records.setFocusPainted(false);
302
        this.records.setContentAreaFilled(false);
303
        this.records.addActionListener(new ActionListener() {
304

    
305
            @Override
306
            public void actionPerformed(ActionEvent ae) {
307
                doGoRecord();
308
            }
309
        });
310
        int height = this.records.getFont().getSize();
311
        if (height < 16) {
312
            height = 16;
313
        }
314
        this.records.setPreferredSize(new Dimension(COUNTER_WIDTH, height));
315
        this.records.setHorizontalAlignment(SwingConstants.CENTER);
316
        contents.add(this.records);
317

    
318
        contents.add(createButton(i18nManager.getTranslation("_next"), "next.png", ACTION_NEXT, KeyEvent.VK_PAGE_DOWN));
319
        contents.add(createButton(i18nManager.getTranslation("_last"), "last.png", ACTION_LAST, KeyEvent.CTRL_MASK+KeyEvent.VK_PAGE_DOWN));
320

    
321
        contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ACTION_SAVE, KeyEvent.CTRL_MASK+KeyEvent.VK_S));
322
        contents.add(createButton(i18nManager.getTranslation("nuevo"), "new.png", ACTION_NEW, KeyEvent.VK_F6));
323
        contents.add(createButton(i18nManager.getTranslation("cancelar nuevo"), "cancelnew.png", ACTION_CANCEL_NEW, KeyEvent.VK_F12));
324
        contents.add(createButton(i18nManager.getTranslation("borrar"), "delete.png", ACTION_DELETE, KeyEvent.SHIFT_MASK+KeyEvent.VK_F2));
325
        contents.add(createButton(i18nManager.getTranslation("search"), "search.png", ACTION_SEARCH, KeyEvent.CTRL_MASK+KeyEvent.VK_F));
326

    
327
        return contents;
328
    }
329

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

    
356
    private void initButtons() {
357
        setActionActive(ACTION_SAVE, isActionActive(ACTION_SAVE));
358
        setActionActive(ACTION_NEW, isActionActive(ACTION_NEW));
359
        setActionActive(ACTION_SEARCH, isActionActive(ACTION_SEARCH));
360
        setActionActive(ACTION_DELETE, isActionActive(ACTION_DELETE));
361
        setActionActive(ACTION_CLOSE, isActionActive(ACTION_CLOSE));
362
    }
363

    
364
    private JComponent createButton(final String tip, final String image, final int action, int mnemonic) {
365

    
366
        URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/" + image);
367
        Icon icon = new ImageIcon(url);
368
        JButton button = new JButton(icon);
369
        if( mnemonic!=KeyEvent.VK_UNDEFINED ) {
370
            button.setMnemonic(mnemonic);
371
        }
372
        button.setBorder(BorderFactory.createEmptyBorder());
373
        button.setBorderPainted(false);
374
        button.setFocusPainted(false);
375
        button.setContentAreaFilled(false);
376
        button.setToolTipText(tip);
377
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
378
        button.addActionListener(new ActionListener() {
379

    
380
            @Override
381
            public void actionPerformed(ActionEvent ae) {
382
                fireEvent(action, null);
383
            }
384
        });
385
        this.buttons[action] = button;
386
        return button;
387
    }
388

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

    
398
    public void setVisible(int action, boolean visible) {
399
        JButton button = this.buttons[action];
400
        if (visible) {
401
            button.setVisible(true);
402
        } else {
403
            button.setVisible(false);
404
        }
405
    }
406

    
407
    public void setVisible(String id, boolean visible) {
408
        JButton button = this.otherButtons.get(id);
409
        if( button == null ) {
410
            return;
411
        }
412
        button.setVisible(visible);
413
    }
414
    
415
    public void setEnabled(String id, boolean enabled) {
416
        JButton button = this.otherButtons.get(id);
417
        if( button == null ) {
418
            return;
419
        }
420
        button.setEnabled(enabled);
421
    }
422
    
423
    public boolean isEnabled(String id) {
424
        JButton button = this.otherButtons.get(id);
425
        if( button == null ) {
426
            throw new IllegalArgumentException("Don't exists action '"+id+"'.");
427
        }
428
        return button.isEnabled();
429
    }
430
    
431
    public JComponent getActionButton(String id) {
432
        JButton button = this.otherButtons.get(id);
433
        return button;
434
    }
435
    
436
    public boolean isVisible(String id) {
437
        JButton button = this.otherButtons.get(id);
438
        if( button == null ) {
439
            throw new IllegalArgumentException("Don't exists action '"+id+"'.");
440
        }
441
        return button.isVisible();
442
    }
443
    
444
    public void setCurrent(int current) {
445
        this.current = current;
446
        updateRecords();
447
    }
448

    
449
    public void setCurrentLabel(String label) {
450
        this.currentLabel = label;
451
        updateRecords();
452
    }
453

    
454
    public void setNumrecords(int numrecords) {
455
        this.numrecords = numrecords;
456
        updateRecords();
457
    }
458

    
459
    public void setActionActive(int action, boolean active) {
460
        if (action == ACTION_SAVE || action == ACTION_DELETE
461
                || action == ACTION_NEW || action == ACTION_SEARCH
462
                || action == ACTION_CLOSE) {
463
            if (this.contents == null) {
464
                this.initComponents();
465
            }
466
            this.activatedActions[action] = active;
467
            this.buttons[action].setVisible(active);
468
        }
469
    }
470

    
471
    public boolean isActionActive(int action) {
472
        return this.activatedActions[action];
473
    }
474

    
475
    public void setReadOnly(boolean readOnly) {
476
        this.readOnly = readOnly;
477
        
478
        this.setEnabled(ACTION_SAVE, !readOnly);
479
        this.setEnabled(ACTION_NEW, !readOnly);
480
        this.setEnabled(ACTION_CANCEL_NEW, !readOnly);
481
        this.setEnabled(ACTION_DELETE, !readOnly);
482
        this.setEnabled(ACTION_SEARCH, true);
483
        this.setEnabled(ACTION_CLOSE, true);
484
        
485
        updateRecords();
486
    }
487

    
488
    public void addAction(Action action) {
489
        this.otherActions.add(action);
490
        if (this.contents == null) {
491
            return;
492
        }
493
        JButton button = new JButton(action);
494
        button.setBorder(BorderFactory.createEmptyBorder());
495
        button.setBorderPainted(false);
496
        button.setFocusPainted(false);
497
        button.setContentAreaFilled(false);
498
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
499
        String id = Objects.toString(action.getValue(Action.ACTION_COMMAND_KEY), null);
500
        if( !StringUtils.isEmpty(id) ) {
501
            this.otherButtons.put(id, button);
502
        }
503
        this.othersButtons.add(button);
504
    }
505
}