]> git.argeo.org Git - lgpl/argeo-commons.git/blob - CmsWizardDialog.java
33841a1bb97bfa498e009b1e423dc9e1309338b9
[lgpl/argeo-commons.git] / CmsWizardDialog.java
1 package org.argeo.cms.jface.dialog;
2
3 import java.lang.reflect.InvocationTargetException;
4
5 import org.argeo.cms.CmsMsg;
6 import org.argeo.cms.swt.CmsSwtUtils;
7 import org.argeo.cms.swt.Selected;
8 import org.argeo.cms.swt.dialogs.LightweightDialog;
9 import org.argeo.eclipse.ui.EclipseUiUtils;
10 import org.eclipse.jface.operation.IRunnableWithProgress;
11 import org.eclipse.jface.wizard.IWizard;
12 import org.eclipse.jface.wizard.IWizardContainer2;
13 import org.eclipse.jface.wizard.IWizardPage;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.layout.FormAttachment;
16 import org.eclipse.swt.layout.FormData;
17 import org.eclipse.swt.layout.FormLayout;
18 import org.eclipse.swt.layout.GridData;
19 import org.eclipse.swt.layout.GridLayout;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Control;
23 import org.eclipse.swt.widgets.Label;
24 import org.eclipse.swt.widgets.Shell;
25
26 /** A wizard dialog based on {@link LightweightDialog}. */
27 public class CmsWizardDialog extends LightweightDialog implements IWizardContainer2 {
28 private static final long serialVersionUID = -2123153353654812154L;
29
30 private IWizard wizard;
31 private IWizardPage currentPage;
32 private int currentPageIndex;
33
34 private Label titleBar;
35 private Label message;
36 private Composite[] pageBodies;
37 private Composite buttons;
38 private Button back;
39 private Button next;
40 private Button finish;
41
42 public CmsWizardDialog(Shell parentShell, IWizard wizard) {
43 super(parentShell);
44 this.wizard = wizard;
45 wizard.setContainer(this);
46 // create the pages
47 wizard.addPages();
48 currentPage = wizard.getStartingPage();
49 if (currentPage == null)
50 throw new IllegalArgumentException("At least one wizard page is required");
51 }
52
53 @Override
54 protected Control createDialogArea(Composite parent) {
55 updateWindowTitle();
56
57 Composite messageArea = new Composite(parent, SWT.NONE);
58 messageArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
59 {
60 messageArea.setLayout(CmsSwtUtils.noSpaceGridLayout(new GridLayout(2, false)));
61 titleBar = new Label(messageArea, SWT.WRAP);
62 titleBar.setFont(EclipseUiUtils.getBoldFont(parent));
63 titleBar.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
64 updateTitleBar();
65 Button cancelButton = new Button(messageArea, SWT.FLAT);
66 cancelButton.setText(CmsMsg.cancel.lead());
67 cancelButton.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false, 1, 3));
68 cancelButton.addSelectionListener((Selected) (e) -> closeShell(CANCEL));
69 message = new Label(messageArea, SWT.WRAP);
70 message.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 2));
71 updateMessage();
72 }
73
74 Composite body = new Composite(parent, SWT.BORDER);
75 body.setLayout(new FormLayout());
76 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
77 pageBodies = new Composite[wizard.getPageCount()];
78 IWizardPage[] pages = wizard.getPages();
79 for (int i = 0; i < pages.length; i++) {
80 pageBodies[i] = new Composite(body, SWT.NONE);
81 pageBodies[i].setLayout(CmsSwtUtils.noSpaceGridLayout());
82 setSwitchingFormData(pageBodies[i]);
83 pages[i].createControl(pageBodies[i]);
84 }
85 showPage(currentPage);
86
87 buttons = new Composite(parent, SWT.NONE);
88 buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
89 {
90 boolean singlePage = wizard.getPageCount() == 1;
91 // singlePage = false;// dev
92 GridLayout layout = new GridLayout(singlePage ? 1 : 3, true);
93 layout.marginWidth = 0;
94 layout.marginHeight = 0;
95 buttons.setLayout(layout);
96 // TODO revert order for right-to-left languages
97
98 if (!singlePage) {
99 back = new Button(buttons, SWT.PUSH);
100 back.setText(CmsMsg.wizardBack.lead());
101 back.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
102 back.addSelectionListener((Selected) (e) -> backPressed());
103
104 next = new Button(buttons, SWT.PUSH);
105 next.setText(CmsMsg.wizardNext.lead());
106 next.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
107 next.addSelectionListener((Selected) (e) -> nextPressed());
108 }
109 finish = new Button(buttons, SWT.PUSH);
110 finish.setText(CmsMsg.wizardFinish.lead());
111 finish.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
112 finish.addSelectionListener((Selected) (e) -> finishPressed());
113
114 updateButtons();
115 }
116 return body;
117 }
118
119 @Override
120 public IWizardPage getCurrentPage() {
121 return currentPage;
122 }
123
124 @Override
125 public Shell getShell() {
126 return getForegoundShell();
127 }
128
129 @Override
130 public void showPage(IWizardPage page) {
131 IWizardPage[] pages = wizard.getPages();
132 int index = -1;
133 for (int i = 0; i < pages.length; i++) {
134 if (page == pages[i]) {
135 index = i;
136 break;
137 }
138 }
139 if (index < 0)
140 throw new IllegalArgumentException("Cannot find index of wizard page " + page);
141 pageBodies[index].moveAbove(pageBodies[currentPageIndex]);
142
143 // // clear
144 // for (Control c : body.getChildren())
145 // c.dispose();
146 // page.createControl(body);
147 // body.layout(true, true);
148 currentPageIndex = index;
149 currentPage = page;
150 }
151
152 @Override
153 public void updateButtons() {
154 if (back != null)
155 back.setEnabled(wizard.getPreviousPage(currentPage) != null);
156 if (next != null)
157 next.setEnabled(wizard.getNextPage(currentPage) != null && currentPage.canFlipToNextPage());
158 if (finish != null) {
159 finish.setEnabled(wizard.canFinish());
160 }
161 }
162
163 @Override
164 public void updateMessage() {
165 if (currentPage.getMessage() != null)
166 message.setText(currentPage.getMessage());
167 }
168
169 @Override
170 public void updateTitleBar() {
171 if (currentPage.getTitle() != null)
172 titleBar.setText(currentPage.getTitle());
173 }
174
175 @Override
176 public void updateWindowTitle() {
177 setTitle(wizard.getWindowTitle());
178 }
179
180 @Override
181 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
182 throws InvocationTargetException, InterruptedException {
183 // FIXME it creates a dependency to Eclipse Core Runtime
184 // runnable.run(null);
185 }
186
187 @Override
188 public void updateSize() {
189 // TODO pack?
190 }
191
192 protected boolean onCancel() {
193 return wizard.performCancel();
194 }
195
196 protected void nextPressed() {
197 IWizardPage page = wizard.getNextPage(currentPage);
198 showPage(page);
199 updateButtons();
200 }
201
202 protected void backPressed() {
203 IWizardPage page = wizard.getPreviousPage(currentPage);
204 showPage(page);
205 updateButtons();
206 }
207
208 protected void finishPressed() {
209 if (wizard.performFinish())
210 closeShell(OK);
211 }
212
213 private static void setSwitchingFormData(Composite composite) {
214 FormData fdLabel = new FormData();
215 fdLabel.top = new FormAttachment(0, 0);
216 fdLabel.left = new FormAttachment(0, 0);
217 fdLabel.right = new FormAttachment(100, 0);
218 fdLabel.bottom = new FormAttachment(100, 0);
219 composite.setLayoutData(fdLabel);
220 }
221
222 }