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.awt.event.ActionEvent;
022import java.awt.event.ActionListener;
023
024import javax.swing.AbstractCellEditor;
025import javax.swing.DefaultCellEditor;
026import javax.swing.JPasswordField;
027import javax.swing.JTable;
028import javax.swing.JTextField;
029import javax.swing.event.DocumentEvent;
030import javax.swing.event.DocumentListener;
031import javax.swing.table.TableCellEditor;
032
033import org.opends.guitools.controlpanel.datamodel.BinaryValue;
034import org.opends.guitools.controlpanel.datamodel.ControlPanelInfo;
035import org.opends.guitools.controlpanel.datamodel.ObjectClassValue;
036import org.opends.guitools.controlpanel.ui.BinaryAttributeEditorPanel;
037import org.opends.guitools.controlpanel.ui.ObjectClassEditorPanel;
038import org.opends.guitools.controlpanel.ui.GenericDialog;
039import org.opends.guitools.controlpanel.ui.components.BinaryCellPanel;
040import org.opends.guitools.controlpanel.ui.components.ObjectClassCellPanel;
041import org.opends.guitools.controlpanel.util.Utilities;
042
043/** The cell editor used in the 'Attribute' View of the entries in the LDAP entry browser. */
044public class AttributeCellEditor extends AbstractCellEditor
045implements TableCellEditor
046{
047  private static final long serialVersionUID = 1979354208925355746L;
048
049  private BinaryCellPanel binaryPanel;
050
051  private ObjectClassCellPanel ocPanel;
052
053  private ObjectClassValue ocValue;
054  private byte[] value;
055  private BinaryValue binaryValue;
056
057  private TableCellEditor defaultEditor;
058  private TableCellEditor passwordEditor;
059
060  private GenericDialog editBinaryDlg;
061  private BinaryAttributeEditorPanel editBinaryPanel;
062
063  private GenericDialog editOcDlg;
064  private ObjectClassEditorPanel editOcPanel;
065
066  private JTable table;
067
068  private JTextField textField;
069
070  private JPasswordField passwordField;
071
072  private ControlPanelInfo info;
073
074  private String attrName;
075
076
077  /** Default constructor. */
078  public AttributeCellEditor()
079  {
080    textField = Utilities.createTextField();
081    textField.getDocument().addDocumentListener(new DocumentListener()
082    {
083      @Override
084      public void changedUpdate(DocumentEvent ev)
085      {
086        if (!textField.hasFocus())
087        {
088          textField.requestFocusInWindow();
089        }
090      }
091
092      @Override
093      public void insertUpdate(DocumentEvent ev)
094      {
095        changedUpdate(ev);
096      }
097
098      @Override
099      public void removeUpdate(DocumentEvent ev)
100      {
101        changedUpdate(ev);
102      }
103    });
104    passwordField = Utilities.createPasswordField();
105    passwordField.getDocument().addDocumentListener(new DocumentListener()
106    {
107      @Override
108      public void changedUpdate(DocumentEvent ev)
109      {
110        if (!passwordField.hasFocus())
111        {
112          passwordField.requestFocusInWindow();
113        }
114      }
115
116      @Override
117      public void insertUpdate(DocumentEvent ev)
118      {
119        changedUpdate(ev);
120      }
121
122      @Override
123      public void removeUpdate(DocumentEvent ev)
124      {
125        changedUpdate(ev);
126      }
127    });
128    this.defaultEditor = new DefaultCellEditor(textField);
129    this.passwordEditor = new DefaultCellEditor(passwordField);
130    binaryPanel = new BinaryCellPanel();
131    binaryPanel.addEditActionListener(new ActionListener()
132    {
133      @Override
134      public void actionPerformed(ActionEvent e)
135      {
136        if (editBinaryDlg == null)
137        {
138          editBinaryPanel = new BinaryAttributeEditorPanel();
139          editBinaryPanel.setInfo(getInfo());
140          editBinaryDlg = new GenericDialog(Utilities.getFrame(table),
141              editBinaryPanel);
142          editBinaryDlg.setModal(true);
143          Utilities.centerGoldenMean(editBinaryDlg,
144              Utilities.getParentDialog(table));
145        }
146        if (binaryValue != null)
147        {
148          editBinaryPanel.setValue(attrName, binaryValue);
149        }
150        else if (value != null)
151        {
152          if (value.length > 0)
153          {
154            editBinaryPanel.setValue(attrName,
155                BinaryValue.createBase64(value));
156          }
157          else
158          {
159            editBinaryPanel.setValue(attrName, null);
160          }
161        }
162        else
163        {
164          editBinaryPanel.setValue(attrName, null);
165        }
166        editBinaryDlg.setVisible(true);
167        if (editBinaryPanel.valueChanged())
168        {
169          BinaryValue changedValue = editBinaryPanel.getBinaryValue();
170          binaryValue = changedValue;
171          value = null;
172          ocValue = null;
173        }
174        fireEditingStopped();
175      }
176    });
177    ocPanel = new ObjectClassCellPanel();
178    ocPanel.addEditActionListener(new ActionListener()
179    {
180      @Override
181      public void actionPerformed(ActionEvent ev)
182      {
183        if (editOcDlg == null)
184        {
185          editOcPanel = new ObjectClassEditorPanel();
186          editOcPanel.setInfo(getInfo());
187          editOcDlg = new GenericDialog(
188              null,
189              editOcPanel);
190          editOcDlg.setModal(true);
191          Utilities.centerGoldenMean(editOcDlg,
192              Utilities.getParentDialog(table));
193        }
194        if (ocValue != null)
195        {
196          editOcPanel.setValue(ocValue);
197        }
198        editOcDlg.setVisible(true);
199        if (editOcPanel.valueChanged())
200        {
201          binaryValue = null;
202          value = null;
203          ocValue = editOcPanel.getObjectClassValue();
204          fireEditingStopped();
205        }
206      }
207    });
208  }
209
210  @Override
211  public Component getTableCellEditorComponent(JTable table, Object value,
212                   boolean isSelected, int row, int column)
213  {
214    this.table = table;
215    if (isPassword(table, row))
216    {
217      this.value = null;
218      this.binaryValue = null;
219      this.ocValue = null;
220      return passwordEditor.getTableCellEditorComponent(table, value,
221          isSelected, row, column);
222    }
223    else if (value instanceof ObjectClassValue)
224    {
225      this.value = null;
226      this.binaryValue = null;
227      this.ocValue = (ObjectClassValue)value;
228      ocPanel.setValue(ocValue);
229      ocPanel.setBorder(CustomCellRenderer.getDefaultFocusBorder(table,
230          value, isSelected, row, column));
231      return ocPanel;
232    }
233    else if (value instanceof byte[] || value instanceof BinaryValue)
234    {
235      attrName = getAttributeName(table, row);
236      boolean isImage = Utilities.hasImageSyntax(attrName,
237          getInfo().getServerDescriptor().getSchema());
238      if (value instanceof byte[])
239      {
240        this.value = (byte[])value;
241        this.binaryValue = null;
242        this.ocValue = null;
243        if (this.value.length > 0)
244        {
245          binaryPanel.setValue(BinaryValue.createBase64(this.value), isImage);
246        }
247        else
248        {
249          binaryPanel.setValue((byte[])null, isImage);
250        }
251      }
252      else
253      {
254        this.value = null;
255        this.ocValue = null;
256        binaryValue = (BinaryValue)value;
257        binaryPanel.setValue(binaryValue, isImage);
258      }
259      binaryPanel.setBorder(CustomCellRenderer.getDefaultFocusBorder(table,
260          value, isSelected, row, column));
261      return binaryPanel;
262    }
263    else
264    {
265      this.value = null;
266      this.binaryValue = null;
267      this.ocValue = null;
268      return defaultEditor.getTableCellEditorComponent(table, value, isSelected,
269          row, column);
270    }
271  }
272
273  @Override
274  public Object getCellEditorValue()
275  {
276    if (binaryValue != null)
277    {
278      return binaryValue;
279    }
280    else if (value != null)
281    {
282      return value;
283    }
284    else if (ocValue != null)
285    {
286      return ocValue;
287    }
288    else
289    {
290      return defaultEditor.getCellEditorValue();
291    }
292  }
293
294  private boolean isPassword(JTable table, int row)
295  {
296    boolean isPassword = false;
297    Object o = table.getValueAt(row, 0);
298    if (Utilities.hasPasswordSyntax(String.valueOf(o),
299        getInfo().getServerDescriptor().getSchema()))
300    {
301      isPassword = true;
302    }
303    return isPassword;
304  }
305
306  private String getAttributeName(JTable table, int row)
307  {
308    return String.valueOf(table.getValueAt(row, 0));
309  }
310
311  /**
312   * Returns the control panel information.
313   * @return the control panel information.
314   */
315  public ControlPanelInfo getInfo()
316  {
317    return info;
318  }
319
320  /**
321   * Sets the control panel information.
322   * @param info the control panel information.
323   */
324  public void setInfo(ControlPanelInfo info)
325  {
326    this.info = info;
327  }
328}