KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > misc > WizardStepGroup


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  * 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.ide.misc;
14
15 import org.eclipse.jface.resource.ImageDescriptor;
16 import org.eclipse.jface.viewers.IContentProvider;
17 import org.eclipse.jface.viewers.ISelectionChangedListener;
18 import org.eclipse.jface.viewers.LabelProvider;
19 import org.eclipse.jface.viewers.TableViewer;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.events.DisposeEvent;
22 import org.eclipse.swt.events.DisposeListener;
23 import org.eclipse.swt.graphics.Font;
24 import org.eclipse.swt.graphics.Image;
25 import org.eclipse.swt.layout.GridData;
26 import org.eclipse.swt.layout.GridLayout;
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.ui.internal.ide.IDEWorkbenchMessages;
31 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
32 import org.eclipse.ui.internal.ide.dialogs.WizardStep;
33 import org.eclipse.ui.model.WorkbenchContentProvider;
34
35 /**
36  * A group of controls used to view the list of
37  * wizard steps to be done.
38  */

39 public class WizardStepGroup {
40     
41      private Image doneImage;
42
43     private Image currentImage;
44
45     private WizardStep currentStep;
46
47     private Composite parentComposite;
48
49     private TableViewer stepViewer;
50
51     private ISelectionChangedListener selectionListener;
52
53     /**
54      * Creates a new instance of the <code>WizardStepGroup</code>
55      */

56     public WizardStepGroup() {
57         super();
58     }
59
60     /**
61      * Create the contents of this group. The basic layout is a table
62      * with a label above it.
63      */

64     public Control createContents(Composite parent) {
65         Font font = parent.getFont();
66         parentComposite = parent;
67
68         // Create a composite to hold everything together
69
Composite composite = new Composite(parent, SWT.NULL);
70         composite.setLayout(new GridLayout());
71         composite.setLayoutData(new GridData(GridData.FILL_VERTICAL));
72         composite.setFont(font);
73         composite.addDisposeListener(new DisposeListener() {
74             public void widgetDisposed(DisposeEvent e) {
75                 if (doneImage != null) {
76                     doneImage.dispose();
77                     doneImage = null;
78                 }
79                 if (currentImage != null) {
80                     currentImage.dispose();
81                     currentImage = null;
82                 }
83             }
84         });
85
86         // Add a label to identify the step list field
87
Label label = new Label(composite, SWT.LEFT);
88         label.setText(IDEWorkbenchMessages.WizardStepGroup_stepsLabel);
89         GridData data = new GridData();
90         data.verticalAlignment = SWT.TOP;
91         label.setLayoutData(data);
92         label.setFont(font);
93
94         // Table viewer of all the steps
95
stepViewer = new TableViewer(composite, SWT.SINGLE | SWT.V_SCROLL
96                 | SWT.BORDER | SWT.FULL_SELECTION);
97         data = new GridData(GridData.FILL_BOTH);
98         stepViewer.getTable().setLayoutData(data);
99         stepViewer.getTable().setFont(font);
100         stepViewer.setContentProvider(getStepProvider());
101         stepViewer.setLabelProvider(new StepLabelProvider());
102
103         if (selectionListener != null) {
104             stepViewer.addSelectionChangedListener(selectionListener);
105         }
106
107         return composite;
108     }
109
110     /**
111      * Creates an image descriptor.
112      */

113     private Image createImage(String JavaDoc iconFileName) {
114         ImageDescriptor desc =
115             IDEWorkbenchPlugin.getIDEImageDescriptor(iconFileName);
116        
117         return desc.createImage();
118     }
119
120     /**
121      * Return the image indicating a step is current
122      */

123     private Image getCurrentImage() {
124         if (currentImage == null) {
125             currentImage = createImage("elcl16/step_current.gif"); //$NON-NLS-1$
126
}
127         return currentImage;
128     }
129
130     /**
131      * Return the image indicating a step is done
132      */

133     private Image getDoneImage() {
134         if (doneImage == null) {
135             doneImage = createImage("elcl16/step_done.gif"); //$NON-NLS-1$
136
}
137         return doneImage;
138     }
139
140     /**
141      * Returns the content provider for the step viewer
142      */

143     private IContentProvider getStepProvider() {
144         return new WorkbenchContentProvider() {
145             public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
146                 if (parentElement instanceof StepRoot) {
147                     return ((StepRoot) parentElement).getSteps();
148                 } else {
149                     return null;
150                 }
151             }
152
153         };
154     }
155
156     /**
157      * Returns the steps.
158      */

