source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/MultiSplitPane.java@ 8444

Last change on this file since 8444 was 8444, checked in by Don-vip, 9 years ago

remove extra whitespaces

  • Property svn:eol-style set to native
File size: 13.2 KB
Line 
1/*
2 * $Id: MultiSplitPane.java,v 1.15 2005/10/26 14:29:54 hansmuller Exp $
3 *
4 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5 * Santa Clara, California 95054, U.S.A. All rights reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21package org.openstreetmap.josm.gui.widgets;
22
23import java.awt.Color;
24import java.awt.Cursor;
25import java.awt.Graphics;
26import java.awt.Graphics2D;
27import java.awt.Rectangle;
28import java.awt.event.KeyEvent;
29import java.awt.event.KeyListener;
30import java.awt.event.MouseEvent;
31
32import javax.accessibility.AccessibleContext;
33import javax.accessibility.AccessibleRole;
34import javax.swing.JPanel;
35import javax.swing.event.MouseInputAdapter;
36
37import org.openstreetmap.josm.gui.widgets.MultiSplitLayout.Divider;
38import org.openstreetmap.josm.gui.widgets.MultiSplitLayout.Node;
39
40/**
41 *
42 * <p>
43 * All properties in this class are bound: when a properties value
44 * is changed, all PropertyChangeListeners are fired.
45 *
46 * @author Hans Muller - SwingX
47 */
48public class MultiSplitPane extends JPanel {
49 private transient AccessibleContext accessibleContext = null;
50 private boolean continuousLayout = true;
51 private transient DividerPainter dividerPainter = new DefaultDividerPainter();
52
53 /**
54 * Creates a MultiSplitPane with it's LayoutManager set to
55 * to an empty MultiSplitLayout.
56 */
57 public MultiSplitPane() {
58 super(new MultiSplitLayout());
59 InputHandler inputHandler = new InputHandler();
60 addMouseListener(inputHandler);
61 addMouseMotionListener(inputHandler);
62 addKeyListener(inputHandler);
63 setFocusable(true);
64 }
65
66 /**
67 * A convenience method that returns the layout manager cast
68 * to MutliSplitLayout.
69 *
70 * @return this MultiSplitPane's layout manager
71 * @see java.awt.Container#getLayout
72 * @see #setModel
73 */
74 public final MultiSplitLayout getMultiSplitLayout() {
75 return (MultiSplitLayout)getLayout();
76 }
77
78 /**
79 * A convenience method that sets the MultiSplitLayout model.
80 * Equivalent to <code>getMultiSplitLayout.setModel(model)</code>
81 *
82 * @param model the root of the MultiSplitLayout model
83 * @see #getMultiSplitLayout
84 * @see MultiSplitLayout#setModel
85 */
86 public final void setModel(Node model) {
87 getMultiSplitLayout().setModel(model);
88 }
89
90 /**
91 * A convenience method that sets the MultiSplitLayout dividerSize
92 * property. Equivalent to
93 * <code>getMultiSplitLayout().setDividerSize(newDividerSize)</code>.
94 *
95 * @param dividerSize the value of the dividerSize property
96 * @see #getMultiSplitLayout
97 * @see MultiSplitLayout#setDividerSize
98 */
99 public final void setDividerSize(int dividerSize) {
100 getMultiSplitLayout().setDividerSize(dividerSize);
101 }
102
103 /**
104 * Sets the value of the <code>continuousLayout</code> property.
105 * If true, then the layout is revalidated continuously while
106 * a divider is being moved. The default value of this property
107 * is true.
108 *
109 * @param continuousLayout value of the continuousLayout property
110 * @see #isContinuousLayout
111 */
112 public void setContinuousLayout(boolean continuousLayout) {
113 boolean oldContinuousLayout = continuousLayout;
114 this.continuousLayout = continuousLayout;
115 firePropertyChange("continuousLayout", oldContinuousLayout, continuousLayout);
116 }
117
118 /**
119 * Returns true if dragging a divider only updates
120 * the layout when the drag gesture ends (typically, when the
121 * mouse button is released).
122 *
123 * @return the value of the <code>continuousLayout</code> property
124 * @see #setContinuousLayout
125 */
126 public boolean isContinuousLayout() {
127 return continuousLayout;
128 }
129
130 /**
131 * Returns the Divider that's currently being moved, typically
132 * because the user is dragging it, or null.
133 *
134 * @return the Divider that's being moved or null.
135 */
136 public Divider activeDivider() {
137 return dragDivider;
138 }
139
140 /**
141 * Draws a single Divider. Typically used to specialize the
142 * way the active Divider is painted.
143 *
144 * @see #getDividerPainter
145 * @see #setDividerPainter
146 */
147 public interface DividerPainter {
148 /**
149 * Paint a single Divider.
150 *
151 * @param g the Graphics object to paint with
152 * @param divider the Divider to paint
153 */
154 public abstract void paint(Graphics g, Divider divider);
155 }
156
157 private class DefaultDividerPainter implements DividerPainter {
158 @Override
159 public void paint(Graphics g, Divider divider) {
160 if ((divider == activeDivider()) && !isContinuousLayout()) {
161 Graphics2D g2d = (Graphics2D)g;
162 g2d.setColor(Color.black);
163 g2d.fill(divider.getBounds());
164 }
165 }
166 }
167
168 /**
169 * The DividerPainter that's used to paint Dividers on this MultiSplitPane.
170 * This property may be null.
171 *
172 * @return the value of the dividerPainter Property
173 * @see #setDividerPainter
174 */
175 public DividerPainter getDividerPainter() {
176 return dividerPainter;
177 }
178
179 /**
180 * Sets the DividerPainter that's used to paint Dividers on this
181 * MultiSplitPane. The default DividerPainter only draws
182 * the activeDivider (if there is one) and then, only if
183 * continuousLayout is false. The value of this property is
184 * used by the paintChildren method: Dividers are painted after
185 * the MultiSplitPane's children have been rendered so that
186 * the activeDivider can appear "on top of" the children.
187 *
188 * @param dividerPainter the value of the dividerPainter property, can be null
189 * @see #paintChildren
190 * @see #activeDivider
191 */
192 public void setDividerPainter(DividerPainter dividerPainter) {
193 this.dividerPainter = dividerPainter;
194 }
195
196 /**
197 * Uses the DividerPainter (if any) to paint each Divider that
198 * overlaps the clip Rectangle. This is done after the call to
199 * <code>super.paintChildren()</code> so that Dividers can be
200 * rendered "on top of" the children.
201 * <p>
202 * {@inheritDoc}
203 */
204 @Override
205 protected void paintChildren(Graphics g) {
206 super.paintChildren(g);
207 DividerPainter dp = getDividerPainter();
208 Rectangle clipR = g.getClipBounds();
209 if ((dp != null) && (clipR != null)) {
210 Graphics dpg = g.create();
211 try {
212 MultiSplitLayout msl = getMultiSplitLayout();
213 for(Divider divider : msl.dividersThatOverlap(clipR)) {
214 dp.paint(dpg, divider);
215 }
216 } finally {
217 dpg.dispose();
218 }
219 }
220 }
221
222 private boolean dragUnderway = false;
223 private transient MultiSplitLayout.Divider dragDivider = null;
224 private Rectangle initialDividerBounds = null;
225 private boolean oldFloatingDividers = true;
226 private int dragOffsetX = 0;
227 private int dragOffsetY = 0;
228 private int dragMin = -1;
229 private int dragMax = -1;
230
231 private void startDrag(int mx, int my) {
232 requestFocusInWindow();
233 MultiSplitLayout msl = getMultiSplitLayout();
234 MultiSplitLayout.Divider divider = msl.dividerAt(mx, my);
235 if (divider != null) {
236 MultiSplitLayout.Node prevNode = divider.previousSibling();
237 MultiSplitLayout.Node nextNode = divider.nextSibling();
238 if ((prevNode == null) || (nextNode == null)) {
239 dragUnderway = false;
240 } else {
241 initialDividerBounds = divider.getBounds();
242 dragOffsetX = mx - initialDividerBounds.x;
243 dragOffsetY = my - initialDividerBounds.y;
244 dragDivider = divider;
245 Rectangle prevNodeBounds = prevNode.getBounds();
246 Rectangle nextNodeBounds = nextNode.getBounds();
247 if (dragDivider.isVertical()) {
248 dragMin = prevNodeBounds.x;
249 dragMax = nextNodeBounds.x + nextNodeBounds.width;
250 dragMax -= dragDivider.getBounds().width;
251 } else {
252 dragMin = prevNodeBounds.y;
253 dragMax = nextNodeBounds.y + nextNodeBounds.height;
254 dragMax -= dragDivider.getBounds().height;
255 }
256 oldFloatingDividers = getMultiSplitLayout().getFloatingDividers();
257 getMultiSplitLayout().setFloatingDividers(false);
258 dragUnderway = true;
259 }
260 } else {
261 dragUnderway = false;
262 }
263 }
264
265 private void repaintDragLimits() {
266 Rectangle damageR = dragDivider.getBounds();
267 if (dragDivider.isVertical()) {
268 damageR.x = dragMin;
269 damageR.width = dragMax - dragMin;
270 } else {
271 damageR.y = dragMin;
272 damageR.height = dragMax - dragMin;
273 }
274 repaint(damageR);
275 }
276
277 private void updateDrag(int mx, int my) {
278 if (!dragUnderway) {
279 return;
280 }
281 Rectangle oldBounds = dragDivider.getBounds();
282 Rectangle bounds = new Rectangle(oldBounds);
283 if (dragDivider.isVertical()) {
284 bounds.x = mx - dragOffsetX;
285 bounds.x = Math.max(bounds.x, dragMin);
286 bounds.x = Math.min(bounds.x, dragMax);
287 } else {
288 bounds.y = my - dragOffsetY;
289 bounds.y = Math.max(bounds.y, dragMin);
290 bounds.y = Math.min(bounds.y, dragMax);
291 }
292 dragDivider.setBounds(bounds);
293 if (isContinuousLayout()) {
294 revalidate();
295 repaintDragLimits();
296 } else {
297 repaint(oldBounds.union(bounds));
298 }
299 }
300
301 private void clearDragState() {
302 dragDivider = null;
303 initialDividerBounds = null;
304 oldFloatingDividers = true;
305 dragOffsetX = dragOffsetY = 0;
306 dragMin = dragMax = -1;
307 dragUnderway = false;
308 }
309
310 private void finishDrag(int x, int y) {
311 if (dragUnderway) {
312 clearDragState();
313 if (!isContinuousLayout()) {
314 revalidate();
315 repaint();
316 }
317 }
318 }
319
320 private void cancelDrag() {
321 if (dragUnderway) {
322 dragDivider.setBounds(initialDividerBounds);
323 getMultiSplitLayout().setFloatingDividers(oldFloatingDividers);
324 setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
325 repaint();
326 revalidate();
327 clearDragState();
328 }
329 }
330
331 private void updateCursor(int x, int y, boolean show) {
332 if (dragUnderway) {
333 return;
334 }
335 int cursorID = Cursor.DEFAULT_CURSOR;
336 if (show) {
337 MultiSplitLayout.Divider divider = getMultiSplitLayout().dividerAt(x, y);
338 if (divider != null) {
339 cursorID = (divider.isVertical()) ?
340 Cursor.E_RESIZE_CURSOR :
341 Cursor.N_RESIZE_CURSOR;
342 }
343 }
344 setCursor(Cursor.getPredefinedCursor(cursorID));
345 }
346
347 private class InputHandler extends MouseInputAdapter implements KeyListener {
348
349 @Override
350 public void mouseEntered(MouseEvent e) {
351 updateCursor(e.getX(), e.getY(), true);
352 }
353
354 @Override
355 public void mouseMoved(MouseEvent e) {
356 updateCursor(e.getX(), e.getY(), true);
357 }
358
359 @Override
360 public void mouseExited(MouseEvent e) {
361 updateCursor(e.getX(), e.getY(), false);
362 }
363
364 @Override
365 public void mousePressed(MouseEvent e) {
366 startDrag(e.getX(), e.getY());
367 }
368 @Override
369 public void mouseReleased(MouseEvent e) {
370 finishDrag(e.getX(), e.getY());
371 }
372 @Override
373 public void mouseDragged(MouseEvent e) {
374 updateDrag(e.getX(), e.getY());
375 }
376 @Override
377 public void keyPressed(KeyEvent e) {
378 if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
379 cancelDrag();
380 }
381 }
382 @Override
383 public void keyReleased(KeyEvent e) { }
384 @Override
385 public void keyTyped(KeyEvent e) { }
386 }
387
388 @Override
389 public AccessibleContext getAccessibleContext() {
390 if (accessibleContext == null) {
391 accessibleContext = new AccessibleMultiSplitPane();
392 }
393 return accessibleContext;
394 }
395
396 protected class AccessibleMultiSplitPane extends AccessibleJPanel {
397 @Override
398 public AccessibleRole getAccessibleRole() {
399 return AccessibleRole.SPLIT_PANE;
400 }
401 }
402}
Note: See TracBrowser for help on using the repository browser.