DefaultWizardDialog.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.component |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.component;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import org.xnap.XNap;
import org.xnap.gui.Dialogs;
import org.xnap.gui.SettingsPanel;
import org.xnap.gui.util.IconHelper;
public class DefaultWizardDialog extends DefaultDialog
{
//--- Constant(s) ---
//--- Data field(s) ---
private JLabel jlTitle;
private JLabel jlIcon;
private MultiLineLabel jtaDescription;
private JPanel ctPanels;
private CardLayout clPanels;
private LinkedList panels = new LinkedList();
private int selectedIndex = 0;
private JButton jbNext;
private JButton jbFinish;
private PreviousAction acPrevious = new PreviousAction();
private NextAction acNext = new NextAction();
private FinishAction acFinish = new FinishAction();
//--- Constructor(s) ---
public DefaultWizardDialog(int buttons)
{
super(buttons);
// top
JPanel jpTop = new JPanel(new BorderLayout());
jpTop.setBackground(Color.white);
jpTop.setBorder(BorderFactory.createEtchedBorder());
getContentPane().add(jpTop, BorderLayout.NORTH);
JPanel jpTitle = new JPanel(new BorderLayout());
jpTitle.setBackground(Color.white);
jpTop.add(jpTitle, BorderLayout.CENTER);
jlTitle = new JLabel(" ");
jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
jlTitle.setFont(new Font("Dialog", Font.BOLD, 16));
jlTitle.setForeground(Color.black);
jpTitle.add(jlTitle, BorderLayout.NORTH);
jtaDescription = new MultiLineLabel(" ");
jtaDescription.setBackground(Color.white);
jtaDescription.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
jtaDescription.setForeground(Color.black);
jpTitle.add(jtaDescription, BorderLayout.CENTER);
jlIcon = new JLabel();
jlIcon.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
jpTop.add(jlIcon, BorderLayout.EAST);
// center
clPanels = new CardLayout();
ctPanels = new JPanel();
ctPanels.setBorder(new EmptyBorder(5, 5, 5, 5));
ctPanels.setLayout(clPanels);
// bottom
JPanel jpButtons = getButtonPanel();
jpButtons.add(new XNapButton(acPrevious), 0);
jbNext = new XNapButton(acNext);
jbNext.setHorizontalTextPosition(SwingConstants.LEFT);
jpButtons.add(jbNext, 1);
jbFinish = new XNapButton(acFinish);
jpButtons.add(jbFinish, 2);
// set me up
setMainComponent(ctPanels);
setSize(640, 480);
}
public DefaultWizardDialog()
{
this(BUTTON_CANCEL);
}
//--- Method(s) ---
public void addPanel(SettingsPanel panel)
{
ctPanels.add(panel.getPanel(), panel.getTitle());
panels.add(panel);
updatePanel(0);
}
public void finish()
{
boolean canClose = true;
for (Iterator i = panels.iterator(); i.hasNext();) {
try {
((SettingsPanel)i.next()).apply();
}
catch (Exception e) {
canClose = false;
Dialogs.error(this, e.toString());
}
}
if (canClose) {
isOkay = true;
close();
}
}
public Action getFinishAction()
{
return acFinish;
}
public Action getNextAction()
{
return acNext;
}
public int getSelectedIndex()
{
return selectedIndex;
}
public SettingsPanel getSelectedPanel()
{
return (SettingsPanel)panels.get(getSelectedIndex());
}
/**
* Invoked when the user presses next or previous.
*
* <p>Sub classes can overwrite this method.
*/
protected void panelChanged(SettingsPanel panel)
{
}
private void updateActions()
{
acPrevious.setEnabled(selectedIndex > 0);
acNext.setEnabled(selectedIndex < panels.size() - 1);
if (acNext.isEnabled()) {
jbNext.requestFocus();
}
else {
jbFinish.requestFocus();
}
}
private void updatePanel(int index)
{
if (index == 1) {
clPanels.next(ctPanels);
}
else if (index == -1) {
clPanels.previous(ctPanels);
}
else if (index != 0) {
// this should never happen
return;
}
selectedIndex += index;
updateActions();
// update top
SettingsPanel panel = getSelectedPanel();
jlTitle.setText(panel.getTitle());
jlIcon.setIcon(panel.getIcon());
jtaDescription.setText(panel.getDescription());
if (index != 0 || (selectedIndex == 0 && index == 0)) {
panelChanged(panel);
}
}
public class PreviousAction extends AbstractAction {
public PreviousAction()
{
putValue(Action.NAME, XNap.tr("Previous"));
putValue(IconHelper.XNAP_ICON, "1leftarrow.png");
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Goes to previous page."));
}
public void actionPerformed(ActionEvent event)
{
updatePanel(-1);
}
}
public class NextAction extends AbstractAction {
public NextAction()
{
putValue(Action.NAME, XNap.tr("Next"));
putValue(IconHelper.XNAP_ICON, "1rightarrow.png");
putValue(Action.SHORT_DESCRIPTION, XNap.tr("Goes to next page."));
}
public void actionPerformed(ActionEvent event)
{
updatePanel(1);
}
}
public class FinishAction extends AbstractAction {
public FinishAction()
{
putValue(Action.NAME, XNap.tr("Finish"));
putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Closes the dialog saving changes."));
}
public void actionPerformed(ActionEvent event)
{
finish();
}
}
protected class PContainer extends JPanel
{
Dimension maxDim = new Dimension(0, 0);
public void add(Component comp, Object constraints)
{
Dimension d = comp.getPreferredSize();
if (d.height > maxDim.height) {
maxDim.height = d.height;
}
if (d.width > maxDim.width) {
maxDim.width = d.width;
}
super.add(comp, constraints);
}
public Dimension getPreferredSize()
{
// looks like the height is too small
return new Dimension(maxDim.width + 10, maxDim.height + 30);
}
}
}
The table below shows all metrics for DefaultWizardDialog.java.




