All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.pepsoft.worldpainter.MixedMaterialChooser Maven / Gradle / Ivy

There is a newer version: 2.23.2
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package org.pepsoft.worldpainter;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.pepsoft.minecraft.Material.DIRT;

/**
 *
 * @author Pepijn Schmitz
 */
@SuppressWarnings("unused") // Managed by NetBeans
public class MixedMaterialChooser extends javax.swing.JPanel {
    /**
     * Creates new form MixedMaterialChooser
     */
    public MixedMaterialChooser() {
        this(false);
    }

    /**
     * Creates new form MixedMaterialChooser
     */
    public MixedMaterialChooser(boolean allowNull) {
        this.allowNull = allowNull;

        initComponents();

        initComboBox(null);
        setControlStates();
    }

    public MixedMaterial getMaterial() {
        return (MixedMaterial) jComboBox1.getSelectedItem();
    }

    public void setMaterial(MixedMaterial material) {
        if (((DefaultComboBoxModel) jComboBox1.getModel()).getIndexOf(material) == -1) {
            // Make sure the list actually contains the material
            initComboBox(material);
        }
        jComboBox1.setSelectedItem(material);
        setControlStates();
        // Don't specify previous material, otherwise the event will not be fired if only the name or colour changed
        firePropertyChange("material", null, material);
    }

    public boolean isExtendedBlockIds() {
        return extendedBlockIds;
    }

    public void setExtendedBlockIds(boolean extendedBlockIds) {
        this.extendedBlockIds = extendedBlockIds;
    }

    public ColourScheme getColourScheme() {
        return colourScheme;
    }

    public void setColourScheme(ColourScheme colourScheme) {
        this.colourScheme = colourScheme;
        cellRenderer.setColourScheme(colourScheme);
        repaint();
    }

    public Platform getPlatform() {
        return platform;
    }

    public void setPlatform(Platform platform) {
        this.platform = platform;
    }

    public void refresh() {
        final MixedMaterial selectedMaterial = (MixedMaterial) jComboBox1.getSelectedItem();
        initComboBox(selectedMaterial);
        jComboBox1.setSelectedItem(selectedMaterial);
    }

    // JComponent
    
    @Override
    public int getBaseline(int width, int height) {
        return jComboBox1.getBaseline(width, height);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        setControlStates();
    }

    private void initComboBox(MixedMaterial unlistedMaterial) {
        MixedMaterial[] materials = MixedMaterialManager.getInstance().getMaterials();
        List list = new ArrayList<>(materials.length + 2);
        if (allowNull) {
            list.add(null);
        }
        list.addAll(Arrays.asList(materials));
        if ((unlistedMaterial != null) && (! list.contains(unlistedMaterial))) {
            list.add(allowNull ? 1 : 0, unlistedMaterial);
        }
        jComboBox1.setModel(new DefaultComboBoxModel<>(list.toArray(new MixedMaterial[list.size()])));
    }
    
    private void addMaterial() {
        MixedMaterial material = MixedMaterial.create(platform, DIRT);
        CustomMaterialDialog dialog = new CustomMaterialDialog(SwingUtilities.getWindowAncestor(this), platform, material, extendedBlockIds, colourScheme);
        dialog.setVisible(true);
        if (! dialog.isCancelled()) {
            material = MixedMaterialManager.getInstance().register(material);
            initComboBox(null);
            jComboBox1.setSelectedItem(material);
            setControlStates();
        }
    }

    private void editMaterial() {
        CustomMaterialDialog dialog = new CustomMaterialDialog(SwingUtilities.getWindowAncestor(this), platform, (MixedMaterial) jComboBox1.getSelectedItem(), extendedBlockIds, colourScheme);
        dialog.setVisible(true);
        if (! dialog.isCancelled()) {
            jComboBox1.repaint();
            firePropertyChange("material", null, jComboBox1.getSelectedItem());
        }
    }

    private void setControlStates() {
        jComboBox1.setEnabled(isEnabled());
        buttonAdd.setEnabled(isEnabled());
        buttonEdit.setEnabled(isEnabled() && (jComboBox1.getSelectedItem() != null));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings({"Convert2Lambda", "Anonymous2MethodRef", "ConstantConditions"}) // Managed by NetBeans
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox1 = new javax.swing.JComboBox<>();
        buttonEdit = new javax.swing.JButton();
        buttonAdd = new javax.swing.JButton();

        jComboBox1.setRenderer(cellRenderer);
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        buttonEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/pepsoft/worldpainter/icons/brick_edit.png"))); // NOI18N
        buttonEdit.setToolTipText("Edit the selected custom material");
        buttonEdit.setEnabled(false);
        buttonEdit.setMargin(new java.awt.Insets(2, 2, 2, 2));
        buttonEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonEditActionPerformed(evt);
            }
        });

        buttonAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/org/pepsoft/worldpainter/icons/brick_add.png"))); // NOI18N
        buttonAdd.setToolTipText("Create a new custom material");
        buttonAdd.setMargin(new java.awt.Insets(2, 2, 2, 2));
        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonAdd)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonEdit))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(buttonEdit)
            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addComponent(buttonAdd)
        );
    }// //GEN-END:initComponents

    private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
        addMaterial();
    }//GEN-LAST:event_buttonAddActionPerformed

    private void buttonEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonEditActionPerformed
        editMaterial();
    }//GEN-LAST:event_buttonEditActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        setControlStates();
        firePropertyChange("material", null, jComboBox1.getSelectedItem());
    }//GEN-LAST:event_jComboBox1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonEdit;
    private javax.swing.JComboBox jComboBox1;
    // End of variables declaration//GEN-END:variables

    private final MixedMaterialListCellRenderer cellRenderer = new MixedMaterialListCellRenderer();
    private final boolean allowNull;
    private boolean extendedBlockIds;
    private ColourScheme colourScheme;
    private Platform platform;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy