implemented scrollbars, fixed npe

This commit is contained in:
Mathis Waldmann
2021-06-22 15:41:56 +02:00
parent f6ae6ea182
commit 138d68f791
3 changed files with 77 additions and 51 deletions

View File

@@ -19,32 +19,9 @@
package com.sheepit.client.standalone; package com.sheepit.client.standalone;
import java.awt.AWTException; import com.formdev.flatlaf.FlatDarkLaf;
import java.awt.GridBagLayout; import com.formdev.flatlaf.FlatLaf;
import java.awt.Image; import com.formdev.flatlaf.FlatLightLaf;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.imageio.ImageIO;
import com.sheepit.client.Client; import com.sheepit.client.Client;
import com.sheepit.client.Configuration; import com.sheepit.client.Configuration;
import com.sheepit.client.Gui; import com.sheepit.client.Gui;
@@ -56,9 +33,33 @@ import com.sheepit.client.standalone.swing.activity.Working;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import com.formdev.flatlaf.FlatLightLaf; // Required for dark & light mode import javax.imageio.ImageIO;
import com.formdev.flatlaf.FlatDarkLaf; import javax.swing.ImageIcon;
import com.formdev.flatlaf.FlatLaf; import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
public class GuiSwing extends JFrame implements Gui { public class GuiSwing extends JFrame implements Gui {
public static final String type = "swing"; public static final String type = "swing";
@@ -143,7 +144,22 @@ public class GuiSwing extends JFrame implements Gui {
panel = new JPanel(); panel = new JPanel();
panel.setLayout(new GridBagLayout()); panel.setLayout(new GridBagLayout());
setContentPane(this.panel); Dimension panelPrefSize = new Dimension(this.getSize().width - 20, this.getSize().height);
panel.setPreferredSize(panelPrefSize);
JScrollPane scrollPane = new JScrollPane(panel);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
//adjust scrollbar thickness
scrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(13, 0));
scrollPane.getHorizontalScrollBar().setPreferredSize(new Dimension(0, 13));
//adjust the speed of scrolling
scrollPane.getVerticalScrollBar().setUnitIncrement(50);
scrollPane.getHorizontalScrollBar().setUnitIncrement(50);
setContentPane(scrollPane);
panel.setBorder(new EmptyBorder(20, 20, 20, 20)); panel.setBorder(new EmptyBorder(20, 20, 20, 20));
activityWorking = new Working(this); activityWorking = new Working(this);
@@ -160,11 +176,17 @@ public class GuiSwing extends JFrame implements Gui {
} }
// Apply the selected theme to swing components // Apply the selected theme to swing components
FlatLaf.updateUI(); SwingUtilities.invokeAndWait(() -> FlatLaf.updateUI());
} }
catch (UnsupportedLookAndFeelException e1) { catch (UnsupportedLookAndFeelException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
catch (InterruptedException e) {
e.printStackTrace();
}
catch (InvocationTargetException e) {
e.printStackTrace();
}
while (waitingForAuthentication) { while (waitingForAuthentication) {
try { try {
@@ -383,6 +405,10 @@ public class GuiSwing extends JFrame implements Gui {
} }
public JPanel getContentPanel() {
return this.panel;
}
private Image extractImageFromSprite(int spriteNumber) { private Image extractImageFromSprite(int spriteNumber) {
// Sprite structure // Sprite structure
// Image 0: base sprite // Image 0: base sprite

View File

@@ -152,12 +152,12 @@ public class Settings implements Activity {
constraints.gridwidth = 2; constraints.gridwidth = 2;
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = currentRow; constraints.gridy = currentRow;
parent.getContentPane().add(labelImage, constraints); parent.getContentPanel().add(labelImage, constraints);
++currentRow; ++currentRow;
constraints.gridy = currentRow; constraints.gridy = currentRow;
parent.getContentPane().add(new JLabel(" "), constraints); // Add a separator between logo and first panel parent.getContentPanel().add(new JLabel(" "), constraints); // Add a separator between logo and first panel
currentRow++; currentRow++;
@@ -185,7 +185,7 @@ public class Settings implements Activity {
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = currentRow; constraints.gridy = currentRow;
constraints.fill = GridBagConstraints.HORIZONTAL; constraints.fill = GridBagConstraints.HORIZONTAL;
parent.getContentPane().add(authentication_panel, constraints); parent.getContentPanel().add(authentication_panel, constraints);
// Theme selection panel // Theme selection panel
CollapsibleJPanel themePanel = new CollapsibleJPanel(new GridLayout(1, 3), this); CollapsibleJPanel themePanel = new CollapsibleJPanel(new GridLayout(1, 3), this);
@@ -215,7 +215,7 @@ public class Settings implements Activity {
constraints.gridy = currentRow; constraints.gridy = currentRow;
constraints.gridwidth = 2; constraints.gridwidth = 2;
parent.getContentPane().add(themePanel, constraints); parent.getContentPanel().add(themePanel, constraints);
// directory // directory
CollapsibleJPanel directory_panel = new CollapsibleJPanel(new GridLayout(1, 3), this); CollapsibleJPanel directory_panel = new CollapsibleJPanel(new GridLayout(1, 3), this);
@@ -248,7 +248,7 @@ public class Settings implements Activity {
constraints.gridy = currentRow; constraints.gridy = currentRow;
constraints.gridwidth = 2; constraints.gridwidth = 2;
parent.getContentPane().add(directory_panel, constraints); parent.getContentPanel().add(directory_panel, constraints);
// compute devices // compute devices
GridBagLayout gridbag = new GridBagLayout(); GridBagLayout gridbag = new GridBagLayout();
@@ -439,7 +439,7 @@ public class Settings implements Activity {
compute_devices_panel.add(new JLabel(" "), compute_devices_constraints); // Add a space between lines compute_devices_panel.add(new JLabel(" "), compute_devices_constraints); // Add a space between lines
compute_devices_panel.add(ram); compute_devices_panel.add(ram);
parent.getContentPane().add(compute_devices_panel, constraints); parent.getContentPanel().add(compute_devices_panel, constraints);
// priority // priority
// ui display low -> high but the actual values are reversed // ui display low -> high but the actual values are reversed
@@ -491,7 +491,7 @@ public class Settings implements Activity {
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = currentRow; constraints.gridy = currentRow;
constraints.gridwidth = 2; constraints.gridwidth = 2;
parent.getContentPane().add(compute_devices_panel, constraints); parent.getContentPanel().add(compute_devices_panel, constraints);
// other // other
CollapsibleJPanel advanced_panel = new CollapsibleJPanel(new GridLayout(4, 2), this); CollapsibleJPanel advanced_panel = new CollapsibleJPanel(new GridLayout(4, 2), this);
@@ -538,7 +538,7 @@ public class Settings implements Activity {
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = currentRow; constraints.gridy = currentRow;
constraints.gridwidth = 2; constraints.gridwidth = 2;
parent.getContentPane().add(advanced_panel, constraints); parent.getContentPanel().add(advanced_panel, constraints);
// general settings // general settings
JPanel general_panel = new JPanel(new GridLayout(1, 2)); JPanel general_panel = new JPanel(new GridLayout(1, 2));
@@ -554,11 +554,11 @@ public class Settings implements Activity {
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = currentRow; constraints.gridy = currentRow;
constraints.gridwidth = 2; constraints.gridwidth = 2;
parent.getContentPane().add(general_panel, constraints); parent.getContentPanel().add(general_panel, constraints);
currentRow++; currentRow++;
constraints.gridy = currentRow; constraints.gridy = currentRow;
parent.getContentPane().add(new JLabel(" "), constraints); // Add a separator between last checkboxes and button parent.getContentPanel().add(new JLabel(" "), constraints); // Add a separator between last checkboxes and button
currentRow++; currentRow++;
String buttonText = "Start"; String buttonText = "Start";
@@ -574,7 +574,7 @@ public class Settings implements Activity {
constraints.gridwidth = 2; constraints.gridwidth = 2;
constraints.gridx = 0; constraints.gridx = 0;
constraints.gridy = currentRow; constraints.gridy = currentRow;
parent.getContentPane().add(saveButton, constraints); parent.getContentPanel().add(saveButton, constraints);
// Increase the size of the app Window to ensure it shows all the information with the Advanced Options panel opened. // Increase the size of the app Window to ensure it shows all the information with the Advanced Options panel opened.
parent.setSize(520,850); parent.setSize(520,850);

View File

@@ -235,7 +235,7 @@ public class Working implements Activity {
ImageIcon image = new ImageIcon(getClass().getResource("/sheepit-logo.png")); ImageIcon image = new ImageIcon(getClass().getResource("/sheepit-logo.png"));
JLabel labelImage = new JLabel(image); JLabel labelImage = new JLabel(image);
labelImage.setAlignmentX(Component.CENTER_ALIGNMENT); labelImage.setAlignmentX(Component.CENTER_ALIGNMENT);
parent.getContentPane().add(labelImage); parent.getContentPanel().add(labelImage);
JPanel buttonsPanel = new JPanel(new GridLayout(2, 2)); JPanel buttonsPanel = new JPanel(new GridLayout(2, 2));
@@ -261,19 +261,19 @@ public class Working implements Activity {
buttonsPanel.add(blockJob); buttonsPanel.add(blockJob);
buttonsPanel.add(exitAfterFrame); buttonsPanel.add(exitAfterFrame);
parent.getContentPane().setLayout(new GridBagLayout()); parent.getContentPanel().setLayout(new GridBagLayout());
GridBagConstraints global_constraints = new GridBagConstraints(); GridBagConstraints global_constraints = new GridBagConstraints();
global_constraints.fill = GridBagConstraints.HORIZONTAL; global_constraints.fill = GridBagConstraints.HORIZONTAL;
global_constraints.weightx = 1; global_constraints.weightx = 1;
global_constraints.gridx = 0; global_constraints.gridx = 0;
parent.getContentPane().add(new JLabel(" "), global_constraints); // Add a separator between logo and first panel parent.getContentPanel().add(new JLabel(" "), global_constraints); // Add a separator between logo and first panel
parent.getContentPane().add(current_project_panel, global_constraints); parent.getContentPanel().add(current_project_panel, global_constraints);
parent.getContentPane().add(global_stats_panel, global_constraints); parent.getContentPanel().add(global_stats_panel, global_constraints);
parent.getContentPane().add(session_info_panel, global_constraints); parent.getContentPanel().add(session_info_panel, global_constraints);
parent.getContentPane().add(last_frame_panel, global_constraints); parent.getContentPanel().add(last_frame_panel, global_constraints);
parent.getContentPane().add(new JLabel(" "), global_constraints); // Add a separator between last panel and buttons parent.getContentPanel().add(new JLabel(" "), global_constraints); // Add a separator between last panel and buttons
parent.getContentPane().add(buttonsPanel, global_constraints); parent.getContentPanel().add(buttonsPanel, global_constraints);
// Set the proper size for the Working (if coming from Settings screen, the window size will be too big for the content!) // Set the proper size for the Working (if coming from Settings screen, the window size will be too big for the content!)
parent.setSize(520, 820); parent.setSize(520, 820);