]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/dialogs/CmsMessageDialog.java
Better distinguish when admin login failed because there was no such JCR
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / ui / dialogs / CmsMessageDialog.java
1 package org.argeo.cms.ui.dialogs;
2
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;
20
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;
26
27 private int kind;
28 private String message;
29
30 public CmsMessageDialog(Shell parentShell, String message, int kind) {
31 super(parentShell);
32 this.kind = kind;
33 this.message = message;
34 }
35
36 protected Control createDialogArea(Composite parent) {
37 parent.setLayout(new GridLayout());
38
39 TraverseListener traverseListener = new TraverseListener() {
40 private static final long serialVersionUID = -1158892811534971856L;
41
42 public void keyTraversed(TraverseEvent e) {
43 if (e.detail == SWT.TRAVERSE_RETURN)
44 okPressed();
45 else if (e.detail == SWT.TRAVERSE_ESCAPE)
46 cancelPressed();
47 }
48 };
49
50 // message
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));
58
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));
63 if (message != null)
64 messageLbl.setText(message);
65
66 // buttons
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);
75
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));
80 close.setFocus();
81 close.addTraverseListener(traverseListener);
82
83 buttons.setTabList(new Control[] { close });
84 } else if (kind == CONFIRM || kind == QUESTION) {
85 Control input = createInputArea(body);
86 if (input != null) {
87 input.addTraverseListener(traverseListener);
88 body.setTabList(new Control[] { input });
89 }
90 GridLayout layout = new GridLayout(2, true);
91 layout.marginWidth = 0;
92 layout.marginHeight = 0;
93 buttons.setLayout(layout);
94
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);
100
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);
106 if (input == null)
107 ok.setFocus();
108 else
109 input.setFocus();
110
111 buttons.setTabList(new Control[] { ok, cancel });
112 }
113 // pack();
114 parent.setTabList(new Control[] { body, buttons });
115 return body;
116 }
117
118 protected Control createInputArea(Composite parent) {
119 return null;
120 }
121
122 protected void okPressed() {
123 closeShell(OK);
124 }
125
126 protected void cancelPressed() {
127 closeShell(CANCEL);
128 }
129
130 protected Point getInitialSize() {
131 return new Point(400, 200);
132 }
133
134 public static boolean open(int kind, Shell parent, String message) {
135 CmsMessageDialog dialog = new CmsMessageDialog(parent, message, kind);
136 return dialog.open() == 0;
137 }
138
139 public static boolean openConfirm(String message) {
140 return open(CONFIRM, Display.getCurrent().getActiveShell(), message);
141 }
142
143 public static void openInformation(String message) {
144 open(INFORMATION, Display.getCurrent().getActiveShell(), message);
145 }
146
147 public static boolean openQuestion(String message) {
148 return open(QUESTION, Display.getCurrent().getActiveShell(), message);
149 }
150
151 public static void openWarning(String message) {
152 open(WARNING, Display.getCurrent().getActiveShell(), message);
153 }
154
155 }