HIDtoVPADNetworkClient/src/net/ash/HIDToVPADNetworkClient/gui/GuiOptionsWindow.java

271 lines
10 KiB
Java
Raw Normal View History

2017-04-10 11:04:06 +02:00
/*******************************************************************************
* Copyright (c) 2017 Ash (QuarkTheAwesome) & Maschell
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*******************************************************************************/
package net.ash.HIDToVPADNetworkClient.gui;
import java.awt.BorderLayout;
2017-04-11 10:49:18 +02:00
import java.awt.Component;
2017-04-10 11:04:06 +02:00
import java.awt.Dimension;
2017-04-11 10:49:18 +02:00
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
2017-04-10 11:04:06 +02:00
2017-04-11 10:49:18 +02:00
import javax.swing.BorderFactory;
2017-04-11 11:10:08 +02:00
import javax.swing.Box;
2017-04-11 10:49:18 +02:00
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
2017-04-10 11:04:06 +02:00
import javax.swing.JFrame;
2017-04-12 07:47:22 +02:00
import javax.swing.JLabel;
2017-04-10 11:04:06 +02:00
import javax.swing.JPanel;
2017-04-12 07:57:32 +02:00
import javax.swing.JScrollPane;
2017-04-11 10:49:18 +02:00
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
2017-04-12 07:47:22 +02:00
import javax.swing.border.EtchedBorder;
2017-04-10 11:04:06 +02:00
import lombok.extern.java.Log;
2017-04-11 12:31:04 +02:00
import net.ash.HIDToVPADNetworkClient.util.Settings;
2017-04-12 09:15:38 +02:00
import net.ash.HIDToVPADNetworkClient.util.StatusReport;
2017-04-10 11:04:06 +02:00
@Log
public class GuiOptionsWindow extends JPanel {
private static final long serialVersionUID = 1L;
2017-04-11 10:49:18 +02:00
private static final GuiOptionsWindow instance = new GuiOptionsWindow();
2017-04-11 10:49:18 +02:00
private final List<Tab> tabs = new ArrayList<Tab>();
2017-04-10 11:04:06 +02:00
public static void showWindow() {
2017-04-12 07:57:32 +02:00
showWindow(null);
}
2017-04-12 07:57:32 +02:00
public static void showWindow(Component parent) {
2017-04-10 11:04:06 +02:00
instance.setOpaque(true);
2017-04-11 10:49:18 +02:00
for (Tab t : instance.tabs) {
t.updateTab();
}
2017-04-11 10:49:18 +02:00
JFrame window = new JFrame("Options");
2017-04-10 11:04:06 +02:00
window.setContentPane(instance);
window.pack();
2017-04-12 07:57:32 +02:00
window.setLocationRelativeTo(parent);
2017-04-10 11:04:06 +02:00
window.setVisible(true);
}
2017-04-10 11:04:06 +02:00
private GuiOptionsWindow() {
2017-04-11 10:49:18 +02:00
super(new GridLayout(1, 1));
2017-04-10 11:04:06 +02:00
log.info("Hello from the Options window!");
setPreferredSize(new Dimension(600, 200));
2017-04-11 10:49:18 +02:00
JTabbedPane tabPane = new JTabbedPane();
tabPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
Tab controllerTab = new ControllerTab();
tabs.add(controllerTab);
tabPane.addTab("Controllers", controllerTab);
2017-04-11 10:49:18 +02:00
Tab infoTab = new InfoTab();
tabs.add(infoTab);
tabPane.addTab("Info", infoTab);
2017-04-11 10:49:18 +02:00
add(tabPane);
}
private class ControllerTab extends Tab {
private static final long serialVersionUID = 1L;
private final ControllerFilteringList cFilterList;
private final JCheckBox cBoxScanForControllers;
private final JCheckBox cBoxAutoActivateControllers;
private ControllerTab() {
super(new GridLayout(1, 2));
cFilterList = new ControllerFilteringList();
2017-04-11 12:31:04 +02:00
for (Settings.ControllerFiltering.Type type : Settings.ControllerFiltering.Type.values()) {
if (!type.isSupportedOnPlatform()) continue;
2017-04-11 12:31:04 +02:00
ControllerFilteringListItem item = new ControllerFilteringListItem(type);
cFilterList.add(item);
}
add(cFilterList);
JPanel rightSideControls = new JPanel();
rightSideControls.setLayout(new BoxLayout(rightSideControls, BoxLayout.PAGE_AXIS));
rightSideControls.add(Box.createVerticalGlue());
cBoxScanForControllers = new JCheckBox("Automatically scan for controllers");
cBoxScanForControllers.setAlignmentX(Component.CENTER_ALIGNMENT);
cBoxScanForControllers.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS = cBoxScanForControllers.isSelected();
}
});
rightSideControls.add(cBoxScanForControllers);
rightSideControls.add(Box.createVerticalStrut(2));
cBoxAutoActivateControllers = new JCheckBox("Automatically activate controllers");
cBoxAutoActivateControllers.setAlignmentX(Component.CENTER_ALIGNMENT);
cBoxAutoActivateControllers.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Settings.AUTO_ACTIVATE_CONTROLLER = cBoxAutoActivateControllers.isSelected();
}
});
rightSideControls.add(cBoxAutoActivateControllers);
rightSideControls.add(Box.createVerticalGlue());
add(rightSideControls);
}
@Override
public void updateTab() {
for (ControllerFilteringListItem c : cFilterList.items) {
c.updateItem();
}
cBoxScanForControllers.setSelected(Settings.SCAN_AUTOMATICALLY_FOR_CONTROLLERS);
cBoxAutoActivateControllers.setSelected(Settings.AUTO_ACTIVATE_CONTROLLER);
}
private class ControllerFilteringList extends JPanel {
private static final long serialVersionUID = 1L;
private List<ControllerFilteringListItem> items = new ArrayList<ControllerFilteringListItem>();
2017-04-12 07:47:22 +02:00
private JPanel innerPanel;
private ControllerFilteringList() {
2017-04-12 07:47:22 +02:00
super(new BorderLayout());
setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 0));
2017-04-12 07:47:22 +02:00
innerPanel = new JPanel();
innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.PAGE_AXIS));
2017-04-12 07:57:32 +02:00
JScrollPane innerPanelWrap = new JScrollPane(innerPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
innerPanelWrap.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
add(innerPanelWrap, BorderLayout.CENTER);
2017-04-12 07:47:22 +02:00
JLabel controllerFilterText = new JLabel("Controllers to show:");
controllerFilterText.setBorder(BorderFactory.createEmptyBorder(0, 0, 5, 0));
add(controllerFilterText, BorderLayout.PAGE_START);
}
public Component add(ControllerFilteringListItem c) {
items.add(c);
2017-04-12 07:47:22 +02:00
return innerPanel.add(c);
}
}
private class ControllerFilteringListItem extends JPanel {
private static final long serialVersionUID = 1L;
private final JCheckBox cBox;
2017-04-11 12:31:04 +02:00
private final Settings.ControllerFiltering.Type type;
2017-04-11 12:31:04 +02:00
private ControllerFilteringListItem(Settings.ControllerFiltering.Type typeIn) {
super(new GridLayout(1, 1));
2017-04-11 12:31:04 +02:00
this.type = typeIn;
2017-04-11 12:31:04 +02:00
cBox = new JCheckBox(type.getName());
cBox.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
cBox.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
2017-04-11 12:31:04 +02:00
Settings.ControllerFiltering.setFilterState(type, cBox.isSelected());
}
});
add(cBox);
}
public void updateItem() {
2017-04-11 12:31:04 +02:00
cBox.setSelected(Settings.ControllerFiltering.getFilterState(type));
}
// I can't believe I didn't figure this out for GuiControllerList
@Override
public Dimension getMaximumSize() {
return new Dimension(Integer.MAX_VALUE, getPreferredSize().height);
}
}
}
2017-04-11 10:49:18 +02:00
private class InfoTab extends Tab {
private static final long serialVersionUID = 1L;
2017-04-11 10:49:18 +02:00
private final JTextArea infoText;
2017-04-12 09:15:38 +02:00
private final JScrollPane infoTextWrap;
2017-04-11 10:49:18 +02:00
private InfoTab() {
super();
setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
2017-04-12 09:15:38 +02:00
setBorder(BorderFactory.createEmptyBorder(5, 5, 10, 5));
2017-04-11 10:49:18 +02:00
infoText = new JTextArea();
2017-04-11 11:10:08 +02:00
infoText.setEditable(false);
2017-04-12 09:15:38 +02:00
infoTextWrap = new JScrollPane(infoText, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
2017-04-11 11:10:08 +02:00
infoTextWrap.setAlignmentX(Component.CENTER_ALIGNMENT);
add(infoTextWrap);
2017-04-12 09:15:38 +02:00
add(Box.createVerticalStrut(10));
2017-04-11 10:49:18 +02:00
JButton copyButton = new JButton("Copy");
copyButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
StringSelection data = new StringSelection(infoText.getText());
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(data, data);
}
});
copyButton.setAlignmentX(Component.CENTER_ALIGNMENT);
add(copyButton);
}
@Override
public void updateTab() {
2017-04-12 09:15:38 +02:00
infoText.setCaretPosition(0);
infoText.setText(StatusReport.generateStatusReport());
infoText.setCaretPosition(0);
2017-04-11 10:49:18 +02:00
}
}
2017-04-11 10:49:18 +02:00
private abstract class Tab extends JPanel {
private static final long serialVersionUID = 1L;
2017-04-11 10:49:18 +02:00
public abstract void updateTab();
2017-04-11 10:49:18 +02:00
public Tab(LayoutManager l) {
super(l);
}
2017-04-11 10:49:18 +02:00
public Tab() {
super();
}
2017-04-10 11:04:06 +02:00
}
}