package org.leumasjaffe.charsheet.view.magic; import javax.swing.JPanel; import org.jdesktop.swingx.VerticalLayout; 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.BoolGate; import org.leumasjaffe.observer.ObservableListener; import org.leumasjaffe.observer.ObserverDispatch; 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 java.util.function.Consumer; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.ScrollPaneConstants; @SuppressWarnings("serial") @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true) public class PrepareSpellsDialog extends JPanel { int[] ready = {0}; int highestSpellLevel; ObservableListener, BoolGate> allReady; 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(); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); 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); List panels = new ArrayList<>(); final BoolGate gate = new BoolGate(highestSpellLevel); allReady = gate.makeListener(btnPrepareTheseSpells::setEnabled); for (int i = 0; i < highestSpellLevel; ++i) { SelectPreparedSpellsPanel lvl = new SelectPreparedSpellsPanel(chara, dclass, gate.handle(i), i); panels.add(lvl); panel.add(lvl); } final Prepared spellBook = (Prepared) dclass.getSpellBook().get(); btnPrepareTheseSpells.addActionListener(e -> { for (int i = 0; i < highestSpellLevel; ++i) { spellBook.prepareSpells(i, panels.get(i).getPrepared()); } ((JDialog) this.getParent().getParent().getParent()).dispose(); }); } @Override public void removeNotify() { super.removeNotify(); ObserverDispatch.unsubscribeAll(allReady); } }