1 package org.woehlke.computer.kurzweil.application;
2
3 import lombok.Getter;
4 import lombok.ToString;
5 import lombok.extern.log4j.Log4j2;
6 import org.woehlke.computer.kurzweil.commons.has.HasPanelSubtitle;
7 import org.woehlke.computer.kurzweil.tabs.ComputerKurzweilTabbedPane;
8 import org.woehlke.computer.kurzweil.tabs.TabPanel;
9 import org.woehlke.computer.kurzweil.commons.layouts.BoxLayoutVertical;
10 import org.woehlke.computer.kurzweil.commons.Startable;
11 import org.woehlke.computer.kurzweil.commons.gui.GuiComponent;
12 import org.woehlke.computer.kurzweil.commons.widgets.PanelCopyright;
13 import org.woehlke.computer.kurzweil.commons.widgets.PanelSubtitle;
14
15 import javax.accessibility.Accessible;
16 import javax.swing.*;
17 import javax.swing.border.CompoundBorder;
18 import java.awt.*;
19 import java.awt.event.*;
20 import java.awt.image.ImageObserver;
21 import java.beans.Transient;
22 import java.io.Serializable;
23 import java.util.List;
24
25 @Log4j2
26 @Getter
27 @ToString(callSuper=true)
28 public class ComputerKurzweilFrame extends JFrame implements Serializable,
29 MenuContainer,
30 ImageObserver,
31 Accessible,
32 WindowListener,
33 WindowFocusListener,
34 WindowStateListener,
35 Startable,
36 GuiComponent, HasPanelSubtitle {
37
38 private final ComputerKurzweilContext ctx;
39 private final ComputerKurzweilTabbedPane tabbedPane;
40 private final ComputerKurzweilMenuBar jMenuBar;
41 private final PanelCopyright panelCopyright;
42 private final PanelSubtitle panelSubtitle;
43 private final BoxLayoutVertical layout;
44 private final CompoundBorder border;
45
46 public ComputerKurzweilFrame(
47 ComputerKurzweilProperties properties
48 ) throws HeadlessException {
49
50 setTitle(properties.getAllinone().getView().getTitle());
51 this.ctx = new ComputerKurzweilContext(properties,this);
52 panelSubtitle = PanelSubtitle.getPanelSubtitleForApplication(this.ctx);
53 tabbedPane = new ComputerKurzweilTabbedPane(this.ctx);
54 panelCopyright = new PanelCopyright(this.ctx);
55 border = this.ctx.getFrameBorder();
56
57 jMenuBar = new ComputerKurzweilMenuBar(this.ctx);
58
59 this.setJMenuBar(jMenuBar);
60 Container contentPane = this.getContentPane();
61 layout = new BoxLayoutVertical( contentPane );
62 contentPane.add(panelSubtitle);
63 contentPane.add(tabbedPane);
64 contentPane.add(panelCopyright);
65
66 contentPane.setLayout(layout);
67 this.setVisible(true);
68 pack();
69 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
70 addWindowListener(this);
71 showMe();
72 }
73
74 public List<TabPanel> getApps(){
75 return tabbedPane.getApps();
76 }
77
78 public void showMe() {
79 pack();
80 Rectangle r = this.getFrameBounds();
81 this.setBounds(r);
82 this.setVisible(true);
83 toFront();
84 repaint();
85 }
86
87 public void windowOpened(WindowEvent e) {
88 showMe();
89 }
90
91 public void windowClosing(WindowEvent e) {
92 exit();
93 }
94
95 public void windowClosed(WindowEvent e) {
96 exit();
97 }
98
99 public void windowIconified(WindowEvent e) { }
100
101 public void windowDeiconified(WindowEvent e) {
102 showMe();
103 }
104
105 public void windowActivated(WindowEvent e) {
106 toFront();
107
108 }
109
110 public void windowDeactivated(WindowEvent e) {
111
112 }
113
114 @Override
115 public void windowGainedFocus(WindowEvent e) {}
116
117 @Override
118 public void windowLostFocus(WindowEvent e) {}
119
120 @Override
121 public void windowStateChanged(WindowEvent e) {}
122
123 public void exit() {
124 this.dispose();
125 System.exit(0);
126 }
127
128 @Override
129 public void start(){
130 log.info("start");
131 showMe();
132 tabbedPane.start();
133 log.info("started");
134 }
135
136 @Override
137 public void stop() {
138 log.info("stop");
139 tabbedPane.stop();
140 log.info("stopped");
141 }
142
143 @Transient
144 public Rectangle getFrameBounds(){
145 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
146 double x = this.getWidth();
147 double y = this.getHeight();
148 double startX = 0d;
149 double startY = 0d;
150 double twoParts = 2d;
151 if(x < screenSize.getWidth()){
152 startX = (screenSize.getWidth() - x ) / twoParts;
153 }
154 if(y < screenSize.getHeight()){
155 startY = (screenSize.getHeight() - y) / twoParts;
156 }
157 int myheight = Double.valueOf( y ).intValue();
158 int mywidth = Double.valueOf( x ).intValue();
159 int mystartX = Double.valueOf( startX ).intValue();
160 int mystartY = Double.valueOf( startY ).intValue();
161 return new Rectangle( mystartX, mystartY, mywidth, myheight );
162 }
163
164 }