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
.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
;
26 /** A wizard dialog based on {@link LightweightDialog}. */
27 public class CmsWizardDialog
extends LightweightDialog
implements IWizardContainer2
{
28 private static final long serialVersionUID
= -2123153353654812154L;
30 private IWizard wizard
;
31 private IWizardPage currentPage
;
32 private int currentPageIndex
;
34 private Label titleBar
;
35 private Label message
;
36 private Composite
[] pageBodies
;
37 private Composite buttons
;
40 private Button finish
;
42 public CmsWizardDialog(Shell parentShell
, IWizard wizard
) {
45 wizard
.setContainer(this);
48 currentPage
= wizard
.getStartingPage();
49 if (currentPage
== null)
50 throw new IllegalArgumentException("At least one wizard page is required");
54 protected Control
createDialogArea(Composite parent
) {
57 Composite messageArea
= new Composite(parent
, SWT
.NONE
);
58 messageArea
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
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));
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));
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
]);
85 showPage(currentPage
);
87 buttons
= new Composite(parent
, SWT
.NONE
);
88 buttons
.setLayoutData(new GridData(SWT
.END
, SWT
.FILL
, true, false));
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
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());
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());
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());
120 public IWizardPage
getCurrentPage() {
125 public Shell
getShell() {
126 return getForegoundShell();
130 public void showPage(IWizardPage page
) {
131 IWizardPage
[] pages
= wizard
.getPages();
133 for (int i
= 0; i
< pages
.length
; i
++) {
134 if (page
== pages
[i
]) {
140 throw new IllegalArgumentException("Cannot find index of wizard page " + page
);
141 pageBodies
[index
].moveAbove(pageBodies
[currentPageIndex
]);
144 // for (Control c : body.getChildren())
146 // page.createControl(body);
147 // body.layout(true, true);
148 currentPageIndex
= index
;
153 public void updateButtons() {
155 back
.setEnabled(wizard
.getPreviousPage(currentPage
) != null);
157 next
.setEnabled(wizard
.getNextPage(currentPage
) != null && currentPage
.canFlipToNextPage());
158 if (finish
!= null) {
159 finish
.setEnabled(wizard
.canFinish());
164 public void updateMessage() {
165 if (currentPage
.getMessage() != null)
166 message
.setText(currentPage
.getMessage());
170 public void updateTitleBar() {
171 if (currentPage
.getTitle() != null)
172 titleBar
.setText(currentPage
.getTitle());
176 public void updateWindowTitle() {
177 setTitle(wizard
.getWindowTitle());
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);
188 public void updateSize() {
192 protected boolean onCancel() {
193 return wizard
.performCancel();
196 protected void nextPressed() {
197 IWizardPage page
= wizard
.getNextPage(currentPage
);
202 protected void backPressed() {
203 IWizardPage page
= wizard
.getPreviousPage(currentPage
);
208 protected void finishPressed() {
209 if (wizard
.performFinish())
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
);