Merge pull request #32 from scribblemaniac/flexible-gui

Implemented flexible layout for swing
This commit is contained in:
Laurent Clouet
2015-07-30 18:15:23 +01:00
3 changed files with 195 additions and 95 deletions

View File

@@ -19,10 +19,15 @@
package com.sheepit.client.standalone; package com.sheepit.client.standalone;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.net.URL; import java.net.URL;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException; import javax.swing.UnsupportedLookAndFeelException;
@@ -78,7 +83,7 @@ public class GuiSwing extends JFrame implements Gui {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel(); panel = new JPanel();
panel.setLayout(null); panel.setLayout(new GridBagLayout());
setContentPane(this.panel); setContentPane(this.panel);
panel.setBorder(new EmptyBorder(5, 5, 5, 5)); panel.setBorder(new EmptyBorder(5, 5, 5, 5));
@@ -148,6 +153,19 @@ public class GuiSwing extends JFrame implements Gui {
client = cli; client = cli;
} }
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);
}
public Configuration getConfiguration() { public Configuration getConfiguration() {
return client.getConfiguration(); return client.getConfiguration();
} }

View File

@@ -1,5 +1,10 @@
package com.sheepit.client.standalone.swing.activity; package com.sheepit.client.standalone.swing.activity;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
@@ -9,12 +14,16 @@ import java.net.MalformedURLException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JCheckBox; import javax.swing.JCheckBox;
import javax.swing.JFileChooser; import javax.swing.JFileChooser;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField; import javax.swing.JPasswordField;
import javax.swing.JSlider; import javax.swing.JSlider;
import javax.swing.JTextField; import javax.swing.JTextField;
@@ -60,86 +69,117 @@ public class Settings implements Activity {
Configuration config = parent.getConfiguration(); Configuration config = parent.getConfiguration();
new SettingsLoader().merge(config); new SettingsLoader().merge(config);
int size_height_label = 24; List<GPUDevice> gpus = GPU.listDevices();
int start_label_left = 109;
int start_label_right = 265; GridBagConstraints constraints = new GridBagConstraints();
int end_label_right = 490; int columns = 4 + (gpus != null ? gpus.size() : 0);
int n = 5; int currentRow = 0;
int sep = 40;
parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
ImageIcon image = new ImageIcon(getClass().getResource("/title.png")); ImageIcon image = new ImageIcon(getClass().getResource("/title.png"));
JLabel labelImage = new JLabel(image); JLabel labelImage = new JLabel(image);
labelImage.setBounds(600 / 2 - 265 / 2, n, 265, 130 + n); constraints.fill = GridBagConstraints.BOTH;
n = labelImage.getHeight(); constraints.weightx = 1.0;
parent.getContentPane().add(labelImage); constraints.weighty = 3.0;
constraints.gridwidth = columns - 2;
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(labelImage, constraints);
n += sep; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
JLabel loginLabel = new JLabel("Login:"); JLabel loginLabel = new JLabel("Login:");
loginLabel.setBounds(start_label_left, n, 170, size_height_label); constraints.fill = GridBagConstraints.HORIZONTAL;
parent.getContentPane().add(loginLabel); constraints.weighty = 0.0;
constraints.gridwidth = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(loginLabel, constraints);
login = new JTextField(); login = new JTextField();
login.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label);
login.setText(parent.getConfiguration().login()); login.setText(parent.getConfiguration().login());
login.setColumns(20); login.setColumns(20);
login.addKeyListener(new CheckCanStart()); login.addKeyListener(new CheckCanStart());
parent.getContentPane().add(login); constraints.gridwidth = columns - 3;
constraints.gridx = 2;
parent.getContentPane().add(login, constraints);
n += sep; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
JLabel passwordLabel = new JLabel("Password:"); JLabel passwordLabel = new JLabel("Password:");
passwordLabel.setBounds(start_label_left, n, 170, size_height_label); constraints.weighty = 0.0;
parent.getContentPane().add(passwordLabel); constraints.gridwidth = 1;
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(passwordLabel, constraints);
password = new JPasswordField(); password = new JPasswordField();
password.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label);
password.setText(parent.getConfiguration().password()); password.setText(parent.getConfiguration().password());
password.setColumns(10); password.setColumns(10);
password.addKeyListener(new CheckCanStart()); password.addKeyListener(new CheckCanStart());
parent.getContentPane().add(password); constraints.gridwidth = columns - 3;
constraints.gridx = 2;
parent.getContentPane().add(password, constraints);
n += sep; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
JLabel proxyLabel = new JLabel("Proxy:"); JLabel proxyLabel = new JLabel("Proxy:");
proxyLabel.setBounds(start_label_left, n, 170, size_height_label);
proxyLabel.setToolTipText("http://login:password@host:port"); proxyLabel.setToolTipText("http://login:password@host:port");
parent.getContentPane().add(proxyLabel); constraints.gridwidth = 1;
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(proxyLabel, constraints);
proxy = new JTextField(); proxy = new JTextField();
proxy.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label);
proxy.setToolTipText("http://login:password@host:port"); proxy.setToolTipText("http://login:password@host:port");
proxy.setText(parent.getConfiguration().getProxy()); proxy.setText(parent.getConfiguration().getProxy());
proxy.addKeyListener(new CheckCanStart()); proxy.addKeyListener(new CheckCanStart());
parent.getContentPane().add(proxy); constraints.gridwidth = columns - 3;
constraints.gridx = 2;
parent.getContentPane().add(proxy, constraints);
n += sep; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
JLabel cacheLabel = new JLabel("Working directory:"); JLabel cacheLabel = new JLabel("Working directory:");
cacheLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.gridwidth = 1;
parent.getContentPane().add(cacheLabel); constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(cacheLabel, constraints);
String destination = DUMMY_CACHE_DIR; String destination = DUMMY_CACHE_DIR;
if (config.getUserSpecifiedACacheDir()) { if (config.getUserSpecifiedACacheDir()) {
destination = config.getStorageDir().getName(); destination = config.getStorageDir().getName();
} }
JPanel cacheDirWrapper = new JPanel();
cacheDirWrapper.setLayout(new BoxLayout(cacheDirWrapper, BoxLayout.LINE_AXIS));
cacheDirText = new JLabel(destination); cacheDirText = new JLabel(destination);
cacheDirText.setBounds(start_label_right, n, 240, size_height_label); cacheDirWrapper.add(cacheDirText);
parent.getContentPane().add(cacheDirText);
cacheDirWrapper.add(Box.createHorizontalGlue());
cacheDirChooser = new JFileChooser(); cacheDirChooser = new JFileChooser();
cacheDirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); cacheDirChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
JButton openButton = new JButton("..."); JButton openButton = new JButton("...");
openButton.addActionListener(new ChooseFileAction()); openButton.addActionListener(new ChooseFileAction());
openButton.setBounds(end_label_right - 50, n, 50, size_height_label); cacheDirWrapper.add(openButton);
parent.getContentPane().add(openButton);
n += sep; constraints.gridwidth = columns - 3;
constraints.gridx = 2;
parent.getContentPane().add(cacheDirWrapper, constraints);
parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
JLabel computeMethodLabel = new JLabel("Use:"); JLabel computeMethodLabel = new JLabel("Use:");
computeMethodLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.gridx = 1;
parent.getContentPane().add(computeMethodLabel); constraints.gridy = currentRow;
parent.getContentPane().add(computeMethodLabel, constraints);
ComputeType method = config.getComputeMethod(); ComputeType method = config.getComputeMethod();
useCPU = new JCheckBox("CPU"); useCPU = new JCheckBox("CPU");
@@ -158,15 +198,15 @@ public class Settings implements Activity {
gpuChecked = true; gpuChecked = true;
} }
int size = 60;
useCPU.addActionListener(new CpuChangeAction()); useCPU.addActionListener(new CpuChangeAction());
useCPU.setBounds(start_label_right, n, size, size_height_label); constraints.gridwidth = Math.max(1, columns - (gpus != null ? gpus.size() : 0) - 3);
parent.getContentPane().add(useCPU); constraints.gridx = 2;
parent.getContentPane().add(useCPU, constraints);
List<GPUDevice> gpus = GPU.listDevices(); constraints.gridwidth = 1;
if (gpus != null) { if (gpus != null) {
for (GPUDevice gpu : gpus) { for (int i=0; i < gpus.size(); i++) {
n += 20; GPUDevice gpu = gpus.get(i);
JCheckBoxGPU gpuCheckBox = new JCheckBoxGPU(gpu); JCheckBoxGPU gpuCheckBox = new JCheckBoxGPU(gpu);
gpuCheckBox.setToolTipText(gpu.getCudaName()); gpuCheckBox.setToolTipText(gpu.getCudaName());
if (gpuChecked) { if (gpuChecked) {
@@ -175,14 +215,15 @@ public class Settings implements Activity {
gpuCheckBox.setSelected(gpuChecked); gpuCheckBox.setSelected(gpuChecked);
} }
} }
gpuCheckBox.setBounds(start_label_right, n, 200, size_height_label);
gpuCheckBox.addActionListener(new GpuChangeAction()); gpuCheckBox.addActionListener(new GpuChangeAction());
parent.getContentPane().add(gpuCheckBox); constraints.gridx = i + 3;
parent.getContentPane().add(gpuCheckBox, constraints);
useGPUs.add(gpuCheckBox); useGPUs.add(gpuCheckBox);
} }
} }
n += sep; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
CPU cpu = new CPU(); CPU cpu = new CPU();
if (cpu.cores() > 1) { // if only one core is available, no need to show the choice if (cpu.cores() > 1) { // if only one core is available, no need to show the choice
@@ -193,31 +234,46 @@ public class Settings implements Activity {
cpuCores.setPaintLabels(true); cpuCores.setPaintLabels(true);
cpuCores.setValue(config.getNbCores() != -1 ? config.getNbCores() : cpuCores.getMaximum()); cpuCores.setValue(config.getNbCores() != -1 ? config.getNbCores() : cpuCores.getMaximum());
JLabel coreLabel = new JLabel("CPU cores:"); JLabel coreLabel = new JLabel("CPU cores:");
coreLabel.setBounds(start_label_left, n, 170, size_height_label); constraints.gridx = 1;
parent.getContentPane().add(coreLabel); constraints.gridy = currentRow;
cpuCores.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label * 2); parent.getContentPane().add(coreLabel, constraints);
parent.getContentPane().add(cpuCores); constraints.gridwidth = columns - 3;
n += sep + size_height_label; constraints.gridx = 2;
parent.getContentPane().add(cpuCores, constraints);
parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
} }
saveFile = new JCheckBox("Save settings", true); saveFile = new JCheckBox("Save settings", true);
saveFile.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label); constraints.gridwidth = columns - 3;
parent.getContentPane().add(saveFile); constraints.gridx = 2;
constraints.gridy = currentRow;
parent.getContentPane().add(saveFile, constraints);
n += 20; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
autoSignIn = new JCheckBox("Auto sign in", config.getAutoSignIn()); autoSignIn = new JCheckBox("Auto sign in", config.getAutoSignIn());
autoSignIn.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label);
autoSignIn.addActionListener(new AutoSignInChangeAction()); autoSignIn.addActionListener(new AutoSignInChangeAction());
parent.getContentPane().add(autoSignIn); constraints.gridy = currentRow;
parent.getContentPane().add(autoSignIn, constraints);
n += sep; parent.addPadding(1, ++currentRow, columns - 2, 1);
++currentRow;
saveButton = new JButton("Start"); saveButton = new JButton("Start");
checkDisplaySaveButton(); checkDisplaySaveButton();
saveButton.setBounds(start_label_right, n, 80, size_height_label);
saveButton.addActionListener(new SaveAction()); saveButton.addActionListener(new SaveAction());
parent.getContentPane().add(saveButton); 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);
if (haveAutoStarted == false && config.getAutoSignIn() && checkDisplaySaveButton()) { if (haveAutoStarted == false && config.getAutoSignIn() && checkDisplaySaveButton()) {
// auto start // auto start

View File

@@ -1,5 +1,7 @@
package com.sheepit.client.standalone.swing.activity; package com.sheepit.client.standalone.swing.activity;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
@@ -8,6 +10,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import javax.swing.JLabel; import javax.swing.JLabel;
@@ -39,75 +42,98 @@ public class Working implements Activity {
@Override @Override
public void show() { public void show() {
int n = 10; GridBagConstraints constraints = new GridBagConstraints();
int size_height_label = 24; int currentRow = 0;
int sep = 45;
int start_label_left = 109; parent.addPadding(1, ++currentRow, 2, 1);
int start_label_right = 280; ++currentRow;
int end_label_right = 490;
ImageIcon image = new ImageIcon(getClass().getResource("/title.png")); ImageIcon image = new ImageIcon(getClass().getResource("/title.png"));
JLabel labelImage = new JLabel(image); JLabel labelImage = new JLabel(image);
labelImage.setBounds(600 / 2 - 265 / 2, n, 265, 130 + n); constraints.fill = GridBagConstraints.BOTH;
n = labelImage.getHeight(); constraints.weightx = 1.0;
parent.getContentPane().add(labelImage); constraints.weighty = 3.0;
constraints.gridwidth = 2;
constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(labelImage, constraints);
n += 40; parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
JLabel statusLabel = new JLabel("Status:"); JLabel statusLabel = new JLabel("Status:");
statusLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.fill = GridBagConstraints.HORIZONTAL;
parent.getContentPane().add(statusLabel); constraints.weighty = 0.0;
constraints.gridwidth = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(statusLabel, constraints);
statusContent.setVerticalAlignment(JLabel.TOP); statusContent.setVerticalAlignment(JLabel.TOP);
statusContent.setVerticalTextPosition(JLabel.TOP); statusContent.setVerticalTextPosition(JLabel.TOP);
statusContent.setBounds(start_label_right, n, 600 - 20 - start_label_right, size_height_label + sep - 3); constraints.gridx = 2;
parent.getContentPane().add(statusContent); parent.getContentPane().add(statusContent, constraints);
n += sep; parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
JLabel creditsEarnedLabel = new JLabel("Credits earned:"); JLabel creditsEarnedLabel = new JLabel("Credits earned:");
creditsEarnedLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.gridx = 1;
parent.getContentPane().add(creditsEarnedLabel); constraints.gridy = currentRow;
parent.getContentPane().add(creditsEarnedLabel, constraints);
creditEarned.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label); constraints.gridx = 2;
parent.getContentPane().add(creditEarned); parent.getContentPane().add(creditEarned, constraints);
n += sep; parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
JLabel renderedFrameLabel = new JLabel("Rendered frames:"); JLabel renderedFrameLabel = new JLabel("Rendered frames:");
renderedFrameLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.gridx = 1;
parent.getContentPane().add(renderedFrameLabel); constraints.gridy = currentRow;
parent.getContentPane().add(renderedFrameLabel, constraints);
renderedFrameContent.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label); constraints.gridx = 2;
parent.getContentPane().add(renderedFrameContent); parent.getContentPane().add(renderedFrameContent, constraints);
n += sep; parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
JLabel remainingFrameLabel = new JLabel("Remaining frames:"); JLabel remainingFrameLabel = new JLabel("Remaining frames:");
remainingFrameLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.gridx = 1;
parent.getContentPane().add(remainingFrameLabel); constraints.gridy = currentRow;
parent.getContentPane().add(remainingFrameLabel, constraints);
remainingFrameContent.setBounds(start_label_right, n, end_label_right - start_label_right, size_height_label); constraints.gridx = 2;
parent.getContentPane().add(remainingFrameContent); parent.getContentPane().add(remainingFrameContent, constraints);
n += sep; parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
JLabel lastRenderedFrameLabel = new JLabel("Last rendered frame:"); JLabel lastRenderedFrameLabel = new JLabel("Last rendered frame:");
lastRenderedFrameLabel.setBounds(start_label_left, n, 240, size_height_label); constraints.gridx = 1;
parent.getContentPane().add(lastRenderedFrameLabel); constraints.gridy = currentRow;
parent.getContentPane().add(lastRenderedFrameLabel, constraints);
lastRender.setBounds(start_label_right, n, 200, 112); constraints.gridx = 2;
parent.getContentPane().add(lastRender); parent.getContentPane().add(lastRender, constraints);
parent.addPadding(1, ++currentRow, 2, 1);
++currentRow;
JButton settingsButton = new JButton("Settings"); JButton settingsButton = new JButton("Settings");
settingsButton.setBounds(220, 500, 100, 25);
settingsButton.addActionListener(new SettingsAction()); settingsButton.addActionListener(new SettingsAction());
parent.getContentPane().add(settingsButton); constraints.gridx = 1;
constraints.gridy = currentRow;
parent.getContentPane().add(settingsButton, constraints);
pauseButton = new JButton("Pause"); pauseButton = new JButton("Pause");
pauseButton.setBounds(330, 500, 100, 25);
pauseButton.addActionListener(new PauseAction()); pauseButton.addActionListener(new PauseAction());
parent.getContentPane().add(pauseButton); constraints.gridx = 2;
parent.getContentPane().add(pauseButton, constraints);
parent.addPadding(1, ++currentRow, 2, 1);
parent.addPadding(0, 0, 1, currentRow + 1);
parent.addPadding(3, 0, 1, currentRow + 1);
} }
public void setStatus(String msg_) { public void setStatus(String msg_) {