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 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 *
035 */
036public class SelectableTableCellRenderer extends CustomCellRenderer
037{
038  private static final long serialVersionUID = 6855042914121526677L;
039  private boolean hasMouseOver;
040  private boolean isBeingPressed;
041  private int lastRowMouseOver;
042
043  private static final Color pressedBackground =
044    ColorAndFontConstants.pressedBackground;
045
046  private static final Color pressedForeground =
047    ColorAndFontConstants.pressedForeground;
048
049  private static final Color mouseOverBackground =
050    ColorAndFontConstants.mouseOverBackground;
051
052  private static final Color mouseOverForeground =
053    ColorAndFontConstants.mouseOverForeground;
054
055  /**
056   * Sets the table that will be rendered by this renderer.
057   * @param table the table to be rendered.
058   */
059  public void setTable(final JTable table)
060  {
061    MouseAdapter mouseListener = new MouseAdapter()
062    {
063      /** {@inheritDoc} */
064      @Override
065      public void mousePressed(MouseEvent ev)
066      {
067        isBeingPressed = true;
068        table.repaint();
069      }
070
071      /** {@inheritDoc} */
072      @Override
073      public void mouseReleased(MouseEvent ev)
074      {
075        isBeingPressed = false;
076      }
077
078      /** {@inheritDoc} */
079      @Override
080      public void mouseExited(MouseEvent ev)
081      {
082        hasMouseOver = false;
083        lastRowMouseOver = -1;
084        table.repaint();
085      }
086
087      /** {@inheritDoc} */
088      @Override
089      public void mouseEntered(MouseEvent ev)
090      {
091        if (ev.getSource() == table)
092        {
093          hasMouseOver = true;
094          lastRowMouseOver = table.rowAtPoint(ev.getPoint());
095        }
096        else
097        {
098          mouseExited(ev);
099        }
100      }
101    };
102    MouseMotionAdapter mouseMotionAdapter = new MouseMotionAdapter()
103    {
104      /** {@inheritDoc} */
105      @Override
106      public void mouseMoved(MouseEvent ev)
107      {
108        lastRowMouseOver = table.rowAtPoint(ev.getPoint());
109        table.repaint();
110      }
111
112      /** {@inheritDoc} */
113      @Override
114      public void mouseDragged(MouseEvent ev)
115      {
116        lastRowMouseOver = -1;
117        table.repaint();
118      }
119    };
120    table.addMouseListener(mouseListener);
121    table.addMouseMotionListener(mouseMotionAdapter);
122  }
123
124  /** {@inheritDoc} */
125  @Override
126  public Component getTableCellRendererComponent(JTable table, Object value,
127      boolean isSelected, boolean hasFocus, int row, int column)
128  {
129    super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
130        column);
131    updateComponent(this, table, row, column, isSelected);
132    return this;
133  }
134
135  void updateComponent(Component comp, JTable table, int row,
136      int column, boolean isSelected)
137  {
138    if (table.isCellEditable(row, column) && !isSelected)
139    {
140      comp.setBackground(ColorAndFontConstants.treeBackground);
141      comp.setForeground(ColorAndFontConstants.treeForeground);
142    }
143    else if (isBeingPressed && hasMouseOver && row == lastRowMouseOver)
144    {
145      comp.setBackground(pressedBackground);
146      comp.setForeground(pressedForeground);
147    }
148    else if ((hasMouseOver && row == lastRowMouseOver) || isSelected)
149    {
150      comp.setBackground(mouseOverBackground);
151      comp.setForeground(mouseOverForeground);
152    }
153    else
154    {
155      comp.setBackground(ColorAndFontConstants.treeBackground);
156      comp.setForeground(ColorAndFontConstants.treeForeground);
157    }
158  }
159}