KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > ConfigurationManagerWindow


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  *******************************************************************************/

11 package org.eclipse.update.internal.ui;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.jobs.IJobChangeEvent;
17 import org.eclipse.core.runtime.jobs.IJobChangeListener;
18 import org.eclipse.core.runtime.jobs.Job;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.action.IMenuManager;
22 import org.eclipse.jface.action.MenuManager;
23 import org.eclipse.jface.action.Separator;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26 import org.eclipse.jface.window.ApplicationWindow;
27 import org.eclipse.osgi.util.NLS;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.graphics.Image;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Control;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.update.internal.ui.views.ConfigurationView;
37 import org.eclipse.update.ui.UpdateJob;
38
39 /**
40  * Configuration Manager window.
41  */

42 public class ConfigurationManagerWindow extends ApplicationWindow {
43     private ConfigurationView view;
44
45     private GlobalAction propertiesAction;
46
47     private IAction propertiesActionHandler;
48
49     private IJobChangeListener jobListener;
50     
51     private Hashtable JavaDoc jobNames;
52
53     class GlobalAction extends Action implements IPropertyChangeListener {
54         private IAction handler;
55
56         public GlobalAction() {
57         }
58
59         public void setActionHandler(IAction action) {
60             if (handler != null) {
61                 handler.removePropertyChangeListener(this);
62                 handler = null;
63             }
64             if (action != null) {
65                 this.handler = action;
66                 action.addPropertyChangeListener(this);
67             }
68             if (handler != null) {
69                 setEnabled(handler.isEnabled());
70                 setChecked(handler.isChecked());
71             }
72         }
73
74         public void propertyChange(PropertyChangeEvent event) {
75             if (event.getProperty().equals(Action.ENABLED)) {
76                 Boolean JavaDoc bool = (Boolean JavaDoc) event.getNewValue();
77                 setEnabled(bool.booleanValue());
78             } else if (event.getProperty().equals(Action.CHECKED)) {
79                 Boolean JavaDoc bool = (Boolean JavaDoc) event.getNewValue();
80                 setChecked(bool.booleanValue());
81             }
82         }
83
84         public void run() {
85             if (handler != null)
86                 handler.run();
87         }
88     }
89
90     /**
91      * @param parentShell
92      */

93     public ConfigurationManagerWindow(Shell parentShell) {
94         super(parentShell);
95         setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL);
96         // Setup window.
97
addMenuBar();
98         addActions();
99         addToolBar(SWT.FLAT);
100         addStatusLine();
101     }
102     
103     public boolean isProgressCanceled() {
104         return getStatusLineManager().getProgressMonitor().isCanceled();
105     }
106
107     private void addActions() {
108         IMenuManager menuBar = getMenuBarManager();
109         IMenuManager fileMenu = new MenuManager(
110                 UpdateUIMessages.ConfigurationManagerWindow_fileMenu);
111         menuBar.add(fileMenu);
112
113         propertiesAction = new GlobalAction();
114         propertiesAction
115                 .setText(UpdateUIMessages.ConfigurationManagerWindow_properties);
116         propertiesAction.setEnabled(false);
117
118         fileMenu.add(propertiesAction);
119         fileMenu.add(new Separator());
120
121         Action closeAction = new Action() {
122             public void run() {
123                 close();
124             }
125         };
126         closeAction.setText(UpdateUIMessages.ConfigurationManagerWindow_close);
127         fileMenu.add(closeAction);
128     }
129
130     private void hookGlobalActions() {
131         if (propertiesActionHandler != null)
132             propertiesAction.setActionHandler(propertiesActionHandler);
133     }
134
135     protected Control createContents(Composite parent) {
136         view = new ConfigurationView(this);
137         Composite container = new Composite(parent, SWT.NULL);
138         GridLayout layout = new GridLayout();
139         layout.marginWidth = layout.marginHeight = 0;
140         layout.verticalSpacing = 0;
141         container.setLayout(layout);
142
143         addSeparator(container);
144         GridData gd;
145
146         view.createPartControl(container);
147         Control viewControl = view.getControl();
148         gd = new GridData(GridData.FILL_BOTH);
149         viewControl.setLayoutData(gd);
150
151         addSeparator(container);
152
153         hookGlobalActions();
154
155         updateActionBars();
156
157         UpdateLabelProvider provider = UpdateUI.getDefault().getLabelProvider();
158         getShell().setImage(provider.get(UpdateUIImages.DESC_CONFIGS_VIEW, 0));
159
160         return container;
161     }
162
163     public void updateStatusLine(String JavaDoc message, Image image) {
164         getStatusLineManager().setMessage(image, message);
165         getStatusLineManager().update(true);
166     }
167
168     public void trackUpdateJob(Job job, String JavaDoc name) {
169         if (jobListener == null) {
170             jobNames = new Hashtable JavaDoc();
171             jobListener = new IJobChangeListener() {
172                 public void aboutToRun(IJobChangeEvent event) {
173                 }
174
175                 public void awake(IJobChangeEvent event) {
176                 }
177
178                 public void done(IJobChangeEvent event) {
179                     Job job = event.getJob();
180                     if (job.belongsTo(UpdateJob.FAMILY)) {
181                         Job [] remaining = Job.getJobManager().find(UpdateJob.FAMILY);
182                         updateProgress(false, remaining);
183                         jobNames.remove(job);
184                     }
185                 }
186
187                 public void running(IJobChangeEvent event) {
188                     Job job = event.getJob();
189                     if (job.belongsTo(UpdateJob.FAMILY)) {
190                         Job [] existing = Job.getJobManager().find(UpdateJob.FAMILY);
191                         updateProgress(true, existing);
192                     }
193                 }
194
195                 public void scheduled(IJobChangeEvent event) {
196                 }
197
198                 public void sleeping(IJobChangeEvent event) {
199                 }
200             };
201             Job.getJobManager().addJobChangeListener(jobListener);
202         }
203         jobNames.put(job, name);
204     }
205
206     private void updateProgress(final boolean begin, final Job[] jobs) {
207         getShell().getDisplay().asyncExec(new Runnable JavaDoc() {
208             public void run() {
209                 IProgressMonitor monitor = getStatusLineManager()
210                         .getProgressMonitor();
211                 if (begin) {
212                     if (jobs.length == 1)
213                         monitor.beginTask("", IProgressMonitor.UNKNOWN); //$NON-NLS-1$
214
updateTaskName(monitor, jobs);
215                     getStatusLineManager().setCancelEnabled(true);
216                 } else {
217                     if (jobs.length == 0) {
218                         getStatusLineManager().setCancelEnabled(false);
219                         monitor.done();
220                     }
221                     else
222                         updateTaskName(monitor, jobs);
223                 }
224                 getStatusLineManager().update(true);
225             }
226         });
227     }
228
229     private void updateTaskName(IProgressMonitor monitor, Job [] jobs) {
230         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
231         for (int i=0; i<jobs.length; i++) {
232             String JavaDoc name = (String JavaDoc)jobNames.get(jobs[i]);
233             if (name!=null) {
234                 if (buf.length()>0)
235                     buf.append(", "); //$NON-NLS-1$
236
buf.append(name);
237             }
238         }
239         monitor.subTask(NLS.bind(
240                 UpdateUIMessages.ConfigurationManagerWindow_searchTaskName,
241                 buf.toString()));
242     }
243
244     private void addSeparator(Composite parent) {
245         GridData gd;
246         Label separator = new Label(parent, SWT.SEPARATOR | SWT.HORIZONTAL);
247         gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
248         gd.heightHint = 1;
249         separator.setLayoutData(gd);
250     }
251
252     private void updateActionBars() {
253         getMenuBarManager().updateAll(false);
254         getToolBarManager().update(false);
255         getStatusLineManager().update(false);
256     }
257
258     /*
259      * (non-Javadoc)
260      *
261      * @see org.eclipse.jface.window.Window#close()
262      */

263     public boolean close() {
264         if (jobListener != null)
265             Job.getJobManager().removeJobChangeListener(jobListener);
266         if (view != null)
267             view.dispose();
268         return super.close();
269     }
270
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.eclipse.jface.window.Window#create()
275      */

276     public void create() {
277         super.create();
278         // set the title
279
getShell().setText(UpdateUIMessages.ConfigurationManagerAction_title);
280         getShell().setSize(800, 600);
281     }
282
283     /*
284      * (non-Javadoc)
285      *
286      * @see org.eclipse.jface.window.Window#open()
287      */

288     public int open() {
289         // update action bars
290
updateActionBars();
291         return super.open();
292     }
293
294     public void setPropertiesActionHandler(IAction handler) {
295         propertiesActionHandler = handler;
296     }
297 }
298
Popular Tags