2015-01-15 23:22:22 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2010-2014 Laurent CLOUET
|
|
|
|
|
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
|
|
|
|
*
|
|
|
|
|
* 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; version 2
|
|
|
|
|
* of the License.
|
|
|
|
|
*
|
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.sheepit.client.standalone;
|
|
|
|
|
|
2015-08-05 19:35:13 +01:00
|
|
|
import java.awt.AWTException;
|
2015-07-29 16:51:34 -06:00
|
|
|
import java.awt.GridBagConstraints;
|
|
|
|
|
import java.awt.GridBagLayout;
|
2015-08-05 19:35:13 +01:00
|
|
|
import java.awt.Image;
|
|
|
|
|
import java.awt.MenuItem;
|
|
|
|
|
import java.awt.PopupMenu;
|
|
|
|
|
import java.awt.SystemTray;
|
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
|
import java.awt.TrayIcon;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
2015-10-27 20:41:20 +00:00
|
|
|
import java.awt.event.WindowEvent;
|
|
|
|
|
import java.awt.event.WindowStateListener;
|
2015-01-15 23:22:22 +01:00
|
|
|
import java.net.URL;
|
|
|
|
|
|
|
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
import javax.swing.JFrame;
|
2015-07-29 16:51:34 -06:00
|
|
|
import javax.swing.JLabel;
|
2015-01-15 23:22:22 +01:00
|
|
|
import javax.swing.JPanel;
|
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
|
import javax.swing.UnsupportedLookAndFeelException;
|
|
|
|
|
import javax.swing.border.EmptyBorder;
|
|
|
|
|
|
|
|
|
|
import com.sheepit.client.Client;
|
|
|
|
|
import com.sheepit.client.Configuration;
|
|
|
|
|
import com.sheepit.client.Gui;
|
|
|
|
|
import com.sheepit.client.standalone.swing.activity.Settings;
|
|
|
|
|
import com.sheepit.client.standalone.swing.activity.Working;
|
|
|
|
|
|
|
|
|
|
public class GuiSwing extends JFrame implements Gui {
|
2015-04-08 20:32:24 +01:00
|
|
|
public static final String type = "swing";
|
2015-01-15 23:22:22 +01:00
|
|
|
public enum ActivityType {
|
|
|
|
|
WORKING, SETTINGS
|
2015-01-25 18:26:21 +00:00
|
|
|
}
|
2015-01-15 23:22:22 +01:00
|
|
|
|
2015-09-07 20:10:06 +01:00
|
|
|
private SystemTray sysTray;
|
2015-01-15 23:22:22 +01:00
|
|
|
private JPanel panel;
|
|
|
|
|
private Working activityWorking;
|
|
|
|
|
private Settings activitySettings;
|
2015-08-05 19:35:13 +01:00
|
|
|
private TrayIcon trayIcon;
|
2016-04-28 19:37:05 +02:00
|
|
|
private boolean useSysTray;
|
2015-01-15 23:22:22 +01:00
|
|
|
|
|
|
|
|
private int framesRendered;
|
|
|
|
|
|
|
|
|
|
private boolean waitingForAuthentication;
|
|
|
|
|
private Client client;
|
|
|
|
|
|
2015-01-27 21:44:32 +00:00
|
|
|
private ThreadClient threadClient;
|
|
|
|
|
|
2016-04-28 19:37:05 +02:00
|
|
|
public GuiSwing(boolean useSysTray_) {
|
2015-01-15 23:22:22 +01:00
|
|
|
framesRendered = 0;
|
2016-04-28 19:37:05 +02:00
|
|
|
useSysTray = useSysTray_;
|
2015-01-15 23:22:22 +01:00
|
|
|
waitingForAuthentication = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void start() {
|
|
|
|
|
try {
|
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
|
}
|
|
|
|
|
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
|
|
|
|
|
e1.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-28 19:37:05 +02:00
|
|
|
if (useSysTray) {
|
|
|
|
|
try {
|
|
|
|
|
sysTray = SystemTray.getSystemTray();
|
|
|
|
|
if (SystemTray.isSupported()) {
|
|
|
|
|
addWindowStateListener(new WindowStateListener() {
|
|
|
|
|
public void windowStateChanged(WindowEvent e) {
|
|
|
|
|
if (e.getNewState() == ICONIFIED) {
|
|
|
|
|
hideToTray();
|
|
|
|
|
}
|
2015-10-27 20:41:20 +00:00
|
|
|
}
|
2016-04-28 19:37:05 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedOperationException e) {
|
|
|
|
|
sysTray = null;
|
2015-10-27 20:41:20 +00:00
|
|
|
}
|
2015-09-07 20:10:06 +01:00
|
|
|
}
|
2016-04-28 19:37:05 +02:00
|
|
|
|
2015-09-07 20:10:06 +01:00
|
|
|
|
2015-01-15 23:22:22 +01:00
|
|
|
URL iconUrl = getClass().getResource("/icon.png");
|
|
|
|
|
if (iconUrl != null) {
|
|
|
|
|
ImageIcon img = new ImageIcon(iconUrl);
|
|
|
|
|
setIconImage(img.getImage());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTitle("SheepIt Render Farm");
|
|
|
|
|
setSize(600, 600);
|
|
|
|
|
|
|
|
|
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
|
|
panel = new JPanel();
|
2015-07-29 16:51:34 -06:00
|
|
|
panel.setLayout(new GridBagLayout());
|
2015-01-15 23:22:22 +01:00
|
|
|
setContentPane(this.panel);
|
|
|
|
|
panel.setBorder(new EmptyBorder(5, 5, 5, 5));
|
|
|
|
|
|
|
|
|
|
activityWorking = new Working(this);
|
|
|
|
|
activitySettings = new Settings(this);
|
|
|
|
|
|
|
|
|
|
this.showActivity(ActivityType.SETTINGS);
|
|
|
|
|
|
|
|
|
|
while (waitingForAuthentication) {
|
|
|
|
|
try {
|
|
|
|
|
synchronized (this) {
|
|
|
|
|
wait();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void stop() {
|
2015-08-05 19:58:11 +01:00
|
|
|
System.exit(0);
|
2015-01-15 23:22:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void status(String msg_) {
|
|
|
|
|
if (activityWorking != null) {
|
|
|
|
|
this.activityWorking.setStatus(msg_);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void error(String msg_) {
|
|
|
|
|
status(msg_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void AddFrameRendered() {
|
|
|
|
|
framesRendered++;
|
|
|
|
|
|
|
|
|
|
if (activityWorking != null) {
|
|
|
|
|
this.activityWorking.setRenderedFrame(framesRendered);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-05-26 20:14:18 +01:00
|
|
|
System.out.println("GuiSwing::AddFrameRendered() error: no working activity");
|
2015-01-15 23:22:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void framesRemaining(int n) {
|
|
|
|
|
if (activityWorking != null) {
|
|
|
|
|
this.activityWorking.setRemainingFrame(n);
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-05-26 20:14:18 +01:00
|
|
|
System.out.println("GuiSwing::framesRemaining(" + n + ") error: no working activity");
|
2015-01-15 23:22:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Client getClient() {
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void setClient(Client cli) {
|
|
|
|
|
client = cli;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-29 16:51:34 -06:00
|
|
|
public void addPadding(int x, int y, int width, int height) {
|
|
|
|
|
GridBagConstraints constraints = new GridBagConstraints();
|
|
|
|
|
JLabel label = new JLabel("");
|
|
|
|
|
constraints.fill = GridBagConstraints.BOTH;
|
|
|
|
|
constraints.weightx = 1.0;
|
|
|
|
|
constraints.weighty = 1.0;
|
|
|
|
|
constraints.gridwidth = width;
|
|
|
|
|
constraints.gridheight = height;
|
|
|
|
|
constraints.gridx = x;
|
|
|
|
|
constraints.gridy = y;
|
|
|
|
|
getContentPane().add(label, constraints);
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-15 23:22:22 +01:00
|
|
|
public Configuration getConfiguration() {
|
|
|
|
|
return client.getConfiguration();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCredentials(String contentLogin, String contentPassword) {
|
|
|
|
|
client.getConfiguration().setLogin(contentLogin);
|
|
|
|
|
client.getConfiguration().setPassword(contentPassword);
|
|
|
|
|
|
|
|
|
|
waitingForAuthentication = false;
|
|
|
|
|
synchronized (this) {
|
|
|
|
|
notifyAll();
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-06 00:44:30 +01:00
|
|
|
if (threadClient == null || threadClient.isAlive() == false) {
|
2015-01-27 21:44:32 +00:00
|
|
|
threadClient = new ThreadClient();
|
|
|
|
|
threadClient.start();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-15 23:22:22 +01:00
|
|
|
showActivity(ActivityType.WORKING);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void showActivity(ActivityType type) {
|
|
|
|
|
panel.removeAll();
|
|
|
|
|
panel.doLayout();
|
|
|
|
|
|
|
|
|
|
if (type == ActivityType.WORKING) {
|
|
|
|
|
activityWorking.show();
|
|
|
|
|
}
|
|
|
|
|
else if (type == ActivityType.SETTINGS) {
|
|
|
|
|
activitySettings.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setVisible(true);
|
|
|
|
|
panel.repaint();
|
|
|
|
|
}
|
2015-01-27 21:44:32 +00:00
|
|
|
|
2015-08-05 19:35:13 +01:00
|
|
|
public void hideToTray() {
|
2015-09-07 20:10:06 +01:00
|
|
|
if (sysTray == null || SystemTray.isSupported() == false) {
|
2015-08-05 19:35:13 +01:00
|
|
|
System.out.println("GuiSwing::hideToTray SystemTray not supported!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
trayIcon = getTrayIcon();
|
2015-09-07 20:10:06 +01:00
|
|
|
sysTray.add(trayIcon);
|
2015-08-05 19:35:13 +01:00
|
|
|
}
|
|
|
|
|
catch (AWTException e) {
|
|
|
|
|
System.out.println("GuiSwing::hideToTray an error occured while trying to add system tray icon (exception: " + e + ")");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setVisible(false);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void restoreFromTray() {
|
2015-09-07 20:10:06 +01:00
|
|
|
if (sysTray != null && SystemTray.isSupported()) {
|
|
|
|
|
sysTray.remove(trayIcon);
|
|
|
|
|
setVisible(true);
|
2015-10-27 20:41:20 +00:00
|
|
|
setExtendedState(getExtendedState() & ~JFrame.ICONIFIED & JFrame.NORMAL); // for toFront and requestFocus to actually work
|
|
|
|
|
toFront();
|
|
|
|
|
requestFocus();
|
2015-09-07 20:10:06 +01:00
|
|
|
}
|
2015-08-05 19:35:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TrayIcon getTrayIcon() {
|
|
|
|
|
final PopupMenu trayMenu = new PopupMenu();
|
|
|
|
|
|
|
|
|
|
URL iconUrl = getClass().getResource("/icon.png");
|
|
|
|
|
Image img = Toolkit.getDefaultToolkit().getImage(iconUrl);
|
|
|
|
|
final TrayIcon icon = new TrayIcon(img);
|
|
|
|
|
|
|
|
|
|
MenuItem exit = new MenuItem("Exit");
|
|
|
|
|
exit.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
trayMenu.add(exit);
|
|
|
|
|
|
|
|
|
|
MenuItem open = new MenuItem("Open...");
|
|
|
|
|
open.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
restoreFromTray();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
trayMenu.add(open);
|
|
|
|
|
|
|
|
|
|
MenuItem settings = new MenuItem("Settings...");
|
|
|
|
|
settings.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
restoreFromTray();
|
|
|
|
|
showActivity(ActivityType.SETTINGS);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
trayMenu.add(settings);
|
|
|
|
|
|
|
|
|
|
icon.setPopupMenu(trayMenu);
|
|
|
|
|
icon.setImageAutoSize(true);
|
|
|
|
|
icon.setToolTip("SheepIt! Client");
|
|
|
|
|
|
|
|
|
|
icon.addActionListener(new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
restoreFromTray();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return icon;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 21:44:32 +00:00
|
|
|
public class ThreadClient extends Thread {
|
|
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
if (GuiSwing.this.client != null) {
|
2015-01-28 19:52:40 +00:00
|
|
|
GuiSwing.this.client.run();
|
2015-01-27 21:44:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-15 23:22:22 +01:00
|
|
|
}
|