View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2002-2003 Bartek Teodorczyk <barteo@barteo.net>
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19   
20  package org.microemu.app.ui.swt;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.events.SelectionAdapter;
24  import org.eclipse.swt.events.SelectionEvent;
25  import org.eclipse.swt.layout.GridData;
26  import org.eclipse.swt.layout.GridLayout;
27  import org.eclipse.swt.widgets.Button;
28  import org.eclipse.swt.widgets.Composite;
29  import org.eclipse.swt.widgets.Control;
30  import org.eclipse.swt.widgets.Label;
31  import org.eclipse.swt.widgets.Shell;
32  
33  
34  public class SwtMessageDialog extends SwtDialog 
35  {
36  	public final static int ERROR = 1;
37  	public final static int WARNING = 2;
38  	public final static int INFORMATION = 3;
39  	public final static int QUESTION = 4;
40  	
41  	private String title;
42  	private String message;
43  	private String[] buttonLabels;
44  	private int defaultIndex;
45  
46  
47  	public SwtMessageDialog(Shell parentShell, String title, String message, int imageType, String[] buttonLabels, int defaultIndex) 
48  	{
49  		super(parentShell);
50  		
51  		this.title = title;
52  		this.message = message;
53  		this.buttonLabels = buttonLabels;
54  		this.defaultIndex = defaultIndex;
55  	}
56  
57  
58  	public static void openMessageDialog(Shell parent, String title, String message, int messageType) 
59  	{
60  		SwtMessageDialog dialog = 
61  				new SwtMessageDialog(parent, title, message, messageType, new String[] {"OK"}, 0);
62  		dialog.open();	
63  	}
64  	
65  	
66  	public static boolean openQuestion(Shell parent, String title, String message) 
67  	{
68  		SwtMessageDialog dialog = 
69  				new SwtMessageDialog(parent, title, message, QUESTION, new String[] {"Yes", "No"}, 0);
70  		return dialog.open() == 0;
71  	}
72  	
73  
74  
75  	protected void configureShell(Shell shell) 
76  	{
77  		super.configureShell(shell);
78  		
79  		if (title != null) {
80  			shell.setText(title);
81  		}
82  	}
83  	
84  	
85  	protected Control createDialogArea(Composite composite) 
86  	{
87  		GridLayout gridLayout = new GridLayout();
88  		gridLayout.numColumns = 1;
89  		composite.setLayout(gridLayout);
90  
91  		Label lbMessage = new Label(composite, SWT.NONE);
92  		lbMessage.setText(message);
93  		lbMessage.setLayoutData(new GridData(GridData.FILL_BOTH));
94  
95  		return composite;
96  	}
97  
98  
99  	protected Control createButtonBar(Composite parent) 
100 	{
101 		Composite composite = new Composite(parent, SWT.NONE);
102 		
103 		composite.setLayout(new GridLayout(buttonLabels.length, false));
104 		composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
105 		composite.setFont(parent.getFont());
106 
107 		for (int i = 0; i < buttonLabels.length; i++) {
108 			Button button = new Button(composite, SWT.PUSH);
109 			button.setText(buttonLabels[i]);
110 			button.setData(new Integer(i));
111 			button.addSelectionListener(new SelectionAdapter() 
112 			{
113 				public void widgetSelected(SelectionEvent event) 
114 				{
115 					buttonPressed(((Integer) event.widget.getData()).intValue());
116 				}
117 			});
118 			
119 			if (i == defaultIndex) {
120 				Shell shell = parent.getShell();
121 				if (shell != null) {
122 					shell.setDefaultButton(button);
123 				}
124 			}
125 		}
126 
127 		return composite;
128 	}
129 
130 
131 	protected void buttonPressed(int buttonId) 
132 	{
133 		setReturnCode(buttonId);
134 		close();
135 	}
136 	
137 
138 }