Parcourir la source

Fixing skill level up, splitting up to support non-stand-alone-dialog use case.

Sam Jaffe il y a 8 ans
Parent
commit
dbdba94f24

+ 8 - 104
src/main/lombok/org/leumasjaffe/charsheet/view/skills/SkillLevelUpDialog.java

@@ -1,128 +1,32 @@
 package org.leumasjaffe.charsheet.view.skills;
 
-import javax.swing.JPanel;
-
 import lombok.AccessLevel;
 import lombok.experimental.FieldDefaults;
-import java.awt.GridBagLayout;
-import javax.swing.JScrollPane;
-import javax.swing.JTextField;
 
-import org.jdesktop.swingx.VerticalLayout;
-import org.leumasjaffe.charsheet.model.Ability;
 import org.leumasjaffe.charsheet.model.DDCharacter;
 import org.leumasjaffe.charsheet.model.DDCharacterClass;
-import org.leumasjaffe.charsheet.model.observable.IntValue;
-import org.leumasjaffe.charsheet.model.skill.DDSkills;
-import org.leumasjaffe.observer.ObservableListener;
-import org.leumasjaffe.observer.ObserverDispatch;
 
 import java.awt.GridBagConstraints;
-import java.awt.Insets;
-import java.util.ArrayList;
-import java.util.List;
 
-import javax.swing.JLabel;
 import javax.swing.JButton;
-import javax.swing.JDialog;
-import java.awt.Dimension;
 
