Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / DefaultJListWithCheckbox.java @ 2046

History | View | Annotate | Download (14.6 KB)

1

    
2
package org.gvsig.tools.swing.impl;
3

    
4
import java.awt.BorderLayout;
5
import java.awt.Color;
6
import java.awt.Component;
7
import java.awt.Dimension;
8
import java.awt.Point;
9
import java.awt.Rectangle;
10
import java.awt.event.MouseAdapter;
11
import java.awt.event.MouseEvent;
12
import java.beans.Transient;
13
import java.util.HashSet;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.Vector;
18
import javax.accessibility.AccessibleContext;
19
import javax.swing.DefaultListSelectionModel;
20
import javax.swing.JCheckBox;
21
import javax.swing.JList;
22
import javax.swing.JPanel;
23
import javax.swing.ListCellRenderer;
24
import javax.swing.ListModel;
25
import javax.swing.ListSelectionModel;
26
import javax.swing.event.ListSelectionEvent;
27
import javax.swing.event.ListSelectionListener;
28
import javax.swing.plaf.ListUI;
29
import javax.swing.text.Position;
30
import org.gvsig.tools.swing.api.JListWithCheckbox;
31
import org.slf4j.Logger;
32
import org.slf4j.LoggerFactory;
33

    
34
public class DefaultJListWithCheckbox extends JListWithCheckbox {
35

    
36
    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultJListWithCheckbox.class);
37

    
38

    
39
    private class CheckListCellRenderer extends JPanel implements ListCellRenderer {
40

    
41
        private final ListCellRenderer delegate;
42
        private ListSelectionModel selectionModel;
43
        private final JCheckBox checkBox = new JCheckBox();
44

    
45
        public CheckListCellRenderer(ListCellRenderer renderer, ListSelectionModel selectionModel) {
46
            this.delegate = renderer;
47
            this.selectionModel = selectionModel;
48
            this.setLayout(new BorderLayout());
49
            this.setOpaque(false);
50
            this.checkBox.setOpaque(false);
51
            checkBoxWidth = (int) this.checkBox.getPreferredSize().getWidth();
52
        }
53

    
54
        @Override
55
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
56
            Component renderer = delegate.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
57
            checkBox.setSelected(selectionModel.isSelectedIndex(index));
58
            removeAll();
59
            add(checkBox, BorderLayout.WEST);
60
            add(renderer, BorderLayout.CENTER);
61
            return this;
62
        }
63
        
64
        public void setCheckedModel(ListSelectionModel selectionModel) {
65
            this.selectionModel = selectionModel;
66
        }
67
    }
68

    
69
    private int checkBoxWidth = 0;
70
    private final JList wrappedList;
71
    private ListSelectionModel checkedsModel = new DefaultListSelectionModel();
72
    private Set<ListSelectionListener> checkedsListeners;
73
    private final ListSelectionListener checksModelListener;
74
    private final CheckListCellRenderer checkListCellRenderer;
75
        
76
    public DefaultJListWithCheckbox(final JList wrappedList) {
77
        this.wrappedList = wrappedList;
78
        this.checkedsModel.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
79
        this.checkedsListeners = new HashSet<>();
80
        
81
        this.checkListCellRenderer = new CheckListCellRenderer(
82
                this.wrappedList.getCellRenderer(),
83
                this.checkedsModel
84
        );
85
        this.wrappedList.setCellRenderer(this.checkListCellRenderer);
86
        this.wrappedList.addMouseListener(new MouseAdapter() {
87
            @Override
88
            public void mouseClicked(MouseEvent me) {
89
                int index = wrappedList.locationToIndex(me.getPoint());
90
                if (index < 0) {
91
                    return;
92
                }
93
                if (me.getX() > wrappedList.getCellBounds(index, index).getX() + checkBoxWidth) {
94
                    return;
95
                }
96
                toggleCheck(index);
97
            }
98
        });
99
        this.checksModelListener = new ListSelectionListener() {
100
            @Override
101
            public void valueChanged(ListSelectionEvent lse) {
102
                wrappedList.repaint();
103
                //fireCheckedsListeners(lse);
104
            }
105
        };
106
        this.setCheckedModel(checkedsModel);
107
    }
108

    
109
    @Override
