1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package davmail.ui.tray;
20
21 import davmail.BundleMessage;
22 import davmail.DavGateway;
23 import davmail.Settings;
24 import davmail.ui.AboutFrame;
25 import davmail.ui.SettingsFrame;
26 import org.apache.log4j.Level;
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.awt.event.ActionListener;
31 import java.util.ArrayList;
32
33
34
35
36 public class FrameGatewayTray implements DavGatewayTrayInterface {
37 protected FrameGatewayTray() {
38 }
39
40 static JFrame mainFrame;
41 static AboutFrame aboutFrame;
42 static SettingsFrame settingsFrame;
43 private static JEditorPane errorArea;
44 private static JLabel errorLabel;
45 private static JEditorPane messageArea;
46 private static ArrayList<Image> frameIcons;
47 private static Image image;
48 private static Image activeImage;
49 private static Image inactiveImage;
50 private boolean isActive = true;
51
52
53
54
55
56
57 @Override
58 public java.util.List<Image> getFrameIcons() {
59 return frameIcons;
60 }
61
62
63
64
65 public void switchIcon() {
66 isActive = true;
67 SwingUtilities.invokeLater(() -> {
68 Image currentImage = mainFrame.getIconImage();
69 if (currentImage != null && currentImage.equals(image)) {
70 mainFrame.setIconImage(activeImage);
71 } else {
72 mainFrame.setIconImage(image);
73 }
74 });
75 }
76
77
78
79
80 public void resetIcon() {
81 SwingUtilities.invokeLater(() -> mainFrame.setIconImage(image));
82 }
83
84
85
86
87 public void inactiveIcon() {
88 isActive = false;
89 SwingUtilities.invokeLater(() -> mainFrame.setIconImage(inactiveImage));
90 }
91
92
93
94
95
96
97 public boolean isActive() {
98 return isActive;
99 }
100
101
102
103
104
105
106
107 public void displayMessage(final String message, final Level level) {
108 SwingUtilities.invokeLater(() -> {
109 if (errorArea != null && messageArea != null) {
110 if (level.equals(Level.INFO)) {
111 errorLabel.setIcon(UIManager.getIcon("OptionPane.informationIcon"));
112 errorArea.setText(message);
113 } else if (level.equals(Level.WARN)) {
114 errorLabel.setIcon(UIManager.getIcon("OptionPane.warningIcon"));
115 errorArea.setText(message);
116 } else if (level.equals(Level.ERROR)) {
117 errorLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));
118 errorArea.setText(message);
119 }
120 messageArea.setText(message);
121 }
122 });
123 }
124
125
126
127
128 public void about() {
129 SwingUtilities.invokeLater(() -> {
130 aboutFrame.update();
131 aboutFrame.setVisible(true);
132 aboutFrame.toFront();
133 aboutFrame.requestFocus();
134 });
135 }
136
137
138
139
140 public void preferences() {
141 SwingUtilities.invokeLater(() -> {
142 settingsFrame.reload();
143 settingsFrame.setVisible(true);
144 settingsFrame.toFront();
145 settingsFrame.repaint();
146 settingsFrame.requestFocus();
147 });
148 }
149
150
151
152
153 public void init() {
154 SwingUtilities.invokeLater(this::createAndShowGUI);
155 }
156
157 public void dispose() {
158
159 settingsFrame.dispose();
160 aboutFrame.dispose();
161 }
162
163 protected void buildMenu() {
164
165 JMenu menu = new JMenu(BundleMessage.format("UI_DAVMAIL_GATEWAY"));
166 JMenuBar menuBar = new JMenuBar();
167 menuBar.add(menu);
168 mainFrame.setJMenuBar(menuBar);
169
170
171 ActionListener aboutListener = e -> about();
172
173 JMenuItem aboutItem = new JMenuItem(BundleMessage.format("UI_ABOUT"));
174 aboutItem.addActionListener(aboutListener);
175 menu.add(aboutItem);
176
177
178
179 ActionListener settingsListener = e -> preferences();
180
181 JMenuItem defaultItem = new JMenuItem(BundleMessage.format("UI_SETTINGS"));
182 defaultItem.addActionListener(settingsListener);
183 menu.add(defaultItem);
184
185
186 ActionListener exitListener = e -> {
187 try {
188 DavGateway.stop();
189 } catch (Exception exc) {
190 DavGatewayTray.error(exc);
191 }
192
193 System.exit(0);
194 };
195
196 JMenuItem exitItem = new JMenuItem(BundleMessage.format("UI_EXIT"));
197 exitItem.addActionListener(exitListener);
198 menu.add(exitItem);
199 }
200
201 protected void createAndShowGUI() {
202
203 if (Settings.isLinux() && System.getProperty("swing.defaultlaf") == null) {
204 System.setProperty("swing.defaultlaf", UIManager.getCrossPlatformLookAndFeelClassName());
205 } else {
206 System.setProperty("swing.defaultlaf", UIManager.getSystemLookAndFeelClassName());
207 }
208 String imageName = AwtGatewayTray.TRAY_PNG;
209 String activeImageName = AwtGatewayTray.TRAY_ACTIVE_PNG;
210 String inactiveImageName = AwtGatewayTray.TRAY_INACTIVE_PNG;
211
212 if (Settings.isLinux()) {
213 imageName = AwtGatewayTray.TRAY128_PNG;
214 activeImageName = AwtGatewayTray.TRAY128_ACTIVE_PNG;
215 inactiveImageName = AwtGatewayTray.TRAY128_INACTIVE_PNG;
216 }
217 image = DavGatewayTray.loadImage(imageName);
218 activeImage = DavGatewayTray.loadImage(activeImageName);
219 inactiveImage = DavGatewayTray.loadImage(inactiveImageName);
220
221 frameIcons = new ArrayList<>();
222 frameIcons.add(image);
223 frameIcons.add(DavGatewayTray.loadImage(AwtGatewayTray.TRAY128_PNG));
224
225 mainFrame = new JFrame();
226 mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
227 mainFrame.setTitle(BundleMessage.format("UI_DAVMAIL_GATEWAY"));
228 mainFrame.setIconImages(frameIcons);
229
230 JPanel errorPanel = new JPanel();
231 errorPanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_LAST_MESSAGE")));
232 errorPanel.setLayout(new BoxLayout(errorPanel, BoxLayout.X_AXIS));
233 errorArea = new JTextPane();
234 errorArea.setEditable(false);
235 errorArea.setBackground(mainFrame.getBackground());
236 errorLabel = new JLabel();
237 errorPanel.add(errorLabel);
238 errorPanel.add(errorArea);
239
240 JPanel messagePanel = new JPanel();
241 messagePanel.setBorder(BorderFactory.createTitledBorder(BundleMessage.format("UI_LAST_LOG")));
242 messagePanel.setLayout(new BoxLayout(messagePanel, BoxLayout.X_AXIS));
243
244 messageArea = new JTextPane();
245 messageArea.setText(BundleMessage.format("LOG_STARTING_DAVMAIL"));
246 messageArea.setEditable(false);
247 messageArea.setBackground(mainFrame.getBackground());
248 messagePanel.add(messageArea);
249
250 JPanel mainPanel = new JPanel();
251 mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
252 mainPanel.add(errorPanel);
253 mainPanel.add(messagePanel);
254 mainFrame.add(mainPanel);
255
256 aboutFrame = new AboutFrame();
257 settingsFrame = new SettingsFrame();
258 buildMenu();
259
260 mainFrame.setMinimumSize(new Dimension(400, 250));
261 mainFrame.pack();
262
263 if (mainFrame.getSize().width < 400 || mainFrame.getSize().height < 180) {
264 mainFrame.setSize(Math.max(mainFrame.getSize().width, 400),
265 Math.max(mainFrame.getSize().height, 180));
266 }
267
268 mainFrame.setLocation(mainFrame.getToolkit().getScreenSize().width / 2 -
269 mainFrame.getSize().width / 2,
270 mainFrame.getToolkit().getScreenSize().height / 2 -
271 mainFrame.getSize().height / 2);
272 mainFrame.setVisible(true);
273
274
275 if (Settings.isFirstStart()) {
276 settingsFrame.setVisible(true);
277 settingsFrame.toFront();
278 settingsFrame.repaint();
279 settingsFrame.requestFocus();
280 }
281 }
282 }