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