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