1 19 20 package org.netbeans.core.windows.view.ui; 21 22 import java.awt.Color ; 23 import java.awt.Dimension ; 24 import java.awt.Graphics ; 25 import java.awt.Point ; 26 import java.awt.Rectangle ; 27 import java.util.Locale ; 28 import javax.accessibility.Accessible ; 29 import javax.accessibility.AccessibleContext ; 30 import javax.accessibility.AccessibleRole ; 31 import javax.accessibility.AccessibleState ; 32 import javax.accessibility.AccessibleStateSet ; 33 import javax.accessibility.AccessibleValue ; 34 35 36 39 public class MultiSplitDivider implements Accessible { 40 41 MultiSplitPane splitPane; 42 Rectangle rect = new Rectangle (); 43 MultiSplitCell first; 44 MultiSplitCell second; 45 46 Point currentDragLocation; 47 int dragMin; 48 int dragMax; 49 int cursorPositionCompensation; 50 51 private AccessibleContext accessibleContext; 52 53 public MultiSplitDivider( MultiSplitPane parent, MultiSplitCell first, MultiSplitCell second ) { 54 assert null != parent; 55 assert null != first; 56 assert null != second; 57 this.splitPane = parent; 58 this.first = first; 59 this.second = second; 60 61 reshape(); 62 } 63 64 boolean isHorizontal() { 65 return splitPane.isHorizontalSplit(); 66 } 67 68 boolean isVertical() { 69 return splitPane.isVerticalSplit(); 70 } 71 72 int getDividerSize() { 73 return splitPane.getDividerSize(); 74 } 75 76 boolean containsPoint( Point p ) { 77 return rect.contains( p ); 78 } 79 80 void paint( Graphics g ) { 81 84 if( null != currentDragLocation ) { 85 Color oldColor = g.getColor(); 86 g.setColor( Color.BLACK ); 87 if( isHorizontal() ) { 88 if( currentDragLocation.x != rect.x ) { 89 g.fillRect( currentDragLocation.x, rect.y, rect.width, rect.height ); 90 } 91 } else { 92 if( currentDragLocation.y != rect.y ) { 93 g.fillRect( rect.x, currentDragLocation.y, rect.width, rect.height ); 94 } 95 } 96 g.setColor( oldColor ); 97 } 98 } 99 100 void startDragging( Point p ) { 101 currentDragLocation = new Point ( rect.x, rect.y ); 102 103 if( isHorizontal() ) 104 cursorPositionCompensation = p.x - rect.x; 105 else 106 cursorPositionCompensation = p.y - rect.y; 107 108 initDragMinMax(); 109 } 110 111 void dragTo( Point p ) { 112 if( isHorizontal() ) { 113 if( p.x < dragMin ) 114 p.x = dragMin; 115 if( p.x > dragMax ) 116 p.x = dragMax; 117 } else { 118 if( p.y < dragMin ) 119 p.y = dragMin; 120 if( p.y > dragMax ) 121 p.y = dragMax; 122 } 123 124 Point prevDragLocation = currentDragLocation; 125 currentDragLocation = p; 126 127 repaintSplitPane( prevDragLocation ); 128 repaintSplitPane( currentDragLocation ); 129 } 130 131 private void repaintSplitPane( Point location ) { 132 if( isHorizontal() ) { 133 splitPane.repaint( location.x, rect.y, rect.width, rect.height ); 134 } else { 135 splitPane.repaint( rect.x, location.y, rect.width, rect.height ); 136 } 137 } 138 139 void finishDraggingTo( Point p ) { 140 if( isHorizontal() ) { 141 p.x -= cursorPositionCompensation; 142 if( p.x < dragMin ) 143 p.x = dragMin; 144 if( p.x > dragMax ) 145 p.x = dragMax; 146 147 if( p.x == rect.x ) { 148 return; 150 } 151 } else { 152 p.y -= cursorPositionCompensation; 153 if( p.y < dragMin ) 154 p.y = dragMin; 155 if( p.y > dragMax ) 156 p.y = dragMax; 157 158 if( p.y == rect.y ) { 159 return; 161 } 162 } 163 currentDragLocation = null; 164 165 int dividerSize = getDividerSize(); 166 167 if( isHorizontal() ) { 168 int delta = p.x - rect.x; 169 int x = first.getLocation(); 170 int y = 0; 171 int width = first.getSize() + delta; 172 int height = rect.height; 173 first.layout( x, y, width, height ); 174 175 x = second.getLocation() + delta; 176 width = second.getSize() - delta; 177 second.layout( x, y, width, height ); 178 179 rect.x = p.x; 180 } else { 181 int delta = p.y - rect.y; 182 int x = 0; 183 int y = first.getLocation(); 184 int width = rect.width; 185 int height = first.getSize() + delta; 186 first.layout( x, y, width, height ); 187 188 y = second.getLocation() + delta; 189 height = second.getSize() - delta; 190 second.layout( x, y, width, height ); 191 192 rect.y = p.y; 193 } 194 splitPane.splitterMoved(); } 196 197 private void initDragMinMax() { 198 int dividerSize = getDividerSize(); 199 int firstSize = first.getSize(); 200 int secondSize = second.getSize(); 201 int firstMinSize = first.getMinimumSize(); 202 int secondMinSize = second.getMinimumSize(); 203 204 if( isHorizontal() ) { 205 dragMin = rect.x; 206 dragMax = rect.x; 207 } else { 208 dragMin = rect.y; 209 dragMax = rect.y; 210 } 211 212 if( firstSize >= firstMinSize ) { 213 dragMin -= firstSize-firstMinSize; 214 } 215 if( secondSize >= secondMinSize ) { 216 dragMax += secondSize-secondMinSize; 217 } 218 } 219 220 void reshape() { 221 Dimension d = splitPane.getSize(); 222 int location = second.getLocation(); 223 224 if( isHorizontal() ) { 225 rect.x = location-getDividerSize(); 226 rect.y = 0; 227 rect.width = getDividerSize(); 228 rect.height = d.height; 229 } else { 230 rect.x = 0; 231 rect.y = location-getDividerSize(); 232 rect.width = d.width; 233 rect.height = getDividerSize(); 234 } 235 } 236 237 240 public AccessibleContext getAccessibleContext() { 241 if( null == accessibleContext ) { 242 accessibleContext = new AccessibleMultiSplitDivider(); 243 } 244 return accessibleContext; 245 } 246 247 protected class AccessibleMultiSplitDivider extends AccessibleContext 248 implements AccessibleValue { 249 250 public AccessibleMultiSplitDivider() { 251 setAccessibleParent( splitPane ); 252 } 253 254 public Accessible getAccessibleChild(int i) { 255 return null; 256 } 257 258 public int getAccessibleChildrenCount() { 259 return 0; 260 } 261 262 public int getAccessibleIndexInParent() { 263 return splitPane.getDividerAccessibleIndex( MultiSplitDivider.this ); 264 } 265 266 public AccessibleRole getAccessibleRole() { 267 return AccessibleRole.SPLIT_PANE; 268 } 269 270 public AccessibleStateSet getAccessibleStateSet() { 271 AccessibleStateSet stateSet = new AccessibleStateSet (); 272 if( isHorizontal() ) { 273 stateSet.add( AccessibleState.HORIZONTAL ); 274 } else { 275 stateSet.add( AccessibleState.VERTICAL ); 276 } 277 return stateSet; 278 } 279 280 public Locale getLocale() throws java.awt.IllegalComponentStateException { 281 return Locale.getDefault(); 282 } 283 284 public boolean setCurrentAccessibleValue(Number n) { 285 initDragMinMax(); 286 int value = n.intValue(); 287 if( value < dragMin || value > dragMax ) { 288 return false; 289 } 290 if( isHorizontal() ) { 291 finishDraggingTo( new Point ( value, 0 ) ); 292 } else { 293 finishDraggingTo( new Point ( 0, value ) ); 294 } 295 return true; 296 } 297 298 public Number getMinimumAccessibleValue() { 299 initDragMinMax(); 300 return Integer.valueOf( dragMin ); 301 } 302 303 public Number getMaximumAccessibleValue() { 304 initDragMinMax(); 305 return Integer.valueOf( dragMax ); 306 } 307 308 public Number getCurrentAccessibleValue() { 309 if( isHorizontal() ) 310 return Integer.valueOf( rect.x ); 311 else 312 return Integer.valueOf( rect.y ); 313 } 314 315 public AccessibleValue getAccessibleValue() { 316 return this; 317 } 318 } } 320 | Popular Tags |