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