Search in sources :

Example 1 with ChangeBuildingUValueCommand

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

the class PopupMenuFactory method createInsulationMenuItem.

static JMenuItem createInsulationMenuItem(final boolean useUValue) {
    final JMenuItem mi = new JMenuItem("Insulation...");
    mi.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 Thermal)) {
                return;
            }
            final String partInfo = selectedPart.toString().substring(0, selectedPart.toString().indexOf(')') + 1);
            final Thermal t = (Thermal) selectedPart;
            final JPanel panel = new JPanel();
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
            String s;
            if (useUValue) {
                if (selectedPart instanceof Door) {
                    s = "<html>U-Value of " + partInfo + "<hr><font size=2>Examples:<br>US 1.20 (uninsulated metal), US 0.60 (insulated metal), US 0.50 (wood)</html>";
                } else {
                    s = "<html>U-Value of " + partInfo + "<hr><font size=2>Examples:<br>US 1.30 (single glass), US 0.48 (double glass), US 0.25 (triple glass)</html>";
                }
            } else {
                s = "<html>Insulation Value of " + partInfo + "<hr><font size=2>Examples:<br>US R13 (cellulose, 3.5\"), US R16 (mineral wool, 5.25\"), US R31 (fiberglass, 10\")</html>";
            }
            final JLabel label = new JLabel(s);
            label.setAlignmentX(Component.LEFT_ALIGNMENT);
            panel.add(label);
            panel.add(Box.createVerticalStrut(15));
            final String partName = selectedPart.getClass().getSimpleName();
            final JRadioButton rb1 = new JRadioButton("Only this " + partName, true);
            final JRadioButton rb2 = new JRadioButton("All " + partName + "s of this Building");
            if (selectedPart instanceof Wall || selectedPart instanceof Window) {
                final JPanel scopePanel = new JPanel();
                scopePanel.setAlignmentX(Component.LEFT_ALIGNMENT);
                scopePanel.setLayout(new BoxLayout(scopePanel, BoxLayout.Y_AXIS));
                scopePanel.setBorder(BorderFactory.createTitledBorder("Apply to:"));
                scopePanel.add(rb1);
                scopePanel.add(rb2);
                final ButtonGroup bg = new ButtonGroup();
                bg.add(rb1);
                bg.add(rb2);
                panel.add(scopePanel);
                panel.add(Box.createVerticalStrut(5));
                switch(selectedScopeIndex) {
                    case 0:
                        rb1.setSelected(true);
                        break;
                    case 1:
                        rb2.setSelected(true);
                        break;
                }
            }
            final JPanel unitPanel = new JPanel(new GridLayout(2, 3, 5, 5));
            unitPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
            panel.add(unitPanel);
            unitPanel.add(new JLabel("U-Value in SI Unit:"));
            final JTextField siField = new JTextField("" + t.getUValue(), 10);
            unitPanel.add(siField);
            unitPanel.add(new JLabel("<html>W/(m<sup>2</sup>&middot;&deg;C)</html>"));
            if (useUValue) {
                unitPanel.add(new JLabel("U-Value in US Unit:"));
                final JTextField uValueField = new JTextField(threeDecimalsFormat.format(Util.toUsUValue(t.getUValue())), 10);
                uValueField.setAlignmentX(Component.LEFT_ALIGNMENT);
                unitPanel.add(uValueField);
                unitPanel.add(new JLabel("<html>Btu/(h&middot;ft<sup>2</sup>&middot;&deg;F)</html>"));
                siField.getDocument().addDocumentListener(new DocumentListener() {

                    private void update() {
                        if (!siField.hasFocus()) {
                            return;
                        }
                        final String newValue = siField.getText();
                        if ("".equals(newValue)) {
                            return;
                        }
                        try {
                            uValueField.setText(threeDecimalsFormat.format(Util.toUsUValue(Double.parseDouble(newValue))));
                        } catch (final Exception exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                    @Override
                    public void removeUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void insertUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void changedUpdate(final DocumentEvent e) {
                        update();
                    }
                });
                uValueField.getDocument().addDocumentListener(new DocumentListener() {

                    private void update() {
                        if (!uValueField.hasFocus()) {
                            return;
                        }
                        final String newValue = uValueField.getText();
                        if ("".equals(newValue)) {
                            return;
                        }
                        try {
                            siField.setText(threeDecimalsFormat.format(1.0 / (Util.toSiRValue(1.0 / Double.parseDouble(newValue)))));
                        } catch (final Exception exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                    @Override
                    public void removeUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void insertUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void changedUpdate(final DocumentEvent e) {
                        update();
                    }
                });
            } else {
                unitPanel.add(new JLabel("R-Value in US Unit:"));
                final JTextField rValueField = new JTextField(integerFormat.format(Util.toUsRValue(t.getUValue())), 10);
                rValueField.setAlignmentX(Component.LEFT_ALIGNMENT);
                unitPanel.add(rValueField);
                unitPanel.add(new JLabel("<html>h&middot;ft<sup>2</sup>&middot;&deg;F/Btu</html>"));
                siField.getDocument().addDocumentListener(new DocumentListener() {

                    private void update() {
                        if (!siField.hasFocus()) {
                            return;
                        }
                        final String newValue = siField.getText();
                        if ("".equals(newValue)) {
                            return;
                        }
                        try {
                            rValueField.setText(integerFormat.format(Util.toUsRValue(Double.parseDouble(newValue))));
                        } catch (final Exception exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                    @Override
                    public void removeUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void insertUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void changedUpdate(final DocumentEvent e) {
                        update();
                    }
                });
                rValueField.getDocument().addDocumentListener(new DocumentListener() {

                    private void update() {
                        if (!rValueField.hasFocus()) {
                            return;
                        }
                        final String newValue = rValueField.getText();
                        if ("".equals(newValue)) {
                            return;
                        }
                        try {
                            siField.setText(threeDecimalsFormat.format(1.0 / Util.toSiRValue(Double.parseDouble(newValue))));
                        } catch (final Exception exception) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                        }
                    }

                    @Override
                    public void removeUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void insertUpdate(final DocumentEvent e) {
                        update();
                    }

                    @Override
                    public void changedUpdate(final DocumentEvent e) {
                        update();
                    }
                });
            }
            while (true) {
                if (JOptionPane.showConfirmDialog(MainFrame.getInstance(), panel, "Input: " + partInfo, JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
                    final String newValue = siField.getText();
                    try {
                        final double val = Double.parseDouble(newValue);
                        if (val <= 0) {
                            JOptionPane.showMessageDialog(MainFrame.getInstance(), "U-value must be positive.", "Range Error", JOptionPane.ERROR_MESSAGE);
                        } else {
                            boolean changed = val != t.getUValue();
                            if (rb1.isSelected()) {
                                if (changed) {
                                    final ChangePartUValueCommand c = new ChangePartUValueCommand(selectedPart);
                                    t.setUValue(val);
                                    SceneManager.getInstance().getUndoManager().addEdit(c);
                                }
                                selectedScopeIndex = 0;
                            } else {
                                if (!changed) {
                                    for (final HousePart x : Scene.getInstance().getPartsOfSameTypeInBuilding(selectedPart)) {
                                        if (val != ((Thermal) x).getUValue()) {
                                            changed = true;
                                            break;
                                        }
                                    }
                                }
                                if (changed) {
                                    final ChangeBuildingUValueCommand c = new ChangeBuildingUValueCommand(selectedPart);
                                    Scene.getInstance().setUValuesOfSameTypeInBuilding(selectedPart, val);
                                    SceneManager.getInstance().getUndoManager().addEdit(c);
                                }
                                selectedScopeIndex = 1;
                            }
                            if (changed) {
                                updateAfterEdit();
                                EnergyPanel.getInstance().getBuildingCostGraph().updateBudget();
                            }
                            break;
                        }
                    } catch (final NumberFormatException exception) {
                        JOptionPane.showMessageDialog(MainFrame.getInstance(), newValue + " is an invalid value!", "Error", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    break;
                }
            }
        }
    });
    return mi;
}
Also used : Window(org.concord.energy3d.model.Window) JPanel(javax.swing.JPanel) DocumentListener(javax.swing.event.DocumentListener) JRadioButton(javax.swing.JRadioButton) Wall(org.concord.energy3d.model.Wall) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) JLabel(javax.swing.JLabel) ChangePartUValueCommand(org.concord.energy3d.undo.ChangePartUValueCommand) JTextField(javax.swing.JTextField) DocumentEvent(javax.swing.event.DocumentEvent) Door(org.concord.energy3d.model.Door) ChangeBuildingUValueCommand(org.concord.energy3d.undo.ChangeBuildingUValueCommand) GridLayout(java.awt.GridLayout) ActionListener(java.awt.event.ActionListener) ButtonGroup(javax.swing.ButtonGroup) JMenuItem(javax.swing.JMenuItem) HousePart(org.concord.energy3d.model.HousePart) Thermal(org.concord.energy3d.model.Thermal)

Aggregations

GridLayout (java.awt.GridLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 BoxLayout (javax.swing.BoxLayout)1 ButtonGroup (javax.swing.ButtonGroup)1 JLabel (javax.swing.JLabel)1 JMenuItem (javax.swing.JMenuItem)1 JPanel (javax.swing.JPanel)1 JRadioButton (javax.swing.JRadioButton)1 JTextField (javax.swing.JTextField)1 DocumentEvent (javax.swing.event.DocumentEvent)1 DocumentListener (javax.swing.event.DocumentListener)1 Door (org.concord.energy3d.model.Door)1 HousePart (org.concord.energy3d.model.HousePart)1 Thermal (org.concord.energy3d.model.Thermal)1 Wall (org.concord.energy3d.model.Wall)1 Window (org.concord.energy3d.model.Window)1 ChangeBuildingUValueCommand (org.concord.energy3d.undo.ChangeBuildingUValueCommand)1 ChangePartUValueCommand (org.concord.energy3d.undo.ChangePartUValueCommand)1