Files
sheepit-shadow-nabber/src/com/sheepit/client/standalone/swing/activity/Working.java

242 lines
6.5 KiB
Java
Raw Normal View History

2015-01-15 23:20:17 +01:00
package com.sheepit.client.standalone.swing.activity;
2015-07-29 16:51:34 -06:00
import java.awt.GridBagConstraints;
2015-08-05 19:35:13 +01:00
import java.awt.SystemTray;
2015-01-15 23:20:17 +01:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
2015-07-29 16:51:34 -06:00
import javax.swing.BorderFactory;
2015-01-15 23:20:17 +01:00
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import com.sheepit.client.Client;
import com.sheepit.client.Server;
import com.sheepit.client.standalone.GuiSwing;
import com.sheepit.client.standalone.GuiSwing.ActivityType;
public class Working implements Activity {
GuiSwing parent;
JLabel statusContent;
JLabel renderedFrameContent;
JLabel remainingFrameContent;
JLabel lastRender;
JLabel creditEarned;
2015-01-17 22:40:24 +01:00
JButton pauseButton;
JButton exitAfterFrame;
2015-01-15 23:20:17 +01:00
public Working(GuiSwing parent_) {
parent = parent_;
statusContent = new JLabel("Init");
renderedFrameContent = new JLabel("0");
remainingFrameContent = new JLabel("0");
creditEarned = new JLabel("");
2015-01-15 23:20:17 +01:00
lastRender = new JLabel();
}
@Override
public void show() {
2015-07-29 16:51:34 -06:00
GridBagConstraints constraints = new GridBagConstraints();
int currentRow = 0;
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-01-15 23:20:17 +01:00
ImageIcon image = new ImageIcon(getClass().getResource("/title.png"));
JLabel labelImage = new JLabel(image);
2015-07-29 16:51:34 -06:00
constraints.fill = GridBagConstraints.BOTH;
constraints.weightx = 1.0;
constraints.weighty = 3.0;
constraints.gridwidth = 2;
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(labelImage, constraints);
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-01-15 23:20:17 +01:00
JLabel statusLabel = new JLabel("Status:");
2015-07-29 16:51:34 -06:00
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weighty = 0.0;
constraints.gridwidth = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(statusLabel, constraints);
2015-01-15 23:20:17 +01:00
2015-01-16 19:13:22 +01:00
statusContent.setVerticalAlignment(JLabel.TOP);
statusContent.setVerticalTextPosition(JLabel.TOP);
2015-07-29 16:51:34 -06:00
constraints.gridx = 2;
parent.getContentPane().add(statusContent, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-01-15 23:20:17 +01:00
JLabel creditsEarnedLabel = new JLabel("Credits earned:");
2015-07-29 16:51:34 -06:00
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(creditsEarnedLabel, constraints);
2015-07-29 16:51:34 -06:00
constraints.gridx = 2;
parent.getContentPane().add(creditEarned, constraints);
2015-07-29 16:51:34 -06:00
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-04-19 02:57:25 +01:00
JLabel renderedFrameLabel = new JLabel("Rendered frames:");
2015-07-29 16:51:34 -06:00
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(renderedFrameLabel, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
constraints.gridx = 2;
parent.getContentPane().add(renderedFrameContent, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-01-15 23:20:17 +01:00
2015-04-19 02:57:25 +01:00
JLabel remainingFrameLabel = new JLabel("Remaining frames:");
2015-07-29 16:51:34 -06:00
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(remainingFrameLabel, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
constraints.gridx = 2;
parent.getContentPane().add(remainingFrameContent, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-01-15 23:20:17 +01:00
JLabel lastRenderedFrameLabel = new JLabel("Last rendered frame:");
2015-07-29 16:51:34 -06:00
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(lastRenderedFrameLabel, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
constraints.gridx = 2;
parent.getContentPane().add(lastRender, constraints);
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
2015-01-15 23:20:17 +01:00
JButton settingsButton = new JButton("Settings");
settingsButton.addActionListener(new SettingsAction());
2015-07-29 16:51:34 -06:00
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(settingsButton, constraints);
2015-01-15 23:20:17 +01:00
2015-01-17 22:40:24 +01:00
pauseButton = new JButton("Pause");
2015-01-15 23:20:17 +01:00
pauseButton.addActionListener(new PauseAction());
2015-07-29 16:51:34 -06:00
constraints.gridx = 2;
constraints.gridy = currentRow;
2015-07-29 16:51:34 -06:00
parent.getContentPane().add(pauseButton, constraints);
++currentRow;
exitAfterFrame = new JButton("Exit after this frame");
constraints.gridx = 2;
constraints.gridy = currentRow;
exitAfterFrame.addActionListener(new ExitAfterAction());
parent.getContentPane().add(exitAfterFrame, constraints);
2015-07-29 16:51:34 -06:00
parent.addPadding(1, ++currentRow, 2, 1);
parent.addPadding(0, 0, 1, currentRow + 1);
parent.addPadding(3, 0, 1, currentRow + 1);
2015-01-15 23:20:17 +01:00
}
public void setStatus(String msg_) {
2015-01-16 19:13:22 +01:00
statusContent.setText("<html>" + msg_ + "</html>"); // html for the text wrapping
2015-01-15 23:20:17 +01:00
}
public void setRemainingFrame(int n) {
remainingFrameContent.setText(String.valueOf(n));
}
public void setRenderedFrame(int n) {
renderedFrameContent.setText(String.valueOf(n));
showCreditEarned();
2015-01-15 23:20:17 +01:00
showLastRender();
}
public void showLastRender() {
Client client = parent.getClient();
if (client != null) {
Server server = client.getServer();
if (server != null) {
byte[] data = server.getLastRender();
if (data != null) {
InputStream is = new ByteArrayInputStream(data);
try {
BufferedImage image = ImageIO.read(is);
lastRender.setIcon(new ImageIcon(image));
}
catch (IOException e) {
System.out.println("Working::showLastRender() exception " + e);
e.printStackTrace();
}
}
}
}
}
public void showCreditEarned() {
Client client = parent.getClient();
if (client != null) {
Server server = client.getServer();
if (server != null) {
String data = server.getCreditEarnedOnCurrentSession();
if (data != null) {
creditEarned.setText(data);
}
}
}
}
2015-01-15 23:20:17 +01:00
class PauseAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Client client = parent.getClient();
if (client != null) {
2015-01-17 22:40:24 +01:00
if (client.isSuspended()) {
pauseButton.setText("Pause");
client.resume();
}
else {
pauseButton.setText("Resume");
client.suspend();
}
2015-01-15 23:20:17 +01:00
}
}
}
class SettingsAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (parent != null) {
parent.showActivity(ActivityType.SETTINGS);
}
}
}
class ExitAfterAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
Client client = parent.getClient();
if (client != null) {
if (client.isRunning()) {
exitAfterFrame.setText("Cancel exit");
client.askForStop();
}
else {
exitAfterFrame.setText("Exit after this frame");
client.cancelStop();
}
}
}
}
2015-08-05 19:35:13 +01:00
2015-01-15 23:20:17 +01:00
}