Procházet zdrojové kódy

Add ability to set config from within the App

Sam Jaffe před 8 roky
rodič
revize
cf413cfb5a

+ 5 - 0
src/main/lombok/org/leumasjaffe/charsheet/view/D20Sheet.java

@@ -99,6 +99,11 @@ public class D20Sheet extends JFrame {
 		mntmSaveAs.addActionListener( e -> saveAs(fc) );
 		mntmSaveAs.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK));
 		mnFile.add(mntmSaveAs);
+		
+		JMenuItem mntmConfig = new JMenuItem("Config");
+		mntmConfig.addActionListener( e -> DialogBuilder.showConfig(this) );
+		mntmConfig.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.ALT_DOWN_MASK));
+		mnFile.add(mntmConfig);
 				
 		JMenuItem mntmExit = new JMenuItem("Exit");
 		mntmExit.addActionListener( e -> { this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING)); } );

+ 6 - 0
src/main/lombok/org/leumasjaffe/charsheet/view/DialogBuilder.java

@@ -8,6 +8,7 @@ import javax.swing.JPanel;
 
 import org.leumasjaffe.charsheet.model.DDCharacter;
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
+import org.leumasjaffe.charsheet.view.config.ConfigPanel;
 import org.leumasjaffe.charsheet.view.magic.PrepareSpellsDialog;
 import org.leumasjaffe.charsheet.view.skills.SkillLevelUpDialog;
 
@@ -32,4 +33,9 @@ public class DialogBuilder {
 	public void createPrepareSpellsDialog(final JFrame parent, DDCharacter chara, DDCharacterClass dclass) {
 		createDialogue(parent, "Prepare Spells - " + dclass.getName(), new PrepareSpellsDialog(chara, dclass));
 	}
+	
+	public void showConfig(final JFrame parent) {
+		createDialogue(parent, "Config", new ConfigPanel());
+	}
+
 }

+ 59 - 0
src/main/lombok/org/leumasjaffe/charsheet/view/config/ConfigEnumPanel.java

@@ -0,0 +1,59 @@
+package org.leumasjaffe.charsheet.view.config;
+
+import javax.swing.JPanel;
+import javax.swing.JRadioButton;
+
+import org.leumasjaffe.charsheet.config.Config;
+
+import java.awt.GridBagLayout;
+
+import javax.swing.ButtonGroup;
+import javax.swing.JLabel;
+import java.awt.GridBagConstraints;
+import java.awt.Insets;
+import java.awt.event.ActionListener;
+import java.util.EnumMap;
+import java.awt.FlowLayout;
+
+@SuppressWarnings("serial")
+public class ConfigEnumPanel<E extends Enum<E>> extends JPanel {
+	EnumMap<E, JRadioButton> buttons;
+
+	public ConfigEnumPanel(String name, Class<E> clazz) {
+		buttons = new EnumMap<>(clazz);
+		
+		GridBagLayout gridBagLayout = new GridBagLayout();
+		gridBagLayout.columnWidths = new int[]{0, 0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0};
+		gridBagLayout.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
+		setLayout(gridBagLayout);
+		
+		JLabel lblName = new JLabel(name);
+		GridBagConstraints gbc_lblName = new GridBagConstraints();
+		gbc_lblName.insets = new Insets(0, 0, 0, 5);
+		gbc_lblName.gridx = 0;
+		gbc_lblName.gridy = 0;
+		add(lblName, gbc_lblName);
+		
+		JPanel panel = new JPanel();
+		GridBagConstraints gbc_panel = new GridBagConstraints();
+		gbc_panel.fill = GridBagConstraints.BOTH;
+		gbc_panel.gridx = 1;
+		gbc_panel.gridy = 0;
+		add(panel, gbc_panel);
+		panel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
+		
+		for (E e : clazz.getEnumConstants()) {
+			JRadioButton button = new JRadioButton(e.name());
+			buttons.put(e, button);
+			panel.add(button);
+		}
+		buttons.get(Config.get(name, clazz.getEnumConstants()[0])).setSelected(true);
+		
+		final ButtonGroup grp = new ButtonGroup();
+		buttons.values().forEach(grp::add);
+		ActionListener listen = e -> Config.set(name, Enum.valueOf(clazz, e.getActionCommand()));
+		buttons.values().forEach(b -> b.addActionListener(listen));
+	}
+}

