Subversion Repositories HomeAutomation

Rev

Rev 15 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 15 Rev 73
1
/**
1
/**
2
 * This code is from:
2
 * This code is from:
3
 * http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-mdi.html
3
 * http://www.javaworld.com/javaworld/jw-05-2001/jw-0525-mdi.html
4
 */
4
 */
5
 
5
 
6
package Macbeth.Utilities;
6
package Macbeth.Utilities;
7
 
7
 
8
import javax.swing.*;
8
import javax.swing.*;
9
import java.awt.*;
9
import java.awt.*;
10
import java.beans.*;
10
import java.beans.*;
11
 
11
 
12
/**
12
/**
13
 * An extension of WDesktopPane that supports often used MDI functionality. This
13
 * An extension of WDesktopPane that supports often used MDI functionality. This
14
 * class also handles setting scroll bars for when windows move too far to the left or
14
 * class also handles setting scroll bars for when windows move too far to the left or
15
 * bottom, providing the MDIDesktopPane is in a ScrollPane.
15
 * bottom, providing the MDIDesktopPane is in a ScrollPane.
16
 */
16
 */
17
public class MDIDesktopPane extends JDesktopPane {
17
public class MDIDesktopPane extends JDesktopPane {
18
    private static int FRAME_OFFSET=20;
18
    private static int FRAME_OFFSET=20;
19
    private MDIDesktopManager manager;
19
    private MDIDesktopManager manager;
20
 
20
 
21
    public MDIDesktopPane() {
21
    public MDIDesktopPane() {
22
        manager=new MDIDesktopManager(this);
22
        manager=new MDIDesktopManager(this);
23
        setDesktopManager(manager);
23
        setDesktopManager(manager);
24
        setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
24
        setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
25
    }
25
    }
26
 
26
 
27
    public void setBounds(int x, int y, int w, int h) {
27
    public void setBounds(int x, int y, int w, int h) {
28
        super.setBounds(x,y,w,h);
28
        super.setBounds(x,y,w,h);
29
        checkDesktopSize();
29
        checkDesktopSize();
30
    }
30
    }
31
 
31
 
32
    public Component add(JInternalFrame frame) {
32
    public Component add(JInternalFrame frame) {
33
        JInternalFrame[] array = getAllFrames();
33
        JInternalFrame[] array = getAllFrames();
34
        Point p;
34
        Point p;
35
        int w;
35
        int w;
36
        int h;
36
        int h;
37
 
37
 
38
        Component retval=super.add(frame);
38
        Component retval=super.add(frame);
39
        checkDesktopSize();
39
        checkDesktopSize();
40
        if (array.length > 0) {
40
        if (array.length > 0) {
41
            p = array[0].getLocation();
41
            p = array[0].getLocation();
42
            p.x = p.x + FRAME_OFFSET;
42
            p.x = p.x + FRAME_OFFSET;
43
            p.y = p.y + FRAME_OFFSET;
43
            p.y = p.y + FRAME_OFFSET;
44
        }
44
        }
45
        else {
45
        else {
46
            p = new Point(0, 0);
46
            p = new Point(0, 0);
47
        }
47
        }
48
        frame.setLocation(p.x, p.y);
48
        frame.setLocation(p.x, p.y);
49
        /* Jimmy: this is a bit buggy (frames get constant size)
49
        /* Jimmy: this is a bit buggy (frames get constant size)
50
        if (frame.isResizable()) {
50
        if (frame.isResizable()) {
51
            w = getWidth() - (getWidth()/3);
51
            w = getWidth() - (getWidth()/3);
52
            h = getHeight() - (getHeight()/3);
52
            h = getHeight() - (getHeight()/3);
53
            if (w < frame.getMinimumSize().getWidth()) w = (int)frame.getMinimumSize().getWidth();
53
            if (w < frame.getMinimumSize().getWidth()) w = (int)frame.getMinimumSize().getWidth();
54
            if (h < frame.getMinimumSize().getHeight()) h = (int)frame.getMinimumSize().getHeight();
54
            if (h < frame.getMinimumSize().getHeight()) h = (int)frame.getMinimumSize().getHeight();
55
            frame.setSize(w, h);
55
            frame.setSize(w, h);
56
        }*/
56
        }*/
57
        moveToFront(frame);
57
        moveToFront(frame);
58
        frame.setVisible(true);
58
        frame.setVisible(true);
59
        try {
59
        try {
60
            frame.setSelected(true);
60
            frame.setSelected(true);
61
        } catch (PropertyVetoException e) {
61
        } catch (PropertyVetoException e) {
62
            frame.toBack();
62
            frame.toBack();
63
        }
63
        }
64
        return retval;
64
        return retval;
65
    }
65
    }
66
 
66
 
67
    public void remove(Component c) {
67
    public void remove(Component c) {
68
        super.remove(c);
68
        super.remove(c);
69
        checkDesktopSize();
69
        checkDesktopSize();
70
    }
70
    }
71
 
71
 
72
    /**
72
    /**
73
     * Cascade all internal frames
73
     * Cascade all internal frames
74
     */
74
     */
75
    public void cascadeFrames() {
75
    public void cascadeFrames() {
76
        int x = 0;
76
        int x = 0;
77
        int y = 0;
77
        int y = 0;
78
        JInternalFrame allFrames[] = getAllFrames();
78
        JInternalFrame allFrames[] = getAllFrames();
79
 
79
 
80
        manager.setNormalSize();
80
        manager.setNormalSize();
81
        int frameHeight = (getBounds().height - 5) - allFrames.length * FRAME_OFFSET;
81
        int frameHeight = (getBounds().height - 5) - allFrames.length * FRAME_OFFSET;
82
        int frameWidth = (getBounds().width - 5) - allFrames.length * FRAME_OFFSET;
82
        int frameWidth = (getBounds().width - 5) - allFrames.length * FRAME_OFFSET;
83
        for (int i = allFrames.length - 1; i >= 0; i--) {
83
        for (int i = allFrames.length - 1; i >= 0; i--) {
84
            allFrames[i].setSize(frameWidth,frameHeight);
84
            allFrames[i].setSize(frameWidth,frameHeight);
85
            allFrames[i].setLocation(x,y);
85
            allFrames[i].setLocation(x,y);
86
            x = x + FRAME_OFFSET;
86
            x = x + FRAME_OFFSET;
87
            y = y + FRAME_OFFSET;
87
            y = y + FRAME_OFFSET;
88
        }
88
        }
89
    }
89
    }
90
 
90
 
91
    /**
91
    /**
92
     * Tile all internal frames
92
     * Tile all internal frames
93
     */
93
     */
94
    public void tileFrames() {
94
    public void tileFrames() {
95
        java.awt.Component allFrames[] = getAllFrames();
95
        java.awt.Component allFrames[] = getAllFrames();
96
        manager.setNormalSize();
96
        manager.setNormalSize();
97
        int frameHeight = getBounds().height/allFrames.length;
97
        int frameHeight = getBounds().height/allFrames.length;
98
        int y = 0;
98
        int y = 0;
99
        for (int i = 0; i < allFrames.length; i++) {
99
        for (int i = 0; i < allFrames.length; i++) {
100
            allFrames[i].setSize(getBounds().width,frameHeight);
100
            allFrames[i].setSize(getBounds().width,frameHeight);
101
            allFrames[i].setLocation(0,y);
101
            allFrames[i].setLocation(0,y);
102
            y = y + frameHeight;
102
            y = y + frameHeight;
103
        }
103
        }
104
    }
104
    }
105
 
105
 
106
    /**
106
    /**
107
     * Sets all component size properties ( maximum, minimum, preferred)
107
     * Sets all component size properties ( maximum, minimum, preferred)
108
     * to the given dimension.
108
     * to the given dimension.
109
     */
109
     */
110
    public void setAllSize(Dimension d){
110
    public void setAllSize(Dimension d){
111
        setMinimumSize(d);
111
        setMinimumSize(d);
112
        setMaximumSize(d);
112
        setMaximumSize(d);
113
        setPreferredSize(d);
113
        setPreferredSize(d);
114
    }
114
    }
115
 
115
 
116
    /**
116
    /**
117
     * Sets all component size properties ( maximum, minimum, preferred)
117
     * Sets all component size properties ( maximum, minimum, preferred)
118
     * to the given width and height.
118
     * to the given width and height.
119
     */
119
     */
120
    public void setAllSize(int width, int height){
120
    public void setAllSize(int width, int height){
121
        setAllSize(new Dimension(width,height));
121
        setAllSize(new Dimension(width,height));
122
    }
122
    }
123
 
123
 
124
    private void checkDesktopSize() {
124
    private void checkDesktopSize() {
125
        if (getParent()!=null&&isVisible()) manager.resizeDesktop();
125
        if (getParent()!=null&&isVisible()) manager.resizeDesktop();
126
    }
126
    }
127
}
127
}
128
 
