package org.leumasjaffe.charsheet.view.magic; import javax.swing.JPopupMenu; import org.leumasjaffe.charsheet.model.DDCharacter; import org.leumasjaffe.charsheet.model.DDCharacterClass; import org.leumasjaffe.charsheet.model.magic.DDSpell; import org.leumasjaffe.charsheet.model.magic.DDSpellbook; import org.leumasjaffe.observer.ObserverDispatch; import javax.swing.JFrame; import javax.swing.JMenuItem; import javax.swing.JOptionPane; class SpellMenu extends JPopupMenu { /** * */ private static final long serialVersionUID = 1L; public SpellMenu(DDCharacter chara, final DDCharacterClass dclass, final DDSpell spell, boolean isPrepared) { final int spellLevel = spell.getClassLevel(dclass.getName()); JMenuItem mntmInfo = new JMenuItem("Info"); mntmInfo.addActionListener( e -> { JFrame frame = new JFrame(spell.getName() + " (" + dclass.getName() + " " + spellLevel + ")"); frame.add(new SpellInfoPanel(chara, dclass, spell)); frame.pack(); frame.setVisible(true); }); add(mntmInfo); if (isPrepared) { JMenuItem mntmCast = new JMenuItem("Cast"); mntmCast.addActionListener(e -> { final DDSpellbook book = dclass.getSpellBook().get(); if (book.numSpellsPerDayRemainingAtLevel(spellLevel) == 0) { JOptionPane.showMessageDialog(this, "Cannot cast any more spells", "Error", JOptionPane.ERROR_MESSAGE); return; } book.castSpell(spellLevel, spell); ObserverDispatch.notifySubscribers(book, null); }); add(mntmCast); } } }