summaryrefslogtreecommitdiff
path: root/sources/smstrade/SFrame.java
blob: 0c223a5199a8c734abc9ff51342e3e48e72cbd48 (plain)
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
/* JSMSTrade v0.2
 * Copyright (c) 2008, 2009 Dipl.-Inform. (FH) Paul C. Buetow
 * jsmstrade@dev.buetow.org - http://jsmstrade.buetow.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * All icons of the icons/ folder are 	under a Creative Commons
 * Attribution-Noncommercial-Share Alike License a CC-by-nc-sa.
 */

package smstrade;

import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

/**
 * The class SFrame. All frames of the tool extend this SFrame class.
 * This class makes sure that all 'subwindows' get closed if its parent gets
 * closed. And it also makes sure to open new windows relative to its parent.
 *
 * @author Paul C. Buetow
 */
public class SFrame extends JFrame {
    /** The serial version uid */
    private static final long serialVersionUID = 1L;

    /** The Constant X_LOCATION_OFFSET. */
    private final static int X_LOCATION_OFFSET = 40;

    /** The Constant Y_LOCATION_OFFSET. */
    private final static int Y_LOCATION_OFFSET = 80;

    /** The parent window/component. */
    private Component parent;

    /** True, if the current window will get disposed with its parent.  */
    private boolean dispose;

    /**
     * Instantiates a SFrame object.
     *
     * @param title the title
     * @param parent the parent
     */
    public SFrame(String title, Component parent) {
        super(title);
        init(parent);
    }

    /**
     * Instantiates a new SFrame object.
     *
     * @param title the title
     */
    public SFrame(String title) {
        super(title);
        init(null);
    }

    /**
     * Inits the SFrame.
     *
     * @param parent the parent
     */
    private void init(Component parent) {
        this.parent = parent;
        this.dispose = false;
    }

    /**
     * Dispose with its parent.
     */
    public void disposeWithParent() {
        if (!dispose && parent != null && parent instanceof Window) {
            Window window = (Window) parent;
            window.addWindowListener(new WindowAdapter() {
                public void windowClosed(WindowEvent we) {
                    SFrame.this.dispose();
                }
            });
        }
        dispose = true;
    }

    /**
     * Sets the correct location of the window.
     */
    private void setCorrectLocation() {
        int x = 0, y = 0;
        final Dimension screenSize =
            Toolkit.getDefaultToolkit().getScreenSize();

        if (parent == null) {
            x = (int) (screenSize.width - getWidth()) / 2;
            y = 50;//(int) (screenSize.height - getHeight()) / 2;

        } else {
            final Point location = parent.getLocation();
            x = (int) location.getX() + X_LOCATION_OFFSET;
            y = (int) location.getY() + Y_LOCATION_OFFSET;
        }

        if (x + super.getWidth() >= screenSize.width)
            x = screenSize.width - super.getWidth();
        else if (x < 0)
            x = 0;

        if (y + super.getHeight() >= screenSize.height)
            y = screenSize.height - super.getHeight();

        super.setLocation(x, y);
    }

    /* (non-Javadoc)
     * @see java.awt.Window#setSize(int, int)
     */
    public void setSize(int width, int height) {
        super.setSize(width, height);
        setCorrectLocation();
    }
}