110
    public final void setCheckedModel(ListSelectionModel checkedsModel) {
111
        this.checkedsModel.removeListSelectionListener(this.checksModelListener);
112
        this.checkedsModel = checkedsModel;
113
        this.checkedsModel.addListSelectionListener(this.checksModelListener);
114
        this.checkListCellRenderer.setCheckedModel(checkedsModel);
115
    }
116
    
117
    @Override
118
    public void toggleCheck(int index) {
119
        if (index < 0) {
120
            return;
121
        }
122
        if (this.checkedsModel.isSelectedIndex(index)) {
123
            this.checkedsModel.removeSelectionInterval(index, index);
124
        } else {
125
            this.checkedsModel.addSelectionInterval(index, index);
126
        }
127
        // FIXME: isAdjusting is set to false always
128
        fireCheckedsListeners(new ListSelectionEvent(this, index, index, false));
129
    }
130

    
131
    @Override
132
    public ListUI getUI() {
133
        return this.wrappedList.getUI();
134
    }
135

    
136
    @Override
137
    public void setUI(ListUI ui) {
138
        this.wrappedList.setUI(ui);
139
    }
140

    
141
    @Override
142
    public void updateUI() {
143
        if( this.wrappedList!= null ) {
144
            this.wrappedList.updateUI();
145
        }
146
    }
147

    
148
    @Override
149
    public String getUIClassID() {
150
        return this.wrappedList.getUIClassID();
151
    }
152

    
153
    @Override
154
    public Object getPrototypeCellValue() {
155
        return this.wrappedList.getPrototypeCellValue();
156
    }
157

    
158
    @Override
159
    public void setPrototypeCellValue(Object prototypeCellValue) {
160
        this.wrappedList.setPrototypeCellValue(prototypeCellValue);
161
    }
162

    
163
    @Override
164
    public int getFixedCellWidth() {
165
        return this.wrappedList.getFixedCellWidth();
166
    }
167

    
168
    @Override
169
    public void setFixedCellWidth(int width) {
170
        this.wrappedList.setFixedCellWidth(width);
171
    }
172

    
173
    @Override
174
    public int getFixedCellHeight() {
175
        return this.wrappedList.getFixedCellHeight();
176
    }
177

    
178
    @Override
179
    public void setFixedCellHeight(int height) {
180
        this.wrappedList.setFixedCellHeight(height);
181
    }
182

    
183
    @Transient
184
    @Override
185
    public ListCellRenderer getCellRenderer() {
186
        return this.wrappedList.getCellRenderer();
187
    }
188

    
189
    @Override
190
    public void setCellRenderer(ListCellRenderer cellRenderer) {
191
        this.wrappedList.setCellRenderer(cellRenderer);
192
    }
193

    
194
    @Override
195
    public Color getSelectionForeground() {
196
        return this.wrappedList.getSelectionForeground();
197
    }
198

    
199
    @Override
200
    public void setSelectionForeground(Color selectionForeground) {
201
        this.wrappedList.setSelectionForeground(selectionForeground);
202
    }
203

    
204
    @Override
205
    public Color getSelectionBackground() {
206
        return this.wrappedList.getSelectionBackground();
207
    }
208

    
209
    @Override
210
    public void setSelectionBackground(Color selectionBackground) {
211
        this.wrappedList.setSelectionBackground(selectionBackground);
212
    }
213

    
214
    @Override
215
    public int getVisibleRowCount() {
216
        return this.wrappedList.getVisibleRowCount();
217
    }
218

    
219
    @Override
220
    public void setVisibleRowCount(int visibleRowCount) {
221
        this.wrappedList.setVisibleRowCount(visibleRowCount);
222
    }
223

    
224
    @Override
225
    public int getLayoutOrientation() {
226
        return this.wrappedList.getLayoutOrientation();
227
    }
228

    
229
    @Override
230
    public void setLayoutOrientation(int layoutOrientation) {
231
        this.wrappedList.setLayoutOrientation(layoutOrientation);
232
    }
233

    
234
    @Override
235
    public int getFirstVisibleIndex() {
236
        return this.wrappedList.getFirstVisibleIndex();
237
    }
238

    
239
    @Override
240
    public int getLastVisibleIndex() {
241
        return this.wrappedList.getLastVisibleIndex();
242
    }
243

    
244
    @Override
245
    public void ensureIndexIsVisible(int index) {
246
        this.wrappedList.ensureIndexIsVisible(index);
247
    }
248

    
249
    @Override
250
    public void setDragEnabled(boolean b) {
251
        this.wrappedList.setDragEnabled(b);
252
    }
253

    
254
    @Override
255
    public boolean getDragEnabled() {
256
        return this.wrappedList.getDragEnabled();
257
    }
258
//
259
//    public final void setDropMode(DropMode dropMode) {
260
//        this.wrappedList.setDropMode(dropMode);
261
//    }
262
//
263
//    public final DropMode getDropMode() {
264
//        
265
//    }
266
//
267
//    public final DropLocation getDropLocation() {
268
//        
269
//    }
270

    
271
    @Override
272
    public int getNextMatch(String prefix, int startIndex, Position.Bias bias) {
273
        return this.wrappedList.getNextMatch(prefix, startIndex, bias);
274
    }
275

    
276
    @Override
277
    public String getToolTipText(MouseEvent event) {
278
        return this.wrappedList.getToolTipText(event);
279
    }
280

    
281
    @Override
282
    public int locationToIndex(Point location) {
283
        return this.wrappedList.locationToIndex(location);
284
    }
285

    
286
    @Override
287
    public Point indexToLocation(int index) {
288
        return this.wrappedList.indexToLocation(index);
289
    }
290

    
291
    @Override
292
    public Rectangle getCellBounds(int index0, int index1) {
293
        return this.wrappedList.getCellBounds(index0, index1);
294
    }
295

    
296
    @Override
297
    public ListModel getModel() {
298
        return this.wrappedList.getModel();
299
    }
300

    
301
    @Override
302
    public void setModel(ListModel model) {
303
        this.wrappedList.setModel(model);
304
    }
305

    
306
    @Override
307
    public void setListData(Object[] listData) {
308
        this.wrappedList.setListData(listData);
309
    }
310

    
311
    @Override
312
    public void setListData(Vector listData) {
313
        this.wrappedList.setListData(listData);
314
    }
315

    
316
    @Override
317
    public ListSelectionModel getSelectionModel() {
318
        return this.wrappedList.getSelectionModel();
319
    }
320

    
321
    @Override
322
    public ListSelectionModel getCheckedModel() {
323
        return this.checkedsModel;
324
    }
325

    
326
    @Override
327
    public void addListSelectionListener(ListSelectionListener listener) {
328
        this.wrappedList.addListSelectionListener(listener);
329
    }
330
    
331
    @Override
332
    public void addChecksListener(ListSelectionListener listener) {
333
       this.checkedsListeners.add(listener);
334
    }
335
    
336
    @Override
337
    public void removeListSelectionListener(ListSelectionListener listener) {
338
        this.wrappedList.removeListSelectionListener(listener);
339
    }    
340
    
341
    @Override
342
    public void removeChecksListener(ListSelectionListener listener) {
343
        this.checkedsListeners.remove(listener);
344
    }
345

    
346
    @Override
347
    public ListSelectionListener[] getListSelectionListeners() {
348
        return this.wrappedList.getListSelectionListeners();
349
    }
350

    
351
    @Override
352
    public ListSelectionListener[] getChecksListeners() {
353
        return this.checkedsListeners.toArray(new ListSelectionListener[1]);
354
    }
355

    
356
    protected void fireCheckedsListeners(ListSelectionEvent event) {
357
        Iterator<ListSelectionListener> it = this.checkedsListeners.iterator();
358
        while(it.hasNext()) {
359
            ListSelectionListener listener = it.next();
360
            try {
361
                listener.valueChanged(event);
362
            } catch(Throwable th) {
363
                LOGGER.warn("Problems calling check listener ("+listener+").",th);
364
            }
365
        }
366
    }
367
    
368
    @Override
369
    public void setSelectionModel(ListSelectionModel selectionModel) {
370
        this.wrappedList.setSelectionModel(selectionModel);
371
    }
372

    
373
    @Override
374
    public void setSelectionMode(int selectionMode) {
375
        this.wrappedList.setSelectionMode(selectionMode);
376
    }
377

    
378
    @Override
379
    public int getSelectionMode() {
380
        return this.wrappedList.getSelectionMode();
381
    }
382

    
383
    @Override
384
    public int getAnchorSelectionIndex() {
385
        return this.wrappedList.getAnchorSelectionIndex();
386
    }
387

    
388
    @Override
389
    public int getLeadSelectionIndex() {
390
        return this.wrappedList.getLeadSelectionIndex();
391
    }
392

    
393
    @Override
394
    public int getMinSelectionIndex() {
395
        return this.wrappedList.getMinSelectionIndex();
396
    }
397

    
398
    @Override
399
    public int getMaxSelectionIndex() {
400
        return this.wrappedList.getMaxSelectionIndex();
401
    }
402

    
403
    @Override
404
    public boolean isSelectedIndex(int index) {
405
        return this.wrappedList.isSelectedIndex(index);
406
    }
407

    
408
    @Override
409
    public boolean isSelectionEmpty() {
410
        return this.wrappedList.isSelectionEmpty();
411
    }
412

    
413
    @Override
414
    public void clearSelection() {
415
        this.wrappedList.clearSelection();
416
    }
417

    
418
    @Override
419
    public void setSelectionInterval(int anchor, int lead) {
420
        this.wrappedList.setSelectionInterval(anchor, lead);
421
    }
422

    
423
    @Override
424
    public void addSelectionInterval(int anchor, int lead) {
425
        this.wrappedList.addSelectionInterval(anchor, lead);
426
    }
427

    
428
    @Override
429
    public void removeSelectionInterval(int index0, int index1) {
430
        this.wrappedList.removeSelectionInterval(index0, index1);
431
    }
432

    
433
    @Override
434
    public void setValueIsAdjusting(boolean b) {
435
        this.wrappedList.setValueIsAdjusting(b);
436
    }
437

    
438
    @Override
439
    public boolean getValueIsAdjusting() {
440
        return this.wrappedList.getValueIsAdjusting();
441
    }
442

    
443
    @Transient
444
    @Override
445
    public int[] getSelectedIndices() {
446
        return this.wrappedList.getSelectedIndices();
447
    }
448

    
449
    @Override
450
    public void setSelectedIndex(int index) {
451
        this.wrappedList.setSelectedIndex(index);
452
    }
453

    
454
    @Override
455
    public void setSelectedIndices(int[] indices) {
456
        this.wrappedList.setSelectedIndices(indices);
457
    }
458

    
459
    @Deprecated
460
    @Override
461
    public Object[] getSelectedValues() {
462
        return this.wrappedList.getSelectedValues();
463
    }
464

    
465
    @Override
466
    public List getSelectedValuesList() {
467
        return this.wrappedList.getSelectedValuesList();
468
    }
469

    
470
    @Override
471
    public int getSelectedIndex() {
472
        return this.wrappedList.getSelectedIndex();
473
    }
474

    
475
    @Override
476
    public Object getSelectedValue() {
477
        return this.wrappedList.getSelectedValue();
478
    }
479

    
480
    @Override
481
    public void setSelectedValue(Object anObject, boolean shouldScroll) {
482
        this.wrappedList.setSelectedValue(anObject, shouldScroll);
483
    }
484

    
485
    @Override
486
    public Dimension getPreferredScrollableViewportSize() {
487
        return this.wrappedList.getPreferredScrollableViewportSize();
488
    }
489

    
490
    @Override
491
    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
492
        return this.wrappedList.getScrollableUnitIncrement(visibleRect, orientation, direction);
493
    }
494

    
495
    @Override
496
    public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
497
        return this.wrappedList.getScrollableBlockIncrement(visibleRect, orientation, direction);
498
    }
499

    
500
    @Override
501
    public boolean getScrollableTracksViewportWidth() {
502
        return this.wrappedList.getScrollableTracksViewportWidth();
503
    }
504

    
505
    @Override
506
    public boolean getScrollableTracksViewportHeight() {
507
        return this.wrappedList.getScrollableTracksViewportHeight();
508
    }
509

    
510
    @Override
511
    public AccessibleContext getAccessibleContext() {
512
        return this.wrappedList.getAccessibleContext();
513
    }
514
}