Search in sources :

Example 6 with MenuEvent

use of javax.swing.event.MenuEvent 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)

Example 7 with MenuEvent

use of javax.swing.event.MenuEvent in project energy3d by concord-consortium.

the class Analysis method createTypesMenu.

JMenu createTypesMenu() {
    final JMenu menu = new JMenu("Types");
    menu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(final MenuEvent e) {
            menu.removeAll();
            final Set<String> dataNames = graph.getDataNames();
            if (!dataNames.isEmpty()) {
                JMenuItem mi = new JMenuItem("Show All");
                mi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        for (final String name : dataNames) {
                            graph.hideData(name, false);
                        }
                        graph.repaint();
                        TimeSeriesLogger.getInstance().logShowCurve(graph.getClass().getSimpleName(), "All", true);
                    }
                });
                menu.add(mi);
                mi = new JMenuItem("Hide All");
                mi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        for (final String name : dataNames) {
                            graph.hideData(name, true);
                        }
                        graph.repaint();
                        TimeSeriesLogger.getInstance().logShowCurve(graph.getClass().getSimpleName(), "All", false);
                    }
                });
                menu.add(mi);
                menu.addSeparator();
                for (final String name : dataNames) {
                    final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(name, !graph.isDataHidden(name));
                    cbmi.addItemListener(new ItemListener() {

                        @Override
                        public void itemStateChanged(final ItemEvent e) {
                            graph.hideData(name, !cbmi.isSelected());
                            graph.repaint();
                            TimeSeriesLogger.getInstance().logShowCurve(graph.getClass().getSimpleName(), name, cbmi.isSelected());
                        }
                    });
                    menu.add(cbmi);
                }
            }
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });
    return menu;
}
Also used : ItemEvent(java.awt.event.ItemEvent) Set(java.util.Set) ActionListener(java.awt.event.ActionListener) MenuListener(javax.swing.event.MenuListener) ActionEvent(java.awt.event.ActionEvent) ItemListener(java.awt.event.ItemListener) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) MenuEvent(javax.swing.event.MenuEvent)

Example 8 with MenuEvent

use of javax.swing.event.MenuEvent in project energy3d by concord-consortium.

the class AnnualAnalysis method createOptionsMenu.

JMenu createOptionsMenu(final JDialog dialog, final List<HousePart> selectedParts, final boolean selectAll, final boolean exportStoredResults) {
    final JMenuItem miClear = new JMenuItem("Clear Previous Results");
    final JMenuItem miView = new JMenuItem("View Raw Data...");
    final JMenuItem miExportStoredResults = new JMenuItem("Export Stored Hourly Results");
    final JMenu menu = new JMenu("Options");
    menu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(final MenuEvent e) {
            miClear.setEnabled(graph.hasRecords());
            miView.setEnabled(graph.hasData());
            miExportStoredResults.setEnabled(Scene.getInstance().getSolarResults() != null);
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });
    final JMenu chartMenu = new JMenu("Chart");
    final ButtonGroup chartGroup = new ButtonGroup();
    menu.add(chartMenu);
    final JRadioButtonMenuItem miLine = new JRadioButtonMenuItem("Line");
    miLine.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                graph.setGraphType(Graph.LINE_CHART);
                graph.repaint();
            }
        }
    });
    chartMenu.add(miLine);
    chartGroup.add(miLine);
    miLine.setSelected(graph.getGraphType() == Graph.LINE_CHART);
    final JRadioButtonMenuItem miArea = new JRadioButtonMenuItem("Area");
    miArea.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                graph.setGraphType(Graph.AREA_CHART);
                graph.repaint();
            }
        }
    });
    chartMenu.add(miArea);
    chartGroup.add(miArea);
    miArea.setSelected(graph.getGraphType() == Graph.AREA_CHART);
    miClear.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            final int i = JOptionPane.showConfirmDialog(dialog, "Are you sure that you want to clear all the previous results\nrelated to the selected object?", "Confirmation", JOptionPane.YES_NO_OPTION);
            if (i != JOptionPane.YES_OPTION) {
                return;
            }
            graph.clearRecords();
            graph.repaint();
            TimeSeriesLogger.getInstance().logClearGraphData(graph.getClass().getSimpleName());
        }
    });
    menu.add(miClear);
    miView.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            if (selectedParts == null) {
                DataViewer.viewRawData(dialog, graph, selectAll);
            } else {
                DataViewer.viewRawData(dialog, graph, selectedParts);
            }
        }
    });
    menu.add(miView);
    final JMenuItem miCopyImage = new JMenuItem("Copy Image");
    miCopyImage.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            new ClipImage().copyImageToClipboard(graph);
        }
    });
    menu.add(miCopyImage);
    if (exportStoredResults) {
        miExportStoredResults.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final double[][] solarResults = Scene.getInstance().getSolarResults();
                if (solarResults != null) {
                    double sum = 0;
                    // Hack to fix the results in case we make a mistake that can be quickly remedied
                    final double scale = 1;
                    for (int i = 0; i < solarResults.length; i++) {
                        for (int j = 0; j < solarResults[i].length; j++) {
                            solarResults[i][j] *= scale;
                            sum += solarResults[i][j];
                        }
                    }
                    sum *= 365.0 / 12.0;
                    String s = "";
                    for (int i = 0; i < solarResults.length; i++) {
                        s += "\"" + AnnualGraph.THREE_LETTER_MONTH[i] + "\": \"";
                        for (int j = 0; j < solarResults[i].length; j++) {
                            s += EnergyPanel.FIVE_DECIMALS.format(solarResults[i][j]).replaceAll(",", "") + " ";
                        }
                        s = s.trim() + "\",\n\t";
                    }
                    s = s.substring(0, s.length() - 1);
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(s), null);
                    JOptionPane.showMessageDialog(dialog, "A total of " + EnergyPanel.TWO_DECIMALS.format(sum) + " KWh was copied to the clipboard.", "Export", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        menu.add(miExportStoredResults);
    }
    return menu;
}
Also used : ItemEvent(java.awt.event.ItemEvent) MenuListener(javax.swing.event.MenuListener) ActionEvent(java.awt.event.ActionEvent) ClipImage(org.concord.energy3d.util.ClipImage) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) StringSelection(java.awt.datatransfer.StringSelection) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) ItemListener(java.awt.event.ItemListener) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu) MenuEvent(javax.swing.event.MenuEvent)

