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