KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 19346 - Dialog font should be
11  * activated and used by other components.
12  *******************************************************************************/

13 package org.eclipse.ui.internal.dialogs;
14
15 import java.util.Hashtable JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.jface.resource.ImageDescriptor;
20 import org.eclipse.jface.viewers.DoubleClickEvent;
21 import org.eclipse.jface.viewers.IDoubleClickListener;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredSelection;
25 import org.eclipse.jface.viewers.SelectionChangedEvent;
26 import org.eclipse.jface.viewers.TableViewer;
27 import org.eclipse.jface.wizard.WizardPage;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.graphics.Font;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Table;
36 import org.eclipse.swt.widgets.TableItem;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.internal.IWorkbenchGraphicConstants;
39 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
40 import org.eclipse.ui.internal.WorkbenchImages;
41 import org.eclipse.ui.internal.WorkbenchMessages;
42 import org.eclipse.ui.internal.WorkbenchPlugin;
43 import org.eclipse.ui.internal.registry.WorkingSetDescriptor;
44
45 /**
46  * The working set type page is used in the new working set
47  * wizard to select from a list of plugin defined working set
48  * types.
49  *
50  * @since 2.0
51  */

52 public class WorkingSetTypePage extends WizardPage {
53     private final static int SIZING_SELECTION_WIDGET_WIDTH = 50;
54
55     private final static int SIZING_SELECTION_WIDGET_HEIGHT = 200;
56
57     private TableViewer typesListViewer;
58
59     private Map JavaDoc icons;
60
61     private WorkingSetDescriptor[] descriptors;
62
63     /**
64      * Creates a new instance of the receiver
65      */

66     public WorkingSetTypePage() {
67         this(WorkbenchPlugin.getDefault().getWorkingSetRegistry().getNewPageWorkingSetDescriptors());
68     }
69
70     /**
71      * @param descriptors a set of working set descriptors which can be selected on the page
72      */

73     public WorkingSetTypePage(WorkingSetDescriptor[] descriptors) {
74         super(
75                 "workingSetTypeSelectionPage", WorkbenchMessages.Select, WorkbenchImages.getImageDescriptor(IWorkbenchGraphicConstants.IMG_WIZBAN_WORKINGSET_WIZ)); //$NON-NLS-1$
76
setDescription(WorkbenchMessages.WorkingSetTypePage_description);
77         icons = new Hashtable JavaDoc();
78         this.descriptors= descriptors;
79     }
80
81     /**
82      * Overrides method in WizardPage
83      *
84      * @see org.eclipse.jface.wizard.IWizardPage#canFlipToNextPage()
85      */

86     public boolean canFlipToNextPage() {
87         return isPageComplete();
88     }
89
90     /**
91      * Populates the working set types list.
92      */

93     private void createContent() {
94         Table table = (Table) typesListViewer.getControl();
95
96         for (int i = 0; i < descriptors.length; i++) {
97             TableItem tableItem = new TableItem(table, SWT.NULL);
98             ImageDescriptor imageDescriptor = descriptors[i].getIcon();
99
100             if (imageDescriptor != null) {
101                 Image icon = (Image) icons.get(imageDescriptor);
102                 if (icon == null) {
103                     icon = imageDescriptor.createImage();
104                     icons.put(imageDescriptor, icon);
105                 }
106                 tableItem.setImage(icon);
107             }
108             tableItem.setText(descriptors[i].getName());
109             tableItem.setData(descriptors[i]);
110         }
111     }
112
113     /**
114      * Implements IDialogPage
115      *
116      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(Composite)
117      */

118     public void createControl(Composite parent) {
119         Font font = parent.getFont();
120         Composite composite = new Composite(parent, SWT.NULL);
121         composite.setLayout(new GridLayout());
122         composite.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
123         setControl(composite);
124
125         PlatformUI.getWorkbench().getHelpSystem().setHelp(composite,
126                 IWorkbenchHelpContextIds.WORKING_SET_TYPE_PAGE);
127         Label typesLabel = new Label(composite, SWT.NONE);
128         typesLabel.setText(WorkbenchMessages.WorkingSetTypePage_typesLabel);
129         GridData data = new GridData(GridData.FILL_HORIZONTAL);
130         typesLabel.setLayoutData(data);
131         typesLabel.setFont(font);
132
133         typesListViewer = new TableViewer(composite, SWT.BORDER | SWT.MULTI);
134         data = new GridData(GridData.FILL_BOTH);
135         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
136         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
137         typesListViewer.getTable().setLayoutData(data);
138         typesListViewer.getTable().setFont(font);
139         typesListViewer
140                 .addSelectionChangedListener(new ISelectionChangedListener() {
141                     public void selectionChanged(SelectionChangedEvent event) {
142                         handleSelectionChanged();
143                     }
144                 });
145         typesListViewer.addDoubleClickListener(new IDoubleClickListener() {
146             public void doubleClick(DoubleClickEvent event) {
147                 handleDoubleClick();
148             }
149         });
150         createContent();
151         setPageComplete(false);
152     }
153
154     /**
155      * Overrides method in DialogPage
156      *
157      * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
158      */

159     public void dispose() {
160         Iterator JavaDoc iterator = icons.values().iterator();
161
162         while (iterator.hasNext()) {
163             Image icon = (Image) iterator.next();
164             icon.dispose();
165         }
166         super.dispose();
167     }
168
169     /**
170      * Returns the page id of the selected working set type.
171      *
172      * @return the page id of the selected working set type.
173      */

174     public String JavaDoc getSelection() {
175         ISelection selection = typesListViewer.getSelection();
176         boolean hasSelection = selection != null
177                 && selection.isEmpty() == false;
178
179         if (hasSelection && selection instanceof IStructuredSelection) {
180             WorkingSetDescriptor workingSetDescriptor = (WorkingSetDescriptor) ((IStructuredSelection) selection)
181                     .getFirstElement();
182             return workingSetDescriptor.getId();
183         }
184         return null;
185     }
186
187     /**
188      * Called when a working set type is double clicked.
189      */

190     private void handleDoubleClick() {
191         handleSelectionChanged();
192         getContainer().showPage(getNextPage());
193     }
194
195     /**
196      * Called when the selection has changed.
197      */

198     private void handleSelectionChanged() {
199         ISelection selection = typesListViewer.getSelection();
200         boolean hasSelection = selection != null
201                 && selection.isEmpty() == false;
202
203         setPageComplete(hasSelection);
204     }
205 }
206
Popular Tags