1 package de.spiritscorp.datasync.gui;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.awt.Font;
24 import java.awt.MenuItem;
25 import java.awt.PopupMenu;
26 import java.awt.SystemTray;
27 import java.awt.TrayIcon;
28 import java.awt.image.BufferedImage;
29 import java.io.IOException;
30 import java.util.Objects;
31
32 import javafx.application.Platform;
33
34 import javax.imageio.ImageIO;
35
36 import de.spiritscorp.datasync.controller.BgController;
37 import de.spiritscorp.datasync.controller.MainViewController;
38 import de.spiritscorp.datasync.io.Debug;
39
40
41
42
43
44
45 public class BgView {
46
47
48 private TrayIcon trayIcon;
49
50 private final BgController bgController;
51
52
53
54
55
56
57 public BgView( final BgController bgController ) {
58 this.bgController = bgController;
59 if( SystemTray.isSupported() ) {
60 initTray();
61 }else {
62 Debug.printDebug( "[DataSync Core] SystemTray wird von diesem Betriebssystem nicht unterstützt." );
63 }
64 }
65
66 private void initTray() {
67 final PopupMenu popup = new PopupMenu();
68 final Font font = new Font( "Comic Sans MS", Font.PLAIN, 14 );
69
70 final MenuItem openItem = new MenuItem( "öffnen" );
71 openItem.setFont( font );
72
73 openItem.addActionListener( _ -> Platform.runLater( () -> bgController.interruptBgJob( MainViewController.BG_TIMEOUT ) ) );
74
75 final MenuItem closeItem = new MenuItem( "schließen" );
76 closeItem.setFont( font );
77 closeItem.addActionListener( _ -> Platform.runLater( bgController::requestApplicationShutdown ) );
78
79 popup.add( openItem );
80 popup.addSeparator();
81 popup.add( closeItem );
82
83
84 BufferedImage trayImage;
85 try {
86 trayImage = ImageIO.read( getClass().getResourceAsStream( "/icons/16x16.png" ) );
87 }catch( IOException _ ) {
88 Debug.printError( "[DataSync] Tray-Icon nicht gefunden, erstelle Fallback-Pixel." );
89 trayImage = new BufferedImage( 16, 16, BufferedImage.TYPE_INT_ARGB );
90 }
91
92 this.trayIcon = new TrayIcon( trayImage, "DataSync Platform Console", popup );
93 this.trayIcon.setImageAutoSize( true );
94
95
96 this.trayIcon.addActionListener( _ -> Platform.runLater( () -> bgController.interruptBgJob( MainViewController.BG_TIMEOUT ) ) );
97 }
98
99
100
101
102 public TrayIcon getTrayIcon() { return trayIcon; }
103
104 @Override
105 public int hashCode() {
106 return Objects.hash( bgController, trayIcon );
107 }
108
109 @Override
110 public boolean equals( final Object obj ) {
111 if( this == obj ) { return true; }
112 if( obj == null ) { return false; }
113 if( getClass() != obj.getClass() ) { return false; }
114 final BgView other = (BgView) obj;
115 return Objects.equals( bgController, other.bgController ) && Objects.equals( trayIcon, other.trayIcon );
116 }
117 }