Search in sources :

Example 1 with ChangeSnowReflectionFactorCommand

use of org.concord.energy3d.undo.ChangeSnowReflectionFactorCommand in project energy3d by concord-consortium.

the class PopupMenuForLand method getPopupMenu.

static JPopupMenu getPopupMenu() {
    if (popupMenuForLand == null) {
        final JMenuItem miInfo = new JMenuItem("Land");
        miInfo.setEnabled(false);
        miInfo.setOpaque(true);
        miInfo.setBackground(Config.isMac() ? Color.DARK_GRAY : Color.GRAY);
        miInfo.setForeground(Color.WHITE);
        final JMenuItem miPaste = new JMenuItem("Paste");
        miPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Config.isMac() ? KeyEvent.META_MASK : InputEvent.CTRL_MASK));
        miPaste.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                SceneManager.getTaskManager().update(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        Scene.getInstance().pasteToPickedLocationOnLand();
                        return null;
                    }
                });
            }
        });
        final JMenuItem miRemoveAllTrees = new JMenuItem("Remove All Trees");
        miRemoveAllTrees.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                SceneManager.getTaskManager().update(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        Scene.getInstance().removeAllTrees();
                        EventQueue.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                MainPanel.getInstance().getEnergyButton().setSelected(false);
                                Scene.getInstance().setEdited(true);
                            }
                        });
                        return null;
                    }
                });
            }
        });
        final JMenuItem miRemoveAllHumans = new JMenuItem("Remove All Humans");
        miRemoveAllHumans.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                SceneManager.getTaskManager().update(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        Scene.getInstance().removeAllHumans();
                        EventQueue.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                MainPanel.getInstance().getEnergyButton().setSelected(false);
                                Scene.getInstance().setEdited(true);
                            }
                        });
                        return null;
                    }
                });
            }
        });
        final JMenuItem miRemoveAllBuildings = new JMenuItem("Remove All Foundations");
        miRemoveAllBuildings.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                SceneManager.getTaskManager().update(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        Scene.getInstance().removeAllFoundations();
                        EventQueue.invokeLater(new Runnable() {

                            @Override
                            public void run() {
                                MainPanel.getInstance().getEnergyButton().setSelected(false);
                                Scene.getInstance().setEdited(true);
                            }
                        });
                        return null;
                    }
                });
            }
        });
        final JMenuItem miImportEnergy3D = new JMenuItem("Import...");
        miImportEnergy3D.setToolTipText("Import the content in an existing Energy3D file into the clicked location on the land as the center");
        miImportEnergy3D.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                MainFrame.getInstance().importFile();
            }
        });
        final JMenuItem miImportCollada = new JMenuItem("Import Collada...");
        miImportCollada.setToolTipText("Import the content in an existing Collada file into the clicked location on the land as the center");
        miImportCollada.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                MainFrame.getInstance().importColladaFile();
            }
        });
        final JMenu miImportPrefabMenu = new JMenu("Import a Prefab");
        addPrefabMenuItem("Back Hip Roof Porch", "prefabs/back-hip-roof-porch.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Balcony", "prefabs/balcony1.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Bell Tower", "prefabs/bell-tower.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Box", "prefabs/box.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Chimney", "prefabs/chimney.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Connecting Porch", "prefabs/connecting-porch.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Cylinder Tower", "prefabs/cylinder-tower.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Fence", "prefabs/fence1.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Flat-Top Porch", "prefabs/flat-top-porch.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Fountain", "prefabs/fountain.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Front Door Overhang", "prefabs/front-door-overhang.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Gable Dormer", "prefabs/gable-dormer.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Hexagonal Gazebo", "prefabs/hexagonal-gazebo.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Hexagonal Tower", "prefabs/hexagonal-tower.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Lighthouse", "prefabs/lighthouse.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Octagonal Tower", "prefabs/octagonal-tower.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Round Tower", "prefabs/round-tower.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Shed Dormer", "prefabs/shed-dormer.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Solarium", "prefabs/solarium1.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Square Tower", "prefabs/square-tower.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Stair", "prefabs/stair1.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Tall Front Door Overhang", "prefabs/tall-front-door-overhang.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Temple Front", "prefabs/temple-front.ng3", miImportPrefabMenu);
        addPrefabMenuItem("Waterfront Deck", "prefabs/waterfront-deck.ng3", miImportPrefabMenu);
        final JMenuItem miAlbedo = new JMenuItem("Albedo...");
        miAlbedo.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final String title = "<html>Background Albedo (dimensionless [0, 1])<hr><font size=2>Examples:<br>0.17 (soil), 0.25 (grass), 0.40 (sand), 0.55 (concrete), snow (0.9)</html>";
                while (true) {
                    final String newValue = JOptionPane.showInputDialog(MainFrame.getInstance(), title, Scene.getInstance().getGround().getAlbedo());
                    if (newValue == null) {
                        break;
                    } else {
                        try {
                            final double val = Double.parseDouble(newValue);
                            if (val < 0 || val > 1) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Albedo value must be in 0-1.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                if (val != Scene.getInstance().getGround().getAlbedo()) {
                                    final ChangeBackgroundAlbedoCommand c = new ChangeBackgroundAlbedoCommand();
                                    Scene.getInstance().getGround().setAlbedo(val);
                                    updateAfterEdit();
                                    SceneManager.getInstance().getUndoManager().addEdit(c);
                                }
                                break;
                            }
                        } catch (final NumberFormatException exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });
        final JMenuItem miSnowReflection = new JMenuItem("Snow Reflection...");
        miSnowReflection.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final JPanel gui = new JPanel(new BorderLayout());
                final String title = "<html>Increase of indirect solar radiation due to snow reflection<br>(a dimensionless parameter within [0, 0.2])</html>";
                gui.add(new JLabel(title), BorderLayout.NORTH);
                final JPanel inputPanel = new JPanel(new SpringLayout());
                inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
                gui.add(inputPanel, BorderLayout.CENTER);
                final JTextField[] fields = new JTextField[12];
                for (int i = 0; i < 12; i++) {
                    final JLabel l = new JLabel(AnnualGraph.THREE_LETTER_MONTH[i] + ": ", JLabel.TRAILING);
                    inputPanel.add(l);
                    fields[i] = new JTextField(threeDecimalsFormat.format(Scene.getInstance().getGround().getSnowReflectionFactor(i)), 5);
                    l.setLabelFor(fields[i]);
                    inputPanel.add(fields[i]);
                }
                SpringUtilities.makeCompactGrid(inputPanel, 12, 2, 6, 6, 6, 6);
                while (true) {
                    if (JOptionPane.showConfirmDialog(MainFrame.getInstance(), gui, "Snow reflection factor", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE) == JOptionPane.CANCEL_OPTION) {
                        break;
                    }
                    boolean pass = true;
                    final double[] val = new double[12];
                    for (int i = 0; i < 12; i++) {
                        try {
                            val[i] = Double.parseDouble(fields[i].getText());
                            if (val[i] < 0 || val[i] > 0.2) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Snow reflection factor must be in 0-0.2.", "Range Error", JOptionPane.ERROR_MESSAGE);
                                pass = false;
                            }
                        } catch (final NumberFormatException exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), fields[i].getText() + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                            pass = false;
                        }
                    }
                    if (pass) {
                        final ChangeSnowReflectionFactorCommand c = new ChangeSnowReflectionFactorCommand();
                        for (int i = 0; i < 12; i++) {
                            Scene.getInstance().getGround().setSnowReflectionFactor(val[i], i);
                        }
                        updateAfterEdit();
                        SceneManager.getInstance().getUndoManager().addEdit(c);
                        break;
                    }
                }
            }
        });
        final JMenuItem miThermalDiffusivity = new JMenuItem("Ground Thermal Diffusivity...");
        miThermalDiffusivity.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final String title = "<html>Ground Thermal Diffusivity (m<sup>2</sup>/s)<hr><font size=2>Examples:<br>0.039 (sand), 0.046 (clay), 0.05 (silt)</html>";
                while (true) {
                    final String newValue = JOptionPane.showInputDialog(MainFrame.getInstance(), title, Scene.getInstance().getGround().getThermalDiffusivity());
                    if (newValue == null) {
                        break;
                    } else {
                        try {
                            final double val = Double.parseDouble(newValue);
                            if (val <= 0) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Ground thermal diffusivity must be positive.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                if (val != Scene.getInstance().getGround().getThermalDiffusivity()) {
                                    final ChangeGroundThermalDiffusivityCommand c = new ChangeGroundThermalDiffusivityCommand();
                                    Scene.getInstance().getGround().setThermalDiffusivity(val);
                                    updateAfterEdit();
                                    SceneManager.getInstance().getUndoManager().addEdit(c);
                                }
                                break;
                            }
                        } catch (final NumberFormatException exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
            }
        });
        final JMenuItem miClearImage = new JMenuItem("Clear Image");
        final JMenuItem miRescaleImage = new JMenuItem("Rescale Image...");
        final JCheckBoxMenuItem miShowImage = new JCheckBoxMenuItem("Show Image");
        final JMenu groundImageMenu = new JMenu("Ground Image");
        groundImageMenu.addMenuListener(new MenuListener() {

            @Override
            public void menuCanceled(final MenuEvent e) {
                miShowImage.setEnabled(true);
                miClearImage.setEnabled(true);
            }

            @Override
            public void menuDeselected(final MenuEvent e) {
                miShowImage.setEnabled(true);
                miClearImage.setEnabled(true);
            }

            @Override
            public void menuSelected(final MenuEvent e) {
                final boolean hasGroundImage = Scene.getInstance().isGroundImageEnabled();
                miShowImage.setEnabled(hasGroundImage);
                miClearImage.setEnabled(hasGroundImage);
                Util.selectSilently(miShowImage, SceneManager.getInstance().getGroundImageLand().isVisible());
            }
        });
        final JMenuItem miUseEarthView = new JMenuItem("Use Image from Earth View...");
        miUseEarthView.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                new MapDialog(MainFrame.getInstance()).setVisible(true);
            }
        });
        groundImageMenu.add(miUseEarthView);
        final JMenuItem miUseImageFile = new JMenuItem("Use Image from File...");
        miUseImageFile.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final File file = FileChooser.getInstance().showDialog(".png", FileChooser.pngFilter, false);
                if (file == null) {
                    return;
                }
                try {
                    Scene.getInstance().setGroundImage(ImageIO.read(file), 1);
                    Scene.getInstance().setGroundImageEarthView(false);
                } catch (final Throwable t) {
                    t.printStackTrace();
                    JOptionPane.showMessageDialog(MainFrame.getInstance(), t.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
                }
                Scene.getInstance().setEdited(true);
            }
        });
        groundImageMenu.add(miUseImageFile);
        groundImageMenu.addSeparator();
        miRescaleImage.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final String title = "Scale the ground image";
                while (true) {
                    final String newValue = JOptionPane.showInputDialog(MainFrame.getInstance(), title, Scene.getInstance().getGroundImageScale());
                    if (newValue == null) {
                        break;
                    } else {
                        try {
                            final double val = Double.parseDouble(newValue);
                            if (val <= 0) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "The scaling factor must be positive.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                // final ChangeGroundThermalDiffusivityCommand c = new ChangeGroundThermalDiffusivityCommand();
                                Scene.getInstance().setGroundImageScale(val);
                                // SceneManager.getInstance().getUndoManager().addEdit(c);
                                break;
                            }
                        } catch (final NumberFormatException exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                }
                Scene.getInstance().setEdited(true);
            }
        });
        groundImageMenu.add(miRescaleImage);
        miClearImage.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                Scene.getInstance().setGroundImage(null, 1);
                Scene.getInstance().setEdited(true);
            }
        });
        groundImageMenu.add(miClearImage);
        miShowImage.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                final boolean b = miShowImage.isSelected();
                SceneManager.getInstance().getGroundImageLand().setVisible(b);
                Scene.getInstance().setShowGroundImage(b);
                Scene.getInstance().setEdited(true);
                SceneManager.getInstance().refresh();
            }
        });
        groundImageMenu.add(miShowImage);
        popupMenuForLand = new JPopupMenu();
        popupMenuForLand.setInvoker(MainPanel.getInstance().getCanvasPanel());
        popupMenuForLand.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
                final HousePart copyBuffer = Scene.getInstance().getCopyBuffer();
                miPaste.setEnabled(copyBuffer instanceof Tree || copyBuffer instanceof Human || copyBuffer instanceof Foundation);
            }

            @Override
            public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
            }

            @Override
            public void popupMenuCanceled(final PopupMenuEvent e) {
            }
        });
        popupMenuForLand.add(miInfo);
        // popupMenuForLand.addSeparator();
        popupMenuForLand.add(miPaste);
        popupMenuForLand.add(miRemoveAllTrees);
        popupMenuForLand.add(miRemoveAllHumans);
        popupMenuForLand.add(miRemoveAllBuildings);
        popupMenuForLand.addSeparator();
        popupMenuForLand.add(miImportEnergy3D);
        popupMenuForLand.add(miImportCollada);
        popupMenuForLand.add(miImportPrefabMenu);
        popupMenuForLand.addSeparator();
        popupMenuForLand.add(groundImageMenu);
        popupMenuForLand.add(colorAction);
        popupMenuForLand.add(miAlbedo);
        popupMenuForLand.add(miSnowReflection);
        popupMenuForLand.add(miThermalDiffusivity);
    }
    return popupMenuForLand;
}
Also used : JPanel(javax.swing.JPanel) ItemEvent(java.awt.event.ItemEvent) ActionEvent(java.awt.event.ActionEvent) PopupMenuListener(javax.swing.event.PopupMenuListener) MenuListener(javax.swing.event.MenuListener) PopupMenuListener(javax.swing.event.PopupMenuListener) JTextField(javax.swing.JTextField) PopupMenuEvent(javax.swing.event.PopupMenuEvent) Callable(java.util.concurrent.Callable) ChangeSnowReflectionFactorCommand(org.concord.energy3d.undo.ChangeSnowReflectionFactorCommand) BorderLayout(java.awt.BorderLayout) Tree(org.concord.energy3d.model.Tree) Foundation(org.concord.energy3d.model.Foundation) JMenuItem(javax.swing.JMenuItem) HousePart(org.concord.energy3d.model.HousePart) PopupMenuEvent(javax.swing.event.PopupMenuEvent) MenuEvent(javax.swing.event.MenuEvent) Human(org.concord.energy3d.model.Human) ChangeBackgroundAlbedoCommand(org.concord.energy3d.undo.ChangeBackgroundAlbedoCommand) ChangeGroundThermalDiffusivityCommand(org.concord.energy3d.undo.ChangeGroundThermalDiffusivityCommand) JLabel(javax.swing.JLabel) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) JPopupMenu(javax.swing.JPopupMenu) ActionListener(java.awt.event.ActionListener) SpringLayout(javax.swing.SpringLayout) ItemListener(java.awt.event.ItemListener) File(java.io.File) JMenu(javax.swing.JMenu)

Aggregations

BorderLayout (java.awt.BorderLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 File (java.io.File)1 Callable (java.util.concurrent.Callable)1 JCheckBoxMenuItem (javax.swing.JCheckBoxMenuItem)1 JLabel (javax.swing.JLabel)1 JMenu (javax.swing.JMenu)1 JMenuItem (javax.swing.JMenuItem)1 JPanel (javax.swing.JPanel)1 JPopupMenu (javax.swing.JPopupMenu)1 JTextField (javax.swing.JTextField)1 SpringLayout (javax.swing.SpringLayout)1 MenuEvent (javax.swing.event.MenuEvent)1 MenuListener (javax.swing.event.MenuListener)1 PopupMenuEvent (javax.swing.event.PopupMenuEvent)1 PopupMenuListener (javax.swing.event.PopupMenuListener)1 Foundation (org.concord.energy3d.model.Foundation)1