Search in sources :

Example 1 with MenuListener

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

the class PopupMenuForSky method getPopupMenu.

static JPopupMenu getPopupMenu() {
    if (popupMenuForSky == null) {
        final JMenuItem miInfo = new JMenuItem("Sky");
        miInfo.setEnabled(false);
        miInfo.setOpaque(true);
        miInfo.setBackground(Config.isMac() ? Color.DARK_GRAY : Color.GRAY);
        miInfo.setForeground(Color.WHITE);
        final JCheckBoxMenuItem miHeliodon = new JCheckBoxMenuItem("Heliodon");
        miHeliodon.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                MainPanel.getInstance().getHeliodonButton().doClick();
            }
        });
        final JMenu weatherMenu = new JMenu("Weather");
        JMenuItem mi = new JMenuItem("Monthly Sunshine Hours...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().checkCity()) {
                    new MonthlySunshineHours().showDialog();
                }
            }
        });
        weatherMenu.add(mi);
        mi = new JMenuItem("Annual Environmental Temperature...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().checkCity()) {
                    new AnnualEnvironmentalTemperature().showDialog();
                }
            }
        });
        weatherMenu.add(mi);
        mi = new JMenuItem("Daily Environmental Temperature...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().checkCity()) {
                    new DailyEnvironmentalTemperature().showDialog();
                }
            }
        });
        weatherMenu.add(mi);
        final JMenu themeMenu = new JMenu("Theme");
        final ButtonGroup themeButtonGroup = new ButtonGroup();
        final JRadioButtonMenuItem miBlueSky = new JRadioButtonMenuItem("Blue Sky");
        miBlueSky.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final ChangeThemeCommand c = new ChangeThemeCommand();
                    Scene.getInstance().setTheme(Scene.BLUE_SKY_THEME);
                    Scene.getInstance().setEdited(true);
                    SceneManager.getInstance().getUndoManager().addEdit(c);
                }
            }
        });
        themeButtonGroup.add(miBlueSky);
        themeMenu.add(miBlueSky);
        final JRadioButtonMenuItem miDesert = new JRadioButtonMenuItem("Desert");
        miDesert.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final ChangeThemeCommand c = new ChangeThemeCommand();
                    Scene.getInstance().setTheme(Scene.DESERT_THEME);
                    Scene.getInstance().setEdited(true);
                    SceneManager.getInstance().getUndoManager().addEdit(c);
                }
            }
        });
        themeButtonGroup.add(miDesert);
        themeMenu.add(miDesert);
        final JRadioButtonMenuItem miGrassland = new JRadioButtonMenuItem("Grassland");
        miGrassland.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final ChangeThemeCommand c = new ChangeThemeCommand();
                    Scene.getInstance().setTheme(Scene.GRASSLAND_THEME);
                    Scene.getInstance().setEdited(true);
                    SceneManager.getInstance().getUndoManager().addEdit(c);
                }
            }
        });
        themeButtonGroup.add(miGrassland);
        themeMenu.add(miGrassland);
        final JRadioButtonMenuItem miForest = new JRadioButtonMenuItem("Forest");
        miForest.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final ChangeThemeCommand c = new ChangeThemeCommand();
                    Scene.getInstance().setTheme(Scene.FOREST_THEME);
                    Scene.getInstance().setEdited(true);
                    SceneManager.getInstance().getUndoManager().addEdit(c);
                }
            }
        });
        themeButtonGroup.add(miForest);
        themeMenu.add(miForest);
        themeMenu.addMenuListener(new MenuListener() {

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

            @Override
            public void menuDeselected(final MenuEvent e) {
                SceneManager.getInstance().refresh();
            }

            @Override
            public void menuSelected(final MenuEvent e) {
                Util.selectSilently(miBlueSky, Scene.getInstance().getTheme() == Scene.BLUE_SKY_THEME);
                Util.selectSilently(miDesert, Scene.getInstance().getTheme() == Scene.DESERT_THEME);
                Util.selectSilently(miGrassland, Scene.getInstance().getTheme() == Scene.GRASSLAND_THEME);
                Util.selectSilently(miForest, Scene.getInstance().getTheme() == Scene.FOREST_THEME);
            }
        });
        final JMenuItem miDustLoss = new JMenuItem("Dust & Pollen...");
        miDustLoss.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                final JPanel gui = new JPanel(new BorderLayout());
                final String title = "<html><b>Soiling loss factor:</b><br>Loss of productivity due to atmospheric dust and pollen<br>(a dimensionless parameter within [0, 1])</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.LEFT);
                    inputPanel.add(l);
                    fields[i] = new JTextField(threeDecimalsFormat.format(Scene.getInstance().getAtmosphere().getDustLoss(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, "Dust and pollen loss", 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] > 1) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Dust and pollen loss value must be in 0-1.", "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) {
                        boolean changed = false;
                        for (int i = 0; i < 12; i++) {
                            if (Math.abs(Scene.getInstance().getAtmosphere().getDustLoss(i) - val[i]) > 0.000001) {
                                changed = true;
                                break;
                            }
                        }
                        if (changed) {
                            final ChangeAtmosphericDustLossCommand c = new ChangeAtmosphericDustLossCommand();
                            for (int i = 0; i < 12; i++) {
                                Scene.getInstance().getAtmosphere().setDustLoss(val[i], i);
                            }
                            updateAfterEdit();
                            SceneManager.getInstance().getUndoManager().addEdit(c);
                        }
                        break;
                    }
                }
            }
        });
        popupMenuForSky = new JPopupMenu();
        popupMenuForSky.setInvoker(MainPanel.getInstance().getCanvasPanel());
        popupMenuForSky.addPopupMenuListener(new PopupMenuListener() {

            @Override
            public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
                Util.selectSilently(miHeliodon, MainPanel.getInstance().getHeliodonButton().isSelected());
            }

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

            @Override
            public void popupMenuCanceled(final PopupMenuEvent e) {
            }
        });
        popupMenuForSky.add(miInfo);
        popupMenuForSky.add(miDustLoss);
        popupMenuForSky.add(miHeliodon);
        popupMenuForSky.addSeparator();
        popupMenuForSky.add(weatherMenu);
        popupMenuForSky.add(themeMenu);
    }
    return popupMenuForSky;
}
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) ChangeAtmosphericDustLossCommand(org.concord.energy3d.undo.ChangeAtmosphericDustLossCommand) BorderLayout(java.awt.BorderLayout) JMenuItem(javax.swing.JMenuItem) ChangeThemeCommand(org.concord.energy3d.undo.ChangeThemeCommand) PopupMenuEvent(javax.swing.event.PopupMenuEvent) MenuEvent(javax.swing.event.MenuEvent) AnnualEnvironmentalTemperature(org.concord.energy3d.simulation.AnnualEnvironmentalTemperature) MonthlySunshineHours(org.concord.energy3d.simulation.MonthlySunshineHours) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) JLabel(javax.swing.JLabel) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) DailyEnvironmentalTemperature(org.concord.energy3d.simulation.DailyEnvironmentalTemperature) JPopupMenu(javax.swing.JPopupMenu) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) SpringLayout(javax.swing.SpringLayout) ItemListener(java.awt.event.ItemListener) JMenu(javax.swing.JMenu)

