]> git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/cms/ui/dialogs/CmsMessageDialog.java
Prepare next development cycle
[lgpl/argeo-commons.git] / argeo / cms / ui / 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 createInputArea(body);
49
50 // buttons
51 Composite buttons = new Composite(parent, SWT.NONE);
52 buttons.setLayoutData(new GridData(SWT.END, SWT.FILL, true, false));
53 if (kind == INFORMATION || kind == WARNING) {
54 GridLayout layout = new GridLayout(1, true);
55 layout.marginWidth = 0;
56 layout.marginHeight = 0;
57 buttons.setLayout(layout);
58
59 Button close = new Button(buttons, SWT.FLAT);
60 close.setText(CmsMsg.close.lead());
61 close.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
62 close.addSelectionListener((Selected) (e) -> closeShell(OK));
63 } else if (kind == CONFIRM || kind == QUESTION) {
64 GridLayout layout = new GridLayout(2, true);
65 layout.marginWidth = 0;
66 layout.marginHeight = 0;
67 buttons.setLayout(layout);
68
69 Button cancel = new Button(buttons, SWT.FLAT);
70 cancel.setText(CmsMsg.cancel.lead());
71 cancel.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
72 cancel.addSelectionListener((Selected) (e) -> cancelPressed());
73
74 Button ok = new Button(buttons, SWT.FLAT);
75 ok.setText(CmsMsg.ok.lead());
76 ok.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false));
77 ok.addSelectionListener((Selected) (e) -> okPressed());
78 }
79 // pack();
80 return body;
81 }
82
83 protected Control createInputArea(Composite parent) {
84 return null;
85 }
86
87 protected void okPressed() {
88 closeShell(OK);
89 }
90
91 protected void cancelPressed() {
92 closeShell(CANCEL);
93 }
94
95 protected Point getInitialSize() {
96 return new Point(400, 200);
97 }
98
99 public static boolean open(int kind, Shell parent, String message) {
100 CmsMessageDialog dialog = new CmsMessageDialog(parent, message, kind);
101 return dialog.open() == 0;
102 }
103
104 public static boolean openConfirm(String message) {
105 return open(CONFIRM, Display.getCurrent().getActiveShell(), message);
106 }
107
108 public static void openInformation(String message) {
109 open(INFORMATION, Display.getCurrent().getActiveShell(), message);
110 }
111
112 public static boolean openQuestion(String message) {
113 return open(QUESTION, Display.getCurrent().getActiveShell(), message);
114 }
115
116 public static void openWarning(String message) {
117 open(WARNING, Display.getCurrent().getActiveShell(), message);
118 }
119
120 }