]> git.argeo.org Git - lgpl/argeo-commons.git/blob - swt/org.argeo.cms.swt/src/org/argeo/cms/swt/dialogs/CmsMessageDialog.java
Start improving single-user login
[lgpl/argeo-commons.git] / swt / org.argeo.cms.swt / src / org / argeo / cms / swt / dialogs / CmsMessageDialog.java
1 package org.argeo.cms.swt.dialogs;
2
3 import org.argeo.cms.CmsMsg;
4 import org.argeo.cms.swt.CmsSwtUtils;
5 import org.argeo.cms.swt.Selected;
6 import org.argeo.cms.ux.widgets.CmsDialog;
7 import org.argeo.eclipse.ui.EclipseUiUtils;
8 import org.eclipse.swt.SWT;
9 import org.eclipse.swt.events.TraverseEvent;
10 import org.eclipse.swt.events.TraverseListener;
11 import org.eclipse.swt.graphics.Point;
12 import org.eclipse.swt.layout.GridData;
13 import org.eclipse.swt.layout.GridLayout;
14 import org.eclipse.swt.widgets.Button;
15 import org.eclipse.swt.widgets.Composite;
16 import org.eclipse.swt.widgets.Control;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Label;
19 import org.eclipse.swt.widgets.Shell;
20
21 /** Base class for dialogs displaying messages or small forms. */
22 public class CmsMessageDialog extends LightweightDialog {
23 public final static int NONE = 0;
24 public final static int ERROR = 1;
25 public final static int INFORMATION = 2;
26 public final static int QUESTION = 3;
27 public final static int WARNING = 4;
28 public final static int CONFIRM = 5;
29 public final static int QUESTION_WITH_CANCEL = 6;
30
31 private int kind;
32 private String message;
33
34 public CmsMessageDialog(Shell parentShell, String message, int kind) {
35 super(parentShell);
36 this.kind = kind;
37 this.message = message;
38 }
39
40 protected Control createDialogArea(Composite parent) {
41 parent.setLayout(new GridLayout());
42
43 TraverseListener traverseListener = new TraverseListener() {
44 private static final long serialVersionUID = -1158892811534971856L;
45
46 public void keyTraversed(TraverseEvent e) {
47 if (e.detail == SWT.TRAVERSE_RETURN)
48 okPressed();
49 else if (e.detail == SWT.TRAVERSE_ESCAPE)
50 cancelPressed();
51 }
52 };
53
54 // message
55 Composite body = new Composite(parent, SWT.NONE);
56 body.addTraverseListener(traverseListener);
57 GridLayout bodyGridLayout = new GridLayout();
58 bodyGridLayout.marginHeight = 20;
59 bodyGridLayout.marginWidth = 20;
60 body.setLayout(bodyGridLayout);
61 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
62
63 if (message != null) {
64 Label messageLbl = new Label(body, SWT.WRAP);
65 CmsSwtUtils.markup(messageLbl);
66 messageLbl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
67 messageLbl.setFont(EclipseUiUtils.getBoldFont(parent));
68 messageLbl.setText(message);
69 }
70
71 // buttons
72 Composite buttons = new Composite(parent, SWT.NONE);
73 buttons.addTraverseListener(traverseListener);
74 buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
75 if (kind == INFORMATION || kind == WARNING || kind == ERROR || kind == ERROR) {
76 GridLayout layout = new GridLayout(1, true);
77 layout.marginWidth = 0;
78 layout.marginHeight = 0;
79 buttons.setLayout(layout);
80
81 Button close = new Button(buttons, SWT.FLAT);
82 close.setText(CmsMsg.close.lead());
83 close.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
84 close.addSelectionListener((Selected) (e) -> closeShell(CmsDialog.OK));
85 close.setFocus();
86 close.addTraverseListener(traverseListener);
87
88 buttons.setTabList(new Control[] { close });
89 } else if (kind == CONFIRM || kind == QUESTION || kind == QUESTION_WITH_CANCEL) {
90 Control input = createInputArea(body);
91 if (input != null) {
92 input.addTraverseListener(traverseListener);
93 body.setTabList(new Control[] { input });
94 }
95 GridLayout layout = new GridLayout(2, true);
96 layout.marginWidth = 0;
97 layout.marginHeight = 0;
98 buttons.setLayout(layout);
99
100 Button cancel = new Button(buttons, SWT.FLAT);
101 cancel.setText(CmsMsg.cancel.lead());
102 cancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
103 cancel.addSelectionListener((Selected) (e) -> cancelPressed());
104 cancel.addTraverseListener(traverseListener);
105
106 Button ok = new Button(buttons, SWT.FLAT);
107 ok.setText(CmsMsg.ok.lead());
108 ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
109 ok.addSelectionListener((Selected) (e) -> okPressed());
110 ok.addTraverseListener(traverseListener);
111 if (input == null)
112 ok.setFocus();
113 else
114 input.setFocus();
115
116 buttons.setTabList(new Control[] { ok, cancel });
117 }
118 // pack();
119 parent.setTabList(new Control[] { body, buttons });
120 return body;
121 }
122
123 protected Control createInputArea(Composite parent) {
124 return null;
125 }
126
127 protected void okPressed() {
128 closeShell(CmsDialog.OK);
129 }
130
131 protected void cancelPressed() {
132 closeShell(CmsDialog.CANCEL);
133 }
134
135 protected void cancel() {
136 closeShell(CmsDialog.CANCEL);
137 }
138
139 protected Point getInitialSize() {
140 return new Point(400, 200);
141 }
142
143 public static boolean open(int kind, Shell parent, String message) {
144 CmsMessageDialog dialog = new CmsMessageDialog(parent, message, kind);
145 return dialog.open() == 0;
146 }
147
148 public static boolean openConfirm(String message) {
149 return open(CONFIRM, Display.getCurrent().getActiveShell(), message);
150 }
151
152 public static void openInformation(String message) {
153 open(INFORMATION, Display.getCurrent().getActiveShell(), message);
154 }
155
156 public static boolean openQuestion(String message) {
157 return open(QUESTION, Display.getCurrent().getActiveShell(), message);
158 }
159
160 public static void openWarning(String message) {
161 open(WARNING, Display.getCurrent().getActiveShell(), message);
162 }
163
164 public static void openError(String message) {
165 open(ERROR, Display.getCurrent().getActiveShell(), message);
166 }
167
168 }