Example 9 with MenuEvent

use of javax.swing.event.MenuEvent in project energy3d by concord-consortium.

the class EnergyAngularAnalysis method show.

public void show(final String title) {
    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
    String s = selectedPart.toString().substring(0, selectedPart.toString().indexOf(')') + 1);
    if (selectedPart instanceof Foundation) {
        s = s.replaceAll("Foundation", "Building");
        if (selectedPart.getChildren().isEmpty()) {
            JOptionPane.showMessageDialog(MainFrame.getInstance(), "There is no building on this foundation.", "No Building", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if (!isBuildingComplete((Foundation) selectedPart)) {
            if (JOptionPane.showConfirmDialog(MainFrame.getInstance(), "The selected building has not been completed.\nAre you sure to continue?", "Incomplete Building", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) != JOptionPane.YES_OPTION) {
                return;
            }
        }
    } else if (selectedPart instanceof Tree) {
        JOptionPane.showMessageDialog(MainFrame.getInstance(), "Energy analysis is not applicable to a tree.", "Not Applicable", JOptionPane.WARNING_MESSAGE);
        return;
    }
    final JDialog dialog = new JDialog(MainFrame.getInstance(), title + ": " + s, true);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    graph.parent = dialog;
    final JMenuBar menuBar = new JMenuBar();
    dialog.setJMenuBar(menuBar);
    final JMenuItem miClear = new JMenuItem("Clear Previous Results");
    final JMenuItem miView = new JMenuItem("View Raw Data...");
    final JMenuItem miCopyImage = new JMenuItem("Copy Image");
    final JMenu menu = new JMenu("Options");
    menu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(final MenuEvent e) {
            miClear.setEnabled(graph.hasRecords());
            miView.setEnabled(graph.hasData());
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });
    menuBar.add(menu);
    miClear.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            final int i = JOptionPane.showConfirmDialog(dialog, "Are you sure that you want to clear all the previous results\nrelated to the selected object?", "Confirmation", JOptionPane.YES_NO_OPTION);
            if (i != JOptionPane.YES_OPTION) {
                return;
            }
            graph.clearRecords();
            graph.repaint();
            TimeSeriesLogger.getInstance().logClearGraphData(graph.getClass().getSimpleName());
        }
    });
    menu.add(miClear);
    miView.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            DataViewer.viewRawData(dialog, graph, false);
        }
    });
    menu.add(miView);
    miCopyImage.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            new ClipImage().copyImageToClipboard(graph);
        }
    });
    menu.add(miCopyImage);
    final JMenu showTypeMenu = new JMenu("Types");
    showTypeMenu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(final MenuEvent e) {
            showTypeMenu.removeAll();
            final Set<String> dataNames = graph.getDataNames();
            if (!dataNames.isEmpty()) {
                JMenuItem mi = new JMenuItem("Show All");
                mi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        for (final String name : dataNames) {
                            graph.hideData(name, false);
                        }
                        graph.repaint();
                        TimeSeriesLogger.getInstance().logShowCurve(graph.getClass().getSimpleName(), "All", true);
                    }
                });
                showTypeMenu.add(mi);
                mi = new JMenuItem("Hide All");
                mi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        for (final String name : dataNames) {
                            graph.hideData(name, true);
                        }
                        graph.repaint();
                        TimeSeriesLogger.getInstance().logShowCurve(graph.getClass().getSimpleName(), "All", false);
                    }
                });
                showTypeMenu.add(mi);
                showTypeMenu.addSeparator();
                for (final String name : dataNames) {
                    final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(name, !graph.isDataHidden(name));
                    cbmi.addItemListener(new ItemListener() {

                        @Override
                        public void itemStateChanged(final ItemEvent e) {
                            graph.hideData(name, !cbmi.isSelected());
                            graph.repaint();
                            TimeSeriesLogger.getInstance().logShowCurve(graph.getClass().getSimpleName(), name, cbmi.isSelected());
                        }
                    });
                    showTypeMenu.add(cbmi);
                }
            }
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });
    menuBar.add(showTypeMenu);
    final JMenu showRunsMenu = new JMenu("Runs");
    showRunsMenu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(final MenuEvent e) {
            showRunsMenu.removeAll();
            if (!AngularGraph.records.isEmpty()) {
                JMenuItem mi = new JMenuItem("Show All");
                mi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        for (final Results r : AngularGraph.records) {
                            graph.hideRun(r.getID(), false);
                        }
                        graph.repaint();
                        TimeSeriesLogger.getInstance().logShowRun(graph.getClass().getSimpleName(), "All", true);
                    }
                });
                showRunsMenu.add(mi);
                mi = new JMenuItem("Hide All");
                mi.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        for (final Results r : AngularGraph.records) {
                            graph.hideRun(r.getID(), true);
                        }
                        graph.repaint();
                        TimeSeriesLogger.getInstance().logShowRun(graph.getClass().getSimpleName(), "All", false);
                    }
                });
                showRunsMenu.add(mi);
                showRunsMenu.addSeparator();
                for (final Results r : AngularGraph.records) {
                    final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(Integer.toString(r.getID()), !graph.isRunHidden(r.getID()));
                    cbmi.addItemListener(new ItemListener() {

                        @Override
                        public void itemStateChanged(final ItemEvent e) {
                            graph.hideRun(r.getID(), !cbmi.isSelected());
                            graph.repaint();
                            TimeSeriesLogger.getInstance().logShowRun(graph.getClass().getSimpleName(), "" + r.getID(), cbmi.isSelected());
                        }
                    });
                    showRunsMenu.add(cbmi);
                }
            }
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });
    menuBar.add(showRunsMenu);
    final JPanel contentPane = new JPanel(new BorderLayout());
    dialog.setContentPane(contentPane);
    final JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(BorderFactory.createEtchedBorder());
    contentPane.add(panel, BorderLayout.CENTER);
    panel.add(graph, BorderLayout.CENTER);
    final JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    contentPane.add(buttonPanel, BorderLayout.SOUTH);
    runButton = new JButton("Run");
    runButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            runButton.setEnabled(false);
            runAnalysis(dialog);
        }
    });
    buttonPanel.add(runButton);
    final JButton button = new JButton("Close");
    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            stopAnalysis();
            if (graph.hasData()) {
                final Object[] options = { "Yes", "No", "Cancel" };
                final int i = JOptionPane.showOptionDialog(dialog, "Do you want to keep the results of this run?", "Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]);
                if (i == JOptionPane.CANCEL_OPTION) {
                    return;
                }
                if (i == JOptionPane.YES_OPTION) {
                    graph.keepResults();
                }
            }
            windowLocation.setLocation(dialog.getLocationOnScreen());
            dialog.dispose();
        }
    });
    buttonPanel.add(button);
    dialog.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(final WindowEvent e) {
            stopAnalysis();
            windowLocation.setLocation(dialog.getLocationOnScreen());
            dialog.dispose();
        }
    });
    dialog.pack();
    if (windowLocation.x > 0 && windowLocation.y > 0) {
        dialog.setLocation(windowLocation);
    } else {
        dialog.setLocationRelativeTo(MainFrame.getInstance());
    }
    dialog.setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) ItemEvent(java.awt.event.ItemEvent) Set(java.util.Set) FlowLayout(java.awt.FlowLayout) MenuListener(javax.swing.event.MenuListener) ActionEvent(java.awt.event.ActionEvent) JButton(javax.swing.JButton) WindowAdapter(java.awt.event.WindowAdapter) 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) MenuEvent(javax.swing.event.MenuEvent) ClipImage(org.concord.energy3d.util.ClipImage) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionListener(java.awt.event.ActionListener) WindowEvent(java.awt.event.WindowEvent) ItemListener(java.awt.event.ItemListener) JDialog(javax.swing.JDialog) JMenuBar(javax.swing.JMenuBar) JMenu(javax.swing.JMenu)

