Gridarta Editor
AutoscrollJTree.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.gui.mapmenu;
21 
22 import java.awt.Container;
23 import java.awt.Dimension;
24 import java.awt.Insets;
25 import java.awt.Point;
26 import java.awt.Rectangle;
27 import java.awt.dnd.Autoscroll;
28 import javax.swing.JTree;
29 import javax.swing.JViewport;
30 import javax.swing.SwingConstants;
31 import javax.swing.tree.TreeModel;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
39 public class AutoscrollJTree extends JTree implements Autoscroll {
40 
44  private static final long serialVersionUID = 1L;
45 
49  private static final int BORDER_SIZE = 12;
50 
55  public AutoscrollJTree(@NotNull final TreeModel treeModel) {
56  super(treeModel);
57  }
58 
59  @NotNull
60  @Override
61  public Insets getAutoscrollInsets() {
62  final Insets insets = new Insets(BORDER_SIZE, BORDER_SIZE, BORDER_SIZE, BORDER_SIZE);
63  final JViewport viewport = getViewport();
64  if (viewport != null) {
65  final Dimension size = getSize();
66  final Rectangle viewRectangle = viewport.getViewRect();
67  insets.top += viewRectangle.y;
68  insets.left += viewRectangle.x;
69  insets.bottom += size.height - (viewRectangle.y + viewRectangle.height);
70  insets.right += size.width - (viewRectangle.x + viewRectangle.width);
71  }
72  return insets;
73  }
74 
75  @Override
76  public void autoscroll(@NotNull final Point cursorLocn) {
77  final JViewport viewport = getViewport();
78  if (viewport == null) {
79  return;
80  }
81 
82  final Insets insets = getAutoscrollInsets();
83  final Rectangle bounds = getBounds();
84  final Rectangle viewRectangle = viewport.getViewRect();
85  if (cursorLocn.x < insets.left) {
86  viewRectangle.x -= getScrollableUnitIncrement(viewRectangle, SwingConstants.HORIZONTAL, -1);
87  } else if (cursorLocn.x > bounds.width - insets.right) {
88  viewRectangle.x += getScrollableUnitIncrement(viewRectangle, SwingConstants.HORIZONTAL, 1);
89  }
90  if (cursorLocn.y < insets.top) {
91  viewRectangle.y -= getScrollableUnitIncrement(viewRectangle, SwingConstants.VERTICAL, -1);
92  } else if (cursorLocn.y > bounds.height - insets.bottom) {
93  viewRectangle.y += getScrollableUnitIncrement(viewRectangle, SwingConstants.VERTICAL, 1);
94  }
95  scrollRectToVisible(viewRectangle);
96  }
97 
102  @Nullable
103  private JViewport getViewport() {
104  final Container parent = getParent();
105  return parent instanceof JViewport ? (JViewport) parent : null;
106  }
107 
108 }
net.sf.gridarta.gui.mapmenu.AutoscrollJTree.autoscroll
void autoscroll(@NotNull final Point cursorLocn)
Definition: AutoscrollJTree.java:76
net.sf.gridarta.gui.mapmenu.AutoscrollJTree.getViewport
JViewport getViewport()
Returns the parent JViewport component.
Definition: AutoscrollJTree.java:103
net.sf.gridarta.gui.mapmenu.AutoscrollJTree.BORDER_SIZE
static final int BORDER_SIZE
The active border size.
Definition: AutoscrollJTree.java:49
net.sf.gridarta.gui.mapmenu.AutoscrollJTree.AutoscrollJTree
AutoscrollJTree(@NotNull final TreeModel treeModel)
Creates a new instance.
Definition: AutoscrollJTree.java:55
net.sf.gridarta.gui.mapmenu.AutoscrollJTree.getAutoscrollInsets
Insets getAutoscrollInsets()
Definition: AutoscrollJTree.java:61
net.sf.gridarta.gui.mapmenu.AutoscrollJTree.serialVersionUID
static final long serialVersionUID
The serial version UID.
Definition: AutoscrollJTree.java:44
net.sf.gridarta.gui.mapmenu.AutoscrollJTree
A JTree that supports auto-scrolling while drag and drop is active.
Definition: AutoscrollJTree.java:39