Newer
Older
AlgebraicDataflowArchitectureModel / AlgebraicDataflowArchitectureModel / src / application / views / NavigationWindow.java
Shohei Yamagiwa 12 days ago 5 KB Fix grid layout
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 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 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");
		controlFlowDelegationButton = new JToggleButton("Control Flow Modeling");
		dataFlowModelingButton.addActionListener(new DataFlowModelingButtonListener());
		pushPullSelectionButton.addActionListener(new PushPullSelectionButtonListener());
		controlFlowDelegationButton.addActionListener(new ControlFlowDelegationButtonListener());
		dataFlowModelingButton.setSelected(true);
		pushPullSelectionButton.setEnabled(false);
		controlFlowDelegationButton.setEnabled(false);
		
		setTitle(TITLE);
		setDefaultCloseOperation(HIDE_ON_CLOSE);
		Container panel = getContentPane();
		panel.setLayout(new GridLayout(3, 1));
		
		ButtonGroup group = new ButtonGroup();
		group.add(dataFlowModelingButton);
		group.add(pushPullSelectionButton);
		group.add(controlFlowDelegationButton);
		panel.add(dataFlowModelingButton);
		panel.add(pushPullSelectionButton);
		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);
			
			if (editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION)) {
				pushPullSelectionButton.setEnabled(true);
			} else {
				pushPullSelectionButton.setEnabled(false);
			}
			if (editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION)) {
				controlFlowDelegationButton.setEnabled(true);
			} else {
				controlFlowDelegationButton.setEnabled(false);
			}
		} else if (newStage instanceof PushPullSelectionStage) {
			pushPullSelectionButton.setSelected(true);
			
			if (editor.canChange(Editor.STAGE_DATA_FLOW_MODELING)) {
				dataFlowModelingButton.setEnabled(true);
			} else {
				dataFlowModelingButton.setEnabled(false);
			}
			if (editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION)) {
				controlFlowDelegationButton.setEnabled(true);
			} else {
				controlFlowDelegationButton.setEnabled(false);
			}
		} else if (newStage instanceof ControlFlowModelingStage) {
			controlFlowDelegationButton.setSelected(true);
			
			if (editor.canChange(Editor.STAGE_DATA_FLOW_MODELING)) {
				dataFlowModelingButton.setEnabled(true);
			} else {
				dataFlowModelingButton.setEnabled(false);
			}
			if (editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION)) {
				pushPullSelectionButton.setEnabled(true);
			} else {
				pushPullSelectionButton.setEnabled(false);
			}
		}
	}
	
	private class DataFlowModelingButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			forbidReentry = true;
			editor.changeStage(Editor.STAGE_DATA_FLOW_MODELING);
			forbidReentry = false;
			
			if (editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION)) {
				pushPullSelectionButton.setEnabled(true);
			} else {
				pushPullSelectionButton.setEnabled(false);
			}
			if (editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION)) {
				controlFlowDelegationButton.setEnabled(true);
			} else {
				controlFlowDelegationButton.setEnabled(false);
			}
		}
	}
	
	private class PushPullSelectionButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			forbidReentry = true;
			editor.changeStage(Editor.STAGE_PUSH_PULL_SELECTION);
			forbidReentry = false;
			
			if (editor.canChange(Editor.STAGE_DATA_FLOW_MODELING)) {
				dataFlowModelingButton.setEnabled(true);
			} else {
				dataFlowModelingButton.setEnabled(false);
			}
			if (editor.canChange(Editor.STAGE_CONTROL_FLOW_DELEGATION)) {
				controlFlowDelegationButton.setEnabled(true);
			} else {
				controlFlowDelegationButton.setEnabled(false);
			}
		}
	}
	
	private class ControlFlowDelegationButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			forbidReentry = true;
			editor.changeStage(Editor.STAGE_CONTROL_FLOW_DELEGATION);
			forbidReentry = false;
			
			if (editor.canChange(Editor.STAGE_DATA_FLOW_MODELING)) {
				dataFlowModelingButton.setEnabled(true);
			} else {
				dataFlowModelingButton.setEnabled(false);
			}
			if (editor.canChange(Editor.STAGE_PUSH_PULL_SELECTION)) {
				pushPullSelectionButton.setEnabled(true);
			} else {
				pushPullSelectionButton.setEnabled(false);
			}
		}
	}
}