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 2014-2016 ForgeRock AS.
016 */
017package org.opends.guitools.controlpanel.ui.renderer;
018
019import static org.opends.messages.AdminToolMessages.*;
020import static com.forgerock.opendj.cli.Utils.OBFUSCATED_VALUE;
021
022import java.awt.Component;
023import java.awt.GridBagConstraints;
024import java.util.ArrayList;
025import java.util.Collection;
026
027import javax.swing.ImageIcon;
028import javax.swing.JLabel;
029import javax.swing.JTable;
030
031import org.forgerock.opendj.ldap.AttributeDescription;
032import org.opends.guitools.controlpanel.browser.IconPool;
033import org.opends.guitools.controlpanel.datamodel.BinaryValue;
034import org.opends.guitools.controlpanel.datamodel.ObjectClassValue;
035import org.opends.guitools.controlpanel.ui.components.BinaryCellPanel;
036import org.opends.guitools.controlpanel.ui.components.ObjectClassCellPanel;
037import org.opends.guitools.controlpanel.util.Utilities;
038import org.opends.server.types.Schema;
039
040/** The renderer used by the table in the 'Attribute View' of the LDAP entry browser. */
041public class LDAPEntryTableCellRenderer extends SelectableTableCellRenderer
042{
043  private static final long serialVersionUID = 3590456676685339618L;
044  private BinaryCellPanel binaryPanel;
045  private ObjectClassCellPanel ocPanel;
046  private JLabel lockLabel = new JLabel();
047  private ImageIcon lockIcon =
048    Utilities.createImageIcon(IconPool.IMAGE_PATH+"/field-locked.png");
049  private Schema schema;
050  private Collection<String> requiredAttrs = new ArrayList<>();
051
052  /** Constructor of the cell renderer. */
053  public LDAPEntryTableCellRenderer()
054  {
055    binaryPanel = new BinaryCellPanel();
056    binaryPanel.setOpaque(true);
057    ocPanel = new ObjectClassCellPanel();
058    ocPanel.setOpaque(true);
059    GridBagConstraints gbc = new GridBagConstraints();
060    add(lockLabel, gbc);
061  }
062
063  @Override
064  public Component getTableCellRendererComponent(JTable table, Object value,
065      boolean isSelected, boolean hasFocus, int row, int column) {
066    if (isRequired(table, row, column))
067    {
068      Utilities.setRequiredIcon(label);
069    }
070    else
071    {
072      label.setIcon(null);
073    }
074    if (isPassword(table, row, column))
075    {
076      return getStringValue(table, OBFUSCATED_VALUE, isSelected,
077          hasFocus, row, column);
078    }
079    else if (value instanceof ObjectClassValue)
080    {
081      final boolean cellEditable = table.isCellEditable(row, column);
082      ocPanel.setLockIconVisible(!cellEditable);
083      ocPanel.setEditButtonVisible(cellEditable);
084      ocPanel.setValue((ObjectClassValue)value);
085      if (hasFocus)
086      {
087        ocPanel.setBorder(getDefaultFocusBorder(table, value, isSelected, row, column));
088      }
089      else
090      {
091        ocPanel.setBorder(defaultBorder);
092      }
093      updateComponent(ocPanel, table, row, column, isSelected);
094      return ocPanel;
095    }
096    else if (value instanceof byte[] || value instanceof BinaryValue)
097    {
098      if (value instanceof byte[])
099      {
100        if (((byte[])value).length > 0)
101        {
102          binaryPanel.setValue((byte[])value, isImage(table, row, column));
103        }
104        else
105        {
106          binaryPanel.setValue((byte[])null, isImage(table, row, column));
107        }
108      }
109      else
110      {
111        binaryPanel.setValue((BinaryValue)value, isImage(table, row, column));
112      }
113      if (!table.isCellEditable(row, column))
114      {
115        binaryPanel.setLockIconVisible(true);
116        binaryPanel.setEditButtonText(INFO_CTRL_PANEL_VIEW_BUTTON_LABEL.get());
117      }
118      else
119      {
120        binaryPanel.setLockIconVisible(false);
121        binaryPanel.setEditButtonText(INFO_CTRL_PANEL_EDIT_BUTTON_LABEL.get());
122      }
123      if (hasFocus)
124      {
125        binaryPanel.setBorder(getDefaultFocusBorder(table, value, isSelected,
126            row, column));
127      }
128      else
129      {
130        binaryPanel.setBorder(defaultBorder);
131      }
132      updateComponent(binaryPanel, table, row, column, isSelected);
133      return binaryPanel;
134    }
135    else
136    {
137      return getStringValue(table, value, isSelected, hasFocus, row, column);
138    }
139  }
140
141  /**
142   * Returns the String representation for a given byte array.
143   * @param value the byte array.
144   * @return the String representation for a given byte array.
145   */
146  public String getString(byte[] value)
147  {
148    return binaryPanel.getString(value, false).toString();
149  }
150
151  /**
152   * Returns the String representation for a given BinaryValue object.
153   * @param value the BinaryValue object.
154   * @return the String representation for the provided BinaryValue object.
155   */
156  public String getString(BinaryValue value)
157  {
158    return binaryPanel.getMessage(value, false).toString();
159  }
160
161  /**
162   * Returns the String representation for a given ObjectClassValue object.
163   * @param value the ObjectClassValue object.
164   * @return the String representation for the provided ObjectClassValue object.
165   */
166  public String getString(ObjectClassValue value)
167  {
168    return ocPanel.getMessage(value).toString();
169  }
170
171  private Component getStringValue(JTable table, Object value,
172      boolean isSelected, boolean hasFocus, int row, int column)
173  {
174    super.getTableCellRendererComponent(table, value, isSelected,
175        hasFocus, row, column);
176    if (table.isCellEditable(row, column) && !isSelected)
177    {
178      lockLabel.setIcon(null);
179    }
180    else if (column == 1 && !table.isCellEditable(row, column))
181    {
182      lockLabel.setIcon(lockIcon);
183    }
184    else
185    {
186      lockLabel.setIcon(null);
187    }
188    return this;
189  }
190
191  private boolean isPassword(JTable table, int row, int col)
192  {
193    if (col == 1)
194    {
195      Object o = table.getValueAt(row, 0);
196      if (Utilities.hasPasswordSyntax((String)o, getSchema()))
197      {
198        return true;
199      }
200    }
201    return false;
202  }
203
204  private boolean isImage(JTable table, int row, int col)
205  {
206    if (col == 1)
207    {
208      Object o = table.getValueAt(row, 0);
209      return Utilities.hasImageSyntax((String)o, schema);
210    }
211    return false;
212  }
213
214  /**
215   * Returns the schema.
216   * @return the schema.
217   */
218  public Schema getSchema()
219  {
220    return schema;
221  }
222
223  /**
224   * Sets the schema.
225   * @param schema the schema.
226   */
227  public void setSchema(Schema schema)
228  {
229    this.schema = schema;
230  }
231
232  /**
233   * Sets the list of required attributes for the entry that is being rendered
234   * using this renderer.
235   * @param requiredAttrs the required attribute names.
236   */
237  public void setRequiredAttrs(Collection<String> requiredAttrs)
238  {
239    this.requiredAttrs.clear();
240    this.requiredAttrs.addAll(requiredAttrs);
241  }
242
243  private boolean isRequired(JTable table, int row, int col)
244  {
245    if (col == 0)
246    {
247      Object o = table.getValueAt(row, 0);
248      return requiredAttrs.contains(
249          AttributeDescription.valueOf((String)o).getNameOrOID().toLowerCase());
250    }
251    return false;
252  }
253}