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