summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/WizardPage.java
blob: fc1da67f5ff1acb06765d84c4c3da6f14bc90564 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package org.openslx.dozmod.gui.wizard;

import java.awt.Color;
import java.awt.Dialog;

import javax.swing.Icon;
import javax.swing.JPanel;
import javax.swing.UIManager;

@SuppressWarnings("serial")
public abstract class WizardPage extends JPanel {

	private static final Color WARNING_COLOR = new Color(200, 100, 0);

	private final String title;

	private String description = null;

	private String message = null;

	private boolean isError = false;

	private boolean isComplete = false;

	protected final Wizard wizard;

	/**
	 * Whether the user is allowed to go back to this page once they clicked
	 * next.
	 */
	protected boolean canComeBack = true;

	public WizardPage(Wizard wizard, String title) {
		if (wizard == null)
			throw new NullPointerException("WizardPage needs a Wizard");
		this.wizard = wizard;
		this.title = title;
	}

	public String getTitle() {
		return this.title;
	}

	public String getMessage() {
		if (this.message != null)
			return this.message;
		return this.description;
	}

	private void updateHeader() {
		if (this.wizard != null)
			this.wizard.updateHeader(this);
	}

	protected Dialog getDialog() {
		return wizard;
	}

	public boolean isComplete() {
		return this.isComplete;
	}

	/*
	 * Feedback from the pages
	 */

	/**
	 * Called when the user wants to advance to the next page. You have the
	 * chance to run some sanity checks and return false if you want to prevent
	 * the user from switching the page, but in general it is recommended to
	 * disable the next button as long as there are errors in the current page.
	 * 
	 * @return true allows to flip the page, false cancels
	 */
	protected boolean wantNextOrFinish() {
		return isComplete;
	}

	/**
	 * Called when the page is being shown. This can be caused by three possible
	 * events:
	 * 1) The wizard opens and this is the fist page
	 * 2) The user clicked "Previous" and this is the previous page
	 * 3) The user clicked "Next" and this is the next page
	 */
	protected void onPageEnter() {
	}

	/**
	 * Called when the page is being hidden. This can be caused by three
	 * possible events:
	 * 1) The user clicked finish and this is the page currently displayed
	 * 2) The user clicked "Previous" and this is the current page
	 * 3) The user clicked "Next" and this is the current page
	 */
	protected void onPageLeave() {
	}

	/*
	 * Setters from derived classes
	 */

	protected void setPageComplete(boolean b) {
		if (isComplete == b)
			return;
		isComplete = b;
		if (wizard != null)
			wizard.updateButtons(this);
	}

	protected void setDescription(String description) {
		this.message = null;
		this.description = description;
		this.updateHeader();
	}

	protected void setErrorMessage(String message) {
		this.message = message;
		this.isError = true;
		this.updateHeader();
	}

	protected void setWarningMessage(String message) {
		this.message = message;
		this.isError = false;
		this.updateHeader();
	}

	protected Icon getMessageIcon() {
		if (message == null)
			return null;
		if (isError)
			return UIManager.getIcon("OptionPane.errorIcon");
		return UIManager.getIcon("OptionPane.warningIcon");
	}

	public Color getMessageColor() {
		if (message == null)
			return Color.BLACK;
		if (isError)
			return Color.RED;
		return WARNING_COLOR;
	}

}