1 package org
.argeo
.cms
.jface
.dialog
;
3 import java
.lang
.reflect
.InvocationTargetException
;
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
;
27 /** A wizard dialog based on {@link LightweightDialog}. */
28 public class CmsWizardDialog
extends LightweightDialog
implements IWizardContainer2
{
29 private static final long serialVersionUID
= -2123153353654812154L;
31 private IWizard wizard
;
32 private IWizardPage currentPage
;
33 private int currentPageIndex
;
35 private Label titleBar
;
36 private Label message
;
37 private Composite
[] pageBodies
;
38 private Composite buttons
;
41 private Button finish
;
43 public CmsWizardDialog(Shell parentShell
, IWizard wizard
) {
46 wizard
.setContainer(this);
49 currentPage
= wizard
.getStartingPage();
50 if (currentPage
== null)
51 throw new IllegalArgumentException("At least one wizard page is required");
55 protected Control
createDialogArea(Composite parent
) {
58 Composite messageArea
= new Composite(parent
, SWT
.NONE
);
59 messageArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
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));
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));
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
]);
86 showPage(currentPage
);
88 buttons
= new Composite(parent
, SWT
.NONE
);
89 buttons
.setLayoutData(new GridData(SWT
.END
, SWT
.FILL
, true, false));
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
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());
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());
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());
121 public IWizardPage
getCurrentPage() {
126 public Shell
getShell() {
127 return getForegoundShell();
131 public void showPage(IWizardPage page
) {
132 IWizardPage
[] pages
= wizard
.getPages();
134 for (int i
= 0; i
< pages
.length
; i
++) {
135 if (page
== pages
[i
]) {
141 throw new IllegalArgumentException("Cannot find index of wizard page " + page
);
142 pageBodies
[index
].moveAbove(pageBodies
[currentPageIndex
]);
145 // for (Control c : body.getChildren())
147 // page.createControl(body);
148 // body.layout(true, true);
149 currentPageIndex
= index
;
154 public void updateButtons() {
156 back
.setEnabled(wizard
.getPreviousPage(currentPage
) != null);
158 next
.setEnabled(wizard
.getNextPage(currentPage
) != null && currentPage
.canFlipToNextPage());
159 if (finish
!= null) {
160 finish
.setEnabled(wizard
.canFinish());
165 public void updateMessage() {
166 if (currentPage
.getMessage() != null)
167 message
.setText(currentPage
.getMessage());
171 public void updateTitleBar() {
172 if (currentPage
.getTitle() != null)
173 titleBar
.setText(currentPage
.getTitle());
177 public void updateWindowTitle() {
178 setTitle(wizard
.getWindowTitle());
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);
189 public void updateSize() {
193 protected boolean onCancel() {
194 return wizard
.performCancel();
197 protected void nextPressed() {
198 IWizardPage page
= wizard
.getNextPage(currentPage
);
203 protected void backPressed() {
204 IWizardPage page
= wizard
.getPreviousPage(currentPage
);
209 protected void finishPressed() {
210 if (wizard
.performFinish())
211 closeShell(CmsDialog
.OK
);
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
);