Example 10 with MenuEvent

use of javax.swing.event.MenuEvent in project energy3d by concord-consortium.

the class DailyAnalysis method createOptionsMenu.

JMenu createOptionsMenu(final JDialog dialog, final List<HousePart> selectedParts, final boolean selectAll) {
    final JMenuItem miClear = new JMenuItem("Clear Previous Results");
    final JMenuItem miView = new JMenuItem("View Raw Data...");
    final JMenu menu = new JMenu("Options");
    menu.addMenuListener(new MenuListener() {

        @Override
        public void menuSelected(final MenuEvent e) {
            miClear.setEnabled(graph.hasRecords());
            miView.setEnabled(graph.hasData());
        }

        @Override
        public void menuDeselected(final MenuEvent e) {
        }

        @Override
        public void menuCanceled(final MenuEvent e) {
        }
    });
    final JMenu chartMenu = new JMenu("Chart");
    final ButtonGroup chartGroup = new ButtonGroup();
    menu.add(chartMenu);
    final JRadioButtonMenuItem miLine = new JRadioButtonMenuItem("Line");
    miLine.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                graph.setGraphType(Graph.LINE_CHART);
                graph.repaint();
            }
        }
    });
    chartMenu.add(miLine);
    chartGroup.add(miLine);
    miLine.setSelected(graph.getGraphType() == Graph.LINE_CHART);
    final JRadioButtonMenuItem miArea = new JRadioButtonMenuItem("Area");
    miArea.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                graph.setGraphType(Graph.AREA_CHART);
                graph.repaint();
            }
        }
    });
    chartMenu.add(miArea);
    chartGroup.add(miArea);
    miArea.setSelected(graph.getGraphType() == Graph.AREA_CHART);
    final JCheckBoxMenuItem miMilitaryTime = new JCheckBoxMenuItem("Military Time");
    miMilitaryTime.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(final ItemEvent e) {
            if (graph instanceof DailyGraph) {
                ((DailyGraph) graph).setMilitaryTime(miMilitaryTime.isSelected());
                graph.repaint();
            }
        }
    });
    menu.add(miMilitaryTime);
    miClear.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            final int i = JOptionPane.showConfirmDialog(dialog, "Are you sure that you want to clear all the previous results\nrelated to the selected object?", "Confirmation", JOptionPane.YES_NO_OPTION);
            if (i != JOptionPane.YES_OPTION) {
                return;
            }
            graph.clearRecords();
            graph.repaint();
            TimeSeriesLogger.getInstance().logClearGraphData(graph.getClass().getSimpleName());
        }
    });
    menu.add(miClear);
    miView.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            if (selectedParts == null) {
                DataViewer.viewRawData(dialog, graph, selectAll);
            } else {
                DataViewer.viewRawData(dialog, graph, selectedParts);
            }
        }
    });
    menu.add(miView);
    final JMenuItem miCopyImage = new JMenuItem("Copy Image");
    miCopyImage.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(final ActionEvent e) {
            new ClipImage().copyImageToClipboard(graph);
        }
    });
    menu.add(miCopyImage);
    return menu;
}
Also used : ItemEvent(java.awt.event.ItemEvent) MenuListener(javax.swing.event.MenuListener) ActionEvent(java.awt.event.ActionEvent) ClipImage(org.concord.energy3d.util.ClipImage) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) ItemListener(java.awt.event.ItemListener) JMenuItem(javax.swing.JMenuItem) JMenu(javax.swing.JMenu) MenuEvent(javax.swing.event.MenuEvent)

Aggregations

MenuEvent (javax.swing.event.MenuEvent)42 MenuListener (javax.swing.event.MenuListener)39 JMenu (javax.swing.JMenu)38 ActionEvent (java.awt.event.ActionEvent)23 JMenuItem (javax.swing.JMenuItem)23 ActionListener (java.awt.event.ActionListener)22 ItemEvent (java.awt.event.ItemEvent)18 ItemListener (java.awt.event.ItemListener)18 JCheckBoxMenuItem (javax.swing.JCheckBoxMenuItem)14 ButtonGroup (javax.swing.ButtonGroup)13 JMenuBar (javax.swing.JMenuBar)10 JPanel (javax.swing.JPanel)10 JRadioButtonMenuItem (javax.swing.JRadioButtonMenuItem)10 BorderLayout (java.awt.BorderLayout)9 HousePart (org.concord.energy3d.model.HousePart)9 Foundation (org.concord.energy3d.model.Foundation)8 JDialog (javax.swing.JDialog)7 JTextField (javax.swing.JTextField)6 BoxLayout (javax.swing.BoxLayout)5 JRadioButton (javax.swing.JRadioButton)5