001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2008 Sun Microsystems, Inc. 025 * Portions Copyright 2015 ForgeRock AS. 026 */ 027 028package org.opends.guitools.controlpanel.ui.renderer; 029 030import java.awt.Color; 031import java.awt.Component; 032import java.awt.event.MouseAdapter; 033import java.awt.event.MouseEvent; 034import java.awt.event.MouseMotionAdapter; 035 036import javax.swing.JTable; 037 038import org.opends.guitools.controlpanel.ui.ColorAndFontConstants; 039 040/** 041 * A table cell renderer that updates the rendering of the cells when the user 042 * moves the mouse over the table. This is done to provide a visual hint that 043 * the table can be selected. 044 * 045 */ 046public class SelectableTableCellRenderer extends CustomCellRenderer 047{ 048 private static final long serialVersionUID = 6855042914121526677L; 049 private boolean hasMouseOver; 050 private boolean isBeingPressed; 051 private int lastRowMouseOver; 052 053 private static final Color pressedBackground = 054 ColorAndFontConstants.pressedBackground; 055 056 private static final Color pressedForeground = 057 ColorAndFontConstants.pressedForeground; 058 059 private static final Color mouseOverBackground = 060 ColorAndFontConstants.mouseOverBackground; 061 062 private static final Color mouseOverForeground = 063 ColorAndFontConstants.mouseOverForeground; 064 065 /** 066 * Sets the table that will be rendered by this renderer. 067 * @param table the table to be rendered. 068 */ 069 public void setTable(final JTable table) 070 { 071 MouseAdapter mouseListener = new MouseAdapter() 072 { 073 /** {@inheritDoc} */ 074 @Override 075 public void mousePressed(MouseEvent ev) 076 { 077 isBeingPressed = true; 078 table.repaint(); 079 } 080 081 /** {@inheritDoc} */ 082 @Override 083 public void mouseReleased(MouseEvent ev) 084 { 085 isBeingPressed = false; 086 } 087 088 /** {@inheritDoc} */ 089 @Override 090 public void mouseExited(MouseEvent ev) 091 { 092 hasMouseOver = false; 093 lastRowMouseOver = -1; 094 table.repaint(); 095 } 096 097 /** {@inheritDoc} */ 098 @Override 099 public void mouseEntered(MouseEvent ev) 100 { 101 if (ev.getSource() == table) 102 { 103 hasMouseOver = true; 104 lastRowMouseOver = table.rowAtPoint(ev.getPoint()); 105 } 106 else 107 { 108 mouseExited(ev); 109 } 110 } 111 }; 112 MouseMotionAdapter mouseMotionAdapter = new MouseMotionAdapter() 113 { 114 /** {@inheritDoc} */ 115 @Override 116 public void mouseMoved(MouseEvent ev) 117 { 118 lastRowMouseOver = table.rowAtPoint(ev.getPoint()); 119 table.repaint(); 120 } 121 122 /** {@inheritDoc} */ 123 @Override 124 public void mouseDragged(MouseEvent ev) 125 { 126 lastRowMouseOver = -1; 127 table.repaint(); 128 } 129 }; 130 table.addMouseListener(mouseListener); 131 table.addMouseMotionListener(mouseMotionAdapter); 132 } 133 134 /** {@inheritDoc} */ 135 @Override 136 public Component getTableCellRendererComponent(JTable table, Object value, 137 boolean isSelected, boolean hasFocus, int row, int column) 138 { 139 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, 140 column); 141 updateComponent(this, table, row, column, isSelected); 142 return this; 143 } 144 145 void updateComponent(Component comp, JTable table, int row, 146 int column, boolean isSelected) 147 { 148 if (table.isCellEditable(row, column) && !isSelected) 149 { 150 comp.setBackground(ColorAndFontConstants.treeBackground); 151 comp.setForeground(ColorAndFontConstants.treeForeground); 152 } 153 else if (isBeingPressed && hasMouseOver && row == lastRowMouseOver) 154 { 155 comp.setBackground(pressedBackground); 156 comp.setForeground(pressedForeground); 157 } 158 else if ((hasMouseOver && row == lastRowMouseOver) || isSelected) 159 { 160 comp.setBackground(mouseOverBackground); 161 comp.setForeground(mouseOverForeground); 162 } 163 else 164 { 165 comp.setBackground(ColorAndFontConstants.treeBackground); 166 comp.setForeground(ColorAndFontConstants.treeForeground); 167 } 168 } 169}