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 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 Label messageLbl
= new Label(body
, SWT
.WRAP
);
64 CmsUiUtils
.markup(messageLbl
);
65 messageLbl
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
66 messageLbl
.setFont(EclipseUiUtils
.getBoldFont(parent
));
68 messageLbl
.setText(message
);
71 Composite buttons
= new Composite(parent
, SWT
.NONE
);
72 buttons
.addTraverseListener(traverseListener
);
73 buttons
.setLayoutData(new GridData(SWT
.END
, SWT
.FILL
, true, false));
74 if (kind
== INFORMATION
|| kind
== WARNING
|| kind
== ERROR
|| kind
== ERROR
) {
75 GridLayout layout
= new GridLayout(1, true);
76 layout
.marginWidth
= 0;
77 layout
.marginHeight
= 0;
78 buttons
.setLayout(layout
);
80 Button close
= new Button(buttons
, SWT
.FLAT
);
81 close
.setText(CmsMsg
.close
.lead());
82 close
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
83 close
.addSelectionListener((Selected
) (e
) -> closeShell(OK
));
85 close
.addTraverseListener(traverseListener
);
87 buttons
.setTabList(new Control
[] { close
});
88 } else if (kind
== CONFIRM
|| kind
== QUESTION
|| kind
== QUESTION_WITH_CANCEL
) {
89 Control input
= createInputArea(body
);
91 input
.addTraverseListener(traverseListener
);
92 body
.setTabList(new Control
[] { input
});
94 GridLayout layout
= new GridLayout(2, true);
95 layout
.marginWidth
= 0;
96 layout
.marginHeight
= 0;
97 buttons
.setLayout(layout
);
99 Button cancel
= new Button(buttons
, SWT
.FLAT
);
100 cancel
.setText(CmsMsg
.cancel
.lead());
101 cancel
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
102 cancel
.addSelectionListener((Selected
) (e
) -> cancelPressed());
103 cancel
.addTraverseListener(traverseListener
);
105 Button ok
= new Button(buttons
, SWT
.FLAT
);
106 ok
.setText(CmsMsg
.ok
.lead());
107 ok
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, false, false));
108 ok
.addSelectionListener((Selected
) (e
) -> okPressed());
109 ok
.addTraverseListener(traverseListener
);
115 buttons
.setTabList(new Control
[] { ok
, cancel
});
118 parent
.setTabList(new Control
[] { body
, buttons
});
122 protected Control
createInputArea(Composite parent
) {
126 protected void okPressed() {
130 protected void cancelPressed() {
134 protected Point
getInitialSize() {
135 return new Point(400, 200);
138 public static boolean open(int kind
, Shell parent
, String message
) {
139 CmsMessageDialog dialog
= new CmsMessageDialog(parent
, message
, kind
);
140 return dialog
.open() == 0;
143 public static boolean openConfirm(String message
) {
144 return open(CONFIRM
, Display
.getCurrent().getActiveShell(), message
);
147 public static void openInformation(String message
) {
148 open(INFORMATION
, Display
.getCurrent().getActiveShell(), message
);
151 public static boolean openQuestion(String message
) {
152 return open(QUESTION
, Display
.getCurrent().getActiveShell(), message
);
155 public static void openWarning(String message
) {
156 open(WARNING
, Display
.getCurrent().getActiveShell(), message
);
159 public static void openError(String message
) {
160 open(ERROR
, Display
.getCurrent().getActiveShell(), message
);