Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / mdiManager / MDIDesktopPane.java @ 682

History | View | Annotate | Download (7.05 KB)

1
package org.gvsig.coreplugin.mdiManager;
2

    
3
import javax.swing.*;
4
import java.awt.*;
5
import java.beans.*;
6

    
7
/*
8
 * Based on portions of code from  freesourcecode.net
9
 * http://freesourcecode.net/javaprojects/14214/MDI-Example-code#.VfQ9El0vDeQ
10
 */
11
/**
12
 * An extension of WDesktopPane that supports often used MDI functionality. This
13
 * class also handles setting scroll bars for when windows move too far to the
14
 * left or bottom, providing the MDIDesktopPane is in a ScrollPane.
15
 */
16
public class MDIDesktopPane extends JDesktopPane {
17

    
18
    private static int FRAME_OFFSET = 20;
19
    private MDIDesktopManager manager;
20

    
21
    public MDIDesktopPane() {
22
        manager = new MDIDesktopManager(this);
23
        setDesktopManager(manager);
24
        setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
25
    }
26

    
27
    public void setBounds(int x, int y, int w, int h) {
28
        super.setBounds(x, y, w, h);
29
        checkDesktopSize();
30
    }
31

    
32
    public Component add(JInternalFrame frame) {
33
        JInternalFrame[] array = getAllFrames();
34
        Point p;
35
        int w;
36
        int h;
37

    
38
        Component retval = super.add(frame);
39
        checkDesktopSize();
40
        if (array.length > 0) {
41
            p = array[0].getLocation();
42
            p.x = p.x + FRAME_OFFSET;
43
            p.y = p.y + FRAME_OFFSET;
44
        } else {
45
            p = new Point(0, 0);
46
        }
47
        frame.setLocation(p.x, p.y);
48
        if (frame.isResizable()) {
49
            w = getWidth() - (getWidth() / 3);
50
            h = getHeight() - (getHeight() / 3);
51
            if (w < frame.getMinimumSize().getWidth()) {
52
                w = (int) frame.getMinimumSize().getWidth();
53
            }
54
            if (h < frame.getMinimumSize().getHeight()) {
55
                h = (int) frame.getMinimumSize().getHeight();
56
            }
57
            frame.setSize(w, h);
58
        }
59
        moveToFront(frame);
60
        frame.setVisible(true);
61
        try {
62
            frame.setSelected(true);
63
        } catch (PropertyVetoException e) {
64
            frame.toBack();
65
        }
66
        return retval;
67
    }
68

    
69
    public void remove(Component c) {
70
        super.remove(c);
71
        checkDesktopSize();
72
    }
73

    
74
    /**
75
     * Cascade all internal frames
76
     */
77
    public void cascadeFrames() {
78
        int x = 0;
79
        int y = 0;
80
        JInternalFrame allFrames[] = getAllFrames();
81

    
82
        manager.setNormalSize();
83
        int frameHeight = (getBounds().height - 5) - allFrames.length * FRAME_OFFSET;
84
        int frameWidth = (getBounds().width - 5) - allFrames.length * FRAME_OFFSET;
85
        for (int i = allFrames.length - 1; i >= 0; i--) {
86
            allFrames[i].setSize(frameWidth, frameHeight);
87
            allFrames[i].setLocation(x, y);
88
            x = x + FRAME_OFFSET;
89
            y = y + FRAME_OFFSET;
90
        }
91
    }
92

    
93
    /**
94
     * Tile all internal frames
95
     */
96
    public void tileFrames() {
97
        java.awt.Component allFrames[] = getAllFrames();
98
        manager.setNormalSize();
99
        int frameHeight = getBounds().height / allFrames.length;
100
        int y = 0;
101
        for (int i = 0; i < allFrames.length; i++) {
102
            allFrames[i].setSize(getBounds().width, frameHeight);
103
            allFrames[i].setLocation(0, y);
104
            y = y + frameHeight;
105
        }
106
    }
107

    
108
    /**
109
     * Sets all component size properties ( maximum, minimum, preferred) to the
110
     * given dimension.
111
     */
112
    public void setAllSize(Dimension d) {
113
        setMinimumSize(d);
114
        setMaximumSize(d);
115
        setPreferredSize(d);
116
    }
117

    
118
    /**
119
     * Sets all component size properties ( maximum, minimum, preferred) to the
120
     * given width and height.
121
     */
122
    public void setAllSize(int width, int height) {
123
        setAllSize(new Dimension(width, height));
124
    }
125

    
126
    private void checkDesktopSize() {
127
        if (getParent() != null && isVisible()) {
128
            manager.resizeDesktop();
129
        }
130
    }
131
}
132

    
133
/**
134
 * Private class used to replace the standard DesktopManager for JDesktopPane.
135
 * Used to provide scrollbar functionality.
136
 */
