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 */
017
018package org.opends.guitools.controlpanel.ui;
019
020import static org.opends.messages.AdminToolMessages.*;
021
022import java.awt.Component;
023import java.awt.GridBagConstraints;
024
025import org.forgerock.i18n.LocalizableMessage;
026import org.forgerock.i18n.slf4j.LocalizedLogger;
027
028import javax.swing.Box;
029import javax.swing.JLabel;
030import javax.swing.JTextField;
031
032import org.opends.guitools.controlpanel.event.ConfigurationChangeEvent;
033import org.opends.guitools.controlpanel.util.BackgroundTask;
034import org.opends.guitools.controlpanel.util.Utilities;
035import org.opends.server.types.Schema;
036
037/** The panel used to display a binary value. */
038public class BinaryValuePanel extends StatusGenericPanel
039{
040  private static final long serialVersionUID = 2536360199438858665L;
041  private JLabel lBase64;
042  private JTextField base64;
043  private JLabel attrName;
044  private JLabel imagePreview;
045  private JLabel lImage = Utilities.createDefaultLabel();
046  private byte[] lastBytes;
047
048  private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass();
049
050  /** Default constructor. */
051  public BinaryValuePanel()
052  {
053    super();
054    createLayout();
055  }
056
057  /**
058   * Sets the value to be displayed in the panel.
059   * @param attr the attribute name.
060   * @param bytes the binary value.
061   */
062  public void setValue(final String attr, final byte[] bytes)
063  {
064    final boolean launchBackground = lastBytes != bytes;
065    lastBytes = bytes;
066    BackgroundTask<Void> worker = new BackgroundTask<Void>()
067    {
068      @Override
069      public Void processBackgroundTask() throws Throwable
070      {
071        try
072        {
073          Thread.sleep(1000);
074        }
075        catch (Throwable t)
076        {
077        }
078        attrName.setText(attr);
079        Schema schema = getInfo().getServerDescriptor().getSchema();
080        if (Utilities.hasImageSyntax(attr, schema))
081        {
082          BinaryAttributeEditorPanel.updateImage(lImage, bytes);
083          lBase64.setVisible(false);
084          base64.setVisible(false);
085          imagePreview.setVisible(true);
086        }
087        else
088        {
089          lImage.setIcon(null);
090          lImage.setText("");
091          imagePreview.setVisible(false);
092          lBase64.setVisible(true);
093          base64.setVisible(true);
094          BinaryAttributeEditorPanel.updateBase64(base64, bytes);
095        }
096        return null;
097      }
098
099      @Override
100      public void backgroundTaskCompleted(Void returnValue, Throwable t)
101      {
102        displayMainPanel();
103        packParentDialog();
104        if (t != null)
105        {
106          logger.warn(LocalizableMessage.raw("Error reading binary contents: "+t, t));
107        }
108      }
109    };
110    if (launchBackground)
111    {
112      displayMessage(INFO_CTRL_PANEL_READING_SUMMARY.get());
113      worker.startBackgroundTask();
114    }
115    else
116    {
117      attrName.setText(attr);
118    }
119  }
120
121  @Override
122  public Component getPreferredFocusComponent()
123  {
124    return base64;
125  }
126
127  @Override
128  public GenericDialog.ButtonType getButtonType()
129  {
130    return GenericDialog.ButtonType.CLOSE;
131  }
132
133  @Override
134  public void okClicked()
135  {
136    // No OK Button
137  }
138
139  @Override
140  public boolean requiresScroll()
141  {
142    return true;
143  }
144
145  @Override
146  public LocalizableMessage getTitle()
147  {
148    return INFO_CTRL_PANEL_VIEW_BINARY_ATTRIBUTE_TITLE.get();
149  }
150
151  @Override
152  public void configurationChanged(ConfigurationChangeEvent ev)
153  {
154  }
155
156  /** Creates the layout of the panel (but the contents are not populated here). */
157  private void createLayout()
158  {
159    GridBagConstraints gbc = new GridBagConstraints();
160    gbc.gridy = 0;
161    gbc.gridx = 0;
162
163    JLabel l = Utilities.createPrimaryLabel(
164        INFO_CTRL_PANEL_ATTRIBUTE_NAME_LABEL.get());
165    add(l, gbc);
166    gbc.gridx ++;
167    gbc.insets.left = 10;
168    gbc.fill = GridBagConstraints.NONE;
169    attrName = Utilities.createDefaultLabel();
170    add(attrName, gbc);
171    gbc.gridx ++;
172    gbc.fill = GridBagConstraints.HORIZONTAL;
173    gbc.insets.left = 0;
174    gbc.weightx = 1.0;
175    add(Box.createHorizontalGlue(), gbc);
176
177    gbc.gridwidth = 3;
178    gbc.anchor = GridBagConstraints.WEST;
179    gbc.insets.top = 10;
180    gbc.gridy ++;
181    gbc.gridx = 0;
182    lBase64 = Utilities.createPrimaryLabel(
183        INFO_CTRL_PANEL_VALUE_IN_BASE_64_LABEL.get());
184    add(lBase64, gbc);
185
186    gbc.gridy ++;
187    gbc.fill = GridBagConstraints.BOTH;
188    gbc.weightx = 1.0;
189    base64 = Utilities.createLongTextField();
190    add(base64, gbc);
191
192    imagePreview = Utilities.createPrimaryLabel(
193        INFO_CTRL_PANEL_IMAGE_PREVIEW_LABEL.get());
194    gbc.gridy ++;
195    gbc.weightx = 0.0;
196    gbc.weighty = 0.0;
197    add(imagePreview, gbc);
198    gbc.gridy ++;
199    gbc.weightx = 0.0;
200    gbc.weighty = 0.0;
201    gbc.insets.top = 5;
202    add(lImage, gbc);
203
204    addBottomGlue(gbc);
205  }
206}