package org.leumasjaffe.charsheet.view.magic; import javax.swing.JPanel; 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.magic.impl.Prepared; import org.leumasjaffe.charsheet.model.observable.IntValue; import lombok.AccessLevel; import lombok.experimental.FieldDefaults; import java.awt.GridBagLayout; import javax.swing.JScrollPane; import java.awt.GridBagConstraints; import java.awt.Insets; import java.util.ArrayList; import java.util.List; import javax.swing.JButton; import javax.swing.JDialog; @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true) public class PrepareSpellsDialog extends JPanel { /** * */ private static final long serialVersionUID = 1L; int[] ready = {0}; int highestSpellLevel; public PrepareSpellsDialog(DDCharacter chara, DDCharacterClass dclass) { highestSpellLevel = dclass.getHighestSpellLevel(); ready[0] = highestSpellLevel; 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_1 = new JPanel(); GridBagConstraints gbc_panel_1 = new GridBagConstraints(); gbc_panel_1.insets = new Insets(0, 0, 5, 0); gbc_panel_1.fill = GridBagConstraints.BOTH; gbc_panel_1.gridx = 0; gbc_panel_1.gridy = 0; add(panel_1, gbc_panel_1); GridBagLayout gbl_panel_1 = new GridBagLayout(); gbl_panel_1.columnWidths = new int[]{0, 0}; gbl_panel_1.rowHeights = new int[]{0, 0}; gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE}; gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE}; panel_1.setLayout(gbl_panel_1); JButton btnPrepareTheseSpells = new JButton("Prepare These Spells"); GridBagConstraints gbc_btnPrepareTheseSpells = new GridBagConstraints(); gbc_btnPrepareTheseSpells.gridx = 0; gbc_btnPrepareTheseSpells.gridy = 0; panel_1.add(btnPrepareTheseSpells, gbc_btnPrepareTheseSpells); 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 panel = new JPanel(new VerticalLayout(5)); scrollPane.setViewportView(panel); final IntValue value = Ability.fields.get(dclass.getProto().getSpells().get().getAbility()).apply(chara.getAbilities().getBase()); final Prepared spellBook = (Prepared) dclass.getSpellBook().get(); List panels = new ArrayList<>(); for (int i = 0; i < highestSpellLevel; ++i) { SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(i, dclass, value); panels.add(lvl); lvl.addPropertyChangeListener(SelectPreparedSpellsPanel.READY, e -> { if ((Boolean) e.getNewValue()) ++ready[0]; else --ready[0]; btnPrepareTheseSpells.setEnabled(ready[0] == highestSpellLevel); }); panel.add(lvl); } btnPrepareTheseSpells.addActionListener(e -> { ((JDialog) this.getParent().getParent().getParent()).dispose(); for (int i = 0; i < highestSpellLevel; ++i) { spellBook.prepareSpells(i, panels.get(i).getPrepared()); } }); } }