KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > AboutDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.dialogs;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.LinkedList JavaDoc;
15
16 import org.eclipse.core.runtime.IBundleGroup;
17 import org.eclipse.core.runtime.IBundleGroupProvider;
18 import org.eclipse.core.runtime.IProduct;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.jface.dialogs.IDialogConstants;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.resource.JFaceColors;
23 import org.eclipse.osgi.util.NLS;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.accessibility.AccessibleAdapter;
26 import org.eclipse.swt.accessibility.AccessibleEvent;
27 import org.eclipse.swt.custom.BusyIndicator;
28 import org.eclipse.swt.custom.StyledText;
29 import org.eclipse.swt.events.DisposeEvent;
30 import org.eclipse.swt.events.DisposeListener;
31 import org.eclipse.swt.events.SelectionAdapter;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.graphics.Color;
34 import org.eclipse.swt.graphics.Cursor;
35 import org.eclipse.swt.graphics.Image;
36 import org.eclipse.swt.layout.GridData;
37 import org.eclipse.swt.layout.GridLayout;
38 import org.eclipse.swt.layout.RowLayout;
39 import org.eclipse.swt.widgets.Button;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Control;
42 import org.eclipse.swt.widgets.Label;
43 import org.eclipse.swt.widgets.Shell;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
46 import org.eclipse.ui.internal.ProductProperties;
47 import org.eclipse.ui.internal.WorkbenchMessages;
48 import org.eclipse.ui.internal.about.AboutBundleGroupData;
49 import org.eclipse.ui.internal.about.AboutFeaturesButtonManager;
50
51 /**
52  * Displays information about the product.
53  */

