Search in sources :

Example 1 with ChangeDoorSizeOnWallCommand

use of org.concord.energy3d.undo.ChangeDoorSizeOnWallCommand 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)

Aggregations

BorderLayout (java.awt.BorderLayout)1 GridLayout (java.awt.GridLayout)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 BoxLayout (javax.swing.BoxLayout)1 ButtonGroup (javax.swing.ButtonGroup)1 JDialog (javax.swing.JDialog)1 JLabel (javax.swing.JLabel)1 JMenu (javax.swing.JMenu)1 JMenuItem (javax.swing.JMenuItem)1 JOptionPane (javax.swing.JOptionPane)1 JPanel (javax.swing.JPanel)1 JRadioButton (javax.swing.JRadioButton)1 JRadioButtonMenuItem (javax.swing.JRadioButtonMenuItem)1 JTextField (javax.swing.JTextField)1 MenuEvent (javax.swing.event.MenuEvent)1 MenuListener (javax.swing.event.MenuListener)1 Door (org.concord.energy3d.model.Door)1