summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/Wizard.java
blob: 3c5e27ba5f59bd9e67498262959994a512d93b98 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package org.openslx.dozmod.gui.wizard;

import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.I18n;
import org.openslx.dozmod.gui.helper.StatusHeader;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

public abstract class Wizard extends JDialog {

	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = 1436816291272809418L;

	private final StatusHeader header;
	private final List<WizardPage> pages = new ArrayList<>();
	private WizardPage postFinishPage = null;
	private boolean isPostFinish = false;
	private final JPanel contentPanel;
	private int currentPage = -1;
	private boolean needsLayout = true;
	private boolean isCancelled = false;
	// Reference if an out of order page is shown.
	protected WizardPage outOfOrderPage = null;

	private final JButton btnPrev;
	private final JButton btnNext;
	private final JButton btnCancel;
	private final JButton btnFinish;

	public Wizard(Window parent) {
		super(parent, ModalityType.APPLICATION_MODAL);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setLayout(new BorderLayout());

		this.header = new StatusHeader(getContentPane(), "<titel>", "<message>");
		// Buttons in footer
		JPanel footer = new JPanel();
		footer.setLayout(new BoxLayout(footer, BoxLayout.LINE_AXIS));
		footer.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		footer.add(Box.createHorizontalGlue());
		btnPrev = new JButton(I18n.WIZARD.getString("Wizard.Button.prev.text"));
		btnNext = new JButton(I18n.WIZARD.getString("Wizard.Button.next.text"));
		btnCancel = new JButton(I18n.WIZARD.getString("Wizard.Button.cancel.text"));
		btnFinish = new JButton(I18n.WIZARD.getString("Wizard.Button.finish.text.0"));
		footer.add(btnPrev);
		footer.add(btnNext);
		footer.add(Box.createRigidArea(new Dimension(10, 10)));
		footer.add(btnCancel);
		footer.add(Box.createRigidArea(new Dimension(5, 5)));
		footer.add(btnFinish);
		add(footer, BorderLayout.PAGE_END);
		// Add content panel
		contentPanel = new JPanel();
		contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
		contentPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
		add(contentPanel, BorderLayout.CENTER);

		// Scale window with font size
		setPreferredSize(Gui.getScaledDimension(550, 420));
		setResizable(false);
		pack();
		Gui.centerShellOverShell(parent, this);

		// Window events
		addWindowListener(new WindowAdapter() {
			@Override public void windowClosing(WindowEvent we) {
				doCancel();
			}
		});
		// Äkschns
		btnNext.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				doNext();
			}
		});
		btnPrev.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				doPrevious();
			}
		});
		btnCancel.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				doCancel();
			}
		});
		btnFinish.addActionListener(new ActionListener() {
			@Override public void actionPerformed(ActionEvent e) {
				doFinish();
			}
		});
	}

	@Override
	public void setVisible(boolean visible) {
		if (visible) {
			if (needsLayout) {
				needsLayout = false;
				pack();
				if (!pages.isEmpty()) {
					showPage(0);
				}
			}
		}

		super.setVisible(visible);
	}

	protected void showPage(int index) {
		if (currentPage != -1) {
			WizardPage old = getPage(currentPage);
			old.onPageLeave();
			old.setVisible(false);
		}
		WizardPage page = getPage(index);
		page.onPageEnter();
		page.setVisible(true);
		currentPage = index;
		updateHeader(page);
		updateButtons(page);
		validate();
	}

	// Show an out of order page e.g. for converting an image
	public void showOutOfOrderPage(WizardPage page) {
		WizardPage old = getPage(currentPage);
		outOfOrderPage = page;
		old.onPageLeave();
		old.setVisible(false);

		page.onPageEnter();
		page.setVisible(true);
		updateHeader(page);
		updateButtons(page);
		validate();
	}

	// Show an out of order page e.g. for converting an image
	/**
	 * @param newPage to return to.
	 * @param oldPage that should be hidden.
	 */
	public void returnAfterOutOfOrderPage(WizardPage newPage, WizardPage oldPage) {
		oldPage.onPageLeave();
		oldPage.setVisible(false);

		newPage.onPageEnter();
		newPage.setVisible(true);
		updateHeader(newPage);
		updateButtons(newPage);
		validate();
	}

	void updateHeader(WizardPage page) {
		if (!isPostFinish && (currentPage == -1 || getPage(currentPage) != page))
			return;
		String pageTitle = page.getTitle();
		String pageDesc = page.getMessage();
		if (pageTitle == null)
			pageTitle = "Step " + currentPage;
		if (pageDesc == null)
			pageDesc = "";
		header.updateHeader(pageTitle, pageDesc, page.getMessageIcon(), page.getMessageColor());
		setTitle(getWindowTitle() + " - " + pageTitle);
	}

	public abstract String getWindowTitle();

	protected final void addPage(WizardPage page) {
		contentPanel.add(page);
		if (!pages.isEmpty()) {
			page.setVisible(false);
		}
		pages.add(page);
	}

	// For adding pages which are not in the normal linear flow
	protected final void addOutOfOrderPage(WizardPage page) {
		contentPanel.add(page);
		page.setVisible(false);
	}

	protected final void removePages(List<WizardPage> currentPages) {
		for (WizardPage i : currentPages) {
			pages.remove(i);
		}
	}

	protected void addSummaryPage(WizardPage page) {
		postFinishPage = page;
	}

	void updateButtons(WizardPage page) {
		if (isPostFinish) {
			btnFinish.setEnabled(postFinishPage.isComplete());
			return;
		}
		// State of finish button
		boolean canFinish = true;
		for (WizardPage p : pages) {
			if (!p.isComplete()) {
				canFinish = false;
				break;
			}
		}
		btnFinish.setEnabled(canFinish);
		// State of next button
		if (outOfOrderPage != null) {
			btnNext.setEnabled(page.isComplete());
		}
		if (currentPage != -1 && getPage(currentPage) == page) {
			btnNext.setEnabled(currentPage + 1 < pages.size() && page.isComplete());
			btnPrev.setEnabled(currentPage > 0 && getPage(currentPage - 1).canComeBack);
		}
	}

	/**
	 * Returns whether the page currently shown is the summary page.
	 *
	 * @return whether the page currently shown is the summary page.
	 */
	protected final boolean isSummaryPage() {
		return isPostFinish;
	}

	/**
	 * Returns true if the wizard was cancelled. Mostly useful in onPageLeave to
	 * distinguish what's happening.
	 *
	 * @return true if the wizard was cancelled.
	 */
	public final boolean isCancelled() {
		return isCancelled;
	}

	public void doNext() {
		if (isPostFinish || !btnNext.isEnabled())
			return;
		if (currentPage + 1 < pages.size()) {
			if (!getPage(currentPage).wantNextOrFinish())
				return; // Page canceled the operation
			showPage(currentPage + 1);
		}
	}

	// protected void returnAfterOutOfOrderPage(int previousPage){
	// 	showPage(previousPage);
	// }

	protected void doPrevious() {
		if (isPostFinish || !btnPrev.isEnabled())
			return;
		if (currentPage > 0) {
			showPage(currentPage - 1);
		}
	}

	protected final void doCancel() {
		if (onCancelRequest()) {
			isCancelled = true;
			if (currentPage != -1) {
				getPage(currentPage).onPageLeave();
			}
			if (isPostFinish) {
				postFinishPage.onPageLeave();
			}
			if (outOfOrderPage != null) {
				outOfOrderPage.onPageLeave();
			}
			dispose();
		}
	}

	protected final void doFinish() {
		if (isPostFinish) {
			postFinishPage.onPageLeave();
			dispose();
			return;
		}
		if (!btnFinish.isEnabled())
			return;
		if (currentPage != -1) {
			if (!getPage(currentPage).wantNextOrFinish())
				return;
		}
		if (wantFinish()) {
			if (currentPage != -1) {
				getPage(currentPage).onPageLeave();
			}
			postFinishPage = performFinish();
			if (postFinishPage == null) {
				dispose();
			} else {
				isPostFinish = true;
				btnPrev.setVisible(false);
				btnNext.setVisible(false);
				btnFinish.setText(I18n.WIZARD.getString("Wizard.Button.finish.text.1"));
				postFinishPage.setVisible(false);
				contentPanel.add(postFinishPage);
				showPage(-1);
			}
		}
	}

	private WizardPage getPage(int index) {
		if (isPostFinish && index == -1)
			return postFinishPage;
		return pages.get(index);
	}

	/*
	 * Callback to wizard implementation
	 */

	/**
	 * User clicked cancel or (X) - when returning false,
	 * wizard will stay open
	 *
	 * @return
	 */
	protected boolean onCancelRequest() {
		return true;
	}

	/**
	 * Called when user clicks finish. Override to do final checks and take
	 * appropriate actions.
	 *
	 * @return <code>true</code> if finish is allowed, <code>false</code>
	 * otherwise
	 */
	protected boolean wantFinish() {
		return true;
	}

	/**
	 * Called so the wizard can perform finishing steps. This is called after
	 * wantFinish() returned true and onPageLeave was called on the currently
	 * visible wizard step.
	 * If a wizard page is returned, it will serve as a summary page shown to
	 * the user within the wizard frame. The prev and next buttons will
	 * disappear, and the finish button will change to a close button.
	 *
	 * @return A wizard page that serves as a summary page, or null if the
	 * wizard should just close
	 */
	protected WizardPage performFinish() {
		return null;
	}

}