| 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.BasicStroke; |
| 18 | import java.awt.Color; |
| 19 | import java.awt.Dimension; |
| 20 | import java.awt.Graphics; |
| 21 | import java.awt.Graphics2D; |
| 22 | import java.awt.Point; |
| 23 | import java.awt.Rectangle; |
| 24 | import java.awt.Stroke; |
| 25 | import java.awt.event.MouseEvent; |
| 26 | |
| 27 | import javax.swing.event.MouseInputAdapter; |
| 28 | |
| 29 | import net.sourceforge.hivegui.component.Thumbnail; |
| 30 | |
| 31 | // Thumbnail that allows display and change (by drag) of an outline |
| 32 | public class OutlineThumbnail extends Thumbnail |
| 33 | { |
| 34 | public OutlineThumbnail(int previewSize) |
| 35 | { |
| 36 | super(previewSize, false); |
| 37 | DragListener listener = new DragListener(); |
| 38 | addMouseListener(listener); |
| 39 | addMouseMotionListener(listener); |
| 40 | } |
| 41 | |
| 42 | public void setOutlineLimits( Point minLocation, |
| 43 | Point maxLocation, |
| 44 | Dimension minSize, |
| 45 | Dimension maxSize) |
| 46 | { |
| 47 | _minLocation = minLocation; |
| 48 | _maxLocation = maxLocation; |
| 49 | _minSize = minSize; |
| 50 | _maxSize = maxSize; |
| 51 | } |
| 52 | |
| 53 | public void setAllowDrag(boolean allowDrag) |
| 54 | { |
| 55 | _allowDrag = allowDrag; |
| 56 | repaint(); |
| 57 | } |
| 58 | |
| 59 | public void setOutline(Rectangle outline) |
| 60 | { |
| 61 | _outline = outline; |
| 62 | repaint(); |
| 63 | } |
| 64 | |
| 65 | public void setOutlineChangeListener(OutlineChangeListener listener) |
| 66 | { |
| 67 | _listener = listener; |
| 68 | } |
| 69 | |
| 70 | @Override public void paintComponent(Graphics g) |
| 71 | { |
| 72 | super.paintComponent(g); |
| 73 | if (_allowDrag) |
| 74 | { |
| 75 | Graphics2D g2 = (Graphics2D) g; |
| 76 | // Save current graphics context |
| 77 | Color previousColor = g2.getColor(); |
| 78 | Stroke previousStroke = g2.getStroke(); |
| 79 | // Draw outline |
| 80 | drawOutline(g2); |
| 81 | // Restore previous graphics context |
| 82 | g2.setPaintMode(); |
| 83 | g2.setColor(previousColor); |
| 84 | g2.setStroke(previousStroke); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private void drawOutline(Graphics2D g) |
| 89 | { |
| 90 | // Setup the graphics for drawing the outline |
| 91 | g.setColor(Color.BLACK); |
| 92 | g.setXORMode(Color.WHITE); |
| 93 | g.setStroke(OUTLINE_STROKE); |
| 94 | // Draw the outline |
| 95 | g.draw(_outline); |
| 96 | // Draw the handle(s) |
| 97 | int x = _outline.x; |
| 98 | int y = _outline.y; |
| 99 | int width = _outline.width; |
| 100 | int height = _outline.height; |
| 101 | for (int i = 0; i < _outlineHandles.length; i++) |
| 102 | { |
| 103 | _outlineHandles[i].drawHandle(g, x, y, width, height); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private class DragListener extends MouseInputAdapter |
| 108 | { |
| 109 | @Override public void mousePressed(MouseEvent e) |
| 110 | { |
| 111 | if (!_allowDrag) |
| 112 | { |
| 113 | return; |
| 114 | } |
| 115 | _dragHandle = -1; |
| 116 | _dragOutline = false; |
| 117 | _dragStartX = e.getX(); |
| 118 | _dragStartY = e.getY(); |
| 119 | // Is that inside an outline handle? |
| 120 | for (int i = 0; i < _outlineHandles.length; i++) |
| 121 | { |
| 122 | if (_outlineHandles[i].contains(e.getPoint(), |
| 123 | _outline.x, |
| 124 | _outline.y, |
| 125 | _outline.width, |
| 126 | _outline.height)) |
| 127 | { |
| 128 | // Then start dragging the handle |
| 129 | _dragHandle = i; |
| 130 | return; |
| 131 | } |
| 132 | } |
| 133 | // Or in the outline itself? |
| 134 | if (_outline.contains(e.getPoint())) |
| 135 | { |
| 136 | // Then start dragging the full outline |
| 137 | _dragOutline = true; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | @Override public void mouseReleased(MouseEvent e) |
| 142 | { |
| 143 | drag(e, false); |
| 144 | _dragHandle = -1; |
| 145 | _dragOutline = false; |
| 146 | } |
| 147 | |
| 148 | @Override public void mouseDragged(MouseEvent e) |
| 149 | { |
| 150 | drag(e, true); |
| 151 | } |
| 152 | |
| 153 | private void drag(MouseEvent e, boolean temporary) |
| 154 | { |
| 155 | int dx = e.getX() - _dragStartX; |
| 156 | int dy = e.getY() - _dragStartY; |
| 157 | if (dx == 0 && dy == 0 && temporary) |
| 158 | { |
| 159 | return; |
| 160 | } |
| 161 | _dragStartX = e.getX(); |
| 162 | _dragStartY = e.getY(); |
| 163 | if (_dragOutline) |
| 164 | { |
| 165 | _outline.translate(dx, dy); |
| 166 | } |
| 167 | else if (_dragHandle >= 0) |
| 168 | { |
| 169 | _outlineHandles[_dragHandle].resizeBounds(_outline, dx, dy); |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | return; |
| 174 | } |
| 175 | // Constrain outline size accroding to limits |
| 176 | constrainOutline(); |
| 177 | // Real-time refresh |
| 178 | repaint(); |
| 179 | // Notify listener |
| 180 | _listener.outlineChanged(_outline, temporary); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private void constrainOutline() |
| 185 | { |
| 186 | if (_minLocation != null) |
| 187 | { |
| 188 | _outline.x = Math.max(_outline.x, _minLocation.x); |
| 189 | _outline.y = Math.max(_outline.y, _minLocation.y); |
| 190 | } |
| 191 | if (_maxLocation != null) |
| 192 | { |
| 193 | _outline.x = Math.min(_outline.x, _maxLocation.x); |
| 194 | _outline.y = Math.min(_outline.y, _maxLocation.y); |
| 195 | } |
| 196 | if (_minSize != null) |
| 197 | { |
| 198 | _outline.width = Math.max(_outline.width, _minSize.width); |
| 199 | _outline.height = Math.max(_outline.height, _minSize.height); |
| 200 | } |
| 201 | if (_maxSize != null) |
| 202 | { |
| 203 | _outline.width = Math.min(_outline.width, _maxSize.width); |
| 204 | _outline.height = Math.min(_outline.height, _maxSize.height); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static final private float[] DASHES = new float[] {5.0f, 3.0f}; |
| 209 | static final private Stroke OUTLINE_STROKE = new BasicStroke( 1.0f, |
| 210 | BasicStroke.CAP_SQUARE, |
| 211 | BasicStroke.JOIN_ROUND, |
| 212 | 0.0f, |
| 213 | DASHES, |
| 214 | 0.0f); |
| 215 | |
| 216 | private OutlineChangeListener _listener; |
| 217 | |
| 218 | // Outline display |
| 219 | private Rectangle _outline; |
| 220 | private ResizeHandle[] _outlineHandles = ResizeHandle.ALL; |
| 221 | |
| 222 | // Drag management |
| 223 | private Point _minLocation; |
| 224 | private Point _maxLocation; |
| 225 | private Dimension _minSize; |
| 226 | private Dimension _maxSize; |
| 227 | private boolean _allowDrag; |
| 228 | private int _dragHandle = -1; |
| 229 | private boolean _dragOutline = false; |
| 230 | private int _dragStartX; |
| 231 | private int _dragStartY; |
| 232 | } |