+ 78 - 0
src/main/lombok/org/leumasjaffe/charsheet/view/config/ConfigPanel.java

@@ -0,0 +1,78 @@
+package org.leumasjaffe.charsheet.view.config;
+
+import javax.swing.JPanel;
+
+import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.charsheet.config.Constants;
+
+import java.awt.GridBagLayout;
+import java.awt.GridBagConstraints;
+import javax.swing.JLabel;
+import java.awt.Insets;
+import java.awt.Component;
+import javax.swing.Box;
+import java.awt.Font;
+
+@SuppressWarnings("serial")
+public class ConfigPanel extends JPanel {
+	public ConfigPanel() {
+		GridBagLayout gridBagLayout = new GridBagLayout();
+		gridBagLayout.columnWidths = new int[]{0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0};
+		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
+		setLayout(gridBagLayout);
+		
+		JPanel panel = new JPanel();
+		GridBagConstraints gbc_panel = new GridBagConstraints();
+		gbc_panel.fill = GridBagConstraints.BOTH;
+		gbc_panel.gridx = 0;
+		gbc_panel.gridy = 0;
+		add(panel, gbc_panel);
+		GridBagLayout gbl_panel = new GridBagLayout();
+		gbl_panel.columnWidths = new int[]{0, 0};
+		gbl_panel.rowHeights = new int[]{0, 0, 0};
+		gbl_panel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
+		gbl_panel.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
+		panel.setLayout(gbl_panel);
+		
+		JLabel lblUnits = new JLabel("Units:");
+		lblUnits.setFont(new Font("Lucida Grande", Font.PLAIN, 16));
+		GridBagConstraints gbc_lblUnits = new GridBagConstraints();
+		gbc_lblUnits.fill = GridBagConstraints.HORIZONTAL;
+		gbc_lblUnits.insets = new Insets(5, 0, 5, 0);
+		gbc_lblUnits.gridx = 0;
+		gbc_lblUnits.gridy = 0;
+		panel.add(lblUnits, gbc_lblUnits);
+		
+		JPanel panel_1 = new JPanel();
+		GridBagConstraints gbc_panel_1 = new GridBagConstraints();
+		gbc_panel_1.fill = GridBagConstraints.BOTH;
+		gbc_panel_1.gridx = 0;
+		gbc_panel_1.gridy = 1;
+		panel.add(panel_1, gbc_panel_1);
+		GridBagLayout gbl_panel_1 = new GridBagLayout();
+		gbl_panel_1.columnWidths = new int[]{0, 0, 0};
+		gbl_panel_1.rowHeights = new int[]{0, 0};
+		gbl_panel_1.columnWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
+		gbl_panel_1.rowWeights = new double[]{1.0, Double.MIN_VALUE};
+		panel_1.setLayout(gbl_panel_1);
+		
+		JPanel panel_2 = new JPanel(new VerticalLayout(0));
+		GridBagConstraints gbc_panel_2 = new GridBagConstraints();
+		gbc_panel_2.fill = GridBagConstraints.BOTH;
+		gbc_panel_2.gridx = 1;
+		gbc_panel_2.gridy = 0;
+		panel_1.add(panel_2, gbc_panel_2);
+		
+		Component horizontalStrut = Box.createHorizontalStrut(20);
+		GridBagConstraints gbc_horizontalStrut = new GridBagConstraints();
+		gbc_horizontalStrut.insets = new Insets(0, 0, 0, 5);
+		gbc_horizontalStrut.gridx = 0;
+		gbc_horizontalStrut.gridy = 0;
+		panel_1.add(horizontalStrut, gbc_horizontalStrut);
+		
+		panel_2.add(new ConfigEnumPanel<>(Constants.K_DISTANCE, Constants.DistanceMeasurement.class));
+		panel_2.add(new ConfigEnumPanel<>(Constants.K_DURATION, Constants.DurationMeasurement.class));
+	}
+}