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

574 lines
17 KiB
Java
Raw Normal View History

2016-10-04 06:33:25 +02:00
/*
* Copyright (C) 2015 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.
*/
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;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
2015-01-15 23:20:17 +01:00
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
2015-01-15 23:20:17 +01:00
import java.io.File;
import java.net.MalformedURLException;
2015-01-15 23:20:17 +01:00
import java.util.LinkedList;
import java.util.List;
2015-07-29 16:51:34 -06:00
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
2015-01-15 23:20:17 +01:00
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
2015-01-15 23:20:17 +01:00
import javax.swing.JPasswordField;
import javax.swing.JSlider;
2015-01-15 23:20:17 +01:00
import javax.swing.JTextField;
import com.sheepit.client.Configuration;
import com.sheepit.client.Configuration.ComputeType;
2015-04-08 20:45:54 +01:00
import com.sheepit.client.SettingsLoader;
import com.sheepit.client.hardware.cpu.CPU;
2015-01-15 23:20:17 +01:00
import com.sheepit.client.hardware.gpu.GPU;
import com.sheepit.client.hardware.gpu.GPUDevice;
import com.sheepit.client.network.Proxy;
2015-01-15 23:20:17 +01:00
import com.sheepit.client.standalone.GuiSwing;
public class Settings implements Activity {
private static final String DUMMY_CACHE_DIR = "Auto detected";
private GuiSwing parent;
private JTextField login;
private JPasswordField password;
private JLabel cacheDirText;
private File cacheDir;
private JFileChooser cacheDirChooser;
private JCheckBox useCPU;
private List<JCheckBoxGPU> useGPUs;
private JSlider cpuCores;
2017-03-19 17:38:22 +01:00
private JSlider priority;
private JTextField proxy;
2015-01-15 23:20:17 +01:00
2015-03-31 21:35:04 +01:00
private JCheckBox saveFile;
private JCheckBox autoSignIn;
JButton saveButton;
private boolean haveAutoStarted;
2016-09-21 23:26:44 +02:00
private JTextField tileSizeValue;
private JLabel tileSizeLabel;
private JCheckBox customTileSize;
2015-01-15 23:20:17 +01:00
public Settings(GuiSwing parent_) {
parent = parent_;
cacheDir = null;
useGPUs = new LinkedList<JCheckBoxGPU>();
haveAutoStarted = false;
2015-01-15 23:20:17 +01:00
}
@Override
public void show() {
Configuration config = parent.getConfiguration();
2015-03-31 00:29:58 +01:00
new SettingsLoader().merge(config);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
List<GPUDevice> gpus = GPU.listDevices();
GridBagConstraints constraints = new GridBagConstraints();
int currentRow = 0;
2015-01-15 23:20:17 +01:00
ImageIcon image = new ImageIcon(getClass().getResource("/title.png"));
constraints.fill = GridBagConstraints.CENTER;
2015-01-15 23:20:17 +01:00
JLabel labelImage = new JLabel(image);
constraints.gridwidth = 2;
constraints.gridx = 0;
2015-07-29 16:51:34 -06:00
constraints.gridy = currentRow;
parent.getContentPane().add(labelImage, constraints);
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
++currentRow;
2015-01-15 23:20:17 +01:00
// authentication
2017-02-18 14:31:43 +01:00
JPanel authentication_panel = new JPanel(new GridLayout(2, 2));
authentication_panel.setBorder(BorderFactory.createTitledBorder("Authentication"));
2015-01-15 23:20:17 +01:00
2017-02-18 14:31:43 +01:00
JLabel loginLabel = new JLabel("Username:");
2015-01-15 23:20:17 +01:00
login = new JTextField();
login.setText(parent.getConfiguration().login());
login.setColumns(20);
login.addKeyListener(new CheckCanStart());
2017-02-18 14:31:43 +01:00
JLabel passwordLabel = new JLabel("Password:");
2015-01-15 23:20:17 +01:00
password = new JPasswordField();
password.setText(parent.getConfiguration().password());
password.setColumns(10);
password.addKeyListener(new CheckCanStart());
2015-01-15 23:20:17 +01:00
2017-02-18 14:31:43 +01:00
authentication_panel.add(loginLabel);
authentication_panel.add(login);
2015-01-15 23:20:17 +01:00
2017-02-18 14:31:43 +01:00
authentication_panel.add(passwordLabel);
authentication_panel.add(password);
constraints.gridx = 0;
2015-07-29 16:51:34 -06:00
constraints.gridy = currentRow;
constraints.fill = GridBagConstraints.HORIZONTAL;
2017-02-18 14:31:43 +01:00
parent.getContentPane().add(authentication_panel, constraints);
2015-01-15 23:20:17 +01:00
// directory
JPanel directory_panel = new JPanel(new GridLayout(1, 3));
directory_panel.setBorder(BorderFactory.createTitledBorder("Cache"));
2017-02-18 14:31:43 +01:00
JLabel cacheLabel = new JLabel("Working directory:");
directory_panel.add(cacheLabel);
2015-01-15 23:20:17 +01:00
String destination = DUMMY_CACHE_DIR;
if (config.getUserSpecifiedACacheDir()) {
2015-01-16 01:39:45 +01:00
destination = config.getStorageDir().getName();
2015-01-15 23:20:17 +01:00
}
JPanel cacheDirWrapper = new JPanel();
cacheDirWrapper.setLayout(new BoxLayout(cacheDirWrapper, BoxLayout.LINE_AXIS));
2015-01-15 23:20:17 +01:00
cacheDirText = new JLabel(destination);
cacheDirWrapper.add(cacheDirText);
cacheDirWrapper.add(Box.createHorizontalGlue());
2015-01-15 23:20:17 +01:00
cacheDirChooser = new JFileChooser();
cacheDirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
JButton openButton = new JButton("...");
openButton.addActionListener(new ChooseFileAction());
cacheDirWrapper.add(openButton);
directory_panel.add(cacheDirWrapper);
2015-01-15 23:20:17 +01:00
currentRow++;
constraints.gridx = 0;
2015-07-29 16:51:34 -06:00
constraints.gridy = currentRow;
constraints.gridwidth = 2;
parent.getContentPane().add(directory_panel, constraints);
// compute devices
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints compute_devices_constraints = new GridBagConstraints();
JPanel compute_devices_panel = new JPanel(gridbag);
compute_devices_panel.setBorder(BorderFactory.createTitledBorder("Compute devices"));
2015-01-15 23:20:17 +01:00
ComputeType method = config.getComputeMethod();
useCPU = new JCheckBox("CPU");
boolean gpuChecked = false;
if (method == ComputeType.CPU_GPU) {
useCPU.setSelected(true);
gpuChecked = true;
}
2015-04-07 20:05:48 +01:00
else if (method == ComputeType.CPU) {
2015-01-15 23:20:17 +01:00
useCPU.setSelected(true);
gpuChecked = false;
}
2015-04-07 20:05:48 +01:00
else if (method == ComputeType.GPU) {
2015-01-15 23:20:17 +01:00
useCPU.setSelected(false);
gpuChecked = true;
}
useCPU.addActionListener(new CpuChangeAction());
compute_devices_constraints.gridx = 1;
compute_devices_constraints.gridy = 0;
compute_devices_constraints.fill = GridBagConstraints.BOTH;
compute_devices_constraints.weightx = 1.0;
compute_devices_constraints.weighty = 1.0;
gridbag.setConstraints(useCPU, compute_devices_constraints);
compute_devices_panel.add(useCPU);
for (GPUDevice gpu : gpus) {
JCheckBoxGPU gpuCheckBox = new JCheckBoxGPU(gpu);
gpuCheckBox.setToolTipText(gpu.getCudaName());
if (gpuChecked) {
GPUDevice config_gpu = config.getGPUDevice();
if (config_gpu != null && config_gpu.getCudaName().equals(gpu.getCudaName())) {
gpuCheckBox.setSelected(gpuChecked);
}
2015-01-15 23:20:17 +01:00
}
gpuCheckBox.addActionListener(new GpuChangeAction());
compute_devices_constraints.gridy++;
gridbag.setConstraints(gpuCheckBox, compute_devices_constraints);
compute_devices_panel.add(gpuCheckBox);
useGPUs.add(gpuCheckBox);
2015-01-15 23:20:17 +01:00
}
CPU cpu = new CPU();
if (cpu.cores() > 1) { // if only one core is available, no need to show the choice
cpuCores = new JSlider(1, cpu.cores());
cpuCores.setMajorTickSpacing(1);
cpuCores.setMinorTickSpacing(1);
cpuCores.setPaintTicks(true);
cpuCores.setPaintLabels(true);
cpuCores.setValue(config.getNbCores() != -1 ? config.getNbCores() : cpuCores.getMaximum());
JLabel coreLabel = new JLabel("CPU cores:");
2015-07-29 16:51:34 -06:00
compute_devices_constraints.weightx = 1.0 / gpus.size();
compute_devices_constraints.gridx = 0;
compute_devices_constraints.gridy++;
gridbag.setConstraints(coreLabel, compute_devices_constraints);
compute_devices_panel.add(coreLabel);
compute_devices_constraints.gridx = 1;
compute_devices_constraints.weightx = 1.0;
gridbag.setConstraints(cpuCores, compute_devices_constraints);
compute_devices_panel.add(cpuCores);
}
2017-03-19 17:38:22 +01:00
// priority
priority = new JSlider(-19, 19);
priority.setMajorTickSpacing(5);
priority.setMinorTickSpacing(1);
priority.setPaintTicks(true);
priority.setPaintLabels(true);
priority.setValue(config.getPriority());
JLabel priorityLabel = new JLabel("Priority (High <-> Low):");
compute_devices_constraints.weightx = 1.0 / gpus.size();
compute_devices_constraints.gridx = 0;
compute_devices_constraints.gridy++;
gridbag.setConstraints(priorityLabel, compute_devices_constraints);
compute_devices_panel.add(priorityLabel);
compute_devices_constraints.gridx = 1;
compute_devices_constraints.weightx = 1.0;
gridbag.setConstraints(priority, compute_devices_constraints);
compute_devices_panel.add(priority);
currentRow++;
constraints.gridx = 0;
2016-09-21 23:26:44 +02:00
constraints.gridy = currentRow;
constraints.gridwidth = 2;
parent.getContentPane().add(compute_devices_panel, constraints);
2016-09-21 23:26:44 +02:00
// other
JPanel advanced_panel = new JPanel(new GridLayout(3, 2));
advanced_panel.setBorder(BorderFactory.createTitledBorder("Advanced options"));
2016-09-21 23:26:44 +02:00
2017-02-18 14:31:43 +01:00
JLabel proxyLabel = new JLabel("Proxy:");
proxyLabel.setToolTipText("http://login:password@host:port");
proxy = new JTextField();
proxy.setToolTipText("http://login:password@host:port");
proxy.setText(parent.getConfiguration().getProxy());
proxy.addKeyListener(new CheckCanStart());
advanced_panel.add(proxyLabel);
advanced_panel.add(proxy);
2017-02-18 14:31:43 +01:00
JLabel customTileSizeLabel = new JLabel("Custom render tile size:");
customTileSize = new JCheckBox("", config.getTileSize() != -1);
advanced_panel.add(customTileSizeLabel);
advanced_panel.add(customTileSize);
customTileSize.addActionListener(new TileSizeChange());
2016-09-21 23:26:44 +02:00
tileSizeLabel = new JLabel("Tile Size:");
tileSizeValue = new JTextField();
int fromConfig = parent.getConfiguration().getTileSize();
if (fromConfig == -1) {
if (parent.getConfiguration().getGPUDevice() != null) {
fromConfig = parent.getConfiguration().getGPUDevice().getRecommandedTileSize();
}
else {
fromConfig = 32;
}
}
tileSizeValue.setText(Integer.toString(fromConfig));
hideCustomTileSize(config.getTileSize() != -1, false);
advanced_panel.add(tileSizeLabel);
advanced_panel.add(tileSizeValue);
currentRow++;
constraints.gridx = 0;
2015-07-29 16:51:34 -06:00
constraints.gridy = currentRow;
constraints.gridwidth = 2;
parent.getContentPane().add(advanced_panel, constraints);
2015-03-31 21:35:04 +01:00
// general settings
JPanel general_panel = new JPanel(new GridLayout(1, 2));
saveFile = new JCheckBox("Save settings", true);
general_panel.add(saveFile);
autoSignIn = new JCheckBox("Auto sign in", config.getAutoSignIn());
autoSignIn.addActionListener(new AutoSignInChangeAction());
general_panel.add(autoSignIn);
currentRow++;
constraints.gridx = 0;
constraints.gridy = currentRow;
constraints.gridwidth = 2;
parent.getContentPane().add(general_panel, constraints);
2015-03-31 21:35:04 +01:00
2016-02-08 13:06:33 +01:00
String buttonText = "Start";
if (parent.getClient() != null) {
if (parent.getClient().isRunning()) {
buttonText = "Save";
}
}
saveButton = new JButton(buttonText);
checkDisplaySaveButton();
2015-01-15 23:20:17 +01:00
saveButton.addActionListener(new SaveAction());
currentRow++;
constraints.gridwidth = 2;
constraints.gridx = 0;
2015-07-29 16:51:34 -06:00
constraints.gridy = currentRow;
parent.getContentPane().add(saveButton, constraints);
if (haveAutoStarted == false && config.getAutoSignIn() && checkDisplaySaveButton()) {
// auto start
haveAutoStarted = true;
new SaveAction().actionPerformed(null);
}
2015-01-15 23:20:17 +01:00
}
2016-09-21 23:26:44 +02:00
public void hideCustomTileSize(boolean hidden, boolean displayWarning) {
tileSizeValue.setVisible(hidden);
tileSizeLabel.setVisible(hidden);
if (customTileSize.isSelected() == true && displayWarning) {
JOptionPane.showMessageDialog(parent.getContentPane(), "<html>These settings should only be changed if you are an advanced user.<br /> Improper settings may lead to invalid and slower renders!</html>", "Warning: Advanced Users Only!", JOptionPane.WARNING_MESSAGE);
}
}
public boolean checkDisplaySaveButton() {
boolean selected = useCPU.isSelected();
for (JCheckBoxGPU box : useGPUs) {
if (box.isSelected()) {
selected = true;
}
}
if (login.getText().isEmpty() || password.getPassword().length == 0 || Proxy.isValidURL(proxy.getText()) == false) {
selected = false;
}
2016-09-21 23:26:44 +02:00
if (customTileSize.isSelected()) {
try {
Integer.parseInt(tileSizeValue.getText().replaceAll(",", ""));
}
catch (NumberFormatException e) {
selected = false;
}
}
saveButton.setEnabled(selected);
return selected;
}
2015-01-15 23:20:17 +01:00
class ChooseFileAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(parent.getContentPane(), "<html>The working directory has to be dedicated directory. <br />Caution, everything not related to SheepIt-Renderfarm will be removed.<br />You should create a directory specifically for it.</html>", "Warning: files will be removed!", JOptionPane.WARNING_MESSAGE);
2015-01-15 23:20:17 +01:00
int returnVal = cacheDirChooser.showOpenDialog(parent.getContentPane());
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = cacheDirChooser.getSelectedFile();
cacheDir = file;
cacheDirText.setText(cacheDir.getName());
}
}
}
class CpuChangeAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
checkDisplaySaveButton();
}
}
2015-01-15 23:20:17 +01:00
class GpuChangeAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
for (JCheckBox box : useGPUs) {
if (box.equals(e.getSource()) == false) {
box.setSelected(false);
}
}
checkDisplaySaveButton();
2015-01-15 23:20:17 +01:00
}
}
class AutoSignInChangeAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (autoSignIn.isSelected()) {
saveFile.setSelected(true);
}
}
}
2016-09-21 23:26:44 +02:00
class TileSizeChange implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
boolean custom = customTileSize.isSelected();
hideCustomTileSize(custom, true);
}
}
2015-01-15 23:20:17 +01:00
class SaveAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (parent == null) {
return;
}
Configuration config = parent.getConfiguration();
if (config == null) {
return;
}
if (cacheDir != null) {
File fromConfig = config.getStorageDir();
if (fromConfig != null && fromConfig.getAbsolutePath().equals(cacheDir.getAbsolutePath()) == false) {
config.setCacheDir(cacheDir);
}
else {
2016-01-06 20:26:46 +01:00
// do nothing because the directory is the same as before
2015-01-15 23:20:17 +01:00
}
}
GPUDevice selected_gpu = null;
for (JCheckBoxGPU box : useGPUs) {
2015-01-15 23:20:17 +01:00
if (box.isSelected()) {
selected_gpu = box.getGPUDevice();
2015-01-15 23:20:17 +01:00
}
}
2015-04-07 20:05:48 +01:00
ComputeType method = ComputeType.CPU;
2015-01-15 23:20:17 +01:00
if (useCPU.isSelected() && selected_gpu == null) {
2015-04-07 20:05:48 +01:00
method = ComputeType.CPU;
2015-01-15 23:20:17 +01:00
}
else if (useCPU.isSelected() == false && selected_gpu != null) {
2015-04-07 20:05:48 +01:00
method = ComputeType.GPU;
2015-01-15 23:20:17 +01:00
}
else if (useCPU.isSelected() && selected_gpu != null) {
method = ComputeType.CPU_GPU;
}
config.setComputeMethod(method);
if (selected_gpu != null) {
config.setUseGPU(selected_gpu);
2015-01-15 23:20:17 +01:00
}
int cpu_cores = -1;
2015-06-05 12:51:27 +00:00
if (cpuCores != null) {
cpu_cores = cpuCores.getValue();
}
if (cpu_cores > 0) {
config.setUseNbCores(cpu_cores);
}
2017-03-19 17:38:22 +01:00
config.setUsePriority(priority.getValue());
String proxyText = null;
if (proxy != null) {
try {
Proxy.set(proxy.getText());
proxyText = proxy.getText();
}
catch (MalformedURLException e1) {
System.err.println("Error: wrong url for proxy");
2017-01-12 20:50:44 +01:00
System.err.println(e1);
System.exit(2);
}
}
2016-09-21 23:26:44 +02:00
String tile = null;
if (customTileSize.isSelected() && tileSizeValue != null) {
try {
tile = tileSizeValue.getText().replaceAll(",", "");
config.setTileSize(Integer.parseInt(tile));
}
catch (NumberFormatException e1) {
System.err.println("Error: wrong tile format");
2017-01-12 20:50:44 +01:00
System.err.println(e1);
2016-09-21 23:26:44 +02:00
System.exit(2);
}
}
else {
config.setTileSize(-1); // no tile
}
2016-09-21 23:26:44 +02:00
2015-01-15 23:20:17 +01:00
parent.setCredentials(login.getText(), new String(password.getPassword()));
2015-03-31 00:29:58 +01:00
String cachePath = null;
if (config.getUserSpecifiedACacheDir() && config.getStorageDir() != null) {
cachePath = config.getStorageDir().getAbsolutePath();
}
2015-03-31 21:35:04 +01:00
if (saveFile.isSelected()) {
2017-03-19 17:38:22 +01:00
new SettingsLoader(login.getText(), new String(password.getPassword()), proxyText, method, selected_gpu, cpu_cores, cachePath, autoSignIn.isSelected(), GuiSwing.type, tile, priority.getValue()).saveFile();
2015-03-31 21:35:04 +01:00
}
else {
try {
new File(new SettingsLoader().getFilePath()).delete();
}
catch (SecurityException e3) {
}
}
2015-01-15 23:20:17 +01:00
}
}
class JCheckBoxGPU extends JCheckBox {
private GPUDevice gpu;
public JCheckBoxGPU(GPUDevice gpu) {
super(gpu.getModel());
this.gpu = gpu;
}
public GPUDevice getGPUDevice() {
return gpu;
}
}
public class CheckCanStart implements KeyListener {
@Override
public void keyPressed(KeyEvent arg0) {
}
@Override
public void keyReleased(KeyEvent arg0) {
checkDisplaySaveButton();
}
@Override
public void keyTyped(KeyEvent arg0) {
}
}
2015-01-15 23:20:17 +01:00
}