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