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