Search in sources :

Example 1 with DefaultFormatter

use of javax.swing.text.DefaultFormatter in project pcgen by PCGen.

the class Initiative method bDuplicateCombatantActionPerformed.

/**
	 * @param evt
	 */
private void bDuplicateCombatantActionPerformed(ActionEvent evt) {
    //TODO: This only works for saved pcgen files and xml combatants.
    //For pcgen files, it reloads the file, since there's no good way
    //curently to clone a PlayerCharacter.
    DefaultFormatter formatter = new NumberFormatter();
    formatter.setAllowsInvalid(false);
    formatter.setCommitsOnValidEdit(true);
    formatter.setValueClass(Integer.class);
    JFormattedTextField field = new JFormattedTextField(formatter);
    field.setValue(1);
    int choice = JOptionPane.showConfirmDialog(GMGenSystem.inst, field, "How many copies?", JOptionPane.OK_CANCEL_OPTION);
    if (choice == JOptionPane.CANCEL_OPTION) {
        return;
    }
    int count = ((Number) field.getValue()).intValue();
    for (InitHolder holderToCopy : getSelected()) {
        if ((holderToCopy instanceof XMLCombatant) || (holderToCopy instanceof PcgCombatant)) {
            if (holderToCopy instanceof PcgCombatant) {
                if ((((PcgCombatant) holderToCopy).getPC().getFileName() != null) && (!((PcgCombatant) holderToCopy).getPC().getFileName().isEmpty())) {
                    pasteNew(holderToCopy, count);
                } else {
                    JOptionPane.showMessageDialog(GMGenSystem.inst, "Combatant " + holderToCopy.getName() + " cannot be duplicated because it has not been saved to a valid .pcg file.", "Cannot Duplicate", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                pasteNew(holderToCopy, count);
            }
        } else {
            JOptionPane.showMessageDialog(GMGenSystem.inst, "Combatant " + holderToCopy.getName() + " cannot be duplicated because it is not a PCGen or XML combatant.", "Cannot Duplicate", JOptionPane.WARNING_MESSAGE);
        }
    }
}
Also used : PcgCombatant(gmgen.plugin.PcgCombatant) JFormattedTextField(javax.swing.JFormattedTextField) DefaultFormatter(javax.swing.text.DefaultFormatter) InitHolder(gmgen.plugin.InitHolder) XMLCombatant(plugin.initiative.XMLCombatant) NumberFormatter(javax.swing.text.NumberFormatter)

Example 2 with DefaultFormatter

use of javax.swing.text.DefaultFormatter in project pcgen by PCGen.

the class AttackDialog method initComponents.

/**
	 * <p>Initiaizes the dialog components.</p>
	 */
private void initComponents() {
    setTitle("Attack: " + m_attack.toString());
    //Set Layout
    getContentPane().setLayout(new BorderLayout());
    //Build the center panel with JTable and scroll pane
    m_attack.getBonusList();
    JPanel center = new JPanel(new BorderLayout());
    getContentPane().add(center, BorderLayout.CENTER);
    //This column model auto-sizes the columns based on contents.
    AutoSizingColumnModel columns = new AutoSizingColumnModel();
    m_tableModel = new AttackTableModel();
    m_tableModel.addTableModelListener(this::handleTableUpdate);
    m_table = new JTable(m_tableModel, columns);
    columns.referenceTable(m_table);
    m_table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    m_table.setAutoCreateColumnsFromModel(true);
    m_table.setPreferredScrollableViewportSize(new Dimension(columns.getTotalPreferredWidth(), m_table.getRowHeight() * m_table.getRowCount()));
    center.add(new JScrollPane(m_table), BorderLayout.CENTER);
    //Build panel to contain buttons, controls at bottom
    JPanel bottom = new JPanel();
    bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));
    bottom.add(Box.createRigidArea(new Dimension(10, 0)));
    m_totalDamageLabel = new JLabel("<html>Total Damage: <b>0</b></html>");
    bottom.add(m_totalDamageLabel);
    bottom.add(Box.createRigidArea(new Dimension(10, 0)));
    JCheckBox checkbox = new JCheckBox("Damage is subdual?");
    checkbox.addActionListener(this::handleSubdualAction);
    bottom.add(checkbox);
    bottom.add(Box.createRigidArea(new Dimension(10, 0)));
    JButton button = null;
    button = new JButton(new AbstractAction("Roll") {

        @Override
        public void actionPerformed(ActionEvent e) {
            performRoll();
        }
    });
    bottom.add(button);
    bottom.add(Box.createRigidArea(new Dimension(10, 0)));
    button = new JButton(new AbstractAction("Ok") {

        @Override
        public void actionPerformed(ActionEvent e) {
            handleOk();
        }
    });
    bottom.add(button);
    bottom.add(Box.createRigidArea(new Dimension(10, 0)));
    button = new JButton(new AbstractAction("Cancel") {

        @Override
        public void actionPerformed(ActionEvent e) {
            handleCancel();
        }
    });
    bottom.add(button);
    getContentPane().add(bottom, BorderLayout.SOUTH);
    JPanel top = new JPanel();
    top.setLayout(new BoxLayout(top, BoxLayout.X_AXIS));
    if ((m_combatants != null) && (!m_combatants.isEmpty())) {
        m_targets = new JComboBox(m_combatants);
        m_table.setDefaultEditor(PcgCombatant.class, new DefaultCellEditor(m_targets));
        //If we have combatants, initialize the top panel and populate
        //the JComboBox
        m_targetsCombo = new JComboBox(m_combatants);
        m_targetsCombo.addActionListener(this::handleTargetAction);
        top.add(new JLabel("Attack Character: "));
        top.add(m_targetsCombo);
        m_acTypeCombo = new JComboBox();
        m_acTypeCombo.addItem("Total");
        m_acTypeCombo.addItem("Flatfooted");
        m_acTypeCombo.addItem("Touch");
        m_acTypeCombo.addActionListener(this::handleAcTypeAction);
        top.add(new JLabel("Use AC Type: "));
        top.add(m_acTypeCombo);
    }
    DefaultFormatter formatter = new DefaultFormatter();
    formatter.setValueClass(Integer.class);
    formatter.setCommitsOnValidEdit(true);
    m_field = new JFormattedTextField(formatter);
    m_field.setFocusLostBehavior(JFormattedTextField.COMMIT_OR_REVERT);
    m_field.setPreferredSize(new Dimension(40, m_field.getPreferredSize().height));
    m_field.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if ((evt.getPropertyName() != null) && evt.getPropertyName().equals("value")) {
                m_tableModel.setArmorClass(((Integer) m_field.getValue()).intValue());
            }
        }
    });
    top.add(new JLabel("AC"));
    top.add(m_field);
    m_field.setValue(15);
    handleTargetAction(null);
    getContentPane().add(top, BorderLayout.NORTH);
    //Pack and locate the dialog
    pack();
    setLocationRelativeTo(GMGenSystem.inst);
}
Also used : PropertyChangeEvent(java.beans.PropertyChangeEvent) PropertyChangeListener(java.beans.PropertyChangeListener) ActionEvent(java.awt.event.ActionEvent) Dimension(java.awt.Dimension) BorderLayout(java.awt.BorderLayout) DefaultFormatter(javax.swing.text.DefaultFormatter)

Aggregations

DefaultFormatter (javax.swing.text.DefaultFormatter)2 InitHolder (gmgen.plugin.InitHolder)1 PcgCombatant (gmgen.plugin.PcgCombatant)1 BorderLayout (java.awt.BorderLayout)1 Dimension (java.awt.Dimension)1 ActionEvent (java.awt.event.ActionEvent)1 PropertyChangeEvent (java.beans.PropertyChangeEvent)1 PropertyChangeListener (java.beans.PropertyChangeListener)1 JFormattedTextField (javax.swing.JFormattedTextField)1 NumberFormatter (javax.swing.text.NumberFormatter)1 XMLCombatant (plugin.initiative.XMLCombatant)1