]> git.argeo.org Git - lgpl/argeo-commons.git/blob - dialogs/CmsMessageDialog.java
Prepare next development cycle
[lgpl/argeo-commons.git] / dialogs / CmsMessageDialog.java
1 package org.argeo.cms.ui.dialogs;
2
3 import org.argeo.cms.CmsMsg;
4 import org.argeo.eclipse.ui.EclipseUiUtils;
5 import org.argeo.eclipse.ui.Selected;
6 import org.argeo.eclipse.ui.dialogs.LightweightDialog;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.graphics.Point;
9 import org.eclipse.swt.layout.GridData;
10 import org.eclipse.swt.layout.GridLayout;
11 import org.eclipse.swt.widgets.Button;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.swt.widgets.Control;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.swt.widgets.Label;
16 import org.eclipse.swt.widgets.Shell;
17
18 public class CmsMessageDialog extends LightweightDialog {
19 public final static int INFORMATION = 2;
20 public final static int QUESTION = 3;
21 public final static int WARNING = 4;
22 public final static int CONFIRM = 5;
23
24 private int kind;
25 private String message;
26
27 public CmsMessageDialog(Shell parentShell, String message, int kind) {
28 super(parentShell);
29 this.kind = kind;
30 this.message = message;
31 }
32
33 protected Control createDialogArea(Composite parent) {
34 parent.setLayout(new GridLayout());
35
36 // message
37 Composite body = new Composite(parent, SWT.NONE);
38 GridLayout bodyGridLayout = new GridLayout();
39 bodyGridLayout.marginHeight = 20;
40 bodyGridLayout.marginWidth = 20;
41 body.setLayout(bodyGridLayout);
42 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
43
44 Label messageLbl = new Label(body, SWT.WRAP);
45 messageLbl.setFont(EclipseUiUtils.getBoldFont(parent));
46 if (message != null)
47 messageLbl.setText(message);
48
49 // buttons
50 Composite buttons = new Composite(parent, SWT.NONE);
51 buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
52 if (kind == INFORMATION || kind == WARNING) {
53 GridLayout layout = new GridLayout(1, true);
54 layout.marginWidth = 0;
55 layout.marginHeight = 0;
56 buttons.setLayout(layout);
57
58 Button close = new Button(buttons, SWT.FLAT);
59 close.setText(CmsMsg.close.lead());
60 close.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
61 close.addSelectionListener((Selected) (e) -> closeShell(OK));
62 } else if (kind == CONFIRM || kind == QUESTION) {
63 GridLayout layout = new GridLayout(2, true);
64 layout.marginWidth = 0;
65 layout.marginHeight = 0;
66 buttons.setLayout(layout);
67
68 Button cancel = new Button(buttons, SWT.FLAT);
69 cancel.setText(CmsMsg.cancel.lead());
70 cancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
71 cancel.addSelectionListener((Selected) (e) -> closeShell(CANCEL));
72
73 Button ok = new Button(buttons, SWT.FLAT);
74 ok.setText(CmsMsg.ok.lead());
75 ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
76 ok.addSelectionListener((Selected) (e) -> closeShell(OK));
77 }
78 // pack();
79 return messageLbl;
80 }
81
82 protected Point getInitialSize() {
83 return new Point(400, 200);
84 }
85
86 public static boolean open(int kind, Shell parent, String message) {
87 CmsMessageDialog dialog = new CmsMessageDialog(parent, message, kind);
88 return dialog.open() == 0;
89 }
90
91 public static boolean openConfirm(String message) {
92 return open(CONFIRM, Display.getCurrent().getActiveShell(), message);
93 }
94
95 public static void openInformation(String message) {
96 open(INFORMATION, Display.getCurrent().getActiveShell(), message);
97 }
98
99 public static boolean openQuestion(String message) {
100 return open(QUESTION, Display.getCurrent().getActiveShell(), message);
101 }
102
103 public static void openWarning(String message) {
104 open(WARNING, Display.getCurrent().getActiveShell(), message);
105 }
106
107 }