1 package org
.argeo
.cms
.swt
.dialogs
;
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
;
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;
32 private String message
;
34 public CmsMessageDialog(Shell parentShell
, String message
, int kind
) {
37 this.message
= message
;
40 protected Control
createDialogArea(Composite parent
) {
41 parent
.setLayout(new GridLayout());
43 TraverseListener traverseListener
= new TraverseListener() {
44 private static final long serialVersionUID
= -1158892811534971856L;
46 public void keyTraversed(TraverseEvent e
) {
47 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
49 else if (e
.detail
== SWT
.TRAVERSE_ESCAPE
)
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));
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
);
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
);
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
));
86 close
.addTraverseListener(traverseListener
);
88 buttons
.setTabList(new Control
[] { close
});
89 } else if (kind
== CONFIRM
|| kind
== QUESTION
|| kind
== QUESTION_WITH_CANCEL
) {
90 Control input
= createInputArea(body
);
92 input
.addTraverseListener(traverseListener
);
93 body
.setTabList(new Control
[] { input
});
95 GridLayout layout
= new GridLayout(2, true);
96 layout
.marginWidth
= 0;
97 layout
.marginHeight
= 0;
98 buttons
.setLayout(layout
);
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
);
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
);
116 buttons
.setTabList(new Control
[] { ok
, cancel
});
119 parent
.setTabList(new Control
[] { body
, buttons
});
123 protected Control
createInputArea(Composite parent
) {
127 protected void okPressed() {
128 closeShell(CmsDialog
.OK
);
131 protected void cancelPressed() {
132 closeShell(CmsDialog
.CANCEL
);
135 protected void cancel() {
136 closeShell(CmsDialog
.CANCEL
);
139 protected Point
getInitialSize() {
140 return new Point(400, 200);
143 public static boolean open(int kind
, Shell parent
, String message
) {
144 CmsMessageDialog dialog
= new CmsMessageDialog(parent
, message
, kind
);
145 return dialog
.open() == 0;
148 public static boolean openConfirm(String message
) {
149 return open(CONFIRM
, Display
.getCurrent().getActiveShell(), message
);
152 public static void openInformation(String message
) {
153 open(INFORMATION
, Display
.getCurrent().getActiveShell(), message
);
156 public static boolean openQuestion(String message
) {
157 return open(QUESTION
, Display
.getCurrent().getActiveShell(), message
);
160 public static void openWarning(String message
) {
161 open(WARNING
, Display
.getCurrent().getActiveShell(), message
);
164 public static void openError(String message
) {
165 open(ERROR
, Display
.getCurrent().getActiveShell(), message
);