123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- /*
- * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.installer.ui;
-
- import com.dmdirc.installer.Step;
-
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Insets;
- import java.awt.LayoutManager2;
- import java.io.Serializable;
- import java.util.ArrayList;
- import java.util.List;
-
- /**
- * Adjusted Card layout.
- */
- public class StepLayout implements LayoutManager2, Serializable {
-
- /**
- * A version number for this class. It should be changed whenever the class
- * structure is changed (or anything else that would prevent serialized
- * objects being unserialized with the new class).
- */
- private static final long serialVersionUID = 2;
- /** Cards vector. */
- private final List<SwingStep> steps;
- /** Current step. */
- private int currentStep;
- /** Vertical gap. */
- private int vGap;
- /** Horiontal gap. */
- private int hGap;
-
- /**
- * Instantiates a new step layout.
- */
- public StepLayout() {
- this(0, 0);
- }
-
- /**
- * Instantiates a new step layout.
- *
- * @param parent Parent component
- */
- public StepLayout(final Container parent) {
- this(0, 0, parent);
- }
-
- /**
- * Instantiates a new step layout with the specified gaps.
- *
- * @param hGap Horizontal gap
- * @param vGap Vertical gap
- */
- public StepLayout(final int hGap, final int vGap) {
- this(hGap, vGap, null);
- }
-
- /**
- * Instantiates a new step layout with the specified gaps.
- *
- * @param hGap Horizontal gap
- * @param vGap Vertical gap
- * @param parent Parent component
- */
- public StepLayout(final int hGap, final int vGap, final Container parent) {
- steps = new ArrayList<SwingStep>();
- currentStep = -1;
- this.hGap = hGap;
- this.vGap = vGap;
- }
-
- /**
- * Returns the number of steps in the layout.
- *
- * @return number of steps >= 0
- */
- public int size() {
- return steps.size();
- }
-
- /**
- * Checks if the layout is empty
- *
- * @return true iif the layout has no steps
- */
- public boolean isEmpty() {
- return steps.isEmpty();
- }
-
- /**
- * Returns the specified step from the layout.
- *
- * @param index Step to retrieve
- *
- * @return Step
- */
- public Step getStep(final int index) {
- return steps.get(index);
- }
-
- /**
- * Returns the step list.
- *
- * @return List of steps
- */
- public List<SwingStep> getSteps() {
- return steps;
- }
-
- /**
- * Returns the current step index.
- *
- * @return Current step index
- */
- public int getCurrentStepIndex() {
- return currentStep;
- }
-
- /**
- * Returns the current step name.
- *
- * @return Current step name
- */
- public String getCurrentStepName() {
- return steps.get(currentStep).getStepName();
- }
-
- /**
- * Returns the current step name.
- *
- * @return Current step name
- */
- public Step getCurrentStep() {
- return steps.get(currentStep);
- }
-
- /**
- * Show the first step.
- *
- * @param parent Parent container
- */
- public void first(final Container parent) {
- show(0, parent);
- }
-
- /**
- * Show the last step.
- *
- * @param parent Parent container
- */
- public void last(final Container parent) {
- show(parent.getComponentCount() - 1, parent);
- }
-
- /**
- * Show the next step.
- *
- * @param parent Parent container
- */
- public void next(final Container parent) {
- show(currentStep + 1, parent);
- }
-
- /**
- * Show the previous step.
- *
- * @param parent Parent container
- */
- public void previous(final Container parent) {
- show(currentStep - 1, parent);
- }
-
- /**
- * Show the specified step.
- *
- * @param step Step to show
- * @param parent Parent container
- */
- public void show(final Step step, final Container parent) {
- show(steps.indexOf(step), parent);
- }
-
- /**
- * Show the step at the specified index.
- *
- * @param step Step to show
- * @param parent Parent container
- */
- public void show(final int step, final Container parent) {
- int stepNumber = step;
- if (stepNumber == -1) {
- if (stepNumber >= steps.size()) {
- stepNumber = steps.size() - 1;
- } else {
- stepNumber = 0;
- }
- }
- synchronized (parent.getTreeLock()) {
- final int componentCount = parent.getComponentCount();
- for (int i = 0; i < componentCount; i++) {
- final Component comp = parent.getComponent(i);
- if (comp.isVisible()) {
- comp.setVisible(false);
- break;
- }
- }
- if (componentCount > 0) {
- currentStep = stepNumber;
- parent.getComponent(currentStep).setVisible(true);
- parent.validate();
- }
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void addLayoutComponent(final Component comp,
- final Object constraints) {
- if (!(comp instanceof Step)) {
- throw new IllegalArgumentException(
- "Component must be an instance of Step");
- }
- addLayoutComponent((SwingStep) comp);
- }
-
- /**
- * {@inheritDoc}
- *
- * @deprecated Use addLayoutComponent(Component, Object) or
- * addLayoutComponent(Component)
- *
- * @see addLayoutComponent(Component)
- * @see addLayoutComponent(Component, Object)
- */
- @Override
- @Deprecated
- public void addLayoutComponent(final String name, final Component comp) {
- if (!(comp instanceof Step)) {
- throw new IllegalArgumentException(
- "Component must be an instance of Step");
- }
- addLayoutComponent((SwingStep) comp);
- }
-
- /**
- * Adds a component to the layout.
- *
- * @param step Component to add
- */
- public void addLayoutComponent(final SwingStep step) {
- synchronized (step.getTreeLock()) {
- if (!steps.isEmpty()) {
- step.setVisible(false);
- }
- steps.add(step);
- }
- }
-
- /** {@inheritDoc} */
- @Override
- public void removeLayoutComponent(final Component comp) {
- synchronized (comp.getTreeLock()) {
- if (comp.isVisible()) {
- comp.setVisible(false);
- }
- next(comp.getParent());
- steps.remove(comp);
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @return Returns the preferred size of the container
- */
- @Override
- public Dimension preferredLayoutSize(final Container parent) {
- synchronized (parent.getTreeLock()) {
- final Insets insets = parent.getInsets();
- final int componentCount = parent.getComponentCount();
- int width = 0;
- int height = 0;
-
- for (int i = 0; i < componentCount; i++) {
- final Component comp = parent.getComponent(i);
- final Dimension preferredDimension = comp.getPreferredSize();
-
- width = Math.max(width, preferredDimension.width);
- height = Math.max(height, preferredDimension.height);
- }
- return new Dimension(insets.left + insets.right + width + hGap * 2,
- insets.top + insets.bottom + height + vGap * 2);
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @return Returns the minimum size of the container
- */
- @Override
- public Dimension minimumLayoutSize(final Container parent) {
- synchronized (parent.getTreeLock()) {
- final Insets insets = parent.getInsets();
- final int componentCount = parent.getComponentCount();
- int width = 0;
- int height = 0;
-
- for (int i = 0; i < componentCount; i++) {
- final Component comp = parent.getComponent(i);
- final Dimension minimumDimension = comp.getMinimumSize();
-
- width = Math.max(width, minimumDimension.width);
- height = Math.max(height, minimumDimension.height);
- }
- return new Dimension(insets.left + insets.right + width + hGap * 2,
- insets.top + insets.bottom + height + vGap * 2);
- }
- }
-
- /**
- * {@inheritDoc}
- *
- * @param parent Container to get the size for
- *
- * @return Returns the maximum size of the container
- */
- @Override
- public Dimension maximumLayoutSize(final Container parent) {
- return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
- }
-
- /**
- * {@inheritDoc}
- *
- * @param target Container to get the alignment from
- *
- * @return Alignment
- */
- @Override
- public float getLayoutAlignmentX(final Container target) {
- return 0.5f;
- }
-
- /**
- * {@inheritDoc}
- *
- * @param target Container to get the alignment from
- *
- * @return Alignment
- */
- @Override
- public float getLayoutAlignmentY(final Container target) {
- return 0.5f;
- }
-
- /**
- * {@inheritDoc}
- *
- * @param target Container to invalidate
- */
- @Override
- public void invalidateLayout(final Container target) {
- //Ignore
- }
-
- /** {@inheritDoc} */
- @Override
- public void layoutContainer(final Container parent) {
- synchronized (parent.getTreeLock()) {
- final Insets insets = parent.getInsets();
- final int componentCount = parent.getComponentCount();
- Component comp = null;
- boolean currentFound = false;
-
- for (int i = 0; i < componentCount; i++) {
- comp = parent.getComponent(i);
- comp.setBounds(hGap + insets.left, vGap + insets.top,
- parent.getWidth() - (hGap * 2 + insets.left +
- insets.right), parent.
- getHeight() - (vGap * 2 +
- insets.top + insets.bottom));
- if (comp.isVisible()) {
- currentFound = true;
- }
- }
-
- if (!currentFound && componentCount > 0) {
- parent.getComponent(0).setVisible(true);
- }
- }
- }
- }
|