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