159     public WizardStep[] getSteps() {
160         if (stepViewer != null) {
161             StepRoot root = (StepRoot) stepViewer.getInput();
162             if (root != null) {
163                 return root.getSteps();
164             }
165         }
166
167         return new WizardStep[0];
168     }
169
170     /**
171      * Marks the current step as being done
172      */

173     public void markStepAsDone() {
174         if (currentStep != null) {
175             currentStep.markAsDone();
176         }
177     }
178
179     /**
180      * Sets the current step being worked on. Assumes
181      * the step provided exist in the steps within the
182      * group's viewer.
183      *
184      * @param step the wizard step being worked on
185      */

186     public void setCurrentStep(WizardStep step) {
187         WizardStep oldStep = currentStep;
188         currentStep = step;
189         if (stepViewer != null) {
190             if (oldStep != null) {
191                 stepViewer.update(oldStep, null);
192             }
193             if (currentStep != null) {
194                 stepViewer.update(currentStep, null);
195             }
196
197             // Update the layout so that there is enough
198
// room for the icon now.
199
if (oldStep == null && currentStep != null) {
200                 parentComposite.layout(true);
201             }
202         }
203     }
204
205     /**
206      * Set the current listener interested when the selection
207      * state changes.
208      *
209      * @param listener The selection listener to set
210      */

211     public void setSelectionListener(ISelectionChangedListener listener) {
212         if (selectionListener != null && stepViewer != null) {
213             stepViewer.removeSelectionChangedListener(selectionListener);
214         }
215         selectionListener = listener;
216         if (selectionListener != null && stepViewer != null) {
217             stepViewer.addSelectionChangedListener(selectionListener);
218         }
219     }
220
221     /**
222      * Sets the steps to be displayed. Ignored is the
223      * method createContents has not been called yet.
224      *
225      * @param steps the collection of steps
226      */

227     public void setSteps(WizardStep[] steps) {
228         if (stepViewer != null) {
229             stepViewer.setInput(new StepRoot(steps));
230             parentComposite.layout(true);
231         }
232     }
233
234     /**
235      * Holder of all the steps within the viewer
236      */

237     private class StepRoot {
238         private WizardStep[] steps;
239
240         public StepRoot(WizardStep[] steps) {
241             super();
242             this.steps = steps;
243         }
244
245         public WizardStep[] getSteps() {
246             if (steps == null) {
247                 return new WizardStep[0];
248             } else {
249                 return steps;
250             }
251         }
252     }
253
254     /**
255      * Label provider for step table viewer
256      */

257     private class StepLabelProvider extends LabelProvider {
258         public String JavaDoc getText(Object JavaDoc element) {
259             if (element instanceof WizardStep) {
260                 WizardStep step = (WizardStep) element;
261                 return String.valueOf(step.getNumber())
262                         + ". " + step.getLabel(); //$NON-NLS-1$
263
}
264
265             return ""; //$NON-NLS-1$
266
}
267
268         public Image getImage(Object JavaDoc element) {
269             if (element instanceof WizardStep) {
270                 WizardStep step = (WizardStep) element;
271                 if (step.isDone()) {
272                     return getDoneImage();
273                 }
274                 if (step == currentStep) {
275                     return getCurrentImage();
276                 }
277             }
278
279             return null;
280         }
281     }
282 }
283
Popular Tags