Revision 2039 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

View differences:

FormSetButtonBar.java
54 54
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_CLOSE;
55 55
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_DELETE;
56 56
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_FIRST;
57
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_LAST;
58 57
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEW;
59 58
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_NEXT;
60 59
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_PREVIOUS;
60
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_LAST;
61 61
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SAVE;
62 62
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SEARCH;
63 63
import static org.gvsig.tools.dynform.JDynFormSet.ACTION_SET_CURRENT_RECORD;
......
68 68

  
69 69
public class FormSetButtonBar implements Component {
70 70

  
71
    protected static final Logger logger = LoggerFactory
71
    protected static final Logger LOGGER = LoggerFactory
72 72
            .getLogger(FormSetButtonBar.class);
73 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;
74 85

  
75 86
    private static final int MAX_ACTIONS = 11;
76 87

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

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

  
......
112 121

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

  
117 128
    private boolean readOnly = false;
118 129

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

  
134

  
122 135
    public FormSetButtonBar() {
123 136
        // Activamos todas las acciones por defecto
124 137
        for (int i = 0; i < this.activatedActions.length; i++) {
125 138
            this.activatedActions[i] = true;
126 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

  
127 153
        this.listeners = new HashSet();
128 154
        this.otherActions = new ArrayList<>();
129 155
        this.otherButtons = new HashMap<>();
......
145 171
        this.listeners.remove(listener);
146 172
    }
147 173

  
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
                }
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));
183 181
            }
182
            
184 183
        } 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;
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);
223 217
                    }
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 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
                }
230 266
            }
231 267
        }
232 268
    }
......
235 271
        setEnabled(ACTION_NEW, true);
236 272
        if (numrecords <= 0) {
237 273
            this.records.setText("0 / 0");
238
            setEnabled(ACTION_FIRST, false);
239
            setEnabled(ACTION_PREVIOUS, false);
240
            setEnabled(ACTION_NEXT, false);
274
            setEnabled(ID_ACTION_FIRST, false);
275
            setEnabled(ID_ACTION_PREVIOUS, false);
276
            setEnabled(ID_ACTION_NEXT, false);
241 277
            setEnabled(ACTION_LAST, false);
242 278
            return;
243 279
        }
244
        if( this.currentLabel==null ) {
280
        if (this.currentLabel == null) {
245 281
            this.records.setText((current + 1) + " / " + numrecords);
246 282
        } else {
247 283
            this.records.setText(this.currentLabel + " / " + numrecords);
248 284
        }
249 285
        if (current <= 0) {
250 286
            current = 0;
251
            setEnabled(ACTION_FIRST, false);
252
            setEnabled(ACTION_PREVIOUS, false);
253
            setEnabled(ACTION_NEXT, true);
287
            setEnabled(ID_ACTION_FIRST, false);
288
            setEnabled(ID_ACTION_PREVIOUS, false);
289
            setEnabled(ID_ACTION_NEXT, true);
254 290
            setEnabled(ACTION_LAST, true);
255 291
        } else if (current >= numrecords - 1) {
256 292
            current = numrecords - 1;
257
            setEnabled(ACTION_NEXT, false);
293
            setEnabled(ID_ACTION_NEXT, false);
258 294
            setEnabled(ACTION_LAST, false);
259
            setEnabled(ACTION_FIRST, true);
260
            setEnabled(ACTION_PREVIOUS, true);
295
            setEnabled(ID_ACTION_FIRST, true);
296
            setEnabled(ID_ACTION_PREVIOUS, true);
261 297
        } else {
262
            setEnabled(ACTION_NEXT, true);
298
            setEnabled(ID_ACTION_NEXT, true);
263 299
            setEnabled(ACTION_LAST, true);
264
            setEnabled(ACTION_FIRST, true);
265
            setEnabled(ACTION_PREVIOUS, true);
300
            setEnabled(ID_ACTION_FIRST, true);
301
            setEnabled(ID_ACTION_PREVIOUS, true);
266 302
        }
267 303
    }
268 304

  
......
290 326
        JPanel contents = new JPanel();
291 327
        contents.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
292 328

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

  
......
316 352
        contents.add(this.records);