128
 
129
/**
129
/**
130
 * Private class used to replace the standard DesktopManager for JDesktopPane.
130
 * Private class used to replace the standard DesktopManager for JDesktopPane.
131
 * Used to provide scrollbar functionality.
131
 * Used to provide scrollbar functionality.
132
 */
132
 */
133
class MDIDesktopManager extends DefaultDesktopManager {
133
class MDIDesktopManager extends DefaultDesktopManager {
134
    private MDIDesktopPane desktop;
134
    private MDIDesktopPane desktop;
135
 
135
 
136
    public MDIDesktopManager(MDIDesktopPane desktop) {
136
    public MDIDesktopManager(MDIDesktopPane desktop) {
137
        this.desktop = desktop;
137
        this.desktop = desktop;
138
    }
138
    }
139
 
139
 
140
    public void endResizingFrame(JComponent f) {
140
    public void endResizingFrame(JComponent f) {
141
        super.endResizingFrame(f);
141
        super.endResizingFrame(f);
142
        resizeDesktop();
142
        resizeDesktop();
143
    }
143
    }
144
 
144
 
145
    public void endDraggingFrame(JComponent f) {
145
    public void endDraggingFrame(JComponent f) {
146
        super.endDraggingFrame(f);
146
        super.endDraggingFrame(f);
147
        resizeDesktop();
147
        resizeDesktop();
148
    }
148
    }
149
 
149
 
150
    public void setNormalSize() {
150
    public void setNormalSize() {
151
        JScrollPane scrollPane=getScrollPane();
151
        JScrollPane scrollPane=getScrollPane();
152
        int x = 0;
152
        int x = 0;
153
        int y = 0;
153
        int y = 0;
154
        Insets scrollInsets = getScrollPaneInsets();
154
        Insets scrollInsets = getScrollPaneInsets();
155
 
155
 
156
        if (scrollPane != null) {
156
        if (scrollPane != null) {
157
            Dimension d = scrollPane.getVisibleRect().getSize();
157
            Dimension d = scrollPane.getVisibleRect().getSize();
158
            if (scrollPane.getBorder() != null) {
158
            if (scrollPane.getBorder() != null) {
159
               d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
159
               d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
160
                         d.getHeight() - scrollInsets.top - scrollInsets.bottom);
160
                         d.getHeight() - scrollInsets.top - scrollInsets.bottom);
161
            }
161
            }
162
 
162
 
163
            d.setSize(d.getWidth() - 20, d.getHeight() - 20);
163
            d.setSize(d.getWidth() - 20, d.getHeight() - 20);
164
            desktop.setAllSize(x,y);
164
            desktop.setAllSize(x,y);
165
            scrollPane.invalidate();
165
            scrollPane.invalidate();
166
            scrollPane.validate();
166
            scrollPane.validate();
167
        }
167
        }
168
    }
168
    }
