1 15 package org.eclipse.ui.internal; 16 17 import org.eclipse.jface.util.Geometry; 18 import org.eclipse.swt.SWT; 19 import org.eclipse.swt.events.SelectionAdapter; 20 import org.eclipse.swt.events.SelectionEvent; 21 import org.eclipse.swt.events.SelectionListener; 22 import org.eclipse.swt.graphics.Rectangle; 23 import org.eclipse.swt.widgets.Composite; 24 import org.eclipse.swt.widgets.Control; 25 import org.eclipse.swt.widgets.Sash; 26 27 class LayoutPartSash extends LayoutPart { 28 29 private Sash sash; 30 private boolean enabled = false; 31 32 private PartSashContainer rootContainer; 33 34 private int style; 35 36 private LayoutPartSash preLimit; 37 38 private LayoutPartSash postLimit; 39 40 SelectionListener selectionListener; 41 42 private int left = 300, right = 300; 43 44 private Rectangle bounds = new Rectangle(0,0,0,0); 45 46 50 private boolean isVisible; 51 52 LayoutPartSash(PartSashContainer rootContainer, int style) { 53 super(null); 54 this.style = style; 55 this.rootContainer = rootContainer; 56 57 selectionListener = new SelectionAdapter() { 58 public void widgetSelected(SelectionEvent e) { 59 checkDragLimit(e); 60 61 if (e.detail != SWT.DRAG) { 62 LayoutPartSash.this.widgetSelected(e.x, e.y, e.width, 63 e.height); 64 } 65 } 66 }; 67 } 68 69 private void checkDragLimit(SelectionEvent event) { 71 LayoutTree root = rootContainer.getLayoutTree(); 72 LayoutTreeNode node = root.findSash(this); 73 Rectangle nodeBounds = node.getBounds(); 74 Rectangle eventRect = new Rectangle(event.x, event.y, event.width, event.height); 75 76 boolean vertical = (style == SWT.VERTICAL); 77 78 if (!vertical) { 81 Geometry.flipXY(nodeBounds); 82 Geometry.flipXY(eventRect); 83 } 84 85 int eventX = eventRect.x; 86 int left = Math.max(0, eventX - nodeBounds.x); 87 left = Math.min(left, nodeBounds.width - LayoutTreeNode.SASH_WIDTH); 88 int right = nodeBounds.width - left - LayoutTreeNode.SASH_WIDTH; 89 90 LayoutTreeNode.ChildSizes sizes = node.computeChildSizes(nodeBounds.width, nodeBounds.height, left, right, nodeBounds.width); 91 92 eventRect.x = nodeBounds.x + sizes.left; 93 94 if (!vertical) { 96 Geometry.flipXY(eventRect); 97 } 98 99 event.x = eventRect.x; 100 event.y = eventRect.y; 101 } 102 103 107 public void createControl(Composite parent) { 108 if (isVisible) { 110 doCreateControl(); 111 } 112 } 113 114 119 private void doCreateControl() { 120 if (sash == null) { 121 sash = new Sash(this.rootContainer.getParent(), style | SWT.SMOOTH); 122 sash.addSelectionListener(selectionListener); 123 sash.setEnabled(enabled); 124 sash.setBounds(bounds); 125 } 126 } 127 128 public void setBounds(Rectangle r) { 129 super.setBounds(r); 130 131 bounds = r; 132 } 133 134 138 public void setVisible(boolean visible) { 139 if (visible == isVisible) { 140 return; 141 } 142 143 if (visible) { 144 doCreateControl(); 145 } else { 146 dispose(); 147 } 148 149 super.setVisible(visible); 150 151 isVisible = visible; 152 } 153 154 public boolean isVisible() { 155 return isVisible; 156 } 157 158 161 public void dispose() { 162 163 if (sash != null) { 164 bounds = sash.getBounds(); 165 sash.dispose(); 166 } 167 sash = null; 168 } 169 170 173 public Rectangle getBounds() { 174 if (sash == null) { 175 return bounds; 176 } 177 178 return sash.getBounds(); 179 } 180 181 184 public Control getControl() { 185 return sash; 186 } 187 188 191 public String getID() { 192 return null; 193 } 194 195 LayoutPartSash getPostLimit() { 196 return postLimit; 197 } 198 199 LayoutPartSash getPreLimit() { 200 return preLimit; 201 } 202 203 int getLeft() { 204 return left; 205 } 206 207 int getRight() { 208 return right; 209 } 210 211 boolean isHorizontal() { 212 return ((style & SWT.HORIZONTAL) == SWT.HORIZONTAL); 213 } 214 215 boolean isVertical() { 216 return ((style & SWT.VERTICAL) == SWT.VERTICAL); 217 } 218 219 void setPostLimit(LayoutPartSash newPostLimit) { 220 postLimit = newPostLimit; 221 } 222 223 void setPreLimit(LayoutPartSash newPreLimit) { 224 preLimit = newPreLimit; 225 } 226 227 void setRatio(float newRatio) { 228 int total = left + right; 229 int newLeft = (int) (total * newRatio); 230 setSizes(newLeft, total - newLeft); 231 } 232 233 void setSizes(int left, int right) { 234 if (left < 0 || right < 0) { 235 return; 236 } 237 238 if (left == this.left && right == this.right) { 239 return; 240 } 241 242 this.left = left; 243 this.right = right; 244 245 flushCache(); 246 } 247 248 private void flushCache() { 249 LayoutTree root = rootContainer.getLayoutTree(); 250 251 if (root != null) { 252 LayoutTreeNode node = root.findSash(this); 253 if (node != null) { 254 node.flushCache(); 255 } 256 } 257 } 258 259 private void widgetSelected(int x, int y, int width, int height) { 260 if (!enabled) { 261 return; 262 } 263 264 LayoutTree root = rootContainer.getLayoutTree(); 265 LayoutTreeNode node = root.findSash(this); 266 Rectangle nodeBounds = node.getBounds(); 267 x -= nodeBounds.x; 269 y -= nodeBounds.y; 270 if (style == SWT.VERTICAL) { 271 setSizes(x, nodeBounds.width - x - LayoutTreeNode.SASH_WIDTH); 272 } else { 273 setSizes(y, nodeBounds.height - y - LayoutTreeNode.SASH_WIDTH); 274 } 275 276 node.setBounds(nodeBounds); 277 } 278 279 283 public void setEnabled(boolean resizable) { 284 this.enabled = resizable; 285 if (sash != null) { 286 sash.setEnabled(enabled); 287 } 288 } 289 290 } 291 | Popular Tags |