317 353

  
318 354
        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));
355
        contents.add(createButton(i18nManager.getTranslation("_last"), "last.png", ACTION_LAST, KeyEvent.CTRL_MASK + KeyEvent.VK_PAGE_DOWN));
320 356

  
321
        contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ACTION_SAVE, KeyEvent.CTRL_MASK+KeyEvent.VK_S));
357
        contents.add(createButton(i18nManager.getTranslation("guardar"), "save.png", ACTION_SAVE, KeyEvent.CTRL_MASK + KeyEvent.VK_S));
322 358
        contents.add(createButton(i18nManager.getTranslation("nuevo"), "new.png", ACTION_NEW, KeyEvent.VK_F6));
323 359
        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));
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));
326 362

  
327 363
        return contents;
328 364
    }
......
354 390
    }
355 391

  
356 392
    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));
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));
362 398
    }
363 399

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

  
400
    private JComponent createButton(final String tip, final String image, final String action, int mnemonic) {
401
        int actionid = this.actionNameToId.get(action);
402
        
366 403
        URL url = this.getClass().getClassLoader().getResource("org/gvsig/tools/dynform/impl/" + image);
367 404
        Icon icon = new ImageIcon(url);
368 405
        JButton button = new JButton(icon);
369
        if( mnemonic!=KeyEvent.VK_UNDEFINED ) {
406
        if (mnemonic != KeyEvent.VK_UNDEFINED) {
370 407
            button.setMnemonic(mnemonic);
371 408
        }
372 409
        button.setBorder(BorderFactory.createEmptyBorder());
......
382 419
                fireEvent(action, null);
383 420
            }
384 421
        });
385
        this.buttons[action] = button;
422
        this.buttons[actionid] = button;
386 423
        return button;
387 424
    }
388 425

  
......
405 442
    }
406 443

  
407 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
        }
408 450
        JButton button = this.otherButtons.get(id);
409
        if( button == null ) {
451
        if (button == null) {
410 452
            return;
411 453
        }
412 454
        button.setVisible(visible);
413 455
    }
414
    
456

  
415 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
        }
416 463
        JButton button = this.otherButtons.get(id);
417
        if( button == null ) {
464
        if (button == null) {
418 465
            return;
419 466
        }
420 467
        button.setEnabled(enabled);
421 468
    }
422
    
469

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

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

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

  
444 503
    public void setCurrent(int current) {
445 504
        this.current = current;
446 505
        updateRecords();
......
457 516
    }
458 517

  
459 518
    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) {
519
        if (action == ID_ACTION_SAVE || action == ID_ACTION_DELETE
520
                || action == ID_ACTION_NEW || action == ID_ACTION_SEARCH
521
                || action == ID_ACTION_CLOSE) {
463 522
            if (this.contents == null) {
464 523
                this.initComponents();
465 524
            }
......
474 533

  
475 534
    public void setReadOnly(boolean readOnly) {
476 535
        this.readOnly = readOnly;
477
        
478
        this.setEnabled(ACTION_SAVE, !readOnly);
536

  
537
        this.setEnabled(ID_ACTION_SAVE, !readOnly);
479 538
        this.setEnabled(ACTION_NEW, !readOnly);
480
        this.setEnabled(ACTION_CANCEL_NEW, !readOnly);
539
        this.setEnabled(ID_ACTION_CANCEL_NEW, !readOnly);
481 540
        this.setEnabled(ACTION_DELETE, !readOnly);
482
        this.setEnabled(ACTION_SEARCH, true);
541
        this.setEnabled(ID_ACTION_SEARCH, true);
483 542
        this.setEnabled(ACTION_CLOSE, true);
484
        
543

  
485 544
        updateRecords();
486 545
    }
487 546

  
......
497 556
        button.setContentAreaFilled(false);
498 557
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));
499 558
        String id = Objects.toString(action.getValue(Action.ACTION_COMMAND_KEY), null);
500
        if( !StringUtils.isEmpty(id) ) {
559
        if (!StringUtils.isEmpty(id)) {
501 560
            this.otherButtons.put(id, button);
502 561
        }
503 562
        this.othersButtons.add(button);

Also available in: Unified diff