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