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