1 19 20 package org.netbeans.core.windows.view.ui; 21 22 import java.awt.Color ; 23 import java.awt.Component ; 24 import java.awt.Cursor ; 25 import java.awt.Dimension ; 26 import java.awt.Graphics ; 27 import java.awt.Insets ; 28 import java.awt.Point ; 29 import java.awt.Rectangle ; 30 import java.awt.Window ; 31 import java.awt.event.MouseAdapter ; 32 import java.awt.event.MouseEvent ; 33 import java.awt.event.MouseMotionListener ; 34 import javax.swing.SwingUtilities ; 35 import javax.swing.border.AbstractBorder ; 36 import javax.swing.border.Border ; 37 38 44 final class DecorationUtils { 45 46 47 private DecorationUtils () { 48 } 49 50 55 public static Border createSeparateBorder () { 56 return new SeparateBorder(); 57 } 58 59 63 public static ResizeHandler createResizeHandler (Insets insets) { 64 return new ResizeHandler(insets); 65 } 66 67 68 private static class SeparateBorder extends AbstractBorder { 69 70 public Insets getBorderInsets (Component c) { 71 return new Insets (3, 3, 3, 3); 72 } 73 74 public void paintBorder (Component c, Graphics g, int x, int y, int width, int height) { 75 g.setColor(Color.DARK_GRAY); 76 g.drawRect(x, y, width - 1, height - 1); 77 } 78 79 } 81 90 static class ResizeHandler extends MouseAdapter implements MouseMotionListener { 91 92 private Insets insets; 93 94 private int cursorType; 95 96 private boolean isPressed = false; 97 98 100 private Rectangle resizedBounds = new Rectangle (); 101 private Rectangle movedBounds = new Rectangle (); 102 103 private Point startDragLoc; 104 private Rectangle startWinBounds; 105 106 107 private Dimension minSize; 108 109 public ResizeHandler (Insets insets) { 110 this.insets = insets; 111 } 112 113 public void mouseDragged(MouseEvent e) { 114 check(e); 115 Window w = SwingUtilities.getWindowAncestor((Component )e.getSource()); 116 117 if (Cursor.DEFAULT_CURSOR == cursorType) { 118 return; 120 } 121 122 Rectangle newBounds = computeNewBounds(w, getScreenLoc(e)); 123 if (!w.getBounds().equals(newBounds)) { 124 w.setBounds(newBounds); 125 } 126 } 127 128 public void mouseMoved(MouseEvent e) { 129 check(e); 130 Component comp = (Component )e.getSource(); 131 movedBounds = comp.getBounds(movedBounds); 132 133 cursorType = getCursorType(movedBounds, e.getPoint()); 134 comp.setCursor(Cursor.getPredefinedCursor(cursorType)); 135 } 136 137 public void mousePressed(MouseEvent e) { 138 isPressed = true; 139 startDragLoc = getScreenLoc(e); 140 141 Window w = SwingUtilities.getWindowAncestor((Component )e.getSource()); 142 startWinBounds = w.getBounds(); 143 resizedBounds.setBounds(startWinBounds); 144 minSize = w.getMinimumSize(); 145 } 146 147 public void mouseReleased(MouseEvent e) { 148 isPressed = false; 149 startDragLoc = null; 150 startWinBounds = null; 151 minSize = null; 152 } 153 154 public void mouseExited(MouseEvent e) { 155 Component comp = (Component )e.getSource(); 156 comp.setCursor(Cursor.getDefaultCursor()); 157 } 158 159 private int getCursorType (Rectangle b, Point p) { 160 int leftDist = p.x - b.x; 161 int rightDist = (b.x + b.width) - p.x; 162 int topDist = p.y - b.y; 163 int bottomDist = (b.y + b.height) - p.y; 164 165 boolean isNearTop = topDist >= 0 && topDist <= insets.top; 166 boolean isNearBottom = bottomDist >= 0 && bottomDist <= insets.bottom; 167 boolean isNearLeft = leftDist >= 0 && leftDist <= insets.left; 168 boolean isNearRight = rightDist >= 0 && rightDist <= insets.right; 169 170 boolean isInTopPart = topDist >= 0 && topDist <= insets.top + 10; 171 boolean isInBottomPart = bottomDist >= 0 && bottomDist <= insets.bottom + 10; 172 boolean isInLeftPart = leftDist >= 0 && leftDist <= insets.left + 10; 173 boolean isInRightPart = rightDist >= 0 && rightDist <= insets.right + 10; 174 175 if (isNearTop && isInLeftPart || isInTopPart && isNearLeft) { 176 return Cursor.NW_RESIZE_CURSOR; 177 } 178 if (isNearTop && isInRightPart || isInTopPart && isNearRight) { 179 return Cursor.NE_RESIZE_CURSOR; 180 } 181 if (isNearBottom && isInLeftPart || isInBottomPart && isNearLeft) { 182 return Cursor.SW_RESIZE_CURSOR; 183 } 184 if (isNearBottom && isInRightPart || isInBottomPart && isNearRight) { 185 return Cursor.SE_RESIZE_CURSOR; 186 } 187 if (isNearTop) { 188 return Cursor.N_RESIZE_CURSOR; 189 } 190 if (isNearLeft) { 191 return Cursor.W_RESIZE_CURSOR; 192 } 193 if (isNearRight) { 194 return Cursor.E_RESIZE_CURSOR; 195 } 196 if (isNearBottom) { 197 return Cursor.S_RESIZE_CURSOR; 198 } 199 return Cursor.DEFAULT_CURSOR; 200 } 201 202 private Rectangle computeNewBounds (Window w, Point dragLoc) { 203 if (startDragLoc == null) { 204 throw new IllegalArgumentException ("Can't compute bounds when startDragLoc is null"); } 206 int xDiff = dragLoc.x - startDragLoc.x; 207 int yDiff = dragLoc.y - startDragLoc.y; 208 resizedBounds.setBounds(startWinBounds); 209 210 switch (cursorType) { 211 case Cursor.E_RESIZE_CURSOR: 212 resizedBounds.width = startWinBounds.width + (dragLoc.x - startDragLoc.x); 213 break; 214 215 case Cursor.W_RESIZE_CURSOR: 216 resizedBounds.width = startWinBounds.width - xDiff; 217 resizedBounds.x = startWinBounds.x + xDiff; 218 break; 219 220 case Cursor.N_RESIZE_CURSOR: 221 resizedBounds.height = startWinBounds.height - yDiff; 222 resizedBounds.y = startWinBounds.y + yDiff; 223 break; 224 225 case Cursor.S_RESIZE_CURSOR: 226 resizedBounds.height = startWinBounds.height + (dragLoc.y - startDragLoc.y); 227 break; 228 229 case Cursor.NE_RESIZE_CURSOR: 230 resize(resizedBounds, 0, yDiff, xDiff, -yDiff, minSize); 231 break; 232 233 case Cursor.NW_RESIZE_CURSOR: 234 resize(resizedBounds, xDiff, yDiff, -xDiff, -yDiff, minSize); 235 break; 236 237 case Cursor.SE_RESIZE_CURSOR: 238 resize(resizedBounds, 0, 0, xDiff, yDiff, minSize); 239 break; 240 241 case Cursor.SW_RESIZE_CURSOR: 242 resize(resizedBounds, xDiff, 0, -xDiff, yDiff, minSize); 243 break; 244 245 default: 246 System.out.println("unknown cursor type : " + cursorType); 247 break; 249 } 250 return resizedBounds; 251 } 252 253 private static void resize (Rectangle rect, int xDiff, int yDiff, int widthDiff, int heightDiff, Dimension minSize) { 254 rect.x += xDiff; 255 rect.y += yDiff; 256 rect.height += heightDiff; 257 rect.width += widthDiff; 258 rect.height = Math.max(rect.height, minSize.height); 260 rect.width = Math.max(rect.width, minSize.width); 261 } 262 263 private Point getScreenLoc (MouseEvent e) { 264 Point screenP = new Point (e.getPoint()); 265 SwingUtilities.convertPointToScreen(screenP, (Component ) e.getSource()); 266 return screenP; 267 } 268 269 270 private void check(MouseEvent e) { 271 Object o = e.getSource(); 272 if (!(o instanceof Component )) { 273 throw new IllegalArgumentException ("ResizeHandler works only with Component, not with " + o); } 275 Window w = SwingUtilities.getWindowAncestor((Component )o); 276 if (w == null) { 277 throw new IllegalStateException ("Can't find and resize the window, not attached."); } 279 } 280 281 } 283 } 284 | Popular Tags |