Skip to content

Commit

Permalink
EditActionsDialog: layout improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed Dec 23, 2024
1 parent 4ace4e9 commit 852b0ce
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ public final class EditActionsDialog extends JDialog {

private static final Logger log = Logger.getLogger(EditActionsDialog.class.getName());

private static final int DIALOG_BOUNDS_WIDTH = 1000;
private static final int DIALOG_BOUNDS_HEIGHT = 600;
private static final int DIALOG_BOUNDS_WIDTH = 1015;
private static final int DIALOG_BOUNDS_HEIGHT = 665;
private static final int DIALOG_BOUNDS_PARENT_OFFSET = 25;

private static final double ACTIONS_LIST_WEIGHT_X = .245d;

private static final List<Class<?>> axisActionClasses;
private static final List<Class<?>> buttonActionClasses;
private static final List<Class<?>> cycleActionClasses;
Expand Down Expand Up @@ -434,8 +436,10 @@ private void init() {
addButton.setEnabled(selectedAvailableAction != null);
});
updateAvailableActions();
actionsPanel.add(new JScrollPane(availableActionsList), new GridBagConstraints(0, 1, 1, 5, .1d, 1d,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

actionsPanel.add(GuiUtils.wrapComponentInScrollPane(availableActionsList),
new GridBagConstraints(0, 1, 1, 5, ACTIONS_LIST_WEIGHT_X, 1d, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

actionsPanel.add(new JLabel(Main.strings.getString("ASSIGNED_ACTIONS_LABEL")), new GridBagConstraints(2, 0, 1,
1, 0d, 0d, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 25));
Expand All @@ -447,7 +451,7 @@ private void init() {

final var propertiesScrollPane = new JScrollPane();
propertiesScrollPane.setVisible(false);
actionsPanel.add(propertiesScrollPane, new GridBagConstraints(3, 1, 1, 5, .8d, 1d, GridBagConstraints.CENTER,
actionsPanel.add(propertiesScrollPane, new GridBagConstraints(3, 1, 1, 5, 1d, 1d, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

assignedActionsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
Expand All @@ -462,14 +466,14 @@ private void init() {
if (selectedAssignedAction != null) {
final var actionClass = selectedAssignedAction.action.getClass();
final var fieldToActionPropertyMap = getFieldToActionPropertiesMap(actionClass);
final var sortedEntires = fieldToActionPropertyMap.entrySet().stream().sorted((entry1, entry2) -> {
final var sortedEntries = fieldToActionPropertyMap.entrySet().stream().sorted((entry1, entry2) -> {
final var action1 = entry1.getValue();
final var action2 = entry2.getValue();

return action1.order() - action2.order();
}).toList();

for (final var entry : sortedEntires) {
for (final var entry : sortedEntries) {
final var field = entry.getKey();
final var annotation = entry.getValue();

Expand All @@ -484,7 +488,7 @@ private void init() {
new Insets(5, 5, 5, 5), 0, 10));

final var propertyNameLabel = new JLabel(Main.strings.getString(annotation.label()));
propertyNameLabel.setPreferredSize(new Dimension(175, 15));
propertyNameLabel.setPreferredSize(new Dimension(155, 15));
propertyPanel.add(propertyNameLabel);

try {
Expand Down Expand Up @@ -522,9 +526,11 @@ private void init() {
}
propertiesLabel.setVisible(anyPropertiesFound);
propertiesScrollPane.setVisible(anyPropertiesFound);
revalidate();
});
actionsPanel.add(new JScrollPane(assignedActionsList), new GridBagConstraints(2, 1, 1, 5, .1d, 1d,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
actionsPanel.add(GuiUtils.wrapComponentInScrollPane(assignedActionsList),
new GridBagConstraints(2, 1, 1, 5, ACTIONS_LIST_WEIGHT_X, 1d, GridBagConstraints.CENTER,
GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

final var okCancelButtonPanel = new JPanel();
okCancelButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/de/bwravencl/controllerbuddy/gui/GuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Frame;
Expand All @@ -46,6 +47,7 @@
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

@SuppressWarnings({ "exports", "missing-explicit-ctor" })
public final class GuiUtils {
Expand Down Expand Up @@ -197,6 +199,21 @@ public static void showMessageDialog(final Main main, @SuppressWarnings("exports
JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
}

static JScrollPane wrapComponentInScrollPane(final java.awt.Component component) {
return wrapComponentInScrollPane(component, null);
}

public static JScrollPane wrapComponentInScrollPane(final java.awt.Component component,
final Dimension preferredSize) {
final var scrollPane = new JScrollPane(component);

if (preferredSize != null) {
scrollPane.setPreferredSize(preferredSize);
}

return scrollPane;
}

static class FrameDragListener extends MouseAdapter {

private final Main main;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/de/bwravencl/controllerbuddy/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public final class Main {
static final int DEFAULT_OVERLAY_SCALING = 1;
static final int BUTTON_DIMENSION_HEIGHT = 25;
@SuppressWarnings("exports")
public static final Dimension BUTTON_DIMENSION = new Dimension(120, BUTTON_DIMENSION_HEIGHT);
public static final Dimension BUTTON_DIMENSION = new Dimension(115, BUTTON_DIMENSION_HEIGHT);
@SuppressWarnings({ "exports", "SuspiciousNameCombination" })
public static final Dimension SQUARE_BUTTON_DIMENSION = new Dimension(BUTTON_DIMENSION_HEIGHT,
BUTTON_DIMENSION_HEIGHT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package de.bwravencl.controllerbuddy.input.action.gui;

import de.bwravencl.controllerbuddy.gui.EditActionsDialog;
import de.bwravencl.controllerbuddy.gui.GuiUtils;
import de.bwravencl.controllerbuddy.gui.Main;
import de.bwravencl.controllerbuddy.input.KeyStroke;
import de.bwravencl.controllerbuddy.input.ScanCode;
Expand All @@ -27,13 +28,13 @@
import java.io.Serial;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
Expand All @@ -42,8 +43,6 @@
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
Expand All @@ -52,7 +51,12 @@
public final class KeystrokeEditorBuilder extends EditorBuilder {

private static final Logger log = Logger.getLogger(KeystrokeEditorBuilder.class.getName());
private final JTextArea keyStrokeTextArea = new JTextArea();

private static final int KEY_LIST_SCROLL_PANE_WIDTH = 110;
private static final Dimension KEY_LIST_SCROLL_PANE_DIMENSION = new Dimension(KEY_LIST_SCROLL_PANE_WIDTH, 200);

private final JPanel visualizationPanel = new JPanel();
private final JLabel plusLabel = new JLabel("+");
private CheckboxJList<?> modifierList;
private CheckboxJList<?> keyList;

Expand All @@ -62,6 +66,21 @@ public KeystrokeEditorBuilder(final EditActionsDialog editActionsDialog, final I
super(editActionsDialog, action, fieldName, fieldType);
}

private static void addScanCodeLabels(final List<?> scanCodes, final JPanel panel) {
if (scanCodes.isEmpty()) {
panel.add(Box.createHorizontalStrut(KEY_LIST_SCROLL_PANE_WIDTH));
return;
}

scanCodes.stream().map(scanCode -> scanCode.toString().replaceAll(" ", "\u00A0")).forEach(text -> {
final var scanCodeLabel = new JLabel(text);
scanCodeLabel
.setPreferredSize(new Dimension(KEY_LIST_SCROLL_PANE_WIDTH, scanCodeLabel.getMinimumSize().height));
scanCodeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
panel.add(scanCodeLabel);
});
}

private static int getListModelIndex(final ListModel<?> model, final Object value) {
if (value == null) {
return -1;
Expand Down Expand Up @@ -91,60 +110,83 @@ public void buildEditor(final JPanel parentPanel) {
final var availableScanCodes = ScanCode.nameToScanCodeMap.keySet();

final var modifiersPanel = new JPanel();
modifiersPanel.setLayout(new BoxLayout(modifiersPanel, BoxLayout.PAGE_AXIS));
modifiersPanel.setLayout(new BoxLayout(modifiersPanel, BoxLayout.Y_AXIS));
final var modifiersLabel = new JLabel(Main.strings.getString("MODIFIERS_LABEL"));
modifiersLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
modifiersLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
modifiersPanel.add(modifiersLabel);
modifiersPanel.add(Box.createVerticalStrut(5));
modifierList = new CheckboxJList<>(availableScanCodes.toArray(String[]::new));
modifierList.addListSelectionListener(new JListSetPropertyListSelectionListener(setterMethod, keyStroke, true));

final var addedModifiers = new ArrayList<String>();
for (final var modifierCode : keyStroke.getModifierCodes()) {
addedModifiers.add(modifierCode.name());
}
addedModifiers.forEach(s1 -> {
final var index1 = getListModelIndex(modifierList.getModel(), s1);
if (index1 >= 0) {
modifierList.addSelectionInterval(index1, index1);
}
});
modifiersPanel.add(GuiUtils.wrapComponentInScrollPane(modifierList, KEY_LIST_SCROLL_PANE_DIMENSION));

final var modifiersScrollPane = new JScrollPane(modifierList);
modifiersScrollPane.setPreferredSize(new Dimension(130, 200));
modifiersPanel.add(modifiersScrollPane);
keystrokePanel.add(modifiersPanel, BorderLayout.WEST);

final var keysPanel = new JPanel();
keysPanel.setLayout(new BoxLayout(keysPanel, BoxLayout.PAGE_AXIS));
keysPanel.setLayout(new BoxLayout(keysPanel, BoxLayout.Y_AXIS));
final var keysLabel = new JLabel(Main.strings.getString("KEYS_LABEL"));
keysLabel.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
keysLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
keysPanel.add(keysLabel);
keysPanel.add(Box.createVerticalStrut(5));
keyList = new CheckboxJList<>(availableScanCodes.toArray(String[]::new));
keyList.addListSelectionListener(new JListSetPropertyListSelectionListener(setterMethod, keyStroke, false));

final var addedKeys = new ArrayList<String>();
for (final var keyCode : keyStroke.getKeyCodes()) {
addedKeys.add(keyCode.name());
}
addedKeys.forEach(s2 -> {
final var index2 = getListModelIndex(keyList.getModel(), s2);
if (index2 >= 0) {
keyList.addSelectionInterval(index2, index2);
keysPanel.add(GuiUtils.wrapComponentInScrollPane(keyList, KEY_LIST_SCROLL_PANE_DIMENSION));

keystrokePanel.add(keysPanel, BorderLayout.EAST);

plusLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
plusLabel.setAlignmentY(Component.CENTER_ALIGNMENT);

visualizationPanel.setLayout(new BoxLayout(visualizationPanel, BoxLayout.X_AXIS));
keystrokePanel.add(visualizationPanel, BorderLayout.SOUTH);

initListSelection(modifierList, keyStroke.getModifierCodes());
initListSelection(keyList, keyStroke.getKeyCodes());

updateUpdateKeyStrokeVisualization();
}

private void initListSelection(final JList<?> list, final ScanCode[] scanCodes) {
Arrays.stream(scanCodes).map(ScanCode::name).forEach(scanCodeName -> {
final var index = getListModelIndex(list.getModel(), scanCodeName);
if (index >= 0) {
list.addSelectionInterval(index, index);
}
});
}

final var keysScrollPane = new JScrollPane(keyList);
keysScrollPane.setPreferredSize(new Dimension(130, 200));
keysPanel.add(keysScrollPane);
keystrokePanel.add(keysPanel, BorderLayout.EAST);
private void updateUpdateKeyStrokeVisualization() {
visualizationPanel.removeAll();

final var selectedModifiersList = modifierList != null ? modifierList.getSelectedValuesList()
: Collections.emptyList();

final var selectedKeysList = keyList != null ? keyList.getSelectedValuesList() : Collections.emptyList();

final var modifierVisualizationPanel = new JPanel();
modifierVisualizationPanel.setLayout(new BoxLayout(modifierVisualizationPanel, BoxLayout.Y_AXIS));
addScanCodeLabels(selectedModifiersList, modifierVisualizationPanel);
visualizationPanel.add(modifierVisualizationPanel);

visualizationPanel.add(Box.createHorizontalGlue());
visualizationPanel.add(Box.createHorizontalStrut(5));

if (!selectedModifiersList.isEmpty() && !selectedKeysList.isEmpty()) {
visualizationPanel.add(plusLabel);
} else {
visualizationPanel.add(Box.createHorizontalStrut(plusLabel.getMinimumSize().width));
}

keyStrokeTextArea.setLineWrap(true);
keyStrokeTextArea.setWrapStyleWord(true);
keyStrokeTextArea.setEditable(false);
keyStrokeTextArea.setFocusable(false);
keystrokePanel.add(keyStrokeTextArea, BorderLayout.SOUTH);
visualizationPanel.add(Box.createHorizontalStrut(5));
visualizationPanel.add(Box.createHorizontalGlue());

final var keyVisualizationPanel = new JPanel();
keyVisualizationPanel.setLayout(new BoxLayout(keyVisualizationPanel, BoxLayout.Y_AXIS));
addScanCodeLabels(selectedKeysList, keyVisualizationPanel);
visualizationPanel.add(keyVisualizationPanel);

visualizationPanel.revalidate();
}

private static final class CheckboxJList<E> extends JList<E> {
Expand Down Expand Up @@ -176,6 +218,10 @@ public void setSelectionInterval(final int index0, final int index1) {
addSelectionInterval(index0, index1);
}
}

@Override
public void setValueIsAdjusting(final boolean isAdjusting) {
}
});
}

Expand Down Expand Up @@ -235,16 +281,7 @@ public void valueChanged(final ListSelectionEvent e) {

setterMethod.invoke(action, keyStroke);

final Set<Object> keyStrokeSet = new LinkedHashSet<>();
if (modifierList != null) {
keyStrokeSet.addAll(modifierList.getSelectedValuesList());
}
if (keyList != null) {
keyStrokeSet.addAll(keyList.getSelectedValuesList());
}

keyStrokeTextArea
.setText(keyStrokeSet.stream().map(Object::toString).collect(Collectors.joining(" + ")));
updateUpdateKeyStrokeVisualization();
} catch (final IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
log.log(Level.SEVERE, e1.getMessage(), e1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public StringEditorBuilder(final EditActionsDialog editActionsDialog, final IAct

@Override
public void buildEditor(final JPanel parentPanel) {
final var textField = new JTextField(23);
final var textField = new JTextField(17);
textField.setText((String) initialValue);
textField.setCaretPosition(0);

Expand Down

0 comments on commit 852b0ce

Please sign in to comment.