]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/CmsWizardDialog.java
Keyring working for E4
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / dialogs / CmsWizardDialog.java
1 package org.argeo.cms.ui.dialogs;
2
3 import java.lang.reflect.InvocationTargetException;
4
5 import org.argeo.cms.CmsException;
6 import org.argeo.cms.util.CmsUtils;
7 import org.argeo.eclipse.ui.EclipseUiUtils;
8 import org.argeo.eclipse.ui.Selected;
9 import org.argeo.eclipse.ui.dialogs.LightweightDialog;
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.GridData;
16 import org.eclipse.swt.layout.GridLayout;
17 import org.eclipse.swt.widgets.Button;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.swt.widgets.Shell;
22
23 public class CmsWizardDialog extends LightweightDialog implements IWizardContainer2 {
24 private static final long serialVersionUID = -2123153353654812154L;
25
26 private IWizard wizard;
27 private IWizardPage currentPage;
28
29 private Label titleBar;
30 private Label message;
31 private Composite body;
32 private Composite buttons;
33 private Button back;
34 private Button next;
35 private Button finish;
36
37 public CmsWizardDialog(Shell parentShell, IWizard wizard) {
38 super(parentShell);
39 this.wizard = wizard;
40 wizard.setContainer(this);
41 // create the pages
42 wizard.addPages();
43 currentPage = wizard.getStartingPage();
44 if (currentPage == null)
45 throw new CmsException("At least one wizard page is required");
46 }
47
48 @Override
49 protected Control createDialogArea(Composite parent) {
50 updateWindowTitle();
51
52 Composite messageArea = new Composite(parent, SWT.NONE);
53 messageArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
54 {
55 messageArea.setLayout(CmsUtils.noSpaceGridLayout(new GridLayout(2, false)));
56 titleBar = new Label(messageArea, SWT.WRAP);
57 titleBar.setFont(EclipseUiUtils.getBoldFont(parent));
58 titleBar.setLayoutData(new GridData(SWT.BEGINNING, SWT.FILL, true, false));
59 updateTitleBar();
60 Button cancelButton = new Button(messageArea, SWT.FLAT);
61 cancelButton.setText("Cancel");
62 cancelButton.setLayoutData(new GridData(SWT.END, SWT.TOP, false, false, 1, 3));
63 cancelButton.addSelectionListener((Selected) (e) -> closeShell(CANCEL));
64 message = new Label(messageArea, SWT.WRAP);
65 message.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 2));
66 updateMessage();
67 }
68
69 body = new Composite(parent, SWT.BORDER);
70 body.setLayout(CmsUtils.noSpaceGridLayout());
71 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
72 showPage(currentPage);
73
74 buttons = new Composite(parent, SWT.NONE);
75 buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
76 {
77 boolean singlePage = wizard.getPageCount() == 1;
78 // singlePage = false;// dev
79 GridLayout layout = new GridLayout(singlePage ? 1 : 3, true);
80 layout.marginWidth = 0;
81 layout.marginHeight = 0;
82 buttons.setLayout(layout);
83 // TODO revert order for right-to-left languages
84
85 if (!singlePage) {
86 back = new Button(buttons, SWT.PUSH);
87 back.setText("Back");
88 back.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
89 back.addSelectionListener((Selected) (e) -> backPressed());
90
91 next = new Button(buttons, SWT.PUSH);
92 next.setText("Next");
93 next.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
94 next.addSelectionListener((Selected) (e) -> nextPressed());
95 }
96 finish = new Button(buttons, SWT.PUSH);
97 finish.setText("Finish");
98 finish.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
99 finish.addSelectionListener((Selected) (e) -> finishPressed());
100
101 updateButtons();
102 }
103 return body;
104 }
105
106 @Override
107 public IWizardPage getCurrentPage() {
108 return currentPage;
109 }
110
111 @Override
112 public Shell getShell() {
113 return getForegoundShell();
114 }
115
116 @Override
117 public void showPage(IWizardPage page) {
118 // clear
119 for (Control c : body.getChildren())
120 c.dispose();
121 page.createControl(body);
122 currentPage = page;
123 }
124
125 @Override
126 public void updateButtons() {
127 if (back != null)
128 back.setEnabled(wizard.getPreviousPage(currentPage) != null);
129 if (next != null)
130 next.setEnabled(wizard.getNextPage(currentPage) != null && currentPage.canFlipToNextPage());
131 finish.setEnabled(wizard.canFinish());
132 }
133
134 @Override
135 public void updateMessage() {
136 if (currentPage.getMessage() != null)
137 message.setText(currentPage.getMessage());
138 }
139
140 @Override
141 public void updateTitleBar() {
142 if (currentPage.getTitle() != null)
143 titleBar.setText(currentPage.getTitle());
144 }
145
146 @Override
147 public void updateWindowTitle() {
148 setTitle(wizard.getWindowTitle());
149 }
150
151 @Override
152 public void run(boolean fork, boolean cancelable, IRunnableWithProgress runnable)
153 throws InvocationTargetException, InterruptedException {
154 runnable.run(null);
155 }
156
157 @Override
158 public void updateSize() {
159 // TODO pack?
160 }
161
162 protected boolean onCancel() {
163 return wizard.performCancel();
164 }
165
166 protected void nextPressed() {
167 IWizardPage page = wizard.getNextPage(currentPage);
168 showPage(page);
169 }
170
171 protected void backPressed() {
172 IWizardPage page = wizard.getPreviousPage(currentPage);
173 showPage(page);
174 }
175
176 protected void finishPressed() {
177 if (wizard.performFinish())
178 closeShell(OK);
179 }
180 }