1 /*
2 * This file is a part of CAST project.
3 * (c) Copyright 2007, AGH University of Science & Technology
4 * https://caribou.iisg.agh.edu.pl/trac/cast
5 *
6 * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7 * You may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 * http://www.eclipse.org/legal/epl-v10.html
10 */
11 /*
12 * File: MsgBoxHelper.java
13 * Created: 2007-00-00
14 * Author: kpietak
15 * $Id: MsgBoxHelper.java 3454 2009-10-09 11:08:40Z kpietak $
16 */
17
18 package pl.edu.agh.cast.ui.util;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.MessageBox;
23
24 /**
25 * Shows different styles of message boxes.
26 *
27 *
28 * @author AGH CAST Team
29 */
30 // [XXX] kpietak show JFace dialogs instead of raw SWT once
31 public class MsgBoxHelper implements IMsgBoxHelper {
32
33 private Composite parent;
34
35 /**
36 * Creates new instance of {@link MsgBoxHelper} for given parent composite.
37 *
38 * @param parent
39 * parent composite
40 * @return a new instance of {@link MsgBoxHelper}
41 */
42 public static final MsgBoxHelper createInstanceFor(Composite parent) {
43 return new MsgBoxHelper(parent);
44 }
45
46 /**
47 * Constructor.
48 *
49 * @param parent
50 * parent composite
51 */
52 public MsgBoxHelper(Composite parent) {
53 if (parent == null) {
54 throw new IllegalArgumentException();
55 }
56 this.parent = parent;
57 }
58
59 /**
60 *
61 * {@inheritDoc}
62 *
63 * @see pl.edu.agh.cast.ui.util.IMsgBoxHelper#showErrorBox(java.lang.String, java.lang.String)
64 */
65 @Override
66 public int showErrorBox(String title, String msg) {
67 return showErrorBox(parent, title, msg);
68 }
69
70 /**
71 *
72 * {@inheritDoc}
73 *
74 * @see pl.edu.agh.cast.ui.util.IMsgBoxHelper#showInfoBox(java.lang.String, java.lang.String)
75 */
76 @Override
77 public int showInfoBox(String title, String msg) {
78 return showInfoBox(parent, title, msg);
79
80 }
81
82 /**
83 *
84 * {@inheritDoc}
85 *
86 * @see pl.edu.agh.cast.ui.util.IMsgBoxHelper#showQuestionBox(java.lang.String, java.lang.String)
87 */
88 @Override
89 public int showQuestionBox(String title, String msg) {
90 return showQuestionBox(parent, title, msg);
91 }
92
93 /**
94 *
95 * {@inheritDoc}
96 *
97 * @see pl.edu.agh.cast.ui.util.IMsgBoxHelper#showWarningBox(java.lang.String, java.lang.String)
98 */
99 @Override
100 public int showWarningBox(String title, String msg) {
101 return showWarningBox(parent, title, msg);
102 }
103
104 /**
105 *
106 * {@inheritDoc}
107 *
108 * @see pl.edu.agh.cast.ui.util.IMsgBoxHelper#showWarningQuestionBox(java.lang.String, java.lang.String)
109 */
110 @Override
111 public int showWarningQuestionBox(String title, String msg) {
112 return showWarningQuestionBox(parent, title, msg);
113 }
114
115 /**
116 * Shows Y/N question box.
117 *
118 * @param parent
119 * parent control
120 * @param title
121 * message box title
122 * @param msg
123 * question message
124 * @return message box result
125 */
126 public static int showQuestionBox(Composite parent, String title, String msg) {
127 int style = SWT.ICON_QUESTION | SWT.YES | SWT.NO;
128 return showBox(parent, title, msg, style);
129 }
130
131 /**
132 * Shows warning box.
133 *
134 * @param parent
135 * parent control
136 * @param title
137 * message box title
138 * @param msg
139 * warning message
140 * @return message box result
141 */
142 public static int showWarningBox(Composite parent, String title, String msg) {
143 int style = SWT.ICON_WARNING | SWT.OK;
144 return showBox(parent, title, msg, style);
145 }
146
147 /**
148 * Shows warning Y/N question box.
149 *
150 * @param parent
151 * parent control
152 * @param title
153 * message box title
154 * @param msg
155 * question message
156 * @return message box result
157 */
158 public static int showWarningQuestionBox(Composite parent, String title, String msg) {
159 int style = SWT.ICON_WARNING | SWT.YES | SWT.NO;
160 return showBox(parent, title, msg, style);
161 }
162
163 /**
164 * Shows error box.
165 *
166 * @param parent
167 * parent control
168 * @param title
169 * message box title
170 * @param msg
171 * error message
172 * @return message box result
173 */
174 public static int showErrorBox(Composite parent, String title, String msg) {
175 int style = SWT.ICON_ERROR | SWT.OK;
176 return showBox(parent, title, msg, style);
177 }
178
179 /**
180 * Shows information box.
181 *
182 * @param parent
183 * parent control
184 * @param title
185 * message box title
186 * @param msg
187 * info message
188 * @return message box result
189 */
190 public static int showInfoBox(Composite parent, String title, String msg) {
191 int style = SWT.ICON_INFORMATION | SWT.OK;
192 return showBox(parent, title, msg, style);
193 }
194
195 private static int showBox(Composite parent, String title, String msg, int style) {
196 MessageBox messageBox = new MessageBox(parent.getShell(), style);
197 messageBox.setText(title);
198 messageBox.setMessage(msg);
199 return messageBox.open();
200 }
201
202 }