Adding option to collapse panels in settings

This commit is contained in:
Robin Jönsson
2019-05-03 15:49:51 +02:00
committed by Laurent Clouet
parent 19c73fad96
commit 242104735b
2 changed files with 162 additions and 4 deletions

View File

@@ -57,6 +57,7 @@ import com.sheepit.client.hardware.gpu.GPUDevice;
import com.sheepit.client.network.Proxy;
import com.sheepit.client.os.OS;
import com.sheepit.client.standalone.GuiSwing;
import com.sheepit.client.standalone.swing.components.CollapsibleJPanel;
public class Settings implements Activity {
private static final String DUMMY_CACHE_DIR = "Auto detected";
@@ -115,7 +116,7 @@ public class Settings implements Activity {
++currentRow;
// authentication
JPanel authentication_panel = new JPanel(new GridLayout(2, 2));
CollapsibleJPanel authentication_panel = new CollapsibleJPanel(new GridLayout(2, 2));
authentication_panel.setBorder(BorderFactory.createTitledBorder("Authentication"));
JLabel loginLabel = new JLabel("Username:");
@@ -141,7 +142,7 @@ public class Settings implements Activity {
parent.getContentPane().add(authentication_panel, constraints);
// directory
JPanel directory_panel = new JPanel(new GridLayout(1, 3));
CollapsibleJPanel directory_panel = new CollapsibleJPanel(new GridLayout(1, 3));
directory_panel.setBorder(BorderFactory.createTitledBorder("Cache"));
JLabel cacheLabel = new JLabel("Working directory:");
directory_panel.add(cacheLabel);
@@ -175,7 +176,7 @@ public class Settings implements Activity {
// compute devices
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints compute_devices_constraints = new GridBagConstraints();
JPanel compute_devices_panel = new JPanel(gridbag);
CollapsibleJPanel compute_devices_panel = new CollapsibleJPanel(gridbag);
compute_devices_panel.setBorder(BorderFactory.createTitledBorder("Compute devices"));
@@ -320,7 +321,7 @@ public class Settings implements Activity {
parent.getContentPane().add(compute_devices_panel, constraints);
// other
JPanel advanced_panel = new JPanel(new GridLayout(5, 2));
CollapsibleJPanel advanced_panel = new CollapsibleJPanel(new GridLayout(5, 2));
advanced_panel.setBorder(BorderFactory.createTitledBorder("Advanced options"));
JLabel proxyLabel = new JLabel("Proxy:");
@@ -379,6 +380,7 @@ public class Settings implements Activity {
constraints.gridy = currentRow;
constraints.gridwidth = 2;
parent.getContentPane().add(advanced_panel, constraints);
advanced_panel.setCollapsed(true);
// general settings
JPanel general_panel = new JPanel(new GridLayout(1, 2));

View File

@@ -0,0 +1,156 @@
/*
* Copyright (C) 2015 Laurent CLOUET
*
* 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.
*/
package com.sheepit.client.standalone.swing.components;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
public class CollapsibleJPanel extends JPanel {
private boolean isCompnentsVisible = true;
private int originalHeight;
private String borderTitle = "";
private int COLLAPSED_HEIGHT = 22;
private boolean[] originalVisibilty;
public CollapsibleJPanel(LayoutManager layoutManager) {
setLayout(layoutManager);
addMouseListener(new onClickHandler());
}
public void setCollapsed(boolean aFlag) {
if (aFlag) {
hideComponents();
}
else {
showComponents();
}
}
public void toggleCollapsed() {
if (isCompnentsVisible) {
setCollapsed(true);
}
else {
setCollapsed(false);
}
}
private void hideComponents() {
Component[] components = getComponents();
originalVisibilty = new boolean[components.length];
// Hide all componens on panel
for (int i = 0; i < components.length; i++) {
originalVisibilty[i] = components[i].isVisible();
components[i].setVisible(false);
}
setHeight(COLLAPSED_HEIGHT);
// Add '+' char to end of border title
//setBorder(BorderFactory.createTitledBorder(BorderFactory.createEmptyBorder(), borderTitle + " + "));
setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), " + " + borderTitle));
// Update flag
isCompnentsVisible = false;
}
private void showComponents() {
Component[] components = getComponents();
// Set all components in panel to visible
for (int i = 0; i < components.length; i++) {
components[i].setVisible(originalVisibilty[i]);
}
setHeight(originalHeight);
// Add '-' char to end of border title
setBorder(BorderFactory.createTitledBorder(" - " + borderTitle));
// Update flag
isCompnentsVisible = true;
}
private void setHeight(int height) {
setPreferredSize(new Dimension(getPreferredSize().width, height));
setMinimumSize(new Dimension(getMinimumSize().width, height));
setMaximumSize(new Dimension(getMaximumSize().width, height));
}
@Override
public Component add(Component component) { // Need this to get the original height of panel
Component returnComponent = super.add(component);
originalHeight = getPreferredSize().height;
return returnComponent;
}
@Override
public void setBorder(Border border) { // Need this to get the border title
if (border instanceof TitledBorder && (borderTitle == "")) {
borderTitle = ((TitledBorder) border).getTitle();
((TitledBorder) border).setTitle(" - " + borderTitle);
}
super.setBorder(border);
}
public class onClickHandler implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
if (e.getPoint().y < COLLAPSED_HEIGHT) { // Only if click is on top of panel
((CollapsibleJPanel) e.getComponent()).toggleCollapsed();
}
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
}
}