+@SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
-public class SkillLevelUpDialog extends JPanel {
-
-	/**
-	 * 
-	 */
-	private static final long serialVersionUID = 1L;
-	private ObservableListener<JTextField, IntValue> purchaseListener;
+public class SkillLevelUpDialog extends SkillLevelUpPanel {
+	JButton btnSubmitSkillChange;
 	
 	public SkillLevelUpDialog(final DDCharacter chara, final DDCharacterClass cclass) {
-		final IntValue pointsAvaliable = new IntValue(Math.max(1, cclass.getSkillPoints() + 
-				Ability.modifier(chara.getAbilities().getInt().baseModifier())));
-		
-		GridBagLayout gridBagLayout = new GridBagLayout();
-		gridBagLayout.columnWidths = new int[]{0, 0};
-		gridBagLayout.rowHeights = new int[]{0, 0, 0};
-		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
-		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
-		setLayout(gridBagLayout);
-		
-		JPanel panel = new JPanel();
-		GridBagConstraints gbc_panel = new GridBagConstraints();
-		gbc_panel.insets = new Insets(0, 0, 5, 0);
-		gbc_panel.fill = GridBagConstraints.HORIZONTAL;
-		gbc_panel.gridx = 0;
-		gbc_panel.gridy = 0;
-		add(panel, gbc_panel);
-		GridBagLayout gbl_panel = new GridBagLayout();
-		gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
-		gbl_panel.rowHeights = new int[]{0, 0};
-		gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
-		gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
-		panel.setLayout(gbl_panel);
-		
-		JLabel lblPointsRemaining = new JLabel("Points Remaining:");
-		GridBagConstraints gbc_lblPointsRemaining = new GridBagConstraints();
-		gbc_lblPointsRemaining.insets = new Insets(0, 0, 0, 5);
-		gbc_lblPointsRemaining.anchor = GridBagConstraints.EAST;
-		gbc_lblPointsRemaining.gridx = 0;
-		gbc_lblPointsRemaining.gridy = 0;
-		panel.add(lblPointsRemaining, gbc_lblPointsRemaining);
-		
-		JTextField pointsRemaining = new JTextField();
-		pointsRemaining.setEditable(false);
-		GridBagConstraints gbc_pointsRemaining = new GridBagConstraints();
-		gbc_pointsRemaining.insets = new Insets(0, 0, 0, 5);
-		gbc_pointsRemaining.fill = GridBagConstraints.HORIZONTAL;
-		gbc_pointsRemaining.gridx = 1;
-		gbc_pointsRemaining.gridy = 0;
-		panel.add(pointsRemaining, gbc_pointsRemaining);
-		pointsRemaining.setColumns(10);
-		
-		JScrollPane scrollPane = new JScrollPane();
-		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
-		gbc_scrollPane.fill = GridBagConstraints.BOTH;
-		gbc_scrollPane.gridx = 0;
-		gbc_scrollPane.gridy = 1;
-		add(scrollPane, gbc_scrollPane);
-		
-		JPanel skillPanel = new JPanel();
-		skillPanel.setMinimumSize(new Dimension(300, 200));
-		scrollPane.setViewportView(skillPanel);
-		skillPanel.setLayout(new VerticalLayout());
-		
-		JButton btnSubmitSkillChange = new JButton("Submit Skill Change");
+		super(chara, cclass);
+		btnSubmitSkillChange = new JButton("Submit Skill Change");
 		btnSubmitSkillChange.setEnabled(false);
 		GridBagConstraints gbc_btnSubmitSkillChange = new GridBagConstraints();
 		gbc_btnSubmitSkillChange.gridx = 2;
 		gbc_btnSubmitSkillChange.gridy = 0;
-		panel.add(btnSubmitSkillChange, gbc_btnSubmitSkillChange);
-		
-		final List<SkillLevelUpLine> lines = new ArrayList<>();
-		final DDSkills skills = chara.getSkills();
-		skills.getSkills().stream().forEach( skill -> {
-			SkillLevelUpLine line = new SkillLevelUpLine(chara, cclass, skill, pointsAvaliable);
-			skillPanel.add(line);
-			lines.add(line);
-		});
-		
-		purchaseListener = new ObservableListener<>(pointsRemaining, (c, v) -> {
-			btnSubmitSkillChange.setEnabled(v.value() == 0);
-			c.setText(Integer.toString(v.value()));
-		});
-		purchaseListener.setObserved(pointsAvaliable);
-		
-		btnSubmitSkillChange.addActionListener(e -> {
-			((JDialog) this.getParent().getParent().getParent()).dispose();
-			lines.stream().forEach(l -> {
-				l.applyChange();
-			});
-		});
+		getPanel().add(btnSubmitSkillChange, gbc_btnSubmitSkillChange);
 	}
 	
 	@Override
-	public void removeNotify() {
-		super.removeNotify();
-		ObserverDispatch.unsubscribeAll(purchaseListener);
+	public void setIsReady(boolean b) {
+		btnSubmitSkillChange.setEnabled(b);
 	}
 }

+ 10 - 3
src/main/lombok/org/leumasjaffe/charsheet/view/skills/SkillLevelUpLine.java

@@ -59,11 +59,11 @@ class SkillLevelUpLine extends JPanel {
 		final int maxPoints = (chara.getLevel() + 3) / pointsPerRank - skill.getRanks().value();
 		
 		setBorder(new MatteBorder(0, 0, 1, 0, (Color) new Color(0, 0, 0)));
-		setPreferredSize(new Dimension(475, 22));
+		setPreferredSize(new Dimension(480, 22));
 		GridBagLayout gridBagLayout = new GridBagLayout();
-		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 		gridBagLayout.rowHeights = new int[]{0, 0};
-		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
+		gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
 		gridBagLayout.rowWeights = new double[]{0.0, Double.MIN_VALUE};
 		setLayout(gridBagLayout);
 		
@@ -194,6 +194,7 @@ class SkillLevelUpLine extends JPanel {
 		points.setText("0");
 		points.setEditable(false);
 		GridBagConstraints gbc_points = new GridBagConstraints();
+		gbc_points.insets = new Insets(0, 0, 0, 5);
 		gbc_points.gridx = 11;
 		gbc_points.gridy = 0;
 		add(points, gbc_points);
@@ -221,6 +222,12 @@ class SkillLevelUpLine extends JPanel {
 					final int mod = p.ability.map(v -> v.baseModifier()).orElse(0);
 					c.setText(StringHelper.toString(skillRanks + mod + p.points.value()));
 				});
+		
+		Component horizontalStrut_1 = Box.createHorizontalStrut(5);
+		GridBagConstraints gbc_horizontalStrut_1 = new GridBagConstraints();
+		gbc_horizontalStrut_1.gridx = 12;
+		gbc_horizontalStrut_1.gridy = 0;
+		add(horizontalStrut_1, gbc_horizontalStrut_1);
 		final Optional<Ability.Scores> ability = getAbility(chara, skill);
 		ability.ifPresent(v -> modifier.setText(StringHelper.toString(v.baseModifier())));
 		totalListener.setObserved(new TotalPacket(ability, skill, current), current);

+ 111 - 0
src/main/lombok/org/leumasjaffe/charsheet/view/skills/SkillLevelUpPanel.java

@@ -0,0 +1,111 @@
+package org.leumasjaffe.charsheet.view.skills;
+
+import java.awt.Dimension;
+import java.awt.GridBagConstraints;
+import java.awt.GridBagLayout;
+import java.awt.Insets;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JScrollPane;
+import javax.swing.JTextField;
+
+import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.charsheet.model.DDCharacter;
+import org.leumasjaffe.charsheet.model.DDCharacterClass;
+import org.leumasjaffe.charsheet.model.observable.IntValue;
+import org.leumasjaffe.charsheet.model.skill.DDSkills;
+import org.leumasjaffe.observer.ObservableListener;
+import org.leumasjaffe.observer.ObserverDispatch;
+
+import lombok.AccessLevel;
+import lombok.Getter;
+import lombok.experimental.FieldDefaults;
+
+@SuppressWarnings("serial")
+@FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
+public abstract class SkillLevelUpPanel extends JPanel {
+	ObservableListener<JTextField, IntValue> purchaseListener;
+	@Getter(AccessLevel.PROTECTED) JPanel panel;
+
+	public SkillLevelUpPanel(final DDCharacter chara, final DDCharacterClass cclass) {
+		final IntValue pointsAvaliable = new IntValue(Math.max(1, cclass.getSkillPoints() + 
+				chara.getAbilities().getInt().baseModifier()));
+		
+		GridBagLayout gridBagLayout = new GridBagLayout();
+		gridBagLayout.columnWidths = new int[]{0, 0};
+		gridBagLayout.rowHeights = new int[]{0, 0, 0};
+		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
+		gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
+		setLayout(gridBagLayout);
+		
+		panel = new JPanel();
+		GridBagConstraints gbc_panel = new GridBagConstraints();
+		gbc_panel.insets = new Insets(0, 0, 5, 0);
+		gbc_panel.fill = GridBagConstraints.HORIZONTAL;
+		gbc_panel.gridx = 0;
+		gbc_panel.gridy = 0;
+		add(panel, gbc_panel);
+		GridBagLayout gbl_panel = new GridBagLayout();
+		gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
+		gbl_panel.rowHeights = new int[]{0, 0};
+		gbl_panel.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
+		gbl_panel.rowWeights = new double[]{0.0, Double.MIN_VALUE};
+		panel.setLayout(gbl_panel);
+		
+		JLabel lblPointsRemaining = new JLabel("Points Remaining:");
+		GridBagConstraints gbc_lblPointsRemaining = new GridBagConstraints();
+		gbc_lblPointsRemaining.insets = new Insets(0, 0, 0, 5);
+		gbc_lblPointsRemaining.anchor = GridBagConstraints.EAST;
+		gbc_lblPointsRemaining.gridx = 0;
+		gbc_lblPointsRemaining.gridy = 0;
+		panel.add(lblPointsRemaining, gbc_lblPointsRemaining);
+		
+		JTextField pointsRemaining = new JTextField();
+		pointsRemaining.setEditable(false);
+		GridBagConstraints gbc_pointsRemaining = new GridBagConstraints();
+		gbc_pointsRemaining.insets = new Insets(0, 0, 0, 5);
+		gbc_pointsRemaining.fill = GridBagConstraints.HORIZONTAL;
+		gbc_pointsRemaining.gridx = 1;
+		gbc_pointsRemaining.gridy = 0;
+		panel.add(pointsRemaining, gbc_pointsRemaining);
+		pointsRemaining.setColumns(10);
+		
+		JScrollPane scrollPane = new JScrollPane();
+		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
+		gbc_scrollPane.fill = GridBagConstraints.BOTH;
+		gbc_scrollPane.gridx = 0;
+		gbc_scrollPane.gridy = 1;
+		add(scrollPane, gbc_scrollPane);
+		
+		JPanel skillPanel = new JPanel();
+		skillPanel.setMinimumSize(new Dimension(300, 200));
+		scrollPane.setViewportView(skillPanel);
+		skillPanel.setLayout(new VerticalLayout());
+				
+		final List<SkillLevelUpLine> lines = new ArrayList<>();
+		final DDSkills skills = chara.getSkills();
+		skills.getSkills().stream().forEach( skill -> {
+			SkillLevelUpLine line = new SkillLevelUpLine(chara, cclass, skill, pointsAvaliable);
+			skillPanel.add(line);
+			lines.add(line);
+		});
+		
+		purchaseListener = new ObservableListener<>(pointsRemaining, (c, v) -> {
+			setIsReady(v.value() == 0);
+			c.setText(Integer.toString(v.value()));
+		});
+		purchaseListener.setObserved(pointsAvaliable);
+	}
+	
+	public abstract void setIsReady(boolean b);
+	
+	
+	@Override
+	public void removeNotify() {
+		super.removeNotify();
+		ObserverDispatch.unsubscribeAll(purchaseListener);
+	}
+}