| 1 | //  Copyright 2004-2007 Jean-Francois Poilpret | 
| 2 | // | 
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
| 4 | // you may not use this file except in compliance with the License. | 
| 5 | // You may obtain a copy of the License at | 
| 6 | // | 
| 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
| 8 | // | 
| 9 | // Unless required by applicable law or agreed to in writing, software | 
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 12 | // See the License for the specific language governing permissions and | 
| 13 | // limitations under the License. | 
| 14 |   | 
| 15 | package net.sourceforge.hiveboard.util; | 
| 16 |   | 
| 17 | import java.awt.Graphics; | 
| 18 | import java.awt.Point; | 
| 19 | import java.awt.Rectangle; | 
| 20 |   | 
| 21 | public final class ResizeHandle | 
| 22 | { | 
| 23 |         static final public int                                _NORTH                = 0; | 
| 24 |         static final public int                                _SOUTH                = 1; | 
| 25 |         static final public int                                _EAST                = 2; | 
| 26 |         static final public int                                _WEST                = 3; | 
| 27 |         static final public int                                _NE                        = 4; | 
| 28 |         static final public int                                _SE                        = 5; | 
| 29 |         static final public int                                _NW                        = 6; | 
| 30 |         static final public int                                _SW                        = 7; | 
| 31 |   | 
| 32 |         static final public ResizeHandle        NORTH = new ResizeHandle(_NORTH); | 
| 33 |         static final public ResizeHandle        SOUTH = new ResizeHandle(_SOUTH); | 
| 34 |         static final public ResizeHandle        EAST = new ResizeHandle(_EAST); | 
| 35 |         static final public ResizeHandle        WEST = new ResizeHandle(_WEST); | 
| 36 |         static final public ResizeHandle        NORTH_EAST = new ResizeHandle(_NE); | 
| 37 |         static final public ResizeHandle        SOUTH_EAST = new ResizeHandle(_SE); | 
| 38 |         static final public ResizeHandle        NORTH_WEST = new ResizeHandle(_NW); | 
| 39 |         static final public ResizeHandle        SOUTH_WEST = new ResizeHandle(_SW); | 
| 40 |   | 
| 41 |         static final public ResizeHandle[]        ALL = | 
| 42 |                 {        NORTH, SOUTH, EAST, WEST, | 
| 43 |                         NORTH_EAST, SOUTH_EAST, NORTH_WEST, SOUTH_WEST}; | 
| 44 |   | 
| 45 |         private ResizeHandle(int value) | 
| 46 |         { | 
| 47 |                 _value = value; | 
| 48 |         } | 
| 49 |   | 
| 50 |         public boolean        contains(Point pt, int x, int y, int width, int height) | 
| 51 |         { | 
| 52 |                 Point origin = getHandleOrigin(x, y, width, height); | 
| 53 |                 return                (pt.x >= origin.x) | 
| 54 |                                 &&        (pt.x < origin.x + HANDLER_SIZE) | 
| 55 |                                 &&        (pt.y >= origin.y) | 
| 56 |                                 &&        (pt.y < origin.y + HANDLER_SIZE); | 
| 57 |         } | 
| 58 |   | 
| 59 |         public Rectangle        resizeBounds(Rectangle bounds, int dx, int dy) | 
| 60 |         { | 
| 61 |                 switch (_value) | 
| 62 |                 { | 
| 63 |                         case _NORTH: | 
| 64 |                         bounds.y += dy; | 
| 65 |                         bounds.height -= dy; | 
| 66 |                         break; | 
| 67 |   | 
| 68 |                         case _SOUTH: | 
| 69 |                         bounds.height += dy; | 
| 70 |                         break; | 
| 71 |   | 
| 72 |                         case _EAST: | 
| 73 |                         bounds.width += dx; | 
| 74 |                         break; | 
| 75 |   | 
| 76 |                         case _WEST: | 
| 77 |                         bounds.x += dx; | 
| 78 |                         bounds.width -= dx; | 
| 79 |                         break; | 
| 80 |   | 
| 81 |                         case _NE: | 
| 82 |                         bounds.y += dy; | 
| 83 |                         bounds.height -= dy; | 
| 84 |                         bounds.width += dx; | 
| 85 |                         break; | 
| 86 |   | 
| 87 |                         case _SE: | 
| 88 |                         bounds.height += dy; | 
| 89 |                         bounds.width += dx; | 
| 90 |                         break; | 
| 91 |   | 
| 92 |                         case _NW: | 
| 93 |                         bounds.y += dy; | 
| 94 |                         bounds.height -= dy; | 
| 95 |                         bounds.x += dx; | 
| 96 |                         bounds.width -= dx; | 
| 97 |                         break; | 
| 98 |   | 
| 99 |                         case _SW: | 
| 100 |                         bounds.height += dy; | 
| 101 |                         bounds.x += dx; | 
| 102 |                         bounds.width -= dx; | 
| 103 |                         break; | 
| 104 |                          | 
| 105 |                         default: | 
| 106 |                         break; | 
| 107 |                 } | 
| 108 |                 bounds.width = Math.max(0, bounds.width); | 
| 109 |                 bounds.height = Math.max(0, bounds.height); | 
| 110 |                 return bounds; | 
| 111 |         } | 
| 112 |   | 
| 113 |         // CSOFF: ParameterAssignmentCheck | 
| 114 |         private Point        getHandleOrigin(int x, int y, int width, int height) | 
| 115 |         { | 
| 116 |                 // Compute the real position based on _value | 
| 117 |                 switch (_value) | 
| 118 |                 { | 
| 119 |                         case _NORTH: | 
| 120 |                         x += (width - HANDLER_SIZE) / 2; | 
| 121 |                         break; | 
| 122 |   | 
| 123 |                         case _SOUTH: | 
| 124 |                         x += (width - HANDLER_SIZE) / 2; | 
| 125 |                         y += height - HANDLER_SIZE; | 
| 126 |                         break; | 
| 127 |   | 
| 128 |                         case _EAST: | 
| 129 |                         x += width - HANDLER_SIZE; | 
| 130 |                         y += (height - HANDLER_SIZE) / 2; | 
| 131 |                         break; | 
| 132 |   | 
| 133 |                         case _WEST: | 
| 134 |                         y += (height - HANDLER_SIZE) / 2; | 
| 135 |                         break; | 
| 136 |   | 
| 137 |                         case _NE: | 
| 138 |                         x += width - HANDLER_SIZE; | 
| 139 |                         break; | 
| 140 |   | 
| 141 |                         case _SE: | 
| 142 |                         x += width - HANDLER_SIZE; | 
| 143 |                         y += height - HANDLER_SIZE; | 
| 144 |                         break; | 
| 145 |   | 
| 146 |                         case _NW: | 
| 147 |                         break; | 
| 148 |   | 
| 149 |                         case _SW: | 
| 150 |                         y += height - HANDLER_SIZE; | 
| 151 |                         break; | 
| 152 |   | 
| 153 |                         default: | 
| 154 |                         break; | 
| 155 |                 } | 
| 156 |                 return new Point(x, y); | 
| 157 |         } | 
| 158 |         // CSON: ParameterAssignmentCheck | 
| 159 |   | 
| 160 |         public void        drawHandle(Graphics g, int x, int y, int width, int height) | 
| 161 |         { | 
| 162 |                 // Compute the real position based on _value | 
| 163 |                 Point origin = getHandleOrigin(x, y, width, height); | 
| 164 |                 // Now fill the rectangle | 
| 165 |                 g.fillRect(origin.x, origin.y, HANDLER_SIZE, HANDLER_SIZE); | 
| 166 |         } | 
| 167 |   | 
| 168 |         private final int _value; | 
| 169 |   | 
| 170 |         static final private int        HANDLER_SIZE = 5; | 
| 171 | } | 
| 172 |   |