Example 2 with MenuListener

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

the class PopupMenuForWindow method getPopupMenu.

static JPopupMenu getPopupMenu() {
    if (popupMenuForWindow == null) {
        final JMenu shutterMenu = new JMenu("Shutters");
        popupMenuForWindow = createPopupMenu(true, true, new Runnable() {

            @Override
            public void run() {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (selectedPart instanceof Window) {
                    shutterMenu.setEnabled(selectedPart.getContainer() instanceof Wall);
                }
            }
        });
        final JMenu muntinMenu = new JMenu("Muntins");
        final JCheckBoxMenuItem cbmiHorizontalBars = new JCheckBoxMenuItem("Horizontal Bars");
        cbmiHorizontalBars.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (selectedPart instanceof Window) {
                    final Window w = (Window) selectedPart;
                    w.setHorizontalBars(cbmiHorizontalBars.isSelected());
                    w.draw();
                    SceneManager.getInstance().refresh();
                    Scene.getInstance().setEdited(true);
                }
            }
        });
        muntinMenu.add(cbmiHorizontalBars);
        final JCheckBoxMenuItem cbmiVerticalBars = new JCheckBoxMenuItem("Vertical Bars");
        cbmiVerticalBars.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (selectedPart instanceof Window) {
                    final Window w = (Window) selectedPart;
                    w.setVerticalBars(cbmiVerticalBars.isSelected());
                    w.draw();
                    SceneManager.getInstance().refresh();
                    Scene.getInstance().setEdited(true);
                }
            }
        });
        muntinMenu.add(cbmiVerticalBars);
        muntinMenu.addSeparator();
        final ButtonGroup muntinButtonGroup = new ButtonGroup();
        final JRadioButtonMenuItem miMoreBars = new JRadioButtonMenuItem("More Bars");
        miMoreBars.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                    if (selectedPart instanceof Window) {
                        final Window w = (Window) selectedPart;
                        w.setStyle(Window.MORE_MUNTIN_BARS);
                        w.draw();
                        SceneManager.getInstance().refresh();
                        Scene.getInstance().setEdited(true);
                    }
                }
            }
        });
        muntinButtonGroup.add(miMoreBars);
        muntinMenu.add(miMoreBars);
        final JRadioButtonMenuItem miMediumBars = new JRadioButtonMenuItem("Medium Bars");
        miMediumBars.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                    if (selectedPart instanceof Window) {
                        final Window w = (Window) selectedPart;
                        w.setStyle(Window.MEDIUM_MUNTIN_BARS);
                        w.draw();
                        SceneManager.getInstance().refresh();
                        Scene.getInstance().setEdited(true);
                    }
                }
            }
        });
        muntinButtonGroup.add(miMediumBars);
        muntinMenu.add(miMediumBars);
        final JRadioButtonMenuItem miLessBars = new JRadioButtonMenuItem("Less Bars");
        miLessBars.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                    if (selectedPart instanceof Window) {
                        final Window w = (Window) selectedPart;
                        w.setStyle(Window.LESS_MUNTIN_BARS);
                        w.draw();
                        SceneManager.getInstance().refresh();
                        Scene.getInstance().setEdited(true);
                    }
                }
            }
        });
        muntinButtonGroup.add(miLessBars);
        muntinMenu.add(miLessBars);
        muntinMenu.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(final MenuEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (selectedPart instanceof Window) {
                    final Window window = (Window) selectedPart;
                    switch(window.getStyle()) {
                        case Window.MORE_MUNTIN_BARS:
                            Util.selectSilently(miMoreBars, true);
                            break;
                        case Window.MEDIUM_MUNTIN_BARS:
                            Util.selectSilently(miMediumBars, true);
                            break;
                        case Window.LESS_MUNTIN_BARS:
                            Util.selectSilently(miLessBars, true);
                            break;
                    }
                    // NO_MUNTIN_BAR backward compatibility
                    Util.selectSilently(cbmiHorizontalBars, window.getStyle() != Window.NO_MUNTIN_BAR && window.getHorizontalBars());
                    Util.selectSilently(cbmiVerticalBars, window.getStyle() != Window.NO_MUNTIN_BAR && window.getVerticalBars());
                }
            }

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

            @Override
            public void menuCanceled(final MenuEvent e) {
                muntinMenu.setEnabled(true);
            }
        });
        final JCheckBoxMenuItem cbmiBothShutters = new JCheckBoxMenuItem("Both Shutters");
        final JCheckBoxMenuItem cbmiLeftShutter = new JCheckBoxMenuItem("Left Shutter");
        final JCheckBoxMenuItem cbmiRightShutter = new JCheckBoxMenuItem("Right Shutter");
        cbmiLeftShutter.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                    if (selectedPart instanceof Window) {
                        final Window w = (Window) selectedPart;
                        final ChangeWindowShuttersCommand c = new ChangeWindowShuttersCommand(w);
                        w.setLeftShutter(cbmiLeftShutter.isSelected());
                        w.draw();
                        Scene.getInstance().setEdited(true);
                        SceneManager.getInstance().getUndoManager().addEdit(c);
                    }
                }
            }
        });
        shutterMenu.add(cbmiLeftShutter);
        cbmiRightShutter.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                    if (selectedPart instanceof Window) {
                        final Window w = (Window) selectedPart;
                        final ChangeWindowShuttersCommand c = new ChangeWindowShuttersCommand(w);
                        w.setRightShutter(cbmiRightShutter.isSelected());
                        w.draw();
                        Scene.getInstance().setEdited(true);
                        SceneManager.getInstance().getUndoManager().addEdit(c);
                    }
                }
            }
        });
        shutterMenu.add(cbmiRightShutter);
        cbmiBothShutters.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                    if (selectedPart instanceof Window) {
                        final Window w = (Window) selectedPart;
                        final ChangeWindowShuttersCommand c = new ChangeWindowShuttersCommand(w);
                        w.setLeftShutter(cbmiBothShutters.isSelected());
                        w.setRightShutter(cbmiBothShutters.isSelected());
                        w.draw();
                        Scene.getInstance().setEdited(true);
                        SceneManager.getInstance().getUndoManager().addEdit(c);
                    }
                }
            }
        });
        shutterMenu.add(cbmiBothShutters);
        shutterMenu.addSeparator();
        final JMenuItem miShutterColor = new JMenuItem("Color...");
        miShutterColor.addActionListener(new ActionListener() {

            // remember the scope selection as the next action will likely be applied to the same scope
            private int selectedScopeIndex = 0;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (!(selectedPart instanceof Window)) {
                    return;
                }
                final Window window = (Window) selectedPart;
                final JColorChooser colorChooser = MainFrame.getInstance().getColorChooser();
                final ReadOnlyColorRGBA color = window.getShutterColor();
                if (color != null) {
                    colorChooser.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue()));
                }
                final ActionListener actionListener = new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        final Color c = colorChooser.getColor();
                        final float[] newColor = c.getComponents(null);
                        final ColorRGBA color = new ColorRGBA(newColor[0], newColor[1], newColor[2], 1);
                        final JPanel panel = new JPanel();
                        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                        panel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                        final JRadioButton rb1 = new JRadioButton("Only this Window", true);
                        final JRadioButton rb2 = new JRadioButton("All Windows on this " + (window.getContainer() instanceof Wall ? "Wall" : "Roof"));
                        final JRadioButton rb3 = new JRadioButton("All Windows of this Building");
                        panel.add(rb1);
                        panel.add(rb2);
                        panel.add(rb3);
                        final ButtonGroup bg = new ButtonGroup();
                        bg.add(rb1);
                        bg.add(rb2);
                        bg.add(rb3);
                        switch(selectedScopeIndex) {
                            case 0:
                                rb1.setSelected(true);
                                break;
                            case 1:
                                rb2.setSelected(true);
                                break;
                            case 2:
                                rb3.setSelected(true);
                                break;
                        }
                        final Object[] options = new Object[] { "OK", "Cancel", "Apply" };
                        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
                        final JDialog dialog = optionPane.createDialog(MainFrame.getInstance(), "Shutter Color");
                        while (true) {
                            dialog.setVisible(true);
                            final Object choice = optionPane.getValue();
                            if (choice == options[1] || choice == null) {
                                break;
                            } else {
                                boolean changed = !color.equals(window.getShutterColor());
                                if (rb1.isSelected()) {
                                    // apply to only this window
                                    if (changed) {
                                        final ChangeShutterColorCommand cmd = new ChangeShutterColorCommand(window);
                                        window.setShutterColor(color);
                                        window.draw();
                                        SceneManager.getInstance().getUndoManager().addEdit(cmd);
                                    }
                                    selectedScopeIndex = 0;
                                } else if (rb2.isSelected()) {
                                    if (!changed) {
                                        if (window.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) window.getContainer();
                                            for (final Window x : wall.getWindows()) {
                                                if (!color.equals(x.getShutterColor())) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        } else if (window.getContainer() instanceof Roof) {
                                            final Roof roof = (Roof) window.getContainer();
                                            for (final Window x : roof.getWindows()) {
                                                if (!color.equals(x.getShutterColor())) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeContainerShutterColorCommand cmd = new ChangeContainerShutterColorCommand(window.getContainer());
                                        Scene.getInstance().setWindowColorInContainer(window.getContainer(), color, true);
                                        SceneManager.getInstance().getUndoManager().addEdit(cmd);
                                    }
                                    selectedScopeIndex = 1;
                                } else {
                                    final Foundation foundation = window.getTopContainer();
                                    if (!changed) {
                                        for (final Window x : foundation.getWindows()) {
                                            if (!color.equals(x.getShutterColor())) {
                                                changed = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeBuildingShutterColorCommand cmd = new ChangeBuildingShutterColorCommand(window);
                                        Scene.getInstance().setShutterColorOfBuilding(window, color);
                                        SceneManager.getInstance().getUndoManager().addEdit(cmd);
                                    }
                                    selectedScopeIndex = 2;
                                }
                                if (changed) {
                                    Scene.getInstance().setEdited(true);
                                    SceneManager.getInstance().refresh();
                                }
                                if (choice == options[0]) {
                                    break;
                                }
                            }
                        }
                    }
                };
                JColorChooser.createDialog(MainFrame.getInstance(), "Select Shutter Color", true, colorChooser, actionListener, null).setVisible(true);
            }
        });
        shutterMenu.add(miShutterColor);
        final JMenuItem miShutterLength = new JMenuItem("Relative Length...");
        miShutterLength.addActionListener(new ActionListener() {

            // remember the scope selection as the next action will likely be applied to the same scope
            private int selectedScopeIndex = 0;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (!(selectedPart instanceof Window)) {
                    return;
                }
                final Window window = (Window) selectedPart;
                final String partInfo = window.toString().substring(0, selectedPart.toString().indexOf(')') + 1);
                final String title = "<html>Relative Length of Shutter for " + partInfo + "</html>";
                final String footnote = "<html><hr width=400></html>";
                final JPanel gui = new JPanel(new BorderLayout());
                final JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                panel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                final JRadioButton rb1 = new JRadioButton("Only this Window Shutter", true);
                final JRadioButton rb2 = new JRadioButton("All Window Shutters on this Wall");
                final JRadioButton rb3 = new JRadioButton("All Window Shutters of this Building");
                panel.add(rb1);
                panel.add(rb2);
                panel.add(rb3);
                final ButtonGroup bg = new ButtonGroup();
                bg.add(rb1);
                bg.add(rb2);
                bg.add(rb3);
                switch(selectedScopeIndex) {
                    case 0:
                        rb1.setSelected(true);
                        break;
                    case 1:
                        rb2.setSelected(true);
                        break;
                    case 2:
                        rb3.setSelected(true);
                        break;
                }
                gui.add(panel, BorderLayout.CENTER);
                final JTextField inputField = new JTextField(window.getShutterLength() + "");
                gui.add(inputField, BorderLayout.SOUTH);
                final Object[] options = new Object[] { "OK", "Cancel", "Apply" };
                final JOptionPane optionPane = new JOptionPane(new Object[] { title, footnote, gui }, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
                final JDialog dialog = optionPane.createDialog(MainFrame.getInstance(), "Shutter Length (Relative)");
                while (true) {
                    dialog.setVisible(true);
                    inputField.selectAll();
                    inputField.requestFocusInWindow();
                    final Object choice = optionPane.getValue();
                    if (choice == options[1] || choice == null) {
                        break;
                    } else {
                        boolean ok = true;
                        double val = 0;
                        try {
                            val = Double.parseDouble(inputField.getText());
                        } catch (final NumberFormatException exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), inputField.getText() + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                            ok = false;
                        }
                        if (ok) {
                            if (val <= 0 || val > 1) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Relative shutter length must be within (0, 1).", "Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                boolean changed = Math.abs(val - window.getShutterLength()) > 0.000001;
                                if (rb1.isSelected()) {
                                    if (changed) {
                                        final ChangeShutterLengthCommand c = new ChangeShutterLengthCommand(window);
                                        window.setShutterLength(val);
                                        window.draw();
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 0;
                                } else if (rb2.isSelected()) {
                                    if (!changed) {
                                        if (window.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) window.getContainer();
                                            for (final Window x : wall.getWindows()) {
                                                if (Math.abs(val - x.getShutterLength()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        } else if (window.getContainer() instanceof Roof) {
                                            final Roof roof = (Roof) window.getContainer();
                                            for (final Window x : roof.getWindows()) {
                                                if (Math.abs(val - x.getShutterLength()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (changed) {
                                        Scene.getInstance().setShutterLengthInContainer(window.getContainer(), val);
                                    }
                                    selectedScopeIndex = 1;
                                } else if (rb3.isSelected()) {
                                    final Foundation foundation = window.getTopContainer();
                                    if (!changed) {
                                        for (final Window x : foundation.getWindows()) {
                                            if (Math.abs(val - x.getShutterLength()) > 0.000001) {
                                                changed = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (changed) {
                                        Scene.getInstance().setShutterLengthOfBuilding(window, val);
                                    }
                                    selectedScopeIndex = 2;
                                }
                                if (changed) {
                                    SceneManager.getInstance().refresh();
                                    Scene.getInstance().setEdited(true);
                                }
                                if (choice == options[0]) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        });
        shutterMenu.add(miShutterLength);
        shutterMenu.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(final MenuEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (selectedPart instanceof Window) {
                    final Window window = (Window) selectedPart;
                    Util.selectSilently(cbmiLeftShutter, window.getLeftShutter());
                    Util.selectSilently(cbmiRightShutter, window.getRightShutter());
                    Util.selectSilently(cbmiBothShutters, window.getLeftShutter() && window.getRightShutter());
                }
            }

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

            @Override
            public void menuCanceled(final MenuEvent e) {
                shutterMenu.setEnabled(true);
            }
        });
        final JMenuItem miShgc = new JMenuItem("Solar Heat Gain Coefficient...");
        miShgc.addActionListener(new ActionListener() {

            // remember the scope selection as the next action will likely be applied to the same scope
            private int selectedScopeIndex = 0;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (!(selectedPart instanceof Window)) {
                    return;
                }
                final String partInfo = selectedPart.toString().substring(0, selectedPart.toString().indexOf(')') + 1);
                final Window window = (Window) selectedPart;
                final String title = "<html>Solar Heat Gain Coefficient of " + partInfo + "</html>";
                final String footnote = "<html><hr><font size=2>Examples:<br><table><tr><td><font size=2>Single glass (clear)</td><td><font size=2>0.66</td></tr><tr><td><font size=2>Single glass (green tint)</td><td><font size=2>0.55</td></tr><tr><td><font size=2>Triple glass (air spaces)</td><td><font size=2>0.39</td></tr></table><hr></html>";
                final JPanel gui = new JPanel(new BorderLayout());
                final JPanel panel = new JPanel();
                panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                panel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                final JRadioButton rb1 = new JRadioButton("Only this Window", true);
                final JRadioButton rb2 = new JRadioButton("All Windows on this " + (selectedPart.getContainer() instanceof Wall ? "Wall" : "Roof"));
                final JRadioButton rb3 = new JRadioButton("All Windows of this Building");
                panel.add(rb1);
                panel.add(rb2);
                panel.add(rb3);
                final ButtonGroup bg = new ButtonGroup();
                bg.add(rb1);
                bg.add(rb2);
                bg.add(rb3);
                switch(selectedScopeIndex) {
                    case 0:
                        rb1.setSelected(true);
                        break;
                    case 1:
                        rb2.setSelected(true);
                        break;
                    case 2:
                        rb3.setSelected(true);
                        break;
                }
                gui.add(panel, BorderLayout.CENTER);
                final JTextField inputField = new JTextField(window.getSolarHeatGainCoefficient() + "");
                gui.add(inputField, BorderLayout.SOUTH);
                final Object[] options = new Object[] { "OK", "Cancel", "Apply" };
                final JOptionPane optionPane = new JOptionPane(new Object[] { title, footnote, gui }, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
                final JDialog dialog = optionPane.createDialog(MainFrame.getInstance(), "Solar Heat Gain Coefficient (SHGC)");
                while (true) {
                    dialog.setVisible(true);
                    inputField.selectAll();
                    inputField.requestFocusInWindow();
                    final Object choice = optionPane.getValue();
                    if (choice == options[1] || choice == null) {
                        break;
                    } else {
                        boolean ok = true;
                        double val = 0;
                        try {
                            val = Double.parseDouble(inputField.getText());
                        } catch (final NumberFormatException exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), inputField.getText() + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                            ok = false;
                        }
                        if (ok) {
                            if (val < 0 || val > 1) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Solar heat gain coefficient must be between 0 and 1.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                boolean changed = Math.abs(val - window.getSolarHeatGainCoefficient()) > 0.000001;
                                if (rb1.isSelected()) {
                                    if (changed) {
                                        final ChangeWindowShgcCommand c = new ChangeWindowShgcCommand(window);
                                        window.setSolarHeatGainCoefficient(val);
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 0;
                                } else if (rb2.isSelected()) {
                                    if (!changed) {
                                        if (window.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) window.getContainer();
                                            for (final Window x : wall.getWindows()) {
                                                if (Math.abs(val - x.getSolarHeatGainCoefficient()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        } else if (window.getContainer() instanceof Roof) {
                                            final Roof roof = (Roof) window.getContainer();
                                            for (final Window x : roof.getWindows()) {
                                                if (Math.abs(val - x.getSolarHeatGainCoefficient()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeContainerWindowShgcCommand c = new ChangeContainerWindowShgcCommand(window.getContainer());
                                        Scene.getInstance().setWindowShgcInContainer(window.getContainer(), val);
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 1;
                                } else if (rb3.isSelected()) {
                                    final Foundation foundation = window.getTopContainer();
                                    if (!changed) {
                                        for (final Window x : foundation.getWindows()) {
                                            if (Math.abs(val - x.getSolarHeatGainCoefficient()) > 0.000001) {
                                                changed = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeBuildingWindowShgcCommand c = new ChangeBuildingWindowShgcCommand(foundation);
                                        Scene.getInstance().setWindowShgcOfBuilding(foundation, val);
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 2;
                                }
                                if (changed) {
                                    updateAfterEdit();
                                }
                                if (choice == options[0]) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        });
        final JMenuItem miTint = new JMenuItem("Tint...");
        miTint.addActionListener(new ActionListener() {

            // remember the scope selection as the next action will likely be applied to the same scope
            private int selectedScopeIndex = 0;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (!(selectedPart instanceof Window)) {
                    return;
                }
                final Window window = (Window) selectedPart;
                final JColorChooser colorChooser = MainFrame.getInstance().getColorChooser();
                final ReadOnlyColorRGBA color = window.getColor();
                if (color != null) {
                    colorChooser.setColor(new Color(color.getRed(), color.getGreen(), color.getBlue()));
                }
                final ActionListener actionListener = new ActionListener() {

                    @Override
                    public void actionPerformed(final ActionEvent e) {
                        final Color c = colorChooser.getColor();
                        final float[] newColor = c.getComponents(null);
                        final ColorRGBA color = new ColorRGBA(newColor[0], newColor[1], newColor[2], (float) (1.0 - window.getSolarHeatGainCoefficient()));
                        final JPanel panel = new JPanel();
                        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
                        panel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                        final JRadioButton rb1 = new JRadioButton("Only this Window", true);
                        final JRadioButton rb2 = new JRadioButton("All Windows on this " + (window.getContainer() instanceof Wall ? "Wall" : "Roof"));
                        final JRadioButton rb3 = new JRadioButton("All Windows of this Building");
                        panel.add(rb1);
                        panel.add(rb2);
                        panel.add(rb3);
                        final ButtonGroup bg = new ButtonGroup();
                        bg.add(rb1);
                        bg.add(rb2);
                        bg.add(rb3);
                        switch(selectedScopeIndex) {
                            case 0:
                                rb1.setSelected(true);
                                break;
                            case 1:
                                rb2.setSelected(true);
                                break;
                            case 2:
                                rb3.setSelected(true);
                                break;
                        }
                        final Object[] options = new Object[] { "OK", "Cancel", "Apply" };
                        final JOptionPane optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
                        final JDialog dialog = optionPane.createDialog(MainFrame.getInstance(), "Window Tint");
                        while (true) {
                            dialog.setVisible(true);
                            final Object choice = optionPane.getValue();
                            if (choice == options[1] || choice == null) {
                                break;
                            } else {
                                boolean changed = !Util.isRGBEqual(color, window.getColor());
                                if (rb1.isSelected()) {
                                    // apply to only this window
                                    if (changed) {
                                        final ChangePartColorCommand cmd = new ChangePartColorCommand(window);
                                        window.setColor(color);
                                        SceneManager.getInstance().getUndoManager().addEdit(cmd);
                                    }
                                    selectedScopeIndex = 0;
                                } else if (rb2.isSelected()) {
                                    if (!changed) {
                                        if (window.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) window.getContainer();
                                            for (final Window x : wall.getWindows()) {
                                                if (!Util.isRGBEqual(color, x.getColor())) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        } else if (window.getContainer() instanceof Roof) {
                                            final Roof roof = (Roof) window.getContainer();
                                            for (final Window x : roof.getWindows()) {
                                                if (!Util.isRGBEqual(color, x.getColor())) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeContainerWindowColorCommand cmd = new ChangeContainerWindowColorCommand(window.getContainer());
                                        Scene.getInstance().setWindowColorInContainer(window.getContainer(), color, false);
                                        SceneManager.getInstance().getUndoManager().addEdit(cmd);
                                    }
                                    selectedScopeIndex = 1;
                                } else if (rb3.isSelected()) {
                                    final Foundation foundation = window.getTopContainer();
                                    if (!changed) {
                                        for (final Window x : foundation.getWindows()) {
                                            if (!Util.isRGBEqual(color, x.getColor())) {
                                                changed = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeBuildingColorCommand cmd = new ChangeBuildingColorCommand(window);
                                        Scene.getInstance().setPartColorOfBuilding(window, color);
                                        SceneManager.getInstance().getUndoManager().addEdit(cmd);
                                    }
                                    selectedScopeIndex = 2;
                                }
                                if (changed) {
                                    updateAfterEdit();
                                }
                                if (choice == options[0]) {
                                    break;
                                }
                            }
                        }
                    }
                };
                JColorChooser.createDialog(MainFrame.getInstance(), "Select Tint", true, colorChooser, actionListener, null).setVisible(true);
            }
        });
        final JMenuItem miSize = new JMenuItem("Size...");
        miSize.addActionListener(new ActionListener() {

            // remember the scope selection as the next action will likely be applied to the same scope
            private int selectedScopeIndex = 0;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (!(selectedPart instanceof Window)) {
                    return;
                }
                final Window window = (Window) selectedPart;
                final HousePart container = window.getContainer();
                final Foundation foundation = window.getTopContainer();
                final String partInfo = window.toString().substring(0, selectedPart.toString().indexOf(')') + 1);
                final JPanel gui = new JPanel(new BorderLayout());
                final JPanel inputPanel = new JPanel(new GridLayout(2, 2, 5, 5));
                gui.add(inputPanel, BorderLayout.CENTER);
                inputPanel.add(new JLabel("Width (m): "));
                final JTextField widthField = new JTextField(threeDecimalsFormat.format(window.getWindowWidth()));
                inputPanel.add(widthField);
                inputPanel.add(new JLabel("Height (m): "));
                final JTextField heightField = new JTextField(threeDecimalsFormat.format(window.getWindowHeight()));
                inputPanel.add(heightField);
                inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
                final JPanel scopePanel = new JPanel();
                scopePanel.setLayout(new BoxLayout(scopePanel, BoxLayout.Y_AXIS));
                scopePanel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                final JRadioButton rb1 = new JRadioButton("Only this Window", true);
                final JRadioButton rb2 = new JRadioButton("All Windows on this " + (window.getContainer() instanceof Wall ? "Wall" : "Roof"));
                final JRadioButton rb3 = new JRadioButton("All Windows of this Building");
                scopePanel.add(rb1);
                scopePanel.add(rb2);
                scopePanel.add(rb3);
                final ButtonGroup bg = new ButtonGroup();
                bg.add(rb1);
                bg.add(rb2);
                bg.add(rb3);
                switch(selectedScopeIndex) {
                    case 0:
                        rb1.setSelected(true);
                        break;
                    case 1:
                        rb2.setSelected(true);
                        break;
                    case 2:
                        rb3.setSelected(true);
                        break;
                }
                gui.add(scopePanel, BorderLayout.NORTH);
                final Object[] options = new Object[] { "OK", "Cancel", "Apply" };
                final JOptionPane optionPane = new JOptionPane(new Object[] { "Set Size for " + partInfo, gui }, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
                final JDialog dialog = optionPane.createDialog(MainFrame.getInstance(), "Window Size");
                while (true) {
                    dialog.setVisible(true);
                    final Object choice = optionPane.getValue();
                    if (choice == options[1] || choice == null) {
                        break;
                    } else {
                        boolean ok = true;
                        double w = 0, h = 0;
                        try {
                            w = Double.parseDouble(widthField.getText());
                            h = Double.parseDouble(heightField.getText());
                        } catch (final NumberFormatException x) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE);
                            ok = false;
                        }
                        if (ok) {
                            double wmax = 10;
                            if (container instanceof Wall) {
                                wmax = ((Wall) container).getWallWidth() * 0.99;
                            }
                            double hmax = 10;
                            if (container instanceof Wall) {
                                hmax = ((Wall) container).getWallHeight() * 0.99;
                            }
                            if (w < 0.1 || w > wmax) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Width must be between 0.1 and " + (int) wmax + " m.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else if (h < 0.1 || h > hmax) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Height must be between 0.1 and " + (int) hmax + " m.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                boolean changed = Math.abs(w - window.getWindowWidth()) > 0.000001 || Math.abs(h - window.getWindowHeight()) > 0.000001;
                                if (rb1.isSelected()) {
                                    if (changed) {
                                        final SetPartSizeCommand c = new SetPartSizeCommand(window);
                                        window.setWindowWidth(w);
                                        window.setWindowHeight(h);
                                        window.draw();
                                        window.getContainer().draw();
                                        SceneManager.getInstance().refresh();
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 0;
                                } else if (rb2.isSelected()) {
                                    if (!changed) {
                                        if (window.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) window.getContainer();
                                            for (final Window x : wall.getWindows()) {
                                                if (Math.abs(w - x.getWindowWidth()) > 0.000001 || Math.abs(h - x.getWindowHeight()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        } else if (window.getContainer() instanceof Roof) {
                                            final Roof roof = (Roof) window.getContainer();
                                            for (final Window x : roof.getWindows()) {
                                                if (Math.abs(w - x.getWindowWidth()) > 0.000001 || Math.abs(h - x.getWindowHeight()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final ChangeContainerWindowSizeCommand c = new ChangeContainerWindowSizeCommand(window.getContainer());
                                        Scene.getInstance().setWindowSizeInContainer(window.getContainer(), w, h);
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 1;
                                } else if (rb3.isSelected()) {
                                    if (!changed) {
                                        for (final Window x : foundation.getWindows()) {
                                            if (Math.abs(w - x.getWindowWidth()) > 0.000001 || Math.abs(h - x.getWindowHeight()) > 0.000001) {
                                                changed = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final SetSizeForWindowsOnFoundationCommand c = new SetSizeForWindowsOnFoundationCommand(foundation);
                                        foundation.setSizeForWindows(w, h);
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 2;
                                }
                                if (changed) {
                                    updateAfterEdit();
                                }
                                if (choice == options[0]) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        });
        popupMenuForWindow.addSeparator();
        popupMenuForWindow.add(miSize);
        popupMenuForWindow.add(miTint);
        popupMenuForWindow.add(createInsulationMenuItem(true));
        popupMenuForWindow.add(miShgc);
        popupMenuForWindow.add(muntinMenu);
        popupMenuForWindow.add(shutterMenu);
        popupMenuForWindow.addSeparator();
        JMenuItem mi = new JMenuItem("Daily Energy Analysis...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().adjustCellSize()) {
                    return;
                }
                if (SceneManager.getInstance().getSelectedPart() instanceof Window) {
                    new EnergyDailyAnalysis().show("Daily Energy for Window");
                }
            }
        });
        popupMenuForWindow.add(mi);
        mi = new JMenuItem("Annual Energy Analysis...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().adjustCellSize()) {
                    return;
                }
                if (SceneManager.getInstance().getSelectedPart() instanceof Window) {
                    new EnergyAnnualAnalysis().show("Annual Energy for Window");
                }
            }
        });
        popupMenuForWindow.add(mi);
    }
    return popupMenuForWindow;
}
Also used : ReadOnlyColorRGBA(com.ardor3d.math.type.ReadOnlyColorRGBA) JPanel(javax.swing.JPanel) ChangeContainerShutterColorCommand(org.concord.energy3d.undo.ChangeContainerShutterColorCommand) ActionEvent(java.awt.event.ActionEvent) SetPartSizeCommand(org.concord.energy3d.undo.SetPartSizeCommand) ChangeBuildingWindowShgcCommand(org.concord.energy3d.undo.ChangeBuildingWindowShgcCommand) ChangeContainerWindowShgcCommand(org.concord.energy3d.undo.ChangeContainerWindowShgcCommand) BorderLayout(java.awt.BorderLayout) Foundation(org.concord.energy3d.model.Foundation) HousePart(org.concord.energy3d.model.HousePart) SetSizeForWindowsOnFoundationCommand(org.concord.energy3d.undo.SetSizeForWindowsOnFoundationCommand) Color(java.awt.Color) ChangeWindowShgcCommand(org.concord.energy3d.undo.ChangeWindowShgcCommand) ChangeWindowShuttersCommand(org.concord.energy3d.undo.ChangeWindowShuttersCommand) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) JOptionPane(javax.swing.JOptionPane) JCheckBoxMenuItem(javax.swing.JCheckBoxMenuItem) ActionListener(java.awt.event.ActionListener) ChangeBuildingColorCommand(org.concord.energy3d.undo.ChangeBuildingColorCommand) JColorChooser(javax.swing.JColorChooser) JDialog(javax.swing.JDialog) EnergyAnnualAnalysis(org.concord.energy3d.simulation.EnergyAnnualAnalysis) ItemEvent(java.awt.event.ItemEvent) Wall(org.concord.energy3d.model.Wall) JRadioButton(javax.swing.JRadioButton) MenuListener(javax.swing.event.MenuListener) BoxLayout(javax.swing.BoxLayout) JTextField(javax.swing.JTextField) Roof(org.concord.energy3d.model.Roof) ChangeContainerWindowColorCommand(org.concord.energy3d.undo.ChangeContainerWindowColorCommand) GridLayout(java.awt.GridLayout) ChangePartColorCommand(org.concord.energy3d.undo.ChangePartColorCommand) JMenuItem(javax.swing.JMenuItem) MenuEvent(javax.swing.event.MenuEvent) Window(org.concord.energy3d.model.Window) ChangeShutterColorCommand(org.concord.energy3d.undo.ChangeShutterColorCommand) JLabel(javax.swing.JLabel) ChangeShutterLengthCommand(org.concord.energy3d.undo.ChangeShutterLengthCommand) ReadOnlyColorRGBA(com.ardor3d.math.type.ReadOnlyColorRGBA) ColorRGBA(com.ardor3d.math.ColorRGBA) ButtonGroup(javax.swing.ButtonGroup) EnergyDailyAnalysis(org.concord.energy3d.simulation.EnergyDailyAnalysis) ItemListener(java.awt.event.ItemListener) ChangeBuildingShutterColorCommand(org.concord.energy3d.undo.ChangeBuildingShutterColorCommand) ChangeContainerWindowSizeCommand(org.concord.energy3d.undo.ChangeContainerWindowSizeCommand) JMenu(javax.swing.JMenu)

Example 3 with MenuListener

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

the class PopupMenuForDoor method getPopupMenu.

static JPopupMenu getPopupMenu() {
    if (popupMenuForDoor == null) {
        final JMenuItem miSize = new JMenuItem("Size...");
        miSize.addActionListener(new ActionListener() {

            // remember the scope selection as the next action will likely be applied to the same scope
            private int selectedScopeIndex = 0;

            @Override
            public void actionPerformed(final ActionEvent e) {
                final HousePart selectedPart = SceneManager.getInstance().getSelectedPart();
                if (!(selectedPart instanceof Door)) {
                    return;
                }
                final Door door = (Door) selectedPart;
                final HousePart container = door.getContainer();
                final Foundation foundation = door.getTopContainer();
                final String partInfo = door.toString().substring(0, selectedPart.toString().indexOf(')') + 1);
                final JPanel gui = new JPanel(new BorderLayout());
                final JPanel inputPanel = new JPanel(new GridLayout(2, 2, 5, 5));
                gui.add(inputPanel, BorderLayout.CENTER);
                inputPanel.add(new JLabel("Width (m): "));
                final JTextField widthField = new JTextField(threeDecimalsFormat.format(door.getDoorWidth()));
                inputPanel.add(widthField);
                inputPanel.add(new JLabel("Height (m): "));
                final JTextField heightField = new JTextField(threeDecimalsFormat.format(door.getDoorHeight()));
                inputPanel.add(heightField);
                inputPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
                final JPanel scopePanel = new JPanel();
                scopePanel.setLayout(new BoxLayout(scopePanel, BoxLayout.Y_AXIS));
                scopePanel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                final JRadioButton rb1 = new JRadioButton("Only this Door", true);
                final JRadioButton rb2 = new JRadioButton("All Doors on this Wall");
                final JRadioButton rb3 = new JRadioButton("All Doors of this Building");
                scopePanel.add(rb1);
                scopePanel.add(rb2);
                scopePanel.add(rb3);
                final ButtonGroup bg = new ButtonGroup();
                bg.add(rb1);
                bg.add(rb2);
                bg.add(rb3);
                switch(selectedScopeIndex) {
                    case 0:
                        rb1.setSelected(true);
                        break;
                    case 1:
                        rb2.setSelected(true);
                        break;
                    case 2:
                        rb3.setSelected(true);
                        break;
                }
                gui.add(scopePanel, BorderLayout.NORTH);
                final Object[] options = new Object[] { "OK", "Cancel", "Apply" };
                final JOptionPane optionPane = new JOptionPane(new Object[] { "Set Size for " + partInfo, gui }, JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_CANCEL_OPTION, null, options, options[2]);
                final JDialog dialog = optionPane.createDialog(MainFrame.getInstance(), "Door Size");
                while (true) {
                    dialog.setVisible(true);
                    final Object choice = optionPane.getValue();
                    if (choice == options[1] || choice == null) {
                        break;
                    } else {
                        boolean ok = true;
                        double w = 0, h = 0;
                        try {
                            w = Double.parseDouble(widthField.getText());
                            h = Double.parseDouble(heightField.getText());
                        } catch (final NumberFormatException x) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), "Invalid input!", "Error", JOptionPane.ERROR_MESSAGE);
                            ok = false;
                        }
                        if (ok) {
                            double wmax = 10;
                            if (container instanceof Wall) {
                                wmax = ((Wall) container).getWallWidth() * 0.99;
                            }
                            double hmax = 10;
                            if (container instanceof Wall) {
                                hmax = ((Wall) container).getWallHeight() * 0.99;
                            }
                            if (w < 0.1 || w > wmax) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Width must be between 0.1 and " + (int) wmax + " m.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else if (h < 0.1 || h > hmax) {
                                JOptionPane.showMessageDialog(MainFrame.getInstance(), "Height must be between 0.1 and " + (int) hmax + " m.", "Range Error", JOptionPane.ERROR_MESSAGE);
                            } else {
                                boolean changed = Math.abs(w - door.getDoorWidth()) > 0.000001 || Math.abs(h - door.getDoorHeight()) > 0.000001;
                                if (rb1.isSelected()) {
                                    if (changed) {
                                        final SetPartSizeCommand c = new SetPartSizeCommand(door);
                                        door.setDoorWidth(w);
                                        door.setDoorHeight(h);
                                        door.draw();
                                        door.getContainer().draw();
                                        SceneManager.getInstance().refresh();
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 0;
                                } else if (rb2.isSelected()) {
                                    if (!changed) {
                                        if (door.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) door.getContainer();
                                            for (final Door x : wall.getDoors()) {
                                                if (Math.abs(w - x.getDoorWidth()) > 0.000001 || Math.abs(h - x.getDoorHeight()) > 0.000001) {
                                                    changed = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                    if (changed) {
                                        if (door.getContainer() instanceof Wall) {
                                            final Wall wall = (Wall) door.getContainer();
                                            final ChangeDoorSizeOnWallCommand c = new ChangeDoorSizeOnWallCommand(wall);
                                            wall.setDoorSize(w, h);
                                            SceneManager.getInstance().getUndoManager().addEdit(c);
                                        }
                                    }
                                    selectedScopeIndex = 1;
                                } else if (rb3.isSelected()) {
                                    if (!changed) {
                                        for (final Door x : foundation.getDoors()) {
                                            if (Math.abs(w - x.getDoorWidth()) > 0.000001 || Math.abs(h - x.getDoorHeight()) > 0.000001) {
                                                changed = true;
                                                break;
                                            }
                                        }
                                    }
                                    if (changed) {
                                        final SetSizeForDoorsOnFoundationCommand c = new SetSizeForDoorsOnFoundationCommand(foundation);
                                        foundation.setSizeForDoors(w, h);
                                        SceneManager.getInstance().getUndoManager().addEdit(c);
                                    }
                                    selectedScopeIndex = 2;
                                }
                                if (changed) {
                                    updateAfterEdit();
                                }
                                if (choice == options[0]) {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        });
        final JMenu textureMenu = new JMenu("Texture");
        final ButtonGroup textureGroup = new ButtonGroup();
        final JRadioButtonMenuItem rbmiTextureNone = new JRadioButtonMenuItem("No Texture");
        rbmiTextureNone.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final ChangeBuildingTextureCommand c = new ChangeBuildingTextureCommand();
                    Scene.getInstance().setTextureMode(TextureMode.None);
                    Scene.getInstance().setEdited(true);
                    if (MainPanel.getInstance().getEnergyButton().isSelected()) {
                        MainPanel.getInstance().getEnergyButton().setSelected(false);
                    }
                    SceneManager.getInstance().getUndoManager().addEdit(c);
                }
            }
        });
        textureGroup.add(rbmiTextureNone);
        textureMenu.add(rbmiTextureNone);
        final JRadioButtonMenuItem rbmiTextureOutline = new JRadioButtonMenuItem("Outline Texture");
        rbmiTextureOutline.addItemListener(new ItemListener() {

            @Override
            public void itemStateChanged(final ItemEvent e) {
                if (e.getStateChange() == ItemEvent.SELECTED) {
                    final ChangeBuildingTextureCommand c = new ChangeBuildingTextureCommand();
                    Scene.getInstance().setTextureMode(TextureMode.Simple);
                    Scene.getInstance().setEdited(true);
                    if (MainPanel.getInstance().getEnergyButton().isSelected()) {
                        MainPanel.getInstance().getEnergyButton().setSelected(false);
                    }
                    SceneManager.getInstance().getUndoManager().addEdit(c);
                }
            }
        });
        textureGroup.add(rbmiTextureOutline);
        textureMenu.add(rbmiTextureOutline);
        textureMenu.addSeparator();
        final JRadioButtonMenuItem rbmiTexture01 = MainFrame.getInstance().createWallTextureMenuItem(Door.TEXTURE_01, "icons/door_01.png");
        textureGroup.add(rbmiTexture01);
        textureMenu.add(rbmiTexture01);
        textureMenu.addMenuListener(new MenuListener() {

            @Override
            public void menuSelected(final MenuEvent e) {
                if (Scene.getInstance().getTextureMode() == TextureMode.None) {
                    Util.selectSilently(rbmiTextureNone, true);
                    return;
                }
                if (Scene.getInstance().getTextureMode() == TextureMode.Simple) {
                    Util.selectSilently(rbmiTextureOutline, true);
                    return;
                }
                switch(Scene.getInstance().getWallTextureType()) {
                    case Door.TEXTURE_01:
                        Util.selectSilently(rbmiTexture01, true);
                        break;
                }
            }

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

            @Override
            public void menuCanceled(final MenuEvent e) {
                textureMenu.setEnabled(true);
            }
        });
        popupMenuForDoor = createPopupMenu(false, false, null);
        popupMenuForDoor.addSeparator();
        popupMenuForDoor.add(miSize);
        popupMenuForDoor.add(colorAction);
        popupMenuForDoor.add(textureMenu);
        popupMenuForDoor.add(createInsulationMenuItem(true));
        popupMenuForDoor.add(createVolumetricHeatCapacityMenuItem());
        popupMenuForDoor.addSeparator();
        JMenuItem mi = new JMenuItem("Daily Energy Analysis...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().adjustCellSize()) {
                    return;
                }
                if (SceneManager.getInstance().getSelectedPart() instanceof Door) {
                    new EnergyDailyAnalysis().show("Daily Energy for Door");
                }
            }
        });
        popupMenuForDoor.add(mi);
        mi = new JMenuItem("Annual Energy Analysis...");
        mi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(final ActionEvent e) {
                if (EnergyPanel.getInstance().adjustCellSize()) {
                    return;
                }
                if (SceneManager.getInstance().getSelectedPart() instanceof Door) {
                    new EnergyAnnualAnalysis().show("Annual Energy for Door");
                }
            }
        });
        popupMenuForDoor.add(mi);
    }
    return popupMenuForDoor;
}
Also used : JPanel(javax.swing.JPanel) EnergyAnnualAnalysis(org.concord.energy3d.simulation.EnergyAnnualAnalysis) ItemEvent(java.awt.event.ItemEvent) JRadioButton(javax.swing.JRadioButton) Wall(org.concord.energy3d.model.Wall) ActionEvent(java.awt.event.ActionEvent) SetPartSizeCommand(org.concord.energy3d.undo.SetPartSizeCommand) MenuListener(javax.swing.event.MenuListener) BoxLayout(javax.swing.BoxLayout) JTextField(javax.swing.JTextField) GridLayout(java.awt.GridLayout) BorderLayout(java.awt.BorderLayout) ChangeDoorSizeOnWallCommand(org.concord.energy3d.undo.ChangeDoorSizeOnWallCommand) Foundation(org.concord.energy3d.model.Foundation) JMenuItem(javax.swing.JMenuItem) HousePart(org.concord.energy3d.model.HousePart) MenuEvent(javax.swing.event.MenuEvent) JLabel(javax.swing.JLabel) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) SetSizeForDoorsOnFoundationCommand(org.concord.energy3d.undo.SetSizeForDoorsOnFoundationCommand) JOptionPane(javax.swing.JOptionPane) Door(org.concord.energy3d.model.Door) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) EnergyDailyAnalysis(org.concord.energy3d.simulation.EnergyDailyAnalysis) ItemListener(java.awt.event.ItemListener) ChangeBuildingTextureCommand(org.concord.energy3d.undo.ChangeBuildingTextureCommand) JDialog(javax.swing.JDialog) JMenu(javax.swing.JMenu)

Example 4 with MenuListener

use of javax.swing.event.MenuListener 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 5 with MenuListener

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

Aggregations

MenuListener (javax.swing.event.MenuListener)40 MenuEvent (javax.swing.event.MenuEvent)39 JMenu (javax.swing.JMenu)35 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 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 JMenuBar (javax.swing.JMenuBar)7 JTextField (javax.swing.JTextField)6 BoxLayout (javax.swing.BoxLayout)5 JRadioButton (javax.swing.JRadioButton)5