forked from dukke/swing-desktopScrollPane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DesktopScrollPane
209 lines (167 loc) · 7.18 KB
/
DesktopScrollPane
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2013 DesktopScrollPane Pedro Duque Vieira. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* o Neither the name of DesktopScrollPane Pedro Duque Vieira nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package Modellus.WinUI.InternalFrames;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
public class DesktopScrollPane extends JScrollPane {
private JDesktopPane desktopPane;
private InternalFrameComponentListener componentListener;
/**
* creates the DesktopScrollPane object
*
* @param desktopPane a reference to the JDesktopPane object
*/
public DesktopScrollPane(JDesktopPane desktopPane) {
componentListener = new InternalFrameComponentListener();
this.desktopPane = desktopPane;
desktopPane.addContainerListener(new ContainerListener() {
@Override
public void componentAdded(ContainerEvent e) {
onComponentAdded(e);
}
@Override
public void componentRemoved(ContainerEvent e) {
onComponentRemoted(e);
}
});
setViewportView(desktopPane);
// set some defaults
setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
}
private void onComponentRemoted(ContainerEvent event) {
Component removedComponent = event.getChild();
if (removedComponent instanceof JInternalFrame)
removedComponent.removeComponentListener(componentListener);
}
private void onComponentAdded(ContainerEvent event) {
Component addedComponent = event.getChild();
if (addedComponent instanceof JInternalFrame)
{
addedComponent.addComponentListener(componentListener);
resizeDesktop();
}
}
/**
* returns all internal frames placed upon the desktop
*
* @return a JInternalFrame array containing references to the internal frames
*/
public JInternalFrame[] getAllFrames() {
return desktopPane.getAllFrames();
}
/**
* sets the preferred size of the desktop
*
* @param dim a Dimension object representing the desired preferred size
*/
public void setDesktopSize(Dimension dim) {
desktopPane.setPreferredSize(dim);
desktopPane.revalidate();
}
/**
* resizes the desktop based upon the locations of its
* internal frames. This updates the desktop scrollbars in real-time.
*/
public void resizeDesktop() {
SwingUtilities.invokeLater(new Runnable() {
public void run(){
// has to go through all the internal frames now and make sure none
// off screen, and if so, add those scroll bars!
Rectangle viewPort = getViewport().getViewRect();
int maxX = viewPort.width + viewPort.x, maxY = viewPort.height + viewPort.y;
int minX = viewPort.x, minY = viewPort.y;
// determine the min/max extents of all internal frames
JInternalFrame frame = null;
JInternalFrame[] frames = getAllFrames();
for (int i=0; i < frames.length; i++) {
frame = frames[i];
if (frame.getX() < minX) { // get minimum X
minX = frame.getX();
}
if ((frame.getX() + frame.getWidth()) > maxX)
{
maxX = frame.getX() + frame.getWidth();
}
if (frame.getY() < minY) { // get minimum Y
minY = frame.getY();
}
if ((frame.getY() + frame.getHeight()) > maxY)
{
maxY = frame.getY() + frame.getHeight();
}
}
// Don't count with frames that get off screen from the left side ant the top
if (minX < 0) minX = 0;
if (minY < 0) minY = 0;
setVisible(false); // don't update the viewport
// while we move everything (otherwise desktop looks 'bouncy')
if (minX != 0 || minY != 0) {
// have to scroll it to the right or up the amount that it's off screen...
// before scroll, move every component to the right / down by that amount
for (int i=0; i < frames.length; i++) {
frame = frames[i];
frame.setLocation(frame.getX()-minX, frame.getY()-minY);
}
// have to scroll (set the viewport) to the right or up the amount
// that it's off screen...
JViewport view = getViewport();
view.setViewSize(new Dimension((maxX-minX),(maxY-minY)));
view.setViewPosition(new Point((viewPort.x-minX),(viewPort.y-minY)));
setViewport(view);
}
// resize the desktop
setDesktopSize(new Dimension(maxX-minX, maxY-minY));
setVisible(true); // update the viewport again
}
});
}
private class InternalFrameComponentListener implements ComponentListener
{
@Override
public void componentResized(ComponentEvent e) {
resizeDesktop();
}
@Override
public void componentMoved(ComponentEvent e) {
resizeDesktop();
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
}
}