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.Component;
031import java.io.File;
032import java.text.DateFormat;
033import java.util.Date;
034
035import javax.swing.BorderFactory;
036import javax.swing.JTable;
037import javax.swing.SwingConstants;
038import javax.swing.border.Border;
039import javax.swing.table.DefaultTableCellRenderer;
040
041import org.opends.guitools.controlpanel.datamodel.BackupDescriptor;
042import org.opends.guitools.controlpanel.datamodel.BackupTableModel;
043import org.opends.guitools.controlpanel.ui.ColorAndFontConstants;
044
045/**
046 * Renderer of the table that contains the list of backups (it is used in the
047 * tables of the verify backup and restore panels).
048 *
049 */
050public class BackupTableCellRenderer extends DefaultTableCellRenderer
051{
052  private static final long serialVersionUID = -4645902129785751854L;
053  private DateFormat formatter = DateFormat.getDateInstance(DateFormat.FULL);
054  private File backupParentPath;
055  private static final Border fullBorder = BorderFactory.createCompoundBorder(
056      BorderFactory.createMatteBorder(1, 0, 0, 0,
057          ColorAndFontConstants.gridColor),
058          BorderFactory.createEmptyBorder(4, 4, 4, 4));
059  private static final Border incrementalBorder =
060    BorderFactory.createEmptyBorder(4, 4, 4, 4);
061
062  /**
063   * Default constructor.
064   *
065   */
066  public BackupTableCellRenderer()
067  {
068    setForeground(ColorAndFontConstants.tableForeground);
069    setBackground(ColorAndFontConstants.tableBackground);
070  }
071
072
073  /**
074   * Sets the path to which the backups are relative.
075   * @param backupParentPath the path to which the backups are relative.
076   */
077  public void setParentPath(File backupParentPath)
078  {
079    this.backupParentPath = backupParentPath;
080  }
081
082  /** {@inheritDoc} */
083  public Component getTableCellRendererComponent(JTable table, Object value,
084      boolean isSelected, boolean hasFocus, int row, int column)
085  {
086    String s;
087    boolean isDate = false;
088    boolean isFull = ((BackupTableModel)table.getModel()).get(row).getType()
089    == BackupDescriptor.Type.FULL;
090    if (value instanceof File)
091    {
092      File f = (File)value;
093      s = "";
094      boolean isParent = false;
095      while (f != null)
096      {
097        if (!f.equals(backupParentPath))
098        {
099          if (s.length() == 0)
100          {
101            s = f.getName();
102          }
103          else
104          {
105            s = f.getName() + File.separator + s;
106          }
107        }
108        else
109        {
110          isParent = true;
111          break;
112        }
113        f = f.getParentFile();
114      }
115      if (isParent)
116      {
117        if (!isFull)
118        {
119          s = "  "+s;
120        }
121      }
122      else
123      {
124        s = value.toString();
125      }
126    }
127    else if (value instanceof Date)
128    {
129      isDate = true;
130      s = formatter.format((Date)value);
131    }
132    else if (value instanceof BackupDescriptor.Type)
133    {
134      if (isFull)
135      {
136        s = "Full";
137      }
138      else
139      {
140        s = "Incremental";
141      }
142    }
143    else if (value instanceof String)
144    {
145      s = (String)value;
146    }
147    else
148    {
149      throw new IllegalArgumentException(
150          "Unknown class for "+value+": "+" row: "+row+ "column: "+column);
151    }
152    super.getTableCellRendererComponent(table, s, isSelected, hasFocus, row, column);
153    if (isFull && row != 0)
154    {
155      setBorder(fullBorder);
156    }
157    else
158    {
159      setBorder(incrementalBorder);
160    }
161    setHorizontalAlignment(isDate ? SwingConstants.RIGHT : SwingConstants.LEFT);
162
163    return this;
164  }
165}