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 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2016 ForgeRock AS.
016 */
017package org.opends.quicksetup.ui;
018
019import java.awt.AlphaComposite;
020import java.awt.Component;
021import java.awt.Dimension;
022import java.awt.Graphics;
023import java.awt.Graphics2D;
024import java.awt.GridBagConstraints;
025import java.awt.GridBagLayout;
026
027import javax.swing.Box;
028import javax.swing.Icon;
029import javax.swing.JPanel;
030
031/**
032 * This class is the panel that is displayed in the QuickSetupDialog.  It
033 * contains 3 panels that are passed in the constructor: the steps panel,
034 * the buttons panel and the current step panel (the main panel of the three).
035 * <p>
036 * The only remarkable thing of this class is that is responsible for
037 * implementing the background.  The three subpanels are transparent and
038 * this class sets a background (with the OpenDJ logo) and uses some basic
039 * transparency effects.
040 */
041public class FramePanel extends JPanel
042{
043  private static final long serialVersionUID = 7733658951410876078L;
044
045  private Icon backgroundIcon;
046  private Component stepsPanel;
047  private Component buttonsPanel;
048  private int buttonsPanelVerticalInsets;
049  private int stepsPanelHorizontalInsets;
050
051  /**
052   * The constructor of the FramePanel.
053   * @param stepsPanel the steps panel that on the top-left side of the
054   * QuickSetupDialog.
055   * @param currentStepPanel the current step panel (the panel that displays
056   * what is associated to the current step).  Is the panel that contains all
057   * the input fields and is located on the top-right side of the
058   * QuickSetupDialog.
059   * @param buttonsPanel the buttons panel that appears on the bottom of the
060   * QuickSetupDialog.
061   */
062  public FramePanel(Component stepsPanel, Component currentStepPanel,
063      Component buttonsPanel)
064  {
065    super(new GridBagLayout());
066    setBackground(UIFactory.DEFAULT_BACKGROUND);
067    GridBagConstraints gbc = new GridBagConstraints();
068
069    gbc.gridwidth = GridBagConstraints.RELATIVE;
070    gbc.weightx = 0.0;
071    gbc.weighty = 1.0;
072    gbc.anchor = GridBagConstraints.NORTHWEST;
073    gbc.fill = GridBagConstraints.VERTICAL;
074    gbc.insets = UIFactory.getStepsPanelInsets();
075    add(stepsPanel, gbc);
076
077    stepsPanelHorizontalInsets = gbc.insets.left + gbc.insets.right;
078
079    JPanel currentPanelContainer = new JPanel(new GridBagLayout());
080    currentPanelContainer.setBorder(UIFactory.CURRENT_STEP_PANEL_BORDER);
081    currentPanelContainer
082        .setBackground(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
083    currentPanelContainer.setOpaque(false);
084
085    gbc.gridwidth = GridBagConstraints.REMAINDER;
086    gbc.weightx = 1.0;
087    gbc.weighty = 1.0;
088    gbc.anchor = GridBagConstraints.NORTHWEST;
089    gbc.fill = GridBagConstraints.BOTH;
090    gbc.insets = UIFactory.getCurrentStepPanelInsets();
091    currentPanelContainer.add(currentStepPanel, gbc);
092    gbc.insets = UIFactory.getEmptyInsets();
093    add(currentPanelContainer, gbc);
094
095    gbc.gridwidth = GridBagConstraints.RELATIVE;
096    gbc.anchor = GridBagConstraints.WEST;
097    gbc.weightx = 0.0;
098    gbc.weighty = 0.0;
099    add(Box.createHorizontalGlue(), gbc);
100
101    gbc.gridwidth = GridBagConstraints.REMAINDER;
102    gbc.weightx = 1.0;
103    gbc.weighty = 0.0;
104    gbc.anchor = GridBagConstraints.NORTHWEST;
105    gbc.fill = GridBagConstraints.HORIZONTAL;
106    gbc.insets = UIFactory.getButtonsPanelInsets();
107    add(buttonsPanel, gbc);
108
109    buttonsPanelVerticalInsets = gbc.insets.top + gbc.insets.bottom;
110
111    backgroundIcon =
112      UIFactory.getImageIcon(UIFactory.IconType.BACKGROUND);
113
114    int backGroundIconWidth = 0;
115    int backGroundIconHeight = 0;
116    if (backgroundIcon != null)
117    {
118      backGroundIconWidth = backgroundIcon.getIconWidth();
119      backGroundIconHeight = backgroundIcon.getIconHeight();
120    }
121
122    this.buttonsPanel = buttonsPanel;
123    this.stepsPanel = stepsPanel;
124    int width =
125        Math.max((int) getPreferredSize().getWidth(), backGroundIconWidth
126            + UIFactory.LEFT_INSET_BACKGROUND
127            + UIFactory.RIGHT_INSET_BACKGROUND);
128    int height =
129        Math.max((int) getPreferredSize().getHeight(), backGroundIconHeight
130            + UIFactory.TOP_INSET_BACKGROUND
131            + UIFactory.BOTTOM_INSET_BACKGROUND);
132    setPreferredSize(new Dimension(width, height));
133  }
134
135  /**
136   * {@inheritDoc}
137   *
138   * This method has been overwritten to be able to have a transparency effect
139   * with the OpenDJ logo background.
140   */
141  @Override
142  protected void paintComponent(Graphics g)
143  {
144      // paint background
145      g.setColor(UIFactory.DEFAULT_BACKGROUND);
146      int width = getWidth();
147      int height = getHeight();
148      int buttonsTotalHeight = buttonsPanel.getHeight()
149      + buttonsPanelVerticalInsets;
150      int stepsPanelTotalWidth = stepsPanel.getWidth()
151      + stepsPanelHorizontalInsets;
152
153      g.fillRect(0, 0, stepsPanelTotalWidth, height);
154      g.fillRect(stepsPanelTotalWidth, height - buttonsTotalHeight, width,
155              height);
156      g.setColor(UIFactory.CURRENT_STEP_PANEL_BACKGROUND);
157      g.fillRect(stepsPanelTotalWidth, 0, width, height - buttonsTotalHeight);
158
159      if (backgroundIcon != null)
160      {
161          // Draw the icon over and over, right aligned.
162          // Copy the Graphics object, which is actually
163          // a Graphics2D object. Cast it so we can
164          // set alpha composite.
165          Graphics2D g2d = (Graphics2D) g.create();
166
167          g2d.setComposite(AlphaComposite.getInstance(
168                  AlphaComposite.SRC_OVER, 0.1f));
169
170          backgroundIcon.paintIcon(this, g2d,
171                  UIFactory.LEFT_INSET_BACKGROUND,
172                  UIFactory.TOP_INSET_BACKGROUND);
173
174          g2d.dispose(); //clean up
175      }
176  }
177}