Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / application / views / NavigationWindow.java
package application.views;

import application.ApplicationWindow;
import application.editor.Editor;
import application.editor.IStageChangeListener;
import application.editor.Stage;
import application.editor.stages.ControlFlowModelingStage;
import application.editor.stages.DataFlowModelingStage;
import application.editor.stages.PushPullSelectionStage;
import application.editor.stages.DependencyModelingStage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class NavigationWindow extends JDialog implements IStageChangeListener {

    private static final String TITLE = "Navigation";
    private final Editor editor;

    private final JToggleButton dataFlowModelingButton;
    private final JToggleButton pushPullSelectionButton;
    private final JToggleButton dependencyModelingButton;
    private final JToggleButton controlFlowDelegationButton;

    private boolean forbidReentry = false;

    public NavigationWindow(ApplicationWindow owner, Editor editor) {
        super(owner);
        this.editor = editor;
        dataFlowModelingButton = new JToggleButton("Data Flow Modeling");
        pushPullSelectionButton = new JToggleButton("Push Pull Selection");
        dependencyModelingButton = new JToggleButton("Dependency Modeling");
        controlFlowDelegationButton = new JToggleButton("Control Flow Modeling");

        dataFlowModelingButton.addActionListener(new DataFlowModelingButtonListener());
        pushPullSelectionButton.addActionListener(new PushPullSelectionButtonListener());
        dependencyModelingButton.addActionListener(new DependencyModelingButtonListener());
        controlFlowDelegationButton.addActionListener(new ControlFlowDelegationButtonListener());

        dataFlowModelingButton.setSelected(true);
        pushPullSelectionButton.setEnabled(true);
        dependencyModelingButton.setEnabled(false);
        controlFlowDelegationButton.setEnabled(false);

        setTitle(TITLE);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
        Container panel = getContentPane();
        panel.setLayout(new GridLayout(4, 1));

        ButtonGroup group = new ButtonGroup();
        group.add(dataFlowModelingButton);
        group.add(pushPullSelectionButton);
        group.add(dependencyModelingButton);
        group.add(controlFlowDelegationButton);
        panel.add(dataFlowModelingButton);
        panel.add(pushPullSelectionButton);
        panel.add(dependencyModelingButton);
        panel.add(controlFlowDelegationButton);

        pack();

        Point location = new Point(owner.getX() + (owner.getWidth() / 2) - (this.getWidth() / 2), owner.getY() + (owner.getHeight() / 2) - (this.getHeight() / 2));
        setLocation(location);

        setResizable(false);
    }

    @Override
    public void stageChanged(Stage newStage) {
        if (forbidReentry) {
            return;
        }

        if (newStage instanceof DataFlowModelingStage) {
            dataFlowModelingButton.setSelected(true);
            pushPullSelectionButton.setEnabled(editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION));
            dependencyModelingButton.setEnabled(false);
            controlFlowDelegationButton.setEnabled(editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION));

        } else if (newStage instanceof PushPullSelectionStage) {
            pushPullSelectionButton.setSelected(true);
            dataFlowModelingButton.setEnabled(true);
            dependencyModelingButton.setEnabled(true);
            controlFlowDelegationButton.setEnabled(editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION));

        } else if (newStage instanceof DependencyModelingStage) {
            dependencyModelingButton.setSelected(true);
            dataFlowModelingButton.setEnabled(true);
            pushPullSelectionButton.setEnabled(true);
            controlFlowDelegationButton.setEnabled(editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION));

        } else if (newStage instanceof ControlFlowModelingStage) {
            controlFlowDelegationButton.setSelected(true);
            dataFlowModelingButton.setEnabled(true);
            pushPullSelectionButton.setEnabled(editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION));
            dependencyModelingButton.setEnabled(true);
        }
    }

    private class DataFlowModelingButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            forbidReentry = true;
            editor.changeStage(Editor.STAGE_DATA_FLOW_MODELING);
            forbidReentry = false;
            // DataFlow → PushPullのみ有効
            dataFlowModelingButton.setEnabled(true);
            pushPullSelectionButton.setEnabled(editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION));
            dependencyModelingButton.setEnabled(false);
            controlFlowDelegationButton.setEnabled(editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION));
        }
    }

    private class PushPullSelectionButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            forbidReentry = true;
            editor.changeStage(Editor.STAGE_PUSH_PULL_SELECTION);
            forbidReentry = false;
            dataFlowModelingButton.setEnabled(true);
            pushPullSelectionButton.setEnabled(true);
            dependencyModelingButton.setEnabled(true);
            controlFlowDelegationButton.setEnabled(editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION));
        }
    }

    private class DependencyModelingButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            forbidReentry = true;
            editor.changeStage(Editor.STAGE_DEPENDENCY_MODELING);
            forbidReentry = false;
            dataFlowModelingButton.setEnabled(true);
            pushPullSelectionButton.setEnabled(true);
            dependencyModelingButton.setEnabled(true);
            controlFlowDelegationButton.setEnabled(editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION));
        }
    }

    private class ControlFlowDelegationButtonListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            forbidReentry = true;
            editor.changeStage(Editor.STAGE_CONTROL_FLOW_DELEGATION);
            forbidReentry = false;
            dataFlowModelingButton.setEnabled(true);
            pushPullSelectionButton.setEnabled(editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION));
            dependencyModelingButton.setEnabled(true);
            controlFlowDelegationButton.setEnabled(true);
        }
    }
}