1 11 package org.eclipse.ui.forms; 12 13 import java.util.Enumeration ; 14 import java.util.Hashtable ; 15 import java.util.Iterator ; 16 17 import org.eclipse.jface.viewers.*; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.custom.BusyIndicator; 20 import org.eclipse.swt.widgets.*; 21 import org.eclipse.ui.forms.widgets.ScrolledPageBook; 22 38 public final class DetailsPart implements IFormPart, IPartSelectionListener { 39 private IManagedForm managedForm; 40 private ScrolledPageBook pageBook; 41 private IFormPart masterPart; 42 private IStructuredSelection currentSelection; 43 private Hashtable pages; 44 private IDetailsPageProvider pageProvider; 45 private int pageLimit=Integer.MAX_VALUE; 46 47 private static class PageBag { 48 private static int counter; 49 private int ticket; 50 private IDetailsPage page; 51 private boolean fixed; 52 53 public PageBag(IDetailsPage page, boolean fixed) { 54 this.page= page; 55 this.fixed = fixed; 56 this.ticket = ++counter; 57 } 58 public int getTicket() { 59 return ticket; 60 } 61 public IDetailsPage getPage() { 62 return page; 63 } 64 public void dispose() { 65 page.dispose(); 66 page=null; 67 } 68 public boolean isDisposed() { 69 return page==null; 70 } 71 public boolean isFixed() { 72 return fixed; 73 } 74 public static int getCurrentTicket() { 75 return counter; 76 } 77 } 78 83 public DetailsPart(IManagedForm mform, ScrolledPageBook pageBook) { 84 this.pageBook = pageBook; 85 pages = new Hashtable (); 86 initialize(mform); 87 } 88 95 public DetailsPart(IManagedForm mform, Composite parent, int style) { 96 this(mform, mform.getToolkit().createPageBook(parent, style|SWT.V_SCROLL|SWT.H_SCROLL)); 97 } 98 105 public void registerPage(Object objectClass, IDetailsPage page) { 106 registerPage(objectClass, page, true); 107 } 108 109 private void registerPage(Object objectClass, IDetailsPage page, boolean fixed) { 110 pages.put(objectClass, new PageBag(page, fixed)); 111 page.initialize(managedForm); 112 } 113 118 public void setPageProvider(IDetailsPageProvider provider) { 119 this.pageProvider = provider; 120 } 121 126 public void commit(boolean onSave) { 127 IDetailsPage page = getCurrentPage(); 128 if (page != null) 129 page.commit(onSave); 130 } 131 135 public IDetailsPage getCurrentPage() { 136 Control control = pageBook.getCurrentPage(); 137 if (control != null) { 138 Object data = control.getData(); 139 if (data instanceof IDetailsPage) 140 return (IDetailsPage) data; 141 } 142 return null; 143 } 144 149 public void dispose() { 150 for (Enumeration enm = pages.elements(); enm.hasMoreElements();) { 151 PageBag pageBag = (PageBag) enm.nextElement(); 152 pageBag.dispose(); 153 } 154 } 155 160 public void initialize(IManagedForm form) { 161 this.managedForm = form; 162 } 163 167 public boolean isDirty() { 168 IDetailsPage page = getCurrentPage(); 169 if (page != null) 170 return page.isDirty(); 171 return false; 172 } 173 177 public boolean isStale() { 178 IDetailsPage page = getCurrentPage(); 179 if (page != null) 180 return page.isStale(); 181 return false; 182 } 183 184 187 public void refresh() { 188 IDetailsPage page = getCurrentPage(); 189 if (page != null) 190 page.refresh(); 191 } 192 195 public void setFocus() { 196 IDetailsPage page = getCurrentPage(); 197 if (page != null) 198 page.setFocus(); 199 } 200 205 public boolean setFormInput(Object input) { 206 return false; 207 } 208 214 public void selectionChanged(IFormPart part, ISelection selection) { 215 this.masterPart = part; 216 if (currentSelection != null) { 217 } 218 if (selection instanceof IStructuredSelection) 219 currentSelection = (IStructuredSelection) selection; 220 else 221 currentSelection = null; 222 update(); 223 } 224 private void update() { 225 Object key = null; 226 if (currentSelection != null) { 227 for (Iterator iter = currentSelection.iterator(); iter.hasNext();) { 228 Object obj = iter.next(); 229 if (key == null) 230 key = getKey(obj); 231 else if (getKey(obj).equals(key) == false) { 232 key = null; 233 break; 234 } 235 } 236 } 237 showPage(key); 238 } 239 private Object getKey(Object object) { 240 if (pageProvider!=null) { 241 Object key = pageProvider.getPageKey(object); 242 if (key!=null) 243 return key; 244 } 245 return object.getClass(); 246 } 247 private void showPage(final Object key) { 248 checkLimit(); 249 final IDetailsPage oldPage = getCurrentPage(); 250 if (key != null) { 251 PageBag pageBag = (PageBag)pages.get(key); 252 IDetailsPage page = pageBag!=null?pageBag.getPage():null; 253 if (page==null) { 254 if (pageProvider!=null) { 256 page = pageProvider.getPage(key); 257 if (page!=null) { 258 registerPage(key, page, false); 259 } 260 } 261 } 262 if (page != null) { 263 final IDetailsPage fpage = page; 264 BusyIndicator.showWhile(pageBook.getDisplay(), new Runnable () { 265 public void run() { 266 if (!pageBook.hasPage(key)) { 267 Composite parent = pageBook.createPage(key); 268 fpage.createContents(parent); 269 parent.setData(fpage); 270 } 271 if (oldPage!=null && oldPage.isDirty()) 273 oldPage.commit(false); 274 if (fpage.isStale()) 276 fpage.refresh(); 277 fpage.selectionChanged(masterPart, currentSelection); 278 pageBook.showPage(key); 279 } 280 }); 281 return; 282 } 283 } 284 if (oldPage!=null && oldPage.isDirty()) 287 oldPage.commit(false); 288 pageBook.showEmptyPage(); 289 } 290 private void checkLimit() { 291 if (pages.size() <= getPageLimit()) return; 292 int currentTicket = PageBag.getCurrentTicket(); 294 int cutoffTicket = currentTicket - getPageLimit(); 295 for (Enumeration enm=pages.keys(); enm.hasMoreElements();) { 296 Object key = enm.nextElement(); 297 PageBag pageBag = (PageBag)pages.get(key); 298 if (pageBag.getTicket()<=cutoffTicket) { 299 if (!pageBag.isFixed() && !pageBag.getPage().equals(getCurrentPage())) { 301 pageBag.dispose(); 303 pages.remove(key); 304 pageBook.removePage(key, false); 305 } 306 } 307 } 308 } 309 319 public int getPageLimit() { 320 return pageLimit; 321 } 322 328 public void setPageLimit(int pageLimit) { 329 this.pageLimit = pageLimit; 330 checkLimit(); 331 } 332 } 333 | Popular Tags |