1 package org
.argeo
.cms
.ui
.dialogs
;
3 import org
.argeo
.cms
.CmsMsg
;
4 import org
.argeo
.cms
.util
.CmsUtils
;
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
;
21 public class CmsMessageDialog
extends LightweightDialog
{
22 public final static int INFORMATION
= 2;
23 public final static int QUESTION
= 3;
24 public final static int WARNING
= 4;
25 public final static int CONFIRM
= 5;
28 private String message
;
30 public CmsMessageDialog(Shell parentShell
, String message
, int kind
) {
33 this.message
= message
;
36 protected Control
createDialogArea(Composite parent
) {
37 parent
.setLayout(new GridLayout());
39 TraverseListener traverseListener
= new TraverseListener() {
40 private static final long serialVersionUID
= -1158892811534971856L;
42 public void keyTraversed(TraverseEvent e
) {
43 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
45 else if (e
.detail
== SWT
.TRAVERSE_ESCAPE
)
51 Composite body
= new Composite(parent
, SWT
.NONE
);
52 body
.addTraverseListener(traverseListener
);
53 GridLayout bodyGridLayout
= new GridLayout();
54 bodyGridLayout
.marginHeight
= 20;
55 bodyGridLayout
.marginWidth
= 20;
56 body
.setLayout(bodyGridLayout
);
57 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
59 Label messageLbl
= new Label(body
, SWT
.WRAP
);
60 CmsUtils
.markup(messageLbl
);
61 messageLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
62 messageLbl
.setFont(EclipseUiUtils
.getBoldFont(parent
));
64 messageLbl
.setText(message
);
67 Composite buttons
= new Composite(parent
, SWT
.NONE
);
68 buttons
.addTraverseListener(traverseListener
);
69 buttons
.setLayoutData(new GridData(SWT
.END
, SWT
.FILL
, true, false));
70 if (kind
== INFORMATION
|| kind
== WARNING
) {
71 GridLayout layout
= new GridLayout(1, true);
72 layout
.marginWidth
= 0;
73 layout
.marginHeight
= 0;
74 buttons
.setLayout(layout
);
76 Button close
= new Button(buttons
, SWT
.FLAT
);
77 close
.setText(CmsMsg
.close
.lead());
78 close
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
79 close
.addSelectionListener((Selected
) (e
) -> closeShell(OK
));
81 close
.addTraverseListener(traverseListener
);
83 buttons
.setTabList(new Control
[] { close
});
84 } else if (kind
== CONFIRM
|| kind
== QUESTION
) {
85 Control input
= createInputArea(body
);
87 input
.addTraverseListener(traverseListener
);
88 body
.setTabList(new Control
[] { input
});
90 GridLayout layout
= new GridLayout(2, true);
91 layout
.marginWidth
= 0;
92 layout
.marginHeight
= 0;
93 buttons
.setLayout(layout
);
95 Button cancel
= new Button(buttons
, SWT
.FLAT
);
96 cancel
.setText(CmsMsg
.cancel
.lead());
97 cancel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
98 cancel
.addSelectionListener((Selected
) (e
) -> cancelPressed());
99 cancel
.addTraverseListener(traverseListener
);
101 Button ok
= new Button(buttons
, SWT
.FLAT
);
102 ok
.setText(CmsMsg
.ok
.lead());
103 ok
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
104 ok
.addSelectionListener((Selected
) (e
) -> okPressed());
105 ok
.addTraverseListener(traverseListener
);
111 buttons
.setTabList(new Control
[] { ok
, cancel
});
114 parent
.setTabList(new Control
[] { body
, buttons
});
118 protected Control
createInputArea(Composite parent
) {
122 protected void okPressed() {
126 protected void cancelPressed() {
130 protected Point
getInitialSize() {
131 return new Point(400, 200);
134 public static boolean open(int kind
, Shell parent
, String message
) {
135 CmsMessageDialog dialog
= new CmsMessageDialog(parent
, message
, kind
);
136 return dialog
.open() == 0;
139 public static boolean openConfirm(String message
) {
140 return open(CONFIRM
, Display
.getCurrent().getActiveShell(), message
);
143 public static void openInformation(String message
) {
144 open(INFORMATION
, Display
.getCurrent().getActiveShell(), message
);
147 public static boolean openQuestion(String message
) {
148 return open(QUESTION
, Display
.getCurrent().getActiveShell(), message
);
151 public static void openWarning(String message
) {
152 open(WARNING
, Display
.getCurrent().getActiveShell(), message
);