KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > activities > ws > EnablementDialog


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.activities.ws;
12
13 import com.ibm.icu.text.MessageFormat;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Properties JavaDoc;
17 import java.util.ResourceBundle JavaDoc;
18 import java.util.Set JavaDoc;
19
20 import org.eclipse.jface.dialogs.Dialog;
21 import org.eclipse.jface.dialogs.IDialogConstants;
22 import org.eclipse.jface.viewers.CheckStateChangedEvent;
23 import org.eclipse.jface.viewers.CheckboxTableViewer;
24 import org.eclipse.jface.viewers.ICheckStateListener;
25 import org.eclipse.jface.viewers.ISelectionChangedListener;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.SelectionChangedEvent;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Font;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Label;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.swt.widgets.Text;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.activities.IActivity;
41 import org.eclipse.ui.activities.IActivityManager;
42 import org.eclipse.ui.activities.NotDefinedException;
43 import org.eclipse.ui.activities.WorkbenchTriggerPointAdvisor;
44
45 /**
46  * Dialog that will prompt the user and confirm that they wish to activate a set
47  * of activities.
48  *
49  * @since 3.0
50  */

51 public class EnablementDialog extends Dialog {
52
53     /**
54      * The translation bundle in which to look up internationalized text.
55      */

56     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle
57             .getBundle(EnablementDialog.class.getName());
58
59     private Button dontAskButton;
60
61     private Set JavaDoc activitiesToEnable = new HashSet JavaDoc(7);
62
63     private Collection JavaDoc activityIds;
64
65     private boolean dontAsk;
66
67     private Button detailsButton;
68
69     boolean showDetails = false;
70
71     private Composite detailsComposite;
72
73     private Label detailsLabel;
74
75     private String JavaDoc selectedActivity;
76
77     private Text detailsText;
78
79     private Properties JavaDoc strings;
80
81     /**
82      * Create a new instance of the reciever.
83      *
84      * @param parentShell the parent shell
85      * @param activityIds the candidate activities
86      * @param strings string overrides
87      */

88     public EnablementDialog(Shell parentShell, Collection JavaDoc activityIds, Properties JavaDoc strings) {
89         super(parentShell);
90         this.activityIds = activityIds;
91         this.strings = strings;
92     }
93
94     /* (non-Javadoc)
95      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
96      */

97     protected Control createDialogArea(Composite parent) {
98         Composite composite = (Composite) super.createDialogArea(parent);
99         Font dialogFont = parent.getFont();
100         composite.setFont(dialogFont);
101         Label text = new Label(composite, SWT.NONE);
102         text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
103         text.setFont(dialogFont);
104         IActivityManager manager = PlatformUI.getWorkbench()
105                 .getActivitySupport().getActivityManager();
106
107         if (activityIds.size() == 1) {
108             String JavaDoc activityId = (String JavaDoc) activityIds.iterator().next();
109             activitiesToEnable.add(activityId);
110             selectedActivity = activityId;
111
112             IActivity activity = manager.getActivity(activityId);
113             String JavaDoc activityText;
114             try {
115                 activityText = activity.getName();
116             } catch (NotDefinedException e) {
117                 activityText = activity.getId();
118             }
119             text.setText(MessageFormat.format(RESOURCE_BUNDLE
120                     .getString("requiresSingle"), //$NON-NLS-1$
121
new Object JavaDoc[] { activityText }));
122
123             text = new Label(composite, SWT.NONE);
124             text
125                     .setText(strings
126                             .getProperty(
127                                     WorkbenchTriggerPointAdvisor.PROCEED_SINGLE,
128                                     RESOURCE_BUNDLE
129                                             .getString(WorkbenchTriggerPointAdvisor.PROCEED_SINGLE)));
130             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
131             text.setFont(dialogFont);
132         } else {
133             text.setText(RESOURCE_BUNDLE.getString("requiresMulti")); //$NON-NLS-1$
134
Set JavaDoc activityIdsCopy = new HashSet JavaDoc(activityIds);
135             CheckboxTableViewer viewer = CheckboxTableViewer.newCheckList(
136                     composite, SWT.CHECK | SWT.BORDER | SWT.SINGLE);
137             viewer.setContentProvider(new ActivityContentProvider());
138             viewer.setLabelProvider(new ActivityLabelProvider(manager));
139             viewer.setInput(activityIdsCopy);
140             viewer.setCheckedElements(activityIdsCopy.toArray());
141             viewer.addCheckStateListener(new ICheckStateListener() {
142
143                 /* (non-Javadoc)
144                  * @see org.eclipse.jface.viewers.ICheckStateListener#checkStateChanged(org.eclipse.jface.viewers.CheckStateChangedEvent)
145                  */

146                 public void checkStateChanged(CheckStateChangedEvent event) {
147                     if (event.getChecked()) {
148                         activitiesToEnable.add(event.getElement());
149                     } else {
150                         activitiesToEnable.remove(event.getElement());
151                     }
152
153                     getButton(Window.OK).setEnabled(
154                             !activitiesToEnable.isEmpty());
155                 }
156             });
157             viewer.addSelectionChangedListener(new ISelectionChangedListener() {
158                 /* (non-Javadoc)
159                  * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
160                  */

161                 public void selectionChanged(SelectionChangedEvent event) {
162                     selectedActivity = (String JavaDoc) ((IStructuredSelection) event
163                             .getSelection()).getFirstElement();
164                     setDetails();
165                 }
166             });
167             activitiesToEnable.addAll(activityIdsCopy);
168
169             viewer.getControl().setLayoutData(
170                     new GridData(GridData.FILL_HORIZONTAL));
171             viewer.getControl().setFont(dialogFont);
172
173             text = new Label(composite, SWT.NONE);
174             text.setText(strings.getProperty(WorkbenchTriggerPointAdvisor.PROCEED_MULTI, RESOURCE_BUNDLE
175                     .getString(WorkbenchTriggerPointAdvisor.PROCEED_MULTI)));
176             text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
177             text.setFont(dialogFont);
178         }
179         Label seperator = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);
180         seperator.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
181
182         dontAskButton = new Button(composite, SWT.CHECK);
183         dontAskButton.setSelection(false);
184         dontAskButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
185         dontAskButton.setText(strings.getProperty(
186                 WorkbenchTriggerPointAdvisor.DONT_ASK, RESOURCE_BUNDLE
187                         .getString(WorkbenchTriggerPointAdvisor.DONT_ASK)));
188         dontAskButton.setFont(dialogFont);
189
190         detailsComposite = new Composite(composite, SWT.NONE);
191         GridLayout layout = new GridLayout();
192         layout.marginHeight = 0;
193         layout.marginWidth = 0;
194         detailsComposite.setLayout(layout);
195         detailsLabel = new Label(detailsComposite, SWT.NONE);
196         detailsLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
197         detailsLabel.setFont(dialogFont);
198
199         detailsText = new Text(detailsComposite, SWT.WRAP | SWT.V_SCROLL
200                 | SWT.BORDER | SWT.READ_ONLY);
201         detailsText.setLayoutData(new GridData(GridData.FILL_BOTH));
202         detailsText.setFont(dialogFont);
203
204         setDetails();
205
206         GridData data = new GridData(GridData.FILL_BOTH);
207         detailsComposite.setLayoutData(data);
208         setDetailHints();
209
210         return composite;
211     }
212
213     /**
214      * Set the text of the detail label and text area.
215      */

216     protected void setDetails() {
217         if (selectedActivity == null) {
218             detailsLabel
219                     .setText(strings
220                             .getProperty(
221                                     WorkbenchTriggerPointAdvisor.NO_DETAILS,
222                                     RESOURCE_BUNDLE
223                                             .getString(WorkbenchTriggerPointAdvisor.NO_DETAILS)));
224             detailsText.setText(""); //$NON-NLS-1$
225
} else {
226             IActivity activity = PlatformUI.getWorkbench().getActivitySupport()
227                     .getActivityManager().getActivity(selectedActivity);
228             String JavaDoc name;
229             try {
230                 name = activity.getName();
231             } catch (NotDefinedException e1) {
232                 name = selectedActivity;
233             }
234             String JavaDoc desc;
235             try {
236                 desc = activity.getDescription();
237             } catch (NotDefinedException e) {
238                 desc = RESOURCE_BUNDLE.getString("noDescAvailable"); //$NON-NLS-1$
239
}
240             detailsLabel.setText(MessageFormat.format(RESOURCE_BUNDLE
241                     .getString("detailsLabel"), new Object JavaDoc[] { name })); //$NON-NLS-1$
242
detailsText.setText(desc);
243         }
244     }
245
246     /**
247      *
248      */

249     protected void setDetailHints() {
250         GridData data = (GridData) detailsComposite.getLayoutData();
251         if (showDetails) {
252             Composite parent = detailsComposite.getParent();
253             data.widthHint = parent.getSize().x - ((GridLayout)parent.getLayout()).marginWidth * 2;
254             data.heightHint = convertHeightInCharsToPixels(5);
255         } else {
256             data.widthHint = 0;
257             data.heightHint = 0;
258         }
259     }
260
261     /**
262      * Set the label of the detail button based on whether we're currently showing the description text.
263      */

264     private void setDetailButtonLabel() {
265         if (!showDetails) {
266             detailsButton.setText(RESOURCE_BUNDLE.getString("showDetails")); //$NON-NLS-1$
267
} else {
268             detailsButton.setText(RESOURCE_BUNDLE.getString("hideDetails")); //$NON-NLS-1$
269
}
270     }
271
272     /* (non-Javadoc)
273      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
274      */

275     protected void configureShell(Shell newShell) {
276         super.configureShell(newShell);
277         newShell.setText(RESOURCE_BUNDLE.getString("title")); //$NON-NLS-1$
278
}
279
280     /**
281      * @return Returns whether the user has declared that there is to be no further
282      * prompting for the supplied activities
283      */

284     public boolean getDontAsk() {
285         return dontAsk;
286     }
287
288     /**
289      * @return Returns the activities to enable
290      */

291     public Set JavaDoc getActivitiesToEnable() {
292         return activitiesToEnable;
293     }
294
295     /* (non-Javadoc)
296      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
297      */

298     protected void okPressed() {
299         dontAsk = dontAskButton.getSelection();
300         super.okPressed();
301     }
302
303     /* (non-Javadoc)
304      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
305      */

306     protected void createButtonsForButtonBar(Composite parent) {
307         super.createButtonsForButtonBar(parent);
308         detailsButton = createButton(parent, IDialogConstants.DETAILS_ID,
309                 "", false); //$NON-NLS-1$
310
setDetailButtonLabel();
311     }
312
313     /* (non-Javadoc)
314      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
315      */

316     protected void buttonPressed(int buttonId) {
317         if (buttonId == IDialogConstants.DETAILS_ID) {
318             detailsPressed();
319             return;
320         }
321         super.buttonPressed(buttonId);
322     }
323
324     /**
325      * Handles selection of the Details button.
326      */

327     private void detailsPressed() {
328         showDetails = !showDetails;
329         setDetailButtonLabel();
330         setDetailHints();
331         setDetails();
332         ((Composite) getDialogArea()).layout(true);
333         getShell().setSize(getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT));
334     }
335 }
336
Popular Tags