Forráskód Böngészése

Begin work to commit changes on level up

Sam Jaffe 8 éve
szülő
commit
a5b2e242c5

+ 12 - 3
src/main/lombok/org/leumasjaffe/charsheet/view/level/LevelUpDialog.java

@@ -30,7 +30,7 @@ public class LevelUpDialog extends JPanel {
 	
 	ChooseClassLevelUpDialog chooseClass;
 	JButton btnCommit;
-	@NonFinal JPanel updateClass;
+	@NonFinal UpdateClassWithLevelPanel updateClass;
 	private GridBagConstraints gbc_main;
 	
 	public static int computeLevelsNeeded(DDCharacter chara, int bonusLevels) {
@@ -75,7 +75,7 @@ public class LevelUpDialog extends JPanel {
 		gbc_btnCancel.gridx = 1;
 		gbc_btnCancel.gridy = 0;
 		panel_1.add(btnCancel, gbc_btnCancel);
-		btnCancel.addActionListener(e -> ((JDialog) this.getParent().getParent().getParent()).dispose());
+		btnCancel.addActionListener(e -> closeOut());
 		
 		btnCommit = new JButton("Commit");
 		GridBagConstraints gbc_btnCommit = new GridBagConstraints();
@@ -85,9 +85,18 @@ public class LevelUpDialog extends JPanel {
 		panel_1.add(btnCommit, gbc_btnCommit);
 		btnCommit.setEnabled(false);
 		btnCommit.addActionListener(e -> {
-			// TODO
+			commitAllChanges();
+			closeOut();
 		});
 	}
+
+	private void commitAllChanges() {
+		updateClass.commitAllChanges();
+	}
+
+	private void closeOut() {
+		((JDialog) this.getParent().getParent().getParent()).dispose();
+	}
 	
 	void dropLayer2() {
 		remove(updateClass);

+ 65 - 34
src/main/lombok/org/leumasjaffe/charsheet/view/level/UpdateClassWithLevelPanel.java

@@ -5,12 +5,16 @@ import static org.leumasjaffe.charsheet.view.level.LevelUpSpellPanel.SpellPickTy
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.Optional;
+import java.util.Set;
 import java.util.function.Consumer;
 
 import javax.swing.JPanel;
 
 import org.jdesktop.swingx.VerticalLayout;
+import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import org.leumasjaffe.charsheet.model.magic.DDSpell;
+import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
 import org.leumasjaffe.charsheet.model.observable.BoolGate;
 import org.leumasjaffe.charsheet.view.magic.SelectSpellsPanel;
 import org.leumasjaffe.charsheet.view.magic.SelectSpellsPanel.Info;
@@ -36,18 +40,22 @@ import javax.swing.JLabel;
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
 class UpdateClassWithLevelPanel extends JPanel {
-	static int CHOOSE_SKILL_INDEX = 0;
-	static int LEARN_SPELL_INDEX = 1;
-	static int PREPARE_SPELL_INDEX = 2;
+	static int CHOOSE_SKILL_INDEX = 0, LEARN_SPELL_INDEX = 1, PREPARE_SPELL_INDEX = 2;
 
+	LevelUpClassInfo levelUpInfo;
+	JTabbedPane tabbedPane;
+	
+	SkillLevelUpPanel skills;
+	@NonFinal Optional<LevelUpSpellPanel> learnSpells = Optional.empty();
+	@NonFinal Optional<LevelUpSpellPanel> prepSpells = Optional.empty();
+	
 	BoolGate readyCount = new BoolGate(3);
 	ObservableListener<Consumer<Boolean>, BoolGate> listener;
-	JTabbedPane tabbedPane;
-	@NonFinal LevelUpSpellPanel learnSpells = null;
 	@NonFinal ObservableListener<UpdateClassWithLevelPanel, BoolGate> learnAndPrepareListener = null;
 	
 	public UpdateClassWithLevelPanel(LevelUpClassInfo info, VoidVoidFunction back,
 			Consumer<Boolean> setReady) {
+		this.levelUpInfo = info;
 		info.ddClass.getLevel().value(info.toLevel);
 		GridBagLayout gridBagLayout = new GridBagLayout();
 		gridBagLayout.columnWidths = new int[]{0, 0};
@@ -70,7 +78,7 @@ class UpdateClassWithLevelPanel extends JPanel {
 		});
 		tabbedPane.addTab("Features", null, features, null);
 
-		JPanel skills = new SkillLevelUpPanel(info.ddCharacter, info.ddClass) {
+		skills = new SkillLevelUpPanel(info.ddCharacter, info.ddClass) {
 			@Override public void setIsReady(boolean b) {
 				readyCount.set(CHOOSE_SKILL_INDEX, b);
 				ObserverDispatch.notifySubscribers(readyCount, null);
@@ -79,30 +87,12 @@ class UpdateClassWithLevelPanel extends JPanel {
 		tabbedPane.addTab("Skills", null, skills, null);
 
 		info.ddClass.getSpellBook().ifPresent(sb -> {
-			readyCount.set(1, !sb.learnsSpells());
-			readyCount.set(2, !sb.preparesSpells());
+			readyCount.set(LEARN_SPELL_INDEX, !sb.learnsSpells());
+			readyCount.set(PREPARE_SPELL_INDEX, !sb.preparesSpells());
 			if (sb.learnsSpells()) {
-				LevelUpSpellPanel spells = new LevelUpSpellPanel(LEARN,
-						new SelectSpellsPanel.Info(info.ddCharacter, info.ddClass),
-						readyCount.handle(LEARN_SPELL_INDEX));
-				tabbedPane.addTab("Learn Spells", null, spells, null);
-				if (sb.preparesSpells()) {
-					learnAndPrepareListener = new ObservableListener<>(this, (c, v) -> {
-						if (v.get(LEARN_SPELL_INDEX)) {
-							c.createPrepareLearnedSpellPanel(info, spells);
-						} else {
-							c.tabbedPane.remove(learnSpells);
-							c.learnSpells = null;
-						}
-					});
-					learnAndPrepareListener.setObserved(readyCount);
-				}
-			}
-			else if (sb.preparesSpells()) {
-				LevelUpSpellPanel spells = new LevelUpSpellPanel(PREPARE,
-						new SelectSpellsPanel.Info(info.ddCharacter, info.ddClass),
-						readyCount.handle(PREPARE_SPELL_INDEX));
-				tabbedPane.addTab("Prepare Spells", null, spells, null);
+				createPanelsForLearnSpell(sb);
+			} else if (sb.preparesSpells()) {
+				createPanelForPrepareSpells();
 			}
 		});
 
@@ -139,7 +129,32 @@ class UpdateClassWithLevelPanel extends JPanel {
 		listener.setObserved(readyCount);
 	}
 
-	private void createPrepareLearnedSpellPanel(LevelUpClassInfo info, LevelUpSpellPanel spells) {
+	private void createPanelForPrepareSpells() {
+		LevelUpSpellPanel spells = new LevelUpSpellPanel(PREPARE,
+				new SelectSpellsPanel.Info(levelUpInfo.ddCharacter, levelUpInfo.ddClass),
+				readyCount.handle(PREPARE_SPELL_INDEX));
+		tabbedPane.addTab("Prepare Spells", null, spells, null);
+	}
+
+	private void createPanelsForLearnSpell(DDSpellbook sb) {
+		learnSpells = Optional.of(new LevelUpSpellPanel(LEARN,
+				new SelectSpellsPanel.Info(levelUpInfo.ddCharacter, levelUpInfo.ddClass),
+				readyCount.handle(LEARN_SPELL_INDEX)));
+		tabbedPane.addTab("Learn Spells", null, learnSpells.get(), null);
+		if (sb.preparesSpells()) {
+			learnAndPrepareListener = new ObservableListener<>(this, (c, v) -> {
+				if (v.get(LEARN_SPELL_INDEX)) {
+					c.createPrepareLearnedSpellPanel(learnSpells.get());
+				} else {
+					c.tabbedPane.remove(prepSpells.get());
+					c.prepSpells = null;
+				}
+			});
+			learnAndPrepareListener.setObserved(readyCount);
+		}
+	}
+
+	private void createPrepareLearnedSpellPanel(LevelUpSpellPanel spells) {
 		LevelUpSpellPanel.SpellPicker pick = new LevelUpSpellPanel.SpellPicker() {
 			@Override
 			public List<List<Integer>> getSpellCounts(Info info) {
@@ -156,10 +171,10 @@ class UpdateClassWithLevelPanel extends JPanel {
 			}
 		};
 
-		learnSpells = new LevelUpSpellPanel(pick,
-				new SelectSpellsPanel.Info(info.ddCharacter, info.ddClass),
-				readyCount.handle(PREPARE_SPELL_INDEX));
-		tabbedPane.addTab("Prepare Spells", null, learnSpells, null);
+		prepSpells = Optional.of(new LevelUpSpellPanel(pick,
+				new SelectSpellsPanel.Info(levelUpInfo.ddCharacter, levelUpInfo.ddClass),
+				readyCount.handle(PREPARE_SPELL_INDEX)));
+		tabbedPane.addTab("Prepare Spells", null, prepSpells.get(), null);
 	}
 	
 	@Override
@@ -168,4 +183,20 @@ class UpdateClassWithLevelPanel extends JPanel {
 		ObserverDispatch.unsubscribeAll(listener);
 		ObserverDispatch.unsubscribeAll(learnAndPrepareListener);
 	}
+
+	
+	public void commitAllChanges() {
+		final String className = levelUpInfo.ddClass.getName();
+		skills.commitAllChanges();
+		learnSpells.ifPresent(pan -> {});
+		prepSpells.ifPresent(pan -> {});
+		final Set<DDCharacterClass> classes = levelUpInfo.ddCharacter.getClasses();
+		if (classes.stream().map(DDCharacterClass::getName).anyMatch(className::equals)) {
+			// Update class to new data
+		} else {
+			// New class
+		}
+		// TODO: Acquire features
+		// TODO: Update spellbook
+	}
 }

+ 7 - 1
src/main/lombok/org/leumasjaffe/charsheet/view/skills/SkillLevelUpDialog.java

@@ -9,6 +9,7 @@ import org.leumasjaffe.charsheet.model.DDCharacterClass;
 import java.awt.GridBagConstraints;
 
 import javax.swing.JButton;
+import javax.swing.JDialog;
 
 @SuppressWarnings("serial")
 @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
@@ -19,6 +20,10 @@ public class SkillLevelUpDialog extends SkillLevelUpPanel {
 		super(chara, cclass);
 		btnSubmitSkillChange = new JButton("Submit Skill Change");
 		btnSubmitSkillChange.setEnabled(false);
+		btnSubmitSkillChange.addActionListener(e -> {
+			commitAllChanges();
+			((JDialog) this.getParent().getParent().getParent()).dispose();
+		});
 		GridBagConstraints gbc_btnSubmitSkillChange = new GridBagConstraints();
 		gbc_btnSubmitSkillChange.gridx = 2;
 		gbc_btnSubmitSkillChange.gridy = 0;
@@ -26,7 +31,8 @@ public class SkillLevelUpDialog extends SkillLevelUpPanel {
 	}
 	
 	@Override
-	public void setIsReady(boolean b) {
+	protected void setIsReady(boolean b) {
+		if (btnSubmitSkillChange == null) return; // Because of the ObservableListener's subscription behavior.
 		btnSubmitSkillChange.setEnabled(b);
 	}
 }

+ 4 - 1
src/main/lombok/org/leumasjaffe/charsheet/view/skills/SkillLevelUpPanel.java

@@ -100,8 +100,11 @@ public abstract class SkillLevelUpPanel extends JPanel {
 		purchaseListener.setObserved(pointsAvaliable);
 	}
 	
-	public abstract void setIsReady(boolean b);
+	protected abstract void setIsReady(boolean b);
 	
+	public void commitAllChanges() {
+		
+	}
 	
 	@Override
 	public void removeNotify() {