169
 
169
 
170
    private Insets getScrollPaneInsets() {
170
    private Insets getScrollPaneInsets() {
171
        JScrollPane scrollPane=getScrollPane();
171
        JScrollPane scrollPane=getScrollPane();
172
        if (scrollPane==null) return new Insets(0,0,0,0);
172
        if (scrollPane==null) return new Insets(0,0,0,0);
173
        else return getScrollPane().getBorder().getBorderInsets(scrollPane);
173
        else return getScrollPane().getBorder().getBorderInsets(scrollPane);
174
    }
174
    }
175
 
175
 
176
    private JScrollPane getScrollPane() {
176
    private JScrollPane getScrollPane() {
177
        if (desktop.getParent() instanceof JViewport) {
177
        if (desktop.getParent() instanceof JViewport) {
178
            JViewport viewPort = (JViewport)desktop.getParent();
178
            JViewport viewPort = (JViewport)desktop.getParent();
179
            if (viewPort.getParent() instanceof JScrollPane)
179
            if (viewPort.getParent() instanceof JScrollPane)
180
                return (JScrollPane)viewPort.getParent();
180
                return (JScrollPane)viewPort.getParent();
181
        }
181
        }
182
        return null;
182
        return null;
183
    }
183
    }
184
 
184
 
185
    protected void resizeDesktop() {
185
    protected void resizeDesktop() {
186
        int x = 0;
186
        int x = 0;
187
        int y = 0;
187
        int y = 0;
188
        JScrollPane scrollPane = getScrollPane();
188
        JScrollPane scrollPane = getScrollPane();
189
        Insets scrollInsets = getScrollPaneInsets();
189
        Insets scrollInsets = getScrollPaneInsets();
190
 
190
 
191
        if (scrollPane != null) {
191
        if (scrollPane != null) {
192
            JInternalFrame allFrames[] = desktop.getAllFrames();
192
            JInternalFrame allFrames[] = desktop.getAllFrames();
193
            for (int i = 0; i < allFrames.length; i++) {
193
            for (int i = 0; i < allFrames.length; i++) {
194
                if (allFrames[i].getX()+allFrames[i].getWidth()>x) {
194
                if (allFrames[i].getX()+allFrames[i].getWidth()>x) {
195
                    x = allFrames[i].getX() + allFrames[i].getWidth();
195
                    x = allFrames[i].getX() + allFrames[i].getWidth();
196
                }
196
                }
197
                if (allFrames[i].getY()+allFrames[i].getHeight()>y) {
197
                if (allFrames[i].getY()+allFrames[i].getHeight()>y) {
198
                    y = allFrames[i].getY() + allFrames[i].getHeight();
198
                    y = allFrames[i].getY() + allFrames[i].getHeight();
199
                }
199
                }
200
            }
200
            }
201
            Dimension d=scrollPane.getVisibleRect().getSize();
201
            Dimension d=scrollPane.getVisibleRect().getSize();
202
            if (scrollPane.getBorder() != null) {
202
            if (scrollPane.getBorder() != null) {
203
               d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
203
               d.setSize(d.getWidth() - scrollInsets.left - scrollInsets.right,
204
                         d.getHeight() - scrollInsets.top - scrollInsets.bottom);
204
                         d.getHeight() - scrollInsets.top - scrollInsets.bottom);
205
            }
205
            }
206
 
206
 
207
            if (x <= d.getWidth()) x = ((int)d.getWidth()) - 20;
207
            if (x <= d.getWidth()) x = ((int)d.getWidth()) - 20;
208
            if (y <= d.getHeight()) y = ((int)d.getHeight()) - 20;
208
            if (y <= d.getHeight()) y = ((int)d.getHeight()) - 20;
209
            desktop.setAllSize(x,y);
209
            desktop.setAllSize(x,y);
210
            scrollPane.invalidate();
210
            scrollPane.invalidate();
211
            scrollPane.validate();
211
            scrollPane.validate();
212
        }
212
        }
213
    }
213
    }
214
}
214
}