CheckRenderer.java revision 1472:c18cbe5936b8
1/*
2 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.filterwindow;
25
26import java.awt.Color;
27import java.awt.Component;
28import java.awt.Dimension;
29import java.awt.Point;
30import java.awt.Rectangle;
31import java.awt.event.MouseAdapter;
32import java.awt.event.MouseEvent;
33import javax.swing.JCheckBox;
34import javax.swing.JList;
35import javax.swing.ListCellRenderer;
36
37/**
38 * @author Thomas Wuerthinger
39 */
40public class CheckRenderer extends JCheckBox implements ListCellRenderer {
41
42    private JList list;
43    private Color startBackground;
44
45    public CheckRenderer(final JList list) {
46        this.list = list;
47        list.addMouseListener(
48                new MouseAdapter() {
49
50                    @Override
51                    public void mouseClicked(MouseEvent e) {
52                        int index = list.locationToIndex(e.getPoint());
53                        Point p2 = list.indexToLocation(index);
54                        Rectangle r = new Rectangle(p2.x, p2.y, getPreferredSize().height, getPreferredSize().height);
55                        if (r.contains(e.getPoint())) {
56                            CheckNode node = ((CheckNodeListModel) list.getModel()).getCheckNodeAt(index);
57                            node.setSelected(!node.isSelected());
58                            list.repaint();
59                            e.consume();
60                        }
61                    }
62                });
63
64        this.setPreferredSize(new Dimension(getPreferredSize().width, getPreferredSize().height - 5));
65        startBackground = this.getBackground();
66    }
67
68    public Component getListCellRendererComponent(final JList list, Object value, final int index, boolean isSelected, boolean cellHasFocus) {
69        setText(value.toString());
70        CheckNode node = ((CheckNodeListModel) list.getModel()).getCheckNodeAt(index);
71        this.setSelected(node.isSelected());
72        this.setEnabled(list.isEnabled());
73
74        if (isSelected && list.hasFocus()) {
75            this.setBackground(list.getSelectionBackground());
76            this.setForeground(list.getSelectionForeground());
77        } else if (isSelected) {
78            assert !list.hasFocus();
79            this.setBackground(startBackground);
80            this.setForeground(list.getForeground());
81
82        } else {
83            this.setBackground(list.getBackground());
84            this.setForeground(list.getForeground());
85        }
86        return this;
87    }
88}
89