1 package org
.argeo
.cms
.ui
.dialogs
;
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
;
21 /** Base class for dialogs displaying messages or small forms. */
22 public class CmsMessageDialog
extends LightweightDialog
{
23 public final static int INFORMATION
= 2;
24 public final static int QUESTION
= 3;
25 public final static int WARNING
= 4;
26 public final static int CONFIRM
= 5;
29 private String message
;
31 public CmsMessageDialog(Shell parentShell
, String message
, int kind
) {
34 this.message
= message
;
37 protected Control
createDialogArea(Composite parent
) {
38 parent
.setLayout(new GridLayout());
40 TraverseListener traverseListener
= new TraverseListener() {
41 private static final long serialVersionUID
= -1158892811534971856L;
43 public void keyTraversed(TraverseEvent e
) {
44 if (e
.detail
== SWT
.TRAVERSE_RETURN
)
46 else if (e
.detail
== SWT
.TRAVERSE_ESCAPE
)
52 Composite body
= new Composite(parent
, SWT
.NONE
);
53 body
.addTraverseListener(traverseListener
);
54 GridLayout bodyGridLayout
= new GridLayout();
55 bodyGridLayout
.marginHeight
= 20;
56 bodyGridLayout
.marginWidth
= 20;
57 body
.setLayout(bodyGridLayout
);
58 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
60 Label messageLbl
= new Label(body
, SWT
.WRAP
);
61 CmsUiUtils
.markup(messageLbl
);
62 messageLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
63 messageLbl
.setFont(EclipseUiUtils
.getBoldFont(parent
));
65 messageLbl
.setText(message
);
68 Composite buttons
= new Composite(parent
, SWT
.NONE
);
69 buttons
.addTraverseListener(traverseListener
);
70 buttons
.setLayoutData(new GridData(SWT
.END
, SWT
.FILL
, true, false));
71 if (kind
== INFORMATION
|| kind
== WARNING
) {
72 GridLayout layout
= new GridLayout(1, true);
73 layout
.marginWidth
= 0;
74 layout
.marginHeight
= 0;
75 buttons
.setLayout(layout
);
77 Button close
= new Button(buttons
, SWT
.FLAT
);
78 close
.setText(CmsMsg
.close
.lead());
79 close
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
80 close
.addSelectionListener((Selected
) (e
) -> closeShell(OK
));
82 close
.addTraverseListener(traverseListener
);
84 buttons
.setTabList(new Control
[] { close
});
85 } else if (kind
== CONFIRM
|| kind
== QUESTION
) {
86 Control input
= createInputArea(body
);
88 input
.addTraverseListener(traverseListener
);
89 body
.setTabList(new Control
[] { input
});
91 GridLayout layout
= new GridLayout(2, true);
92 layout
.marginWidth
= 0;
93 layout
.marginHeight
= 0;
94 buttons
.setLayout(layout
);
96 Button cancel
= new Button(buttons
, SWT
.FLAT
);
97 cancel
.setText(CmsMsg
.cancel
.lead());
98 cancel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
99 cancel
.addSelectionListener((Selected
) (e
) -> cancelPressed());
100 cancel
.addTraverseListener(traverseListener
);
102 Button ok
= new Button(buttons
, SWT
.FLAT
);
103 ok
.setText(CmsMsg
.ok
.lead());
104 ok
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
105 ok
.addSelectionListener((Selected
) (e
) -> okPressed());
106 ok
.addTraverseListener(traverseListener
);
112 buttons
.setTabList(new Control
[] { ok
, cancel
});
115 parent
.setTabList(new Control
[] { body
, buttons
});
119 protected Control
createInputArea(Composite parent
) {
123 protected void okPressed() {
127 protected void cancelPressed() {
131 protected Point
getInitialSize() {
132 return new Point(400, 200);
135 public static boolean open(int kind
, Shell parent
, String message
) {
136 CmsMessageDialog dialog
= new CmsMessageDialog(parent
, message
, kind
);
137 return dialog
.open() == 0;
140 public static boolean openConfirm(String message
) {
141 return open(CONFIRM
, Display
.getCurrent().getActiveShell(), message
);
144 public static void openInformation(String message
) {
145 open(INFORMATION
, Display
.getCurrent().getActiveShell(), message
);
148 public static boolean openQuestion(String message
) {
149 return open(QUESTION
, Display
.getCurrent().getActiveShell(), message
);
152 public static void openWarning(String message
) {
153 open(WARNING
, Display
.getCurrent().getActiveShell(), message
);