]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/CmsMessageDialog.java
Improve CMS App.
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / dialogs / CmsMessageDialog.java
1 package org.argeo.cms.ui.dialogs;
2
3 import org.argeo.cms.CmsMsg;
4 import org.argeo.cms.ui.util.CmsUiUtils;
5 import org.argeo.eclipse.ui.EclipseUiUtils;
6 import org.argeo.eclipse.ui.Selected;
7 import org.argeo.eclipse.ui.dialogs.LightweightDialog;
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 Label messageLbl = new Label(body, SWT.WRAP);
64 CmsUiUtils.markup(messageLbl);
65 messageLbl.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
66 messageLbl.setFont(EclipseUiUtils.getBoldFont(parent));
67 if (message != null)
68 messageLbl.setText(message);
69
70 // buttons
71 Composite buttons = new Composite(parent, SWT.NONE);
72 buttons.addTraverseListener(traverseListener);
73 buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
74 if (kind == INFORMATION || kind == WARNING || kind == ERROR || kind == ERROR) {
75 GridLayout layout = new GridLayout(1, true);
76 layout.marginWidth = 0;
77 layout.marginHeight = 0;
78 buttons.setLayout(layout);
79
80 Button close = new Button(buttons, SWT.FLAT);
81 close.setText(CmsMsg.close.lead());
82 close.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
83 close.addSelectionListener((Selected) (e) -> closeShell(OK));
84 close.setFocus();
85 close.addTraverseListener(traverseListener);
86
87 buttons.setTabList(new Control[] { close });
88 } else if (kind == CONFIRM || kind == QUESTION || kind == QUESTION_WITH_CANCEL) {
89 Control input = createInputArea(body);
90 if (input != null) {
91 input.addTraverseListener(traverseListener);
92 body.setTabList(new Control[] { input });
93 }
94 GridLayout layout = new GridLayout(2, true);
95 layout.marginWidth = 0;
96 layout.marginHeight = 0;
97 buttons.setLayout(layout);
98
99 Button cancel = new Button(buttons, SWT.FLAT);
100 cancel.setText(CmsMsg.cancel.lead());
101 cancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
102 cancel.addSelectionListener((Selected) (e) -> cancelPressed());
103 cancel.addTraverseListener(traverseListener);
104
105 Button ok = new Button(buttons, SWT.FLAT);
106 ok.setText(CmsMsg.ok.lead());
107 ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
108 ok.addSelectionListener((Selected) (e) -> okPressed());
109 ok.addTraverseListener(traverseListener);
110 if (input == null)
111 ok.setFocus();
112 else
113 input.setFocus();
114
115 buttons.setTabList(new Control[] { ok, cancel });
116 }
117 // pack();
118 parent.setTabList(new Control[] { body, buttons });
119 return body;
120 }
121
122 protected Control createInputArea(Composite parent) {
123 return null;
124 }
125
126 protected void okPressed() {
127 closeShell(OK);
128 }
129
130 protected void cancelPressed() {
131 closeShell(CANCEL);
132 }
133
134 protected Point getInitialSize() {
135 return new Point(400, 200);
136 }
137
138 public static boolean open(int kind, Shell parent, String message) {
139 CmsMessageDialog dialog = new CmsMessageDialog(parent, message, kind);
140 return dialog.open() == 0;
141 }
142
143 public static boolean openConfirm(String message) {
144 return open(CONFIRM, Display.getCurrent().getActiveShell(), message);
145 }
146
147 public static void openInformation(String message) {
148 open(INFORMATION, Display.getCurrent().getActiveShell(), message);
149 }
150
151 public static boolean openQuestion(String message) {
152 return open(QUESTION, Display.getCurrent().getActiveShell(), message);
153 }
154
155 public static void openWarning(String message) {
156 open(WARNING, Display.getCurrent().getActiveShell(), message);
157 }
158
159 public static void openError(String message) {
160 open(ERROR, Display.getCurrent().getActiveShell(), message);
161 }
162
163 }