1 11 package org.eclipse.update.internal.ui; 12 13 import java.util.Hashtable ; 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 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 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 bool = (Boolean ) event.getNewValue(); 77 setEnabled(bool.booleanValue()); 78 } else if (event.getProperty().equals(Action.CHECKED)) { 79 Boolean bool = (Boolean ) event.getNewValue(); 80 setChecked(bool.booleanValue()); 81 } 82 } 83 84 public void run() { 85 if (handler != null) 86 handler.run(); 87 } 88 } 89 90 93 public ConfigurationManagerWindow(Shell parentShell) { 94 super(parentShell); 95 setShellStyle(getShellStyle() | SWT.APPLICATION_MODAL); 96 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 message, Image image) { 164 getStatusLineManager().setMessage(image, message); 165 getStatusLineManager().update(true); 166 } 167 168 public void trackUpdateJob(Job job, String name) { 169 if (jobListener == null) { 170 jobNames = new Hashtable (); 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 () { 208 public void run() { 209 IProgressMonitor monitor = getStatusLineManager() 210 .getProgressMonitor(); 211 if (begin) { 212 if (jobs.length == 1) 213 monitor.beginTask("", IProgressMonitor.UNKNOWN); 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 buf = new StringBuffer (); 231 for (int i=0; i<jobs.length; i++) { 232 String name = (String )jobNames.get(jobs[i]); 233 if (name!=null) { 234 if (buf.length()>0) 235 buf.append(", "); 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 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 276 public void create() { 277 super.create(); 278 getShell().setText(UpdateUIMessages.ConfigurationManagerAction_title); 280 getShell().setSize(800, 600); 281 } 282 283 288 public int open() { 289 updateActionBars(); 291 return super.open(); 292 } 293 294 public void setPropertiesActionHandler(IAction handler) { 295 propertiesActionHandler = handler; 296 } 297 } 298 | Popular Tags |