SpellMenu.java 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package org.leumasjaffe.charsheet.view.magic;
  2. import javax.swing.JPopupMenu;
  3. import org.leumasjaffe.charsheet.model.DDCharacter;
  4. import org.leumasjaffe.charsheet.model.DDCharacterClass;
  5. import org.leumasjaffe.charsheet.model.magic.DDSpell;
  6. import org.leumasjaffe.charsheet.model.magic.DDSpellbook;
  7. import org.leumasjaffe.observer.ObserverDispatch;
  8. import javax.swing.JFrame;
  9. import javax.swing.JMenuItem;
  10. import javax.swing.JOptionPane;
  11. class SpellMenu extends JPopupMenu {
  12. /**
  13. *
  14. */
  15. private static final long serialVersionUID = 1L;
  16. public SpellMenu(DDCharacter chara, final DDCharacterClass dclass, final DDSpell spell, boolean isPrepared) {
  17. final int spellLevel = spell.getClassLevel(dclass.getName());
  18. JMenuItem mntmInfo = new JMenuItem("Info");
  19. mntmInfo.addActionListener( e -> {
  20. JFrame frame = new JFrame(spell.getName() + " (" + dclass.getName() + " " + spellLevel + ")");
  21. frame.add(new SpellInfoPanel(chara, dclass, spell));
  22. frame.pack();
  23. frame.setVisible(true);
  24. });
  25. add(mntmInfo);
  26. if (isPrepared) {
  27. JMenuItem mntmCast = new JMenuItem("Cast");
  28. mntmCast.addActionListener(e -> {
  29. final DDSpellbook book = dclass.getSpellBook().get();
  30. if (book.numSpellsPerDayRemainingAtLevel(spellLevel) == 0) {
  31. JOptionPane.showMessageDialog(this, "Cannot cast any more spells", "Error", JOptionPane.ERROR_MESSAGE);
  32. return;
  33. }
  34. book.castSpell(spellLevel, spell);
  35. ObserverDispatch.notifySubscribers(book, null);
  36. });
  37. add(mntmCast);
  38. }
  39. }
  40. }