PrepareSpellsDialog.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package org.leumasjaffe.charsheet.view.magic;
  2. import javax.swing.JPanel;
  3. import org.jdesktop.swingx.VerticalLayout;
  4. import org.leumasjaffe.charsheet.controller.magic.ChooseSpellTuple;
  5. import org.leumasjaffe.charsheet.controller.magic.PrepareSpellPicker;
  6. import org.leumasjaffe.charsheet.controller.magic.SpellPicker;
  7. import org.leumasjaffe.charsheet.model.DDCharacter;
  8. import org.leumasjaffe.charsheet.model.DDCharacterClass;
  9. import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
  10. import org.leumasjaffe.charsheet.model.magic.impl.Prepared;
  11. import org.leumasjaffe.charsheet.model.observable.BoolGate;
  12. import org.leumasjaffe.observer.ObservableListener;
  13. import org.leumasjaffe.observer.ObserverDispatch;
  14. import lombok.AccessLevel;
  15. import lombok.experimental.FieldDefaults;
  16. import java.awt.GridBagLayout;
  17. import javax.swing.JScrollPane;
  18. import java.awt.GridBagConstraints;
  19. import java.awt.Insets;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.function.Consumer;
  23. import javax.swing.JButton;
  24. import javax.swing.JDialog;
  25. import javax.swing.ScrollPaneConstants;
  26. @SuppressWarnings("serial")
  27. @FieldDefaults(level=AccessLevel.PRIVATE, makeFinal=true)
  28. public class PrepareSpellsDialog extends JPanel {
  29. int[] ready = {0};
  30. int highestSpellLevel;
  31. ObservableListener<Consumer<Boolean>, BoolGate> allReady;
  32. public PrepareSpellsDialog(DDCharacter chara, DDCharacterClass dclass, DDSpellbook spellBook) {
  33. highestSpellLevel = dclass.getHighestSpellLevel();
  34. ready[0] = highestSpellLevel;
  35. GridBagLayout gridBagLayout = new GridBagLayout();
  36. gridBagLayout.columnWidths = new int[]{0, 0};
  37. gridBagLayout.rowHeights = new int[]{0, 0, 0};
  38. gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
  39. gridBagLayout.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
  40. setLayout(gridBagLayout);
  41. JPanel panel_1 = new JPanel();
  42. GridBagConstraints gbc_panel_1 = new GridBagConstraints();
  43. gbc_panel_1.insets = new Insets(0, 0, 5, 0);
  44. gbc_panel_1.fill = GridBagConstraints.BOTH;
  45. gbc_panel_1.gridx = 0;
  46. gbc_panel_1.gridy = 0;
  47. add(panel_1, gbc_panel_1);
  48. GridBagLayout gbl_panel_1 = new GridBagLayout();
  49. gbl_panel_1.columnWidths = new int[]{0, 0};
  50. gbl_panel_1.rowHeights = new int[]{0, 0};
  51. gbl_panel_1.columnWeights = new double[]{0.0, Double.MIN_VALUE};
  52. gbl_panel_1.rowWeights = new double[]{0.0, Double.MIN_VALUE};
  53. panel_1.setLayout(gbl_panel_1);
  54. JButton btnPrepareTheseSpells = new JButton("Prepare These Spells");
  55. GridBagConstraints gbc_btnPrepareTheseSpells = new GridBagConstraints();
  56. gbc_btnPrepareTheseSpells.gridx = 0;
  57. gbc_btnPrepareTheseSpells.gridy = 0;
  58. panel_1.add(btnPrepareTheseSpells, gbc_btnPrepareTheseSpells);
  59. JScrollPane scrollPane = new JScrollPane();
  60. scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  61. scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  62. GridBagConstraints gbc_scrollPane = new GridBagConstraints();
  63. gbc_scrollPane.fill = GridBagConstraints.BOTH;
  64. gbc_scrollPane.gridx = 0;
  65. gbc_scrollPane.gridy = 1;
  66. add(scrollPane, gbc_scrollPane);
  67. JPanel panel = new JPanel(new VerticalLayout(5));
  68. scrollPane.setViewportView(panel);
  69. List<SelectSpellsPanel> panels = new ArrayList<>();
  70. final BoolGate gate = new BoolGate(highestSpellLevel);
  71. allReady = gate.makeListener(btnPrepareTheseSpells::setEnabled);
  72. ChooseSpellTuple tup = new ChooseSpellTuple(chara, dclass, spellBook);
  73. SpellPicker pick = new PrepareSpellPicker(tup);
  74. for (int i = 0; i < highestSpellLevel; ++i) {
  75. SelectSpellsPanel lvl = new SelectSpellsPanel(pick, gate.handle(i), i,
  76. ((Prepared) spellBook).getSpellsPreparedPreviouslyForLevel(i), null);
  77. panels.add(lvl);
  78. panel.add(lvl);
  79. }
  80. btnPrepareTheseSpells.addActionListener(e -> {
  81. for (int i = 0; i < highestSpellLevel; ++i) {
  82. spellBook.prepareSpells(i, panels.get(i).getPrepared());
  83. }
  84. ((JDialog) this.getParent().getParent().getParent()).dispose();
  85. });
  86. }
  87. @Override
  88. public void removeNotify() {
  89. super.removeNotify();
  90. ObserverDispatch.unsubscribeAll(allReady);
  91. }
  92. }