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 javax.imageio.ImageIO;
33
34 import de.spiritscorp.datasync.Main;
35 import de.spiritscorp.datasync.controller.BgController;
36 import de.spiritscorp.datasync.io.Debug;
37 import javafx.application.Platform;
38
39
40
41
42
43
44
45 public class BgView {
46
47 private TrayIcon trayIcon;
48 private final BgController controller;
49
50
51
52
53
54
55 public BgView( BgController controller ) {
56 this.controller = controller;
57 if( SystemTray.isSupported() ) {
58 initTray();
59 }else {
60 Debug.printDebug( "[DataSync Core] SystemTray wird von diesem Betriebssystem nicht unterstützt." );
61 }
62 }
63
64 private void initTray() {
65 final PopupMenu popup = new PopupMenu();
66 final Font font = new Font( "Comic Sans MS", Font.PLAIN, 14 );
67
68 final MenuItem openItem = new MenuItem( "öffnen" );
69 openItem.setFont( font );
70
71 openItem.addActionListener( e -> Platform.runLater( () -> {
72 controller.interruptBgJob( Main.BACKGROUND_THREAD_TIMEOUT );
73 } ) );
74
75 final MenuItem closeItem = new MenuItem( "schließen" );
76 closeItem.setFont( font );
77 closeItem.addActionListener( e -> Platform.runLater( controller::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( final IOException e ) {
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( e -> Platform.runLater( () -> controller.interruptBgJob( Main.BACKGROUND_THREAD_TIMEOUT ) ) );
97 }
98
99
100
101
102 public TrayIcon getTrayIcon() { return trayIcon; }
103
104 @Override
105 public int hashCode() {
106 return Objects.hash( controller, 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( controller, other.controller ) && Objects.equals( trayIcon, other.trayIcon );
116 }
117 }