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.Color ;
2015-07-29 21:59:58 -06:00
import java.awt.Component ;
2015-07-29 16:51:34 -06:00
import java.awt.Dimension ;
import java.awt.GridBagConstraints ;
import java.awt.Insets ;
2015-01-15 23:20:17 +01:00
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
2015-04-03 19:39:52 +01:00
import java.awt.event.KeyEvent ;
import java.awt.event.KeyListener ;
2015-01-15 23:20:17 +01:00
import java.io.File ;
2015-07-06 18:41:28 +01:00
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 ;
2015-07-29 21:59:58 -06:00
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 ;
2015-02-02 22:43:42 +00:00
import javax.swing.JOptionPane ;
2015-07-29 21:59:58 -06:00
import javax.swing.JPanel ;
2015-01-15 23:20:17 +01:00
import javax.swing.JPasswordField ;
2015-04-20 21:26:07 +01:00
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 ;
2015-04-20 21:26:07 +01:00
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 ;
2015-07-06 18:41:28 +01:00
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 ;
2015-01-24 17:49:27 +00:00
private List < JCheckBoxGPU > useGPUs ;
2015-04-20 21:26:07 +01:00
private JSlider cpuCores ;
2015-07-06 18:41:28 +01:00
private JTextField proxy ;
2015-01-15 23:20:17 +01:00
2015-03-31 21:35:04 +01:00
private JCheckBox saveFile ;
2015-04-03 19:50:58 +01:00
private JCheckBox autoSignIn ;
2015-01-28 01:02:05 +00:00
JButton saveButton ;
2015-04-03 19:50:58 +01:00
private boolean haveAutoStarted ;
2015-01-15 23:20:17 +01:00
public Settings ( GuiSwing parent_ ) {
parent = parent_ ;
cacheDir = null ;
2015-01-24 17:49:27 +00:00
useGPUs = new LinkedList < JCheckBoxGPU > ( ) ;
2015-04-03 19:50:58 +01:00
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 ( ) ;
2015-07-29 21:59:58 -06:00
int columns = 4 + ( gpus ! = null ? gpus . size ( ) : 0 ) ;
2015-07-29 16:51:34 -06:00
int currentRow = 0 ;
parent . addPadding ( 1 , + + currentRow , columns - 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 = columns - 2 ;
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( labelImage , constraints ) ;
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-01-15 23:20:17 +01:00
JLabel loginLabel = new JLabel ( " Login: " ) ;
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 ( loginLabel , constraints ) ;
2015-01-15 23:20:17 +01:00
login = new JTextField ( ) ;
login . setText ( parent . getConfiguration ( ) . login ( ) ) ;
login . setColumns ( 20 ) ;
2015-04-03 19:39:52 +01:00
login . addKeyListener ( new CheckCanStart ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = columns - 3 ;
constraints . gridx = 2 ;
parent . getContentPane ( ) . add ( login , constraints ) ;
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-01-15 23:20:17 +01:00
JLabel passwordLabel = new JLabel ( " Password: " ) ;
2015-07-29 16:51:34 -06:00
constraints . weighty = 0 . 0 ;
constraints . gridwidth = 1 ;
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( passwordLabel , constraints ) ;
2015-01-15 23:20:17 +01:00
password = new JPasswordField ( ) ;
password . setText ( parent . getConfiguration ( ) . password ( ) ) ;
password . setColumns ( 10 ) ;
2015-04-03 19:39:52 +01:00
password . addKeyListener ( new CheckCanStart ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = columns - 3 ;
constraints . gridx = 2 ;
parent . getContentPane ( ) . add ( password , constraints ) ;
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-01-15 23:20:17 +01:00
2015-07-06 18:41:28 +01:00
JLabel proxyLabel = new JLabel ( " Proxy: " ) ;
proxyLabel . setToolTipText ( " http://login:password@host:port " ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = 1 ;
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( proxyLabel , constraints ) ;
2015-07-06 18:41:28 +01:00
proxy = new JTextField ( ) ;
proxy . setToolTipText ( " http://login:password@host:port " ) ;
proxy . setText ( parent . getConfiguration ( ) . getProxy ( ) ) ;
proxy . addKeyListener ( new CheckCanStart ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = columns - 3 ;
constraints . gridx = 2 ;
parent . getContentPane ( ) . add ( proxy , constraints ) ;
2015-07-06 18:41:28 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-07-06 18:41:28 +01:00
2015-04-19 02:57:25 +01:00
JLabel cacheLabel = new JLabel ( " Working directory: " ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = 1 ;
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( cacheLabel , constraints ) ;
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
}
2015-07-29 21:59:58 -06: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 ) ;
2015-07-29 21:59:58 -06:00
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 ( ) ) ;
2015-07-29 21:59:58 -06:00
cacheDirWrapper . add ( openButton ) ;
constraints . gridwidth = columns - 3 ;
constraints . gridx = 2 ;
parent . getContentPane ( ) . add ( cacheDirWrapper , constraints ) ;
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-01-15 23:20:17 +01:00
2015-04-19 02:57:25 +01:00
JLabel computeMethodLabel = new JLabel ( " Use: " ) ;
2015-07-29 16:51:34 -06:00
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( computeMethodLabel , constraints ) ;
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 ;
}
2015-01-28 01:02:05 +00:00
useCPU . addActionListener ( new CpuChangeAction ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = Math . max ( 1 , columns - ( gpus ! = null ? gpus . size ( ) : 0 ) - 3 ) ;
constraints . gridx = 2 ;
parent . getContentPane ( ) . add ( useCPU , constraints ) ;
2015-01-15 23:20:17 +01:00
2015-07-29 16:51:34 -06:00
constraints . gridwidth = 1 ;
2015-01-15 23:20:17 +01:00
if ( gpus ! = null ) {
2015-07-29 16:51:34 -06:00
for ( int i = 0 ; i < gpus . size ( ) ; i + + ) {
GPUDevice gpu = gpus . get ( i ) ;
2015-01-24 17:49:27 +00:00
JCheckBoxGPU gpuCheckBox = new JCheckBoxGPU ( gpu ) ;
2015-01-27 20:43:40 +01:00
gpuCheckBox . setToolTipText ( gpu . getCudaName ( ) ) ;
2015-01-29 14:30:53 +00:00
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 ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridx = i + 3 ;
parent . getContentPane ( ) . add ( gpuCheckBox , constraints ) ;
2015-01-15 23:20:17 +01:00
useGPUs . add ( gpuCheckBox ) ;
}
}
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-01-15 23:20:17 +01:00
2015-04-20 21:26:07 +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
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( coreLabel , constraints ) ;
constraints . gridwidth = columns - 3 ;
constraints . gridx = 2 ;
parent . getContentPane ( ) . add ( cpuCores , constraints ) ;
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-04-20 21:26:07 +01:00
}
2015-03-31 21:35:04 +01:00
saveFile = new JCheckBox ( " Save settings " , true ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = columns - 3 ;
constraints . gridx = 2 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( saveFile , constraints ) ;
2015-03-31 21:35:04 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-04-03 19:50:58 +01:00
autoSignIn = new JCheckBox ( " Auto sign in " , config . getAutoSignIn ( ) ) ;
autoSignIn . addActionListener ( new AutoSignInChangeAction ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( autoSignIn , constraints ) ;
2015-04-03 19:50:58 +01:00
2015-07-29 16:51:34 -06:00
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
+ + currentRow ;
2015-03-31 21:35:04 +01:00
2015-01-28 01:02:05 +00:00
saveButton = new JButton ( " Start " ) ;
2015-07-10 20:54:08 -06:00
checkDisplaySaveButton ( ) ;
2015-01-15 23:20:17 +01:00
saveButton . addActionListener ( new SaveAction ( ) ) ;
2015-07-29 16:51:34 -06:00
constraints . gridwidth = columns - 2 ;
constraints . gridx = 1 ;
constraints . gridy = currentRow ;
parent . getContentPane ( ) . add ( saveButton , constraints ) ;
parent . addPadding ( 1 , + + currentRow , columns - 2 , 1 ) ;
parent . addPadding ( 0 , 0 , 1 , currentRow + 1 ) ;
parent . addPadding ( columns - 1 , 0 , 1 , currentRow + 1 ) ;
2015-01-15 23:20:17 +01:00
2015-04-03 19:50:58 +01:00
if ( haveAutoStarted = = false & & config . getAutoSignIn ( ) & & checkDisplaySaveButton ( ) ) {
// auto start
haveAutoStarted = true ;
new SaveAction ( ) . actionPerformed ( null ) ;
}
2015-01-15 23:20:17 +01:00
}
2015-04-03 19:50:58 +01:00
public boolean checkDisplaySaveButton ( ) {
2015-01-28 01:02:05 +00:00
boolean selected = useCPU . isSelected ( ) ;
for ( JCheckBoxGPU box : useGPUs ) {
if ( box . isSelected ( ) ) {
selected = true ;
}
}
2015-07-06 18:41:28 +01:00
if ( login . getText ( ) . isEmpty ( ) | | password . getPassword ( ) . length = = 0 | | Proxy . isValidURL ( proxy . getText ( ) ) = = false ) {
2015-04-03 19:39:52 +01:00
selected = false ;
}
2015-01-28 01:02:05 +00:00
saveButton . setEnabled ( selected ) ;
2015-04-03 19:50:58 +01:00
return selected ;
2015-01-28 01:02:05 +00:00
}
2015-01-15 23:20:17 +01:00
class ChooseFileAction implements ActionListener {
@Override
public void actionPerformed ( ActionEvent arg0 ) {
2015-02-02 22:43:42 +00:00
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 ( ) ) ;
}
}
}
2015-01-28 01:02:05 +00:00
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 ) ;
}
}
2015-01-28 01:02:05 +00:00
checkDisplaySaveButton ( ) ;
2015-01-15 23:20:17 +01:00
}
}
2015-04-03 19:39:52 +01:00
class AutoSignInChangeAction implements ActionListener {
@Override
public void actionPerformed ( ActionEvent e ) {
if ( autoSignIn . isSelected ( ) ) {
saveFile . setSelected ( 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
}
}
2015-01-24 17:49:27 +00:00
GPUDevice selected_gpu = null ;
for ( JCheckBoxGPU box : useGPUs ) {
2015-01-15 23:20:17 +01:00
if ( box . isSelected ( ) ) {
2015-01-24 17:49:27 +00:00
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 ) ;
2015-01-24 17:49:27 +00:00
if ( selected_gpu ! = null ) {
config . setUseGPU ( selected_gpu ) ;
2015-01-15 23:20:17 +01:00
}
2015-04-20 21:26:07 +01:00
int cpu_cores = - 1 ;
2015-06-05 12:51:27 +00:00
if ( cpuCores ! = null ) {
2015-04-20 21:26:07 +01:00
cpu_cores = cpuCores . getValue ( ) ;
}
if ( cpu_cores > 0 ) {
config . setUseNbCores ( cpu_cores ) ;
}
2015-07-06 18:41:28 +01:00
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 " ) ;
System . err . println ( e ) ;
System . exit ( 2 ) ;
}
}
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 ( ) ) {
2015-07-06 18:41:28 +01:00
new SettingsLoader ( login . getText ( ) , new String ( password . getPassword ( ) ) , proxyText , method , selected_gpu , cpu_cores , cachePath , autoSignIn . isSelected ( ) , GuiSwing . type ) . saveFile ( ) ;
2015-03-31 21:35:04 +01:00
}
2015-04-02 20:31:16 +01:00
else {
try {
new File ( new SettingsLoader ( ) . getFilePath ( ) ) . delete ( ) ;
}
catch ( SecurityException e3 ) {
}
}
2015-01-15 23:20:17 +01:00
}
}
2015-01-24 17:49:27 +00:00
class JCheckBoxGPU extends JCheckBox {
private GPUDevice gpu ;
public JCheckBoxGPU ( GPUDevice gpu ) {
super ( gpu . getModel ( ) ) ;
this . gpu = gpu ;
}
public GPUDevice getGPUDevice ( ) {
return gpu ;
}
}
2015-04-03 19:39:52 +01:00
public class CheckCanStart implements KeyListener {
@Override
public void keyPressed ( KeyEvent arg0 ) {
}
@Override
public void keyReleased ( KeyEvent arg0 ) {
2015-07-10 20:54:08 -06:00
checkDisplaySaveButton ( ) ;
2015-04-03 19:39:52 +01:00
}
@Override
public void keyTyped ( KeyEvent arg0 ) {
}
}
2015-01-15 23:20:17 +01:00
}