1 11 package org.eclipse.ui.forms; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.custom.SashForm; 18 import org.eclipse.swt.graphics.GC; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.layout.GridData; 21 import org.eclipse.swt.layout.GridLayout; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Event; 25 import org.eclipse.swt.widgets.Listener; 26 import org.eclipse.swt.widgets.Sash; 27 import org.eclipse.ui.forms.widgets.FormToolkit; 28 import org.eclipse.ui.forms.widgets.ScrolledForm; 29 30 56 public abstract class MasterDetailsBlock { 57 62 protected DetailsPart detailsPart; 63 64 68 protected SashForm sashForm; 69 70 static final int DRAGGER_SIZE = 40; 71 72 class MDSashForm extends SashForm { 73 ArrayList sashes = new ArrayList (); 74 Listener listener = new Listener () { 75 public void handleEvent(Event e) { 76 switch (e.type) { 77 case SWT.MouseEnter: 78 e.widget.setData("hover", Boolean.TRUE); ((Control)e.widget).redraw(); 80 break; 81 case SWT.MouseExit: 82 e.widget.setData("hover", null); ((Control)e.widget).redraw(); 84 break; 85 case SWT.Paint: 86 onSashPaint(e); 87 break; 88 case SWT.Resize: 89 hookSashListeners(); 90 break; 91 } 92 } 93 }; 94 public MDSashForm(Composite parent, int style) { 95 super(parent, style); 96 } 97 98 public void layout(boolean changed) { 99 super.layout(changed); 100 hookSashListeners(); 101 } 102 103 public void layout(Control [] children) { 104 super.layout(children); 105 hookSashListeners(); 106 } 107 108 private void hookSashListeners() { 109 purgeSashes(); 110 Control [] children = getChildren(); 111 for (int i=0; i<children.length; i++) { 112 if (children[i] instanceof Sash) { 113 Sash sash = (Sash)children[i]; 114 if (sashes.contains(sash)) 115 continue; 116 sash.addListener(SWT.Paint, listener); 117 sash.addListener(SWT.MouseEnter, listener); 118 sash.addListener(SWT.MouseExit, listener); 119 sashes.add(sash); 120 } 121 } 122 } 123 private void purgeSashes() { 124 for (Iterator iter=sashes.iterator(); iter.hasNext();) { 125 Sash sash = (Sash)iter.next(); 126 if (sash.isDisposed()) 127 iter.remove(); 128 } 129 } 130 } 131 132 139 public void createContent(IManagedForm managedForm) { 140 final ScrolledForm form = managedForm.getForm(); 141 FormToolkit toolkit = managedForm.getToolkit(); 142 GridLayout layout = new GridLayout(); 143 layout.marginWidth = 0; 144 layout.marginHeight = 0; 145 form.getBody().setLayout(layout); 146 sashForm = new MDSashForm(form.getBody(), SWT.NULL); 147 sashForm.setData("form", managedForm); toolkit.adapt(sashForm, false, false); 149 sashForm.setMenu(form.getBody().getMenu()); 150 sashForm.setLayoutData(new GridData(GridData.FILL_BOTH)); 151 createMasterPart(managedForm, sashForm); 152 createDetailsPart(managedForm, sashForm); 153 hookResizeListener(); 154 createToolBarActions(managedForm); 155 form.updateToolBar(); 156 } 157 158 private void hookResizeListener() { 159 Listener listener = ((MDSashForm)sashForm).listener; 160 Control [] children = sashForm.getChildren(); 161 for (int i=0; i<children.length; i++) { 162 if (children[i] instanceof Sash) continue; 163 children[i].addListener(SWT.Resize, listener); 164 } 165 } 166 167 176 protected abstract void createMasterPart(IManagedForm managedForm, 177 Composite parent); 178 179 187 protected abstract void registerPages(DetailsPart detailsPart); 188 189 196 protected abstract void createToolBarActions(IManagedForm managedForm); 197 198 private void createDetailsPart(final IManagedForm mform, Composite parent) { 199 detailsPart = new DetailsPart(mform, parent, SWT.NULL); 200 mform.addPart(detailsPart); 201 registerPages(detailsPart); 202 } 203 204 private void onSashPaint(Event e) { 205 Sash sash = (Sash)e.widget; 206 IManagedForm form = (IManagedForm)sash.getParent().getData("form"); FormColors colors = form.getToolkit().getColors(); 208 boolean vertical = (sash.getStyle() & SWT.VERTICAL)!=0; 209 GC gc = e.gc; 210 Boolean hover = (Boolean )sash.getData("hover"); gc.setBackground(colors.getColor(IFormColors.TB_BG)); 212 gc.setForeground(colors.getColor(IFormColors.TB_BORDER)); 213 Point size = sash.getSize(); 214 if (vertical) { 215 if (hover!=null) 216 gc.fillRectangle(0, 0, size.x, size.y); 217 } 220 else { 221 if (hover!=null) 222 gc.fillRectangle(0, 0, size.x, size.y); 223 } 226 } 227 } 228 | Popular Tags |