54 public class AboutDialog extends ProductInfoDialog {
55     private final static int MAX_IMAGE_WIDTH_FOR_TEXT = 250;
56
57     private final static int FEATURES_ID = IDialogConstants.CLIENT_ID + 1;
58
59     private final static int PLUGINS_ID = IDialogConstants.CLIENT_ID + 2;
60
61     private final static int INFO_ID = IDialogConstants.CLIENT_ID + 3;
62
63     private String JavaDoc productName;
64
65     private IProduct product;
66
67     private AboutBundleGroupData[] bundleGroupInfos;
68
69     private ArrayList JavaDoc images = new ArrayList JavaDoc();
70
71     private AboutFeaturesButtonManager buttonManager = new AboutFeaturesButtonManager();
72
73     // TODO should the styled text be disposed? if not then it likely
74
// doesn't need to be a member
75
private StyledText text;
76
77     /**
78      * Create an instance of the AboutDialog for the given window.
79      * @param parentShell The parent of the dialog.
80      */

81     public AboutDialog(Shell parentShell) {
82         super(parentShell);
83
84         product = Platform.getProduct();
85         if (product != null) {
86             productName = product.getName();
87         }
88         if (productName == null) {
89             productName = WorkbenchMessages.AboutDialog_defaultProductName;
90         }
91
92         // create a descriptive object for each BundleGroup
93
IBundleGroupProvider[] providers = Platform.getBundleGroupProviders();
94         LinkedList JavaDoc groups = new LinkedList JavaDoc();
95         if (providers != null) {
96             for (int i = 0; i < providers.length; ++i) {
97                 IBundleGroup[] bundleGroups = providers[i].getBundleGroups();
98                 for (int j = 0; j < bundleGroups.length; ++j) {
99                     groups.add(new AboutBundleGroupData(bundleGroups[j]));
100                 }
101             }
102         }
103         bundleGroupInfos = (AboutBundleGroupData[]) groups
104                 .toArray(new AboutBundleGroupData[0]);
105     }
106
107     /*
108      * (non-Javadoc) Method declared on Dialog.
109      */

110     protected void buttonPressed(int buttonId) {
111         switch (buttonId) {
112         case FEATURES_ID:
113             new AboutFeaturesDialog(getShell(), productName, bundleGroupInfos)
114                     .open();
115             break;
116         case PLUGINS_ID:
117             new AboutPluginsDialog(getShell(), productName).open();
118             break;
119         case INFO_ID:
120             BusyIndicator.showWhile(null, new Runnable JavaDoc() {
121                 public void run() {
122                     new AboutSystemDialog(getShell()).open();
123                 }
124             });
125             break;
126         default:
127             super.buttonPressed(buttonId);
128             break;
129         }
130     }
131
132     public boolean close() {
133         // dispose all images
134
for (int i = 0; i < images.size(); ++i) {
135             Image image = (Image) images.get(i);
136             image.dispose();
137         }
138
139         return super.close();
140     }
141
142     /*
143      * (non-Javadoc) Method declared on Window.
144      */

145     protected void configureShell(Shell newShell) {
146         super.configureShell(newShell);
147         newShell.setText(NLS.bind(WorkbenchMessages.AboutDialog_shellTitle,productName ));
148         PlatformUI.getWorkbench().getHelpSystem().setHelp(newShell,
149                 IWorkbenchHelpContextIds.ABOUT_DIALOG);
150     }
151
152     /**
153      * Add buttons to the dialog's button bar.
154      *
155      * Subclasses should override.
156      *
157      * @param parent
158      * the button bar composite
159      */

160     protected void createButtonsForButtonBar(Composite parent) {
161         parent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
162
163         // bug 64232: the feature details button should only be created if there
164
// are features to show
165
if (bundleGroupInfos != null && bundleGroupInfos.length > 0) {
166             createButton(parent, FEATURES_ID, WorkbenchMessages.AboutDialog_featureInfo, false);
167         }
168
169         createButton(parent, PLUGINS_ID, WorkbenchMessages.AboutDialog_pluginInfo, false);
170         createButton(parent, INFO_ID, WorkbenchMessages.AboutDialog_systemInfo, false);
171
172         Label l = new Label(parent, SWT.NONE);
173         l.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
174         GridLayout layout = (GridLayout) parent.getLayout();
175         layout.numColumns++;
176         layout.makeColumnsEqualWidth = false;
177
178         Button b = createButton(parent, IDialogConstants.OK_ID,
179                 IDialogConstants.OK_LABEL, true);
180         b.setFocus();
181     }
182
183     /**
184      * Creates and returns the contents of the upper part
185      * of the dialog (above the button bar).
186      *
187      * Subclasses should overide.
188      *
189      * @param parent the parent composite to contain the dialog area
190      * @return the dialog area control
191      */

192     protected Control createDialogArea(Composite parent) {
193         final Cursor hand = new Cursor(parent.getDisplay(), SWT.CURSOR_HAND);
194         final Cursor busy = new Cursor(parent.getDisplay(), SWT.CURSOR_WAIT);
195         setHandCursor(hand);
196         setBusyCursor(busy);
197         getShell().addDisposeListener(new DisposeListener() {
198             public void widgetDisposed(DisposeEvent e) {
199                 setHandCursor(null);
200                 hand.dispose();
201                 setBusyCursor(null);
202                 busy.dispose();
203             }
204         });
205
206         // brand the about box if there is product info
207
Image aboutImage = null;
208         if (product != null) {
209             ImageDescriptor imageDescriptor = ProductProperties
210                     .getAboutImage(product);
211             if (imageDescriptor != null) {
212                 aboutImage = imageDescriptor.createImage();
213             }
214
215             // if the about image is small enough, then show the text
216
if (aboutImage == null
217                     || aboutImage.getBounds().width <= MAX_IMAGE_WIDTH_FOR_TEXT) {
218                 String JavaDoc aboutText = ProductProperties.getAboutText(product);
219                 if (aboutText != null) {
220                     setItem(scan(aboutText));
221                 }
222             }
223
224             if (aboutImage != null) {
225                 images.add(aboutImage);
226             }
227         }
228
229         // create a composite which is the parent of the top area and the bottom
230
// button bar, this allows there to be a second child of this composite with
231
// a banner background on top but not have on the bottom
232
Composite workArea = new Composite(parent, SWT.NONE);
233         GridLayout workLayout = new GridLayout();
234         workLayout.marginHeight = 0;
235         workLayout.marginWidth = 0;
236         workLayout.verticalSpacing = 0;
237         workLayout.horizontalSpacing = 0;
238         workArea.setLayout(workLayout);
239         workArea.setLayoutData(new GridData(GridData.FILL_BOTH));
240
241         // page group
242
Color background = JFaceColors.getBannerBackground(parent.getDisplay());
243         Color foreground = JFaceColors.getBannerForeground(parent.getDisplay());
244         Composite top = (Composite) super.createDialogArea(workArea);
245
246         // override any layout inherited from createDialogArea
247
GridLayout layout = new GridLayout();
248         layout.marginHeight = 0;
249         layout.marginWidth = 0;
250         layout.verticalSpacing = 0;
251         layout.horizontalSpacing = 0;
252         top.setLayout(layout);
253         top.setLayoutData(new GridData(GridData.FILL_BOTH));
254         top.setBackground(background);
255         top.setForeground(foreground);
256
257         // the image & text
258
Composite topContainer = new Composite(top, SWT.NONE);
259         topContainer.setBackground(background);
260         topContainer.setForeground(foreground);
261
262         layout = new GridLayout();
263         layout.numColumns = (aboutImage == null || getItem() == null ? 1 : 2);
264         layout.marginWidth = 0;
265         layout.marginHeight = 0;
266         layout.verticalSpacing = 0;
267         layout.horizontalSpacing = 0;
268         topContainer.setLayout(layout);
269         GridData data = new GridData();
270         data.horizontalAlignment = GridData.FILL;
271         data.grabExcessHorizontalSpace = true;
272         topContainer.setLayoutData(data);
273
274         //image on left side of dialog
275
if (aboutImage != null) {
276             Label imageLabel = new Label(topContainer, SWT.NONE);
277             imageLabel.setBackground(background);
278             imageLabel.setForeground(foreground);
279
280             data = new GridData();
281             data.horizontalAlignment = GridData.FILL;
282             data.verticalAlignment = GridData.BEGINNING;
283             data.grabExcessHorizontalSpace = false;
284             imageLabel.setLayoutData(data);
285             imageLabel.setImage(aboutImage);
286         }
287
288         if (getItem() != null) {
289             // there is no margins around the image, so insert an extra composite
290
// here to provide some margins for the text.
291
Composite textContainer = new Composite(topContainer, SWT.NONE);
292             textContainer.setBackground(background);
293             textContainer.setForeground(foreground);
294
295             layout = new GridLayout();
296             layout.numColumns = 1;
297             textContainer.setLayout(layout);
298             data = new GridData();
299             data.horizontalAlignment = GridData.FILL;
300             data.verticalAlignment = GridData.BEGINNING;
301             data.grabExcessHorizontalSpace = true;
302             textContainer.setLayoutData(data);
303
304             
305             // text on the right
306
text = new StyledText(textContainer, SWT.MULTI | SWT.READ_ONLY);
307             text.setCaret(null);
308             text.setFont(parent.getFont());
309             data = new GridData();
310             data.horizontalAlignment = GridData.FILL;
311             data.verticalAlignment = GridData.BEGINNING;
312             data.grabExcessHorizontalSpace = true;
313             text.setText(getItem().getText());
314             text.setLayoutData(data);
315             text.setCursor(null);
316             text.setBackground(background);
317             text.setForeground(foreground);
318
319             setLinkRanges(text, getItem().getLinkRanges());
320             addListeners(text);
321         }
322
323         // horizontal bar
324
Label bar = new Label(workArea, SWT.HORIZONTAL | SWT.SEPARATOR);
325         data = new GridData();
326         data.horizontalAlignment = GridData.FILL;
327         bar.setLayoutData(data);
328
329         // add image buttons for bundle groups that have them
330
Composite bottom = (Composite) super.createDialogArea(workArea);
331         // override any layout inherited from createDialogArea
332
layout = new GridLayout();
333         bottom.setLayout(layout);
334         bottom.setLayoutData(new GridData(GridData.FILL_BOTH));
335
336         createFeatureImageButtonRow(bottom);
337
338         // spacer
339
bar = new Label(bottom, SWT.NONE);
340         data = new GridData();
341         data.horizontalAlignment = GridData.FILL;
342         bar.setLayoutData(data);
343
344         return workArea;
345     }
346
347     private void createFeatureImageButtonRow(Composite parent) {
348         Composite featureContainer = new Composite(parent, SWT.NONE);
349         RowLayout rowLayout = new RowLayout();
350         rowLayout.wrap = true;
351         featureContainer.setLayout(rowLayout);
352         GridData data = new GridData();
353         data.horizontalAlignment = GridData.FILL;
354         featureContainer.setLayoutData(data);
355
356         for (int i = 0; i < bundleGroupInfos.length; i++) {
357             createFeatureButton(featureContainer, bundleGroupInfos[i]);
358         }
359     }
360
361     private Button createFeatureButton(Composite parent,
362             final AboutBundleGroupData info) {
363         if (!buttonManager.add(info)) {
364             return null;
365         }
366
367         ImageDescriptor desc = info.getFeatureImage();
368         Image featureImage = null;
369
370         Button button = new Button(parent, SWT.FLAT | SWT.PUSH);
371         button.setData(info);
372         featureImage = desc.createImage();
373         images.add(featureImage);
374         button.setImage(featureImage);
375         button.setToolTipText(info.getProviderName());
376         
377         button.getAccessible().addAccessibleListener(new AccessibleAdapter(){
378             /* (non-Javadoc)
379              * @see org.eclipse.swt.accessibility.AccessibleAdapter#getName(org.eclipse.swt.accessibility.AccessibleEvent)
380              */

381             public void getName(AccessibleEvent e) {
382                 e.result = info.getProviderName();
383             }
384         });
385         button.addSelectionListener(new SelectionAdapter() {
386             public void widgetSelected(SelectionEvent event) {
387                 AboutBundleGroupData[] groupInfos = buttonManager
388                         .getRelatedInfos(info);
389                 AboutBundleGroupData selection = (AboutBundleGroupData) event.widget
390                         .getData();
391
392                 AboutFeaturesDialog d = new AboutFeaturesDialog(getShell(),
393                         productName, groupInfos);
394                 d.setInitialSelection(selection);
395                 d.open();
396             }
397         });
398
399         return button;
400     }
401 }
402
Popular Tags