137
class MDIDesktopManager extends DefaultDesktopManager {
138

    
139
    private MDIDesktopPane desktop;
140

    
141
    public MDIDesktopManager(MDIDesktopPane desktop) {
142
        this.desktop = desktop;
143
    }
144

    
145
    public void endResizingFrame(JComponent f) {
146
        super.endResizingFrame(f);
147
        resizeDesktop();
148
    }
149

    
150
    public void endDraggingFrame(JComponent f) {
151
        super.endDraggingFrame(f);
152
        resizeDesktop();
153
    }
154

    
155
    public void setNormalSize() {
156
        JScrollPane scrollPane = getScrollPane();
157
        int x = 0;
158
        int y = 0;
159
        Insets scrollInsets = getScrollPaneInsets();
160

    
161
        if (scrollPane != null) {
162
            Dimension d = scrollPane.getVisibleRect().getSize();
163
            if (scrollPane.getBorder() != null) {
164
                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
165
                        d.getHeight() - scrollInsets.top - scrollInsets.bottom);
166
            }
167

    
168
            d.setSize(d.getWidth() - 20, d.getHeight() - 20);
169
            desktop.setAllSize(x, y);
170
            scrollPane.invalidate();
171
            scrollPane.validate();
172
        }
173
    }
174

    
175
    private Insets getScrollPaneInsets() {
176
        JScrollPane scrollPane = getScrollPane();
177
        if (scrollPane == null) {
178
            return new Insets(0, 0, 0, 0);
179
        } else {
180
            return getScrollPane().getBorder().getBorderInsets(scrollPane);
181
        }
182
    }
183

    
184
    private JScrollPane getScrollPane() {
185
        if (desktop.getParent() instanceof JViewport) {
186
            JViewport viewPort = (JViewport) desktop.getParent();
187
            if (viewPort.getParent() instanceof JScrollPane) {
188
                return (JScrollPane) viewPort.getParent();
189
            }
190
        }
191
        return null;
192
    }
193

    
194
    protected void resizeDesktop() {
195
        int x = 0;
196
        int y = 0;
197
        JScrollPane scrollPane = getScrollPane();
198
        Insets scrollInsets = getScrollPaneInsets();
199

    
200
        if (scrollPane != null) {
201
            JInternalFrame allFrames[] = desktop.getAllFrames();
202
            for (int i = 0; i < allFrames.length; i++) {
203
                JInternalFrame frame = allFrames[i];
204
                // No tenemos en cuenta los frames maximizados para calcular el 
205
                // tama?o de la ventana.
206
                if (!frame.isMaximum()) { 
207
                    if (frame.getX() + frame.getWidth() > x) {
208
                        x = frame.getX() + frame.getWidth();
209
                    }
210
                    if (frame.getY() + frame.getHeight() > y) {
211
                        y = frame.getY() + frame.getHeight();
212
                    }
213
                }
214
            }
215
            Dimension d = scrollPane.getVisibleRect().getSize();
216
            if (scrollPane.getBorder() != null) {
217
                d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
218
                        d.getHeight() - scrollInsets.top - scrollInsets.bottom);
219
            }
220

    
221
            if (x <= d.getWidth()) {
222
                x = ((int) d.getWidth()) - 20;
223
            }
224
            if (y <= d.getHeight()) {
225
                y = ((int) d.getHeight()) - 20;
226
            }
227
            desktop.setAllSize(x, y);
228
            scrollPane.invalidate();
229
            scrollPane.validate();
230
        }
231
    }
232
}