/*
 * VolumePlugin.java 12 Dec 2008
 *
 * Copyright (c) 2024 Space Mushrooms info@sweethome3d.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package com.eteks.test;

import javax.swing.JOptionPane;

import com.eteks.sweethome3d.model.PieceOfFurniture;
import com.eteks.sweethome3d.plugin.Plugin;
import com.eteks.sweethome3d.plugin.PluginAction;

/**
 * A simple Sweet Home 3D plug-in able to compute the volume 
 * of the movable furniture of the current home.  
 * @author Emmanuel Puybaret
 */
public class VolumePlugin extends Plugin {
    @Override
    public PluginAction[] getActions() {
        return new PluginAction [] {new VolumeAction()};
    }

    public class VolumeAction extends PluginAction {
        public VolumeAction() {
           putPropertyValue(Property.NAME, "Compute volume");
           putPropertyValue(Property.MENU, "Tools");
           // Enables the action by default
           setEnabled(true);
        }
        
        @Override
        public void execute() {
            float volumeInCm3 = 0;
            // Compute the sum of the volume of the bounding box of 
            // each movable piece of furniture in home
            for (PieceOfFurniture piece : getHome().getFurniture()) {
                if (piece.isMovable()) {
                    volumeInCm3 += piece.getWidth() 
                                   * piece.getDepth() 
                                   * piece.getHeight();
                }
            }
            
            // Display the result in a message box (\u00b3 is for 3 in supercript)
            String message = String.format(
                    "The maximum volume of the movable furniture in home is %.2f m\u00b3.",
                    volumeInCm3 / 1000000);
            JOptionPane.showMessageDialog(null, message);
        }
    }
}
