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