1 11 package org.eclipse.ui.forms; 12 13 import java.util.Vector ; 14 import org.eclipse.jface.viewers.ISelection; 15 import org.eclipse.swt.widgets.Composite; 16 import org.eclipse.ui.forms.widgets.*; 17 import org.eclipse.ui.internal.forms.MessageManager; 18 19 37 public class ManagedForm implements IManagedForm { 38 private Object input; 39 40 private ScrolledForm form; 41 42 private FormToolkit toolkit; 43 44 private Object container; 45 46 private boolean ownsToolkit; 47 48 private boolean initialized; 49 50 private MessageManager messageManager; 51 52 private Vector parts = new Vector (); 53 54 61 public ManagedForm(Composite parent) { 62 toolkit = new FormToolkit(parent.getDisplay()); 63 ownsToolkit = true; 64 form = toolkit.createScrolledForm(parent); 65 } 66 67 73 public ManagedForm(FormToolkit toolkit, ScrolledForm form) { 74 this.form = form; 75 this.toolkit = toolkit; 76 } 77 78 83 public void addPart(IFormPart part) { 84 parts.add(part); 85 part.initialize(this); 86 } 87 88 93 public void removePart(IFormPart part) { 94 parts.remove(part); 95 } 96 97 102 public IFormPart[] getParts() { 103 return (IFormPart[]) parts.toArray(new IFormPart[parts.size()]); 104 } 105 106 111 public FormToolkit getToolkit() { 112 return toolkit; 113 } 114 115 120 public ScrolledForm getForm() { 121 return form; 122 } 123 124 129 public void reflow(boolean changed) { 130 form.reflow(changed); 131 } 132 133 143 public void fireSelectionChanged(IFormPart part, ISelection selection) { 144 for (int i = 0; i < parts.size(); i++) { 145 IFormPart cpart = (IFormPart) parts.get(i); 146 if (part.equals(cpart)) 147 continue; 148 if (cpart instanceof IPartSelectionListener) { 149 ((IPartSelectionListener) cpart).selectionChanged(part, 150 selection); 151 } 152 } 153 } 154 155 159 public void initialize() { 160 if (initialized) 161 return; 162 for (int i = 0; i < parts.size(); i++) { 163 IFormPart part = (IFormPart) parts.get(i); 164 part.initialize(this); 165 } 166 initialized = true; 167 } 168 169 172 public void dispose() { 173 for (int i = 0; i < parts.size(); i++) { 174 IFormPart part = (IFormPart) parts.get(i); 175 part.dispose(); 176 } 177 if (ownsToolkit) { 178 toolkit.dispose(); 179 } 180 } 181 182 188 public void refresh() { 189 Thread t = Thread.currentThread(); 190 Thread dt = toolkit.getColors().getDisplay().getThread(); 191 if (t.equals(dt)) 192 doRefresh(); 193 else { 194 toolkit.getColors().getDisplay().asyncExec(new Runnable () { 195 public void run() { 196 doRefresh(); 197 } 198 }); 199 } 200 } 201 202 private void doRefresh() { 203 int nrefreshed = 0; 204 for (int i = 0; i < parts.size(); i++) { 205 IFormPart part = (IFormPart) parts.get(i); 206 if (part.isStale()) { 207 part.refresh(); 208 nrefreshed++; 209 } 210 } 211 if (nrefreshed > 0) 212 form.reflow(true); 213 } 214 215 220 public void commit(boolean onSave) { 221 for (int i = 0; i < parts.size(); i++) { 222 IFormPart part = (IFormPart) parts.get(i); 223 if (part.isDirty()) 224 part.commit(onSave); 225 } 226 } 227 228 233 public boolean setInput(Object input) { 234 boolean pageResult = false; 235 236 this.input = input; 237 for (int i = 0; i < parts.size(); i++) { 238 IFormPart part = (IFormPart) parts.get(i); 239 boolean result = part.setFormInput(input); 240 if (result) 241 pageResult = true; 242 } 243 return pageResult; 244 } 245 246 251 public Object getInput() { 252 return input; 253 } 254 255 258 public void setFocus() { 259 if (parts.size() > 0) { 260 IFormPart part = (IFormPart) parts.get(0); 261 part.setFocus(); 262 } 263 } 264 265 270 public boolean isDirty() { 271 for (int i = 0; i < parts.size(); i++) { 272 IFormPart part = (IFormPart) parts.get(i); 273 if (part.isDirty()) 274 return true; 275 } 276 return false; 277 } 278 279 284 public boolean isStale() { 285 for (int i = 0; i < parts.size(); i++) { 286 IFormPart part = (IFormPart) parts.get(i); 287 if (part.isStale()) 288 return true; 289 } 290 return false; 291 } 292 293 298 public void dirtyStateChanged() { 299 } 300 301 306 public void staleStateChanged() { 307 } 308 309 314 public Object getContainer() { 315 return container; 316 } 317 318 323 public void setContainer(Object container) { 324 this.container = container; 325 } 326 327 330 public IMessageManager getMessageManager() { 331 if (messageManager == null) 332 messageManager = new MessageManager(form); 333 return messageManager; 334 } 335 } 336 | Popular Tags |