|
1 /* |
|
2 * Copyright (c) 2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package com.nokia.s60tools.ui.dialogs; |
|
20 |
|
21 import org.eclipse.jface.dialogs.Dialog; |
|
22 import org.eclipse.jface.dialogs.IDialogConstants; |
|
23 import org.eclipse.swt.SWT; |
|
24 import org.eclipse.swt.layout.GridData; |
|
25 import org.eclipse.swt.layout.GridLayout; |
|
26 import org.eclipse.swt.widgets.Button; |
|
27 import org.eclipse.swt.widgets.Composite; |
|
28 import org.eclipse.swt.widgets.Control; |
|
29 import org.eclipse.swt.widgets.Label; |
|
30 import org.eclipse.swt.widgets.Shell; |
|
31 import org.eclipse.swt.widgets.Text; |
|
32 import org.eclipse.ui.PlatformUI; |
|
33 |
|
34 import com.nokia.s60tools.ui.S60ToolsUIConstants; |
|
35 |
|
36 /** |
|
37 * Implements a list box dialog with read-only text content. |
|
38 * The dialog can be parameterized with title, resizable property, |
|
39 * scroll bar owning property, and with size attributes. |
|
40 * Dialog has just OK button which closes the dialog.<br> |
|
41 * |
|
42 * Usage example: |
|
43 * |
|
44 * <code> |
|
45 * <pre> |
|
46 * |
|
47 * // Parent shell |
|
48 * Shell sh = Display.getCurrent().getActiveShell() |
|
49 * |
|
50 * String dialogTitle = "Product Name"; |
|
51 * String listBoxContent = "My List Box Content String"; |
|
52 * |
|
53 * S60ToolsListBoxDialog dlg = new S60ToolsListBoxDialog(sh, |
|
54 * dialogTitle, |
|
55 * listBoxContent, |
|
56 * false, // not resizable |
|
57 * true, // no vertical scroll bar |
|
58 * true, // has horizontal scroll bar |
|
59 * 300, // default width |
|
60 * 250 // default height |
|
61 * ); |
|
62 * dlg.create(); |
|
63 * dlg.open(); |
|
64 * |
|
65 * </pre> |
|
66 * </code> |
|
67 * |
|
68 * It is also possible to enhance the dialog with Cancel button and |
|
69 * with optional message for the contents shown in the list box. |
|
70 * |
|
71 * <code> |
|
72 * <pre> |
|
73 * |
|
74 * // Parent shell |
|
75 * Shell sh = Display.getCurrent().getActiveShell() |
|
76 * |
|
77 * String dialogTitle = "Product Name"; |
|
78 * String listBoxContent = "My List Box Content String"; |
|
79 * String confirmMsg = "Do you want to really perform this operation?"; |
|
80 * |
|
81 * S60ToolsListBoxDialog dlg = new S60ToolsListBoxDialog(sh, |
|
82 * dialogTitle, |
|
83 * listBoxContent, |
|
84 * false, // not resizable |
|
85 * true, // no vertical scroll bar |
|
86 * true, // has horizontal scroll bar |
|
87 * 300, // default width |
|
88 * 250 // default height |
|
89 * true, //Creating Cancel button |
|
90 * confirmMsg // message show above text box |
|
91 * ); |
|
92 * dlg.create(); |
|
93 * int userAnswer = dlg.open(); |
|
94 * if(userAnswer == Dialog.OK){ |
|
95 * // Performing queried operation only, if user selected OK |
|
96 * ... |
|
97 * } |
|
98 * } |
|
99 * |
|
100 * </pre> |
|
101 * </code> |
|
102 * |
|
103 * @see org.eclipse.jface.dialogs.Dialog |
|
104 */ |
|
105 public class S60ToolsListBoxDialog extends Dialog{ |
|
106 |
|
107 /** |
|
108 * List box dialog title. |
|
109 */ |
|
110 private final String dialogTitle; |
|
111 |
|
112 /** |
|
113 * List box dialog content. |
|
114 */ |
|
115 private final String listBoxContentString; |
|
116 |
|
117 /** |
|
118 * List box dialog text control for showing the content. |
|
119 */ |
|
120 private Text listBoxContentTextControl; |
|
121 |
|
122 /** |
|
123 * Owning of vertical scroll bar. |
|
124 */ |
|
125 private final boolean hasVerticalScrollBar; |
|
126 |
|
127 /** |
|
128 * Owning of horizontal scroll bar. |
|
129 */ |
|
130 private final boolean hasHorizontalScrollBar; |
|
131 |
|
132 /** |
|
133 * Preferred width. |
|
134 */ |
|
135 private final int widthHint; |
|
136 |
|
137 /** |
|
138 * Preferred height. |
|
139 */ |
|
140 private final int heightHint; |
|
141 |
|
142 /** |
|
143 * Default style for the list box dialog |
|
144 */ |
|
145 private static final int DEFAULT_STYLE = SWT.MULTI | SWT.READ_ONLY | SWT.BORDER; |
|
146 |
|
147 /** |
|
148 * This is set to <code>true</code> if the dialog has a Cancel button, |
|
149 * otherwise set to <code>false</code>. |
|
150 */ |
|
151 private final boolean hasCancelButton; |
|
152 |
|
153 /** |
|
154 * This stores optional text that can be shown |
|
155 * on top of the text box control. |
|
156 */ |
|
157 private final String optionalTextAboveListBox; |
|
158 |
|
159 /** |
|
160 * This is the UI control for showing <code>optionalTextAboveListBox</code>. |
|
161 */ |
|
162 private Label labelControlAboveListBox; |
|
163 |
|
164 /** |
|
165 * ID for context sensitive-help, <code>null</code> means not used/supported by default. |
|
166 */ |
|
167 private String contextHelpID = null; |
|
168 |
|
169 /** |
|
170 * Constructor, with OK button only and with no extra Label. |
|
171 * @param parentShell Parent shell. |
|
172 * @param dialogTitle Dialog title. |
|
173 * @param listBoxContent Content to be showed in the dialog. |
|
174 * @param isResizable If <code>true</code>, dialog will be resizable. |
|
175 * @param hasVerticalScrollBar If <code>true</code>, dialog will have vertical scroll bar. |
|
176 * @param hasHorizontalScrollBar If <code>true</code>, dialog will have horizontal scroll bar. |
|
177 * @param widthHint Preferred width for the dialog. |
|
178 * @param heightHint Preferred height for the dialog. |
|
179 */ |
|
180 public S60ToolsListBoxDialog(Shell parentShell, |
|
181 String dialogTitle, |
|
182 String listBoxContent, |
|
183 boolean isResizable, |
|
184 boolean hasVerticalScrollBar, |
|
185 boolean hasHorizontalScrollBar, |
|
186 int widthHint, |
|
187 int heightHint) { |
|
188 |
|
189 |
|
190 this(parentShell,dialogTitle, listBoxContent, isResizable, |
|
191 hasVerticalScrollBar, hasHorizontalScrollBar, |
|
192 widthHint,heightHint, false, null ); |
|
193 } |
|
194 |
|
195 /** |
|
196 * Constructor, with support for Cancel button, extra label |
|
197 * but not context-sensitive help ID. |
|
198 * @param parentShell Parent shell. |
|
199 * @param dialogTitle Dialog title. |
|
200 * @param listBoxContent Content to be showed in the dialog. |
|
201 * @param isResizable If <code>true</code>, dialog will be resizable. |
|
202 * @param hasVerticalScrollBar If <code>true</code>, dialog will have vertical scroll bar. |
|
203 * @param hasHorizontalScrollBar If <code>true</code>, dialog will have horizontal scroll bar. |
|
204 * @param widthHint Preferred width for the dialog. |
|
205 * @param heightHint Preferred height for the dialog. |
|
206 * @param hasCancelButton If <code>true</code>, dialog will have also Cancel button. |
|
207 * @param optionalTextAboveListBox If not <code>null</code>, dialog will have also a label above the list box. |
|
208 */ |
|
209 public S60ToolsListBoxDialog(Shell parentShell, |
|
210 String dialogTitle, |
|
211 String listBoxContent, |
|
212 boolean isResizable, |
|
213 boolean hasVerticalScrollBar, |
|
214 boolean hasHorizontalScrollBar, |
|
215 int widthHint, |
|
216 int heightHint, |
|
217 boolean hasCancelButton, |
|
218 String optionalTextAboveListBox) { |
|
219 |
|
220 this(parentShell,dialogTitle, listBoxContent, isResizable, |
|
221 hasVerticalScrollBar, hasHorizontalScrollBar, |
|
222 widthHint,heightHint, hasCancelButton, |
|
223 optionalTextAboveListBox, null ); |
|
224 } |
|
225 |
|
226 |
|
227 /** |
|
228 * Constructor, with support for Cancel button, extra label |
|
229 * and context-sensitive help ID. |
|
230 * @param parentShell Parent shell. |
|
231 * @param dialogTitle Dialog title. |
|
232 * @param listBoxContent Content to be showed in the dialog. |
|
233 * @param isResizable If <code>true</code>, dialog will be resizable. |
|
234 * @param hasVerticalScrollBar If <code>true</code>, dialog will have vertical scroll bar. |
|
235 * @param hasHorizontalScrollBar If <code>true</code>, dialog will have horizontal scroll bar. |
|
236 * @param widthHint Preferred width for the dialog. |
|
237 * @param heightHint Preferred height for the dialog. |
|
238 * @param hasCancelButton If <code>true</code>, dialog will have also Cancel button. |
|
239 * @param optionalTextAboveListBox If not <code>null</code>, dialog will have also a label above the list box. |
|
240 * @param contextHelpID ID for context sensitive-help, <code>null</code> means not used. |
|
241 */ |
|
242 public S60ToolsListBoxDialog(Shell parentShell, |
|
243 String dialogTitle, |
|
244 String listBoxContent, |
|
245 boolean isResizable, |
|
246 boolean hasVerticalScrollBar, |
|
247 boolean hasHorizontalScrollBar, |
|
248 int widthHint, |
|
249 int heightHint, |
|
250 boolean hasCancelButton, |
|
251 String optionalTextAboveListBox, |
|
252 String contextHelpID) { |
|
253 |
|
254 super(parentShell); |
|
255 if(isResizable){ |
|
256 setShellStyle(getShellStyle() | SWT.RESIZE); |
|
257 } |
|
258 this.dialogTitle = dialogTitle; |
|
259 this.listBoxContentString = listBoxContent; |
|
260 this.hasVerticalScrollBar = hasVerticalScrollBar; |
|
261 this.hasHorizontalScrollBar = hasHorizontalScrollBar; |
|
262 this.widthHint = widthHint; |
|
263 this.heightHint = heightHint; |
|
264 |
|
265 this.hasCancelButton = hasCancelButton; |
|
266 this.optionalTextAboveListBox = optionalTextAboveListBox; |
|
267 this.contextHelpID = contextHelpID; |
|
268 } |
|
269 |
|
270 |
|
271 /* (non-Javadoc) |
|
272 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
|
273 */ |
|
274 protected Control createDialogArea(Composite parent) { |
|
275 |
|
276 //If label is set, showing it |
|
277 if(optionalTextAboveListBox != null){ |
|
278 Composite container = new Composite(parent, SWT.NO_FOCUS); |
|
279 labelControlAboveListBox = new Label(container, SWT.LEFT); |
|
280 container.setLayout(new GridLayout(1,false)); |
|
281 labelControlAboveListBox.setText(optionalTextAboveListBox); |
|
282 } |
|
283 |
|
284 Composite dialogAreaComposite = (Composite) super.createDialogArea(parent); |
|
285 |
|
286 // |
|
287 //Adding custom controls |
|
288 // |
|
289 final int cols = 1; |
|
290 GridLayout gdl = new GridLayout(cols, false); |
|
291 GridData gd = new GridData(GridData.FILL_BOTH); |
|
292 gd.widthHint = widthHint; |
|
293 gd.heightHint = heightHint; |
|
294 dialogAreaComposite.setLayout(gdl); |
|
295 dialogAreaComposite.setLayoutData(gd); |
|
296 |
|
297 int style = DEFAULT_STYLE; |
|
298 |
|
299 if(hasHorizontalScrollBar){ |
|
300 style = style | SWT.H_SCROLL; |
|
301 } |
|
302 |
|
303 if(hasVerticalScrollBar){ |
|
304 style = style | SWT.V_SCROLL; |
|
305 } |
|
306 |
|
307 GridLayout gdl2 = new GridLayout(cols, false); |
|
308 GridData gd2 = new GridData(GridData.FILL_BOTH); |
|
309 final int margins = 2 * S60ToolsUIConstants.MARGIN_BTW_FRAME_AND_CONTENTS; |
|
310 gd2.widthHint = widthHint - margins; |
|
311 gd2.heightHint = heightHint - margins; |
|
312 |
|
313 dialogAreaComposite.setLayoutData(gd2); |
|
314 |
|
315 listBoxContentTextControl = new Text(dialogAreaComposite, style); |
|
316 listBoxContentTextControl.setText(listBoxContentString); |
|
317 dialogAreaComposite.setLayout(gdl2); |
|
318 listBoxContentTextControl.setLayoutData(gd2); |
|
319 |
|
320 // Optionally adding context-sensitive help support |
|
321 if(contextHelpID != null){ |
|
322 PlatformUI.getWorkbench().getHelpSystem().setHelp(listBoxContentTextControl, contextHelpID); |
|
323 } |
|
324 |
|
325 return dialogAreaComposite; |
|
326 } |
|
327 |
|
328 /* (non-Javadoc) |
|
329 * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
|
330 */ |
|
331 protected void configureShell(Shell shell) { |
|
332 super.configureShell(shell); |
|
333 if (dialogTitle != null) |
|
334 shell.setText(dialogTitle); |
|
335 } |
|
336 |
|
337 /* (non-Javadoc) |
|
338 * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) |
|
339 */ |
|
340 protected void createButtonsForButtonBar(Composite parent) { |
|
341 // Creating OK button |
|
342 Button okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, |
|
343 true); |
|
344 // Optionally adding context-sensitive help support |
|
345 if(contextHelpID != null){ |
|
346 PlatformUI.getWorkbench().getHelpSystem().setHelp(okButton, contextHelpID); |
|
347 } |
|
348 //if cancel button also required |
|
349 if(hasCancelButton){ |
|
350 Button cancelButton = createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, |
|
351 true); |
|
352 // Optionally adding context-sensitive help support |
|
353 if(contextHelpID != null){ |
|
354 PlatformUI.getWorkbench().getHelpSystem().setHelp(cancelButton, contextHelpID); |
|
355 } |
|
356 } |
|
357 } |
|
358 |
|
359 } |