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