1
2
3
4
5
6
7
8
9
10
11
12 package sk.uniba.euromath.editor.wizards;
13 import org.eclipse.jface.wizard.Wizard;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.swt.widgets.Control;
19 import sk.uniba.euromath.editor.widgets.ValidityMessages;
20 /***
21 * Extends the <code>WizardPage</code> class and adds some functionality.
22 * @author Martin Vysny
23 */
24 public abstract class BaseWizardPage {
25 /***
26 * This page's name.
27 */
28 protected final String name;
29 /***
30 * This page's title.
31 */
32 final String title;
33 /***
34 * This page's title image.
35 */
36 final Image titleImage;
37 /***
38 * The wizard to which this page belongs; <code>null</code> if this page
39 * has yet to be added to a wizard.
40 */
41 private IWizardPageContext context = null;
42 /***
43 * Current validity messages.
44 */
45 private ValidityMessages msgs = null;
46 /***
47 * Determines by current validity messages if this page contains error or
48 * not. If page does not contain errors then it may be flipped to next page.
49 * A page can be always flipped to a previous page.
50 * @return <code>true</code> if the page is satisfied with its data.
51 */
52 public final boolean hasErrors() {
53 return ValidityMessages.isError(msgs);
54 }
55 /***
56 * Provides default image.
57 */
58 private final static Wizard WIZARD = new Wizard() {
59
60
61
62
63 @Override
64 public boolean performFinish() {
65 return true;
66 }
67 };
68 /***
69 * Creates a new wizard page with the given name, title, and image.
70 * @param pageName the name of the page
71 * @param title the title for this wizard page, or <code>null</code> if
72 * none. It should be a command, something like 'Please configure the foo
73 * properties'.
74 * @param titleImage the image descriptor for the title of this wizard page,
75 * or <code>null</code> if none.
76 */
77 protected BaseWizardPage(String pageName, String title, Image titleImage) {
78 this.name = pageName;
79 this.title = title;
80 this.titleImage = (titleImage == null ? WIZARD.getDefaultPageImage()
81 : titleImage);
82 msgs = new ValidityMessages();
83 msgs.info = title;
84 }
85 /***
86 * Updates messages on this page. The <code>info</code> part is ignored
87 * and always rewritten by the <code>title</code>.
88 * @param messages validity messages. If <code>null</code> then all
89 * messages are cleared.
90 */
91 protected final void setMessages(ValidityMessages messages) {
92 if (ValidityMessages.equalsTopLevelMsg(msgs, messages))
93 return;
94 msgs = new ValidityMessages(messages);
95 msgs.info = title;
96 if (context != null)
97 context.updateMessages();
98 }
99 /***
100 * Returns current validity messages.
101 * @return current validity messages.
102 */
103 final ValidityMessages getMessages() {
104 return new ValidityMessages(msgs);
105 }
106 /***
107 * Sets context to the page.
108 * @param context new context. Replaces previous one. Must not be
109 * <code>null</code>.
110 */
111 final void setContext(IWizardPageContext context) {
112 if (context == null)
113 throw new IllegalArgumentException();
114 this.context = context;
115 context.updateMessages();
116 }
117 /***
118 * Returns the context instance.
119 * @return the context instance.
120 */
121 public final IWizardPageContext getContext() {
122 return context;
123 }
124
125
126
127
128 @Override
129 public String toString() {
130 return name;
131 }
132 /***
133 * <p>
134 * Creates the top level control for this dialog page under the given parent
135 * composite.
136 * </p>
137 * <p>
138 * This method is called only once.
139 * </p>
140 * @param parent the parent composite
141 */
142 final void createControl(Composite parent) {
143 if (control != null)
144 throw new IllegalStateException("Control is already created.");
145 this.control = handleCreateControl(parent);
146 if (control == null)
147 throw new IllegalStateException("Control is null");
148 this.control.setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true));
149 }
150 /***
151 * Returns the top-level control.
152 * @return control where all page controls are placed.
153 */
154 final Control getControl() {
155 return control;
156 }
157 /***
158 * The top-level control.
159 */
160 private Control control;
161 /***
162 * <p>
163 * Creates the top level control for this dialog page under the given parent
164 * composite.
165 * </p>
166 * <p>
167 * This method is called only once.
168 * </p>
169 * @param parent the parent composite
170 * @return the control.
171 */
172 protected abstract Control handleCreateControl(Composite parent);
173 /***
174 * Disposes the resources and controls allocated by this dialog page.
175 */
176 final void dispose() {
177 handleDispose();
178 control.dispose();
179 }
180 /***
181 * Disposes the resources allocated by this dialog page. The control is
182 * disposed automatically when this method ends.
183 */
184 protected abstract void handleDispose();
185 }