1/* 2 * Copyright (c) 2008, 2015, 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.*; 27import java.awt.event.MouseAdapter; 28import java.awt.event.MouseEvent; 29import javax.swing.JCheckBox; 30import javax.swing.JList; 31import javax.swing.ListCellRenderer; 32 33/** 34 * @author Thomas Wuerthinger 35 */ 36public class CheckRenderer extends JCheckBox implements ListCellRenderer<Object> { 37 38 private JList<Object> list; 39 private Color startBackground; 40 41 public CheckRenderer(final JList<Object> list) { 42 this.list = list; 43 list.addMouseListener( 44 new MouseAdapter() { 45 46 @Override 47 public void mouseClicked(MouseEvent e) { 48 int index = list.locationToIndex(e.getPoint()); 49 Point p2 = list.indexToLocation(index); 50 Rectangle r = new Rectangle(p2.x, p2.y, getPreferredSize().height, getPreferredSize().height); 51 if (r.contains(e.getPoint())) { 52 CheckNode node = ((CheckNodeListModel) list.getModel()).getCheckNodeAt(index); 53 node.setSelected(!node.isSelected()); 54 list.repaint(); 55 e.consume(); 56 } 57 } 58 }); 59 60 this.setPreferredSize(new Dimension(getPreferredSize().width, getPreferredSize().height - 5)); 61 startBackground = this.getBackground(); 62 } 63 64 @Override 65 public Component getListCellRendererComponent(final JList<? extends Object> list, Object value, final int index, boolean isSelected, boolean cellHasFocus) { 66 setText(value.toString()); 67 CheckNode node = ((CheckNodeListModel) list.getModel()).getCheckNodeAt(index); 68 this.setSelected(node.isSelected()); 69 this.setEnabled(list.isEnabled()); 70 71 if (isSelected && list.hasFocus()) { 72 this.setBackground(list.getSelectionBackground()); 73 this.setForeground(list.getSelectionForeground()); 74 } else if (isSelected) { 75 assert !list.hasFocus(); 76 this.setBackground(startBackground); 77 this.setForeground(list.getForeground()); 78 79 } else { 80 this.setBackground(list.getBackground()); 81 this.setForeground(list.getForeground()); 82 } 83 return this; 84 } 85} 86