ProjektGraph/OurApplication/OurHybridWindow.java

352 lines
12 KiB
Java
Raw Permalink Normal View History

2024-07-02 19:13:30 +00:00
package OurApplication;
2024-07-07 17:49:31 +00:00
import logging.Algorithm;
import logging.LogElement;
import logging.LogElementList;
2024-07-07 17:49:31 +00:00
import visualisation.*;
import visualisation.TextArea;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
/**
2024-07-08 22:17:31 +00:00
* This class provides an example for using visualization.HybridWindow.
* It extends HybridWindow to integrate graphical components with algorithmic operations.
*
* @param <DRAW> Type of the draw area used in the hybrid window.
* @param <TEXT> Type of the text area used in the hybrid window.
* @param <PARAM> Type of the parameter area used in the hybrid window.
* @param <ALGORITHM> Type of the algorithm used in the hybrid window.
* @param <LOG> Type of logging elements used in the hybrid window.
* @param <LEGEND> Type of legend area used in the hybrid window.
* @param <METHODBUTTONS> Type of method buttons used in the hybrid window.
*
2024-07-02 19:13:30 +00:00
* @see HybridWindow
* @see OurApplication
*/
2024-07-07 17:49:31 +00:00
public class OurHybridWindow<DRAW extends DrawArea,
TEXT extends TextArea,
PARAM extends ParameterArea,
ALGORITHM extends Algorithm,
LOG extends LogElement,
LEGEND extends LegendArea,
2024-07-08 22:17:31 +00:00
METHODBUTTONS extends ParameterArea> extends HybridWindow {
2024-07-07 17:49:31 +00:00
protected METHODBUTTONS methodButtons;
protected StartListener startListener;
/** Action listener for stop button. */
protected StopListener stopListener;
/** Action listener for previous button. */
protected PrevListener prevListener;
/** Action listener for next button. */
protected NextListener nextListener;
/** Action listener for first button. */
protected FirstListener firstListener;
/** Action listener for last button. */
protected LastListener lastListener;
/** Item listener for auto button. */
protected AutoButtonListener autoButtonListener;
/** Change listener for auto slider. */
protected AutoSliderListener autoSliderListener;
/** Item listener for forward button. */
protected AutoForwardListener autoForwardListener;
/** Item listener for backward button. */
protected AutoBackwardListener autoBackwardListener;
/** Item listener for pause button. */
protected PauseButtonListener pauseButtonListener;
/**
* Overwritten standard constructor.
2024-07-08 22:17:31 +00:00
* Creates a HybridWindow with specified draw area, text area, parameter area,
* algorithm, log list, legend area, and method buttons.
*
* @param drawArea the draw area used in the hybrid window
* @param textArea the text area used in the hybrid window
* @param parameterArea the parameter area used in the hybrid window
* @param algorithm the algorithm used in the hybrid window
* @param logList the log element list used in the hybrid window
* @param legendArea the legend area used in the hybrid window
* @param methodButtons the method buttons used in the hybrid window
*/
2024-07-08 22:17:31 +00:00
public OurHybridWindow(DRAW drawArea, TEXT textArea, PARAM parameterArea, ALGORITHM algorithm,
LogElementList<LOG> logList, LEGEND legendArea, METHODBUTTONS methodButtons) {
2024-07-07 17:49:31 +00:00
super(drawArea, textArea, parameterArea, algorithm, logList, legendArea);
this.methodButtons = methodButtons;
}
2024-07-08 22:17:31 +00:00
/**
* Initializes the hybrid window by setting up UI components and event listeners.
*/
public void init() {
startButton = new JButton("start");
stopButton = new JButton("stop");
nextButton = new JButton("next");
prevButton = new JButton("prev");
lastButton = new JButton("last");
firstButton = new JButton("first");
autoButton = new JToggleButton("automatic");
autoSlider = new JSlider(1, 100, 1);
autoForwardButton = new JRadioButton("forward");
autoBackwardButton = new JRadioButton("backward");
pauseButton = new JButton("pause");
ButtonGroup autoDirection = new ButtonGroup();
2024-07-07 17:49:31 +00:00
autoDirection.add(autoForwardButton);
autoDirection.add(autoBackwardButton);
autoForwardButton.setSelected(true);
2024-07-08 22:17:31 +00:00
JPanel panelStartStopControls = new JPanel();
panelStartStopControls.setLayout(new GridLayout(4, 1, 5, 5));
2024-07-07 17:49:31 +00:00
panelStartStopControls.add(startButton);
panelStartStopControls.add(stopButton);
panelStartStopControls.add(pauseButton);
panelStartStopControls.add(methodButtons);
panelStartStopControls.setBorder(BorderFactory.createTitledBorder("Start / Stop"));
2024-07-08 22:17:31 +00:00
JPanel panelStepwiseExecutionControls = new JPanel();
panelStepwiseExecutionControls.setLayout(new GridLayout(4, 1, 5, 5));
2024-07-07 17:49:31 +00:00
panelStepwiseExecutionControls.add(firstButton);
panelStepwiseExecutionControls.add(nextButton);
panelStepwiseExecutionControls.add(prevButton);
panelStepwiseExecutionControls.add(lastButton);
panelStepwiseExecutionControls.setBorder(BorderFactory.createTitledBorder("Stepwise execution"));
2024-07-08 22:17:31 +00:00
JPanel panelAutomaticExecutionControls = new JPanel();
panelAutomaticExecutionControls.setLayout(new GridLayout(5, 1, 5, 5));
2024-07-07 17:49:31 +00:00
panelAutomaticExecutionControls.add(autoButton);
panelAutomaticExecutionControls.add(autoSlider);
panelAutomaticExecutionControls.add(autoForwardButton);
panelAutomaticExecutionControls.add(autoBackwardButton);
panelAutomaticExecutionControls.setBorder(BorderFactory.createTitledBorder("Automatic execution"));
2024-07-08 22:17:31 +00:00
JPanel buttonLine = new JPanel();
buttonLine.setLayout(new GridLayout(3, 1, 5, 5));
buttonLine.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
2024-07-07 17:49:31 +00:00
buttonLine.add(panelStartStopControls);
buttonLine.add(panelStepwiseExecutionControls);
buttonLine.add(panelAutomaticExecutionControls);
2024-07-08 22:17:31 +00:00
JLabel headline = new JLabel(algorithm.getTitle());
headline.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
headline.setFont(new Font("SansSerif", Font.PLAIN, 20));
2024-07-07 17:49:31 +00:00
JScrollPane parameterAreaScrollPane = parameterArea.getScrollPane();
JScrollPane legendAreaScrollPane = legendArea.getScrollPane();
parameterAreaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
legendAreaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
2024-07-08 22:17:31 +00:00
JSplitPane hybridWindowSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, parameterAreaScrollPane, legendAreaScrollPane);
2024-07-07 17:49:31 +00:00
hybridWindowSplitPane.setOneTouchExpandable(true);
hybridWindowSplitPane.setResizeWeight(0.465);
2024-07-08 22:17:31 +00:00
tabbedPane.insertTab(drawArea.getDrawAreaName(), null, drawArea.getScrollPane(), drawArea.getDrawAreaName(), 0);
2024-07-07 17:49:31 +00:00
tabbedPane.setSelectedIndex(0);
2024-07-08 22:17:31 +00:00
setLayout(new BorderLayout(10, 10));
2024-07-07 17:49:31 +00:00
2024-07-08 22:17:31 +00:00
add(headline, BorderLayout.NORTH);
add(hybridWindowSplitPane, BorderLayout.WEST);
add(tabbedPane, BorderLayout.CENTER);
add(textArea.getScrollPane(), BorderLayout.SOUTH);
add(buttonLine, BorderLayout.EAST);
2024-07-07 17:49:31 +00:00
setState(PARAMETERSTATE);
2024-07-08 22:17:31 +00:00
startListener = new StartListener();
stopListener = new StopListener();
prevListener = new PrevListener();
nextListener = new NextListener();
firstListener = new FirstListener();
lastListener = new LastListener();
autoButtonListener = new AutoButtonListener();
autoSliderListener = new AutoSliderListener();
autoForwardListener = new AutoForwardListener();
autoBackwardListener = new AutoBackwardListener();
pauseButtonListener = new PauseButtonListener();
2024-07-07 17:49:31 +00:00
startButton.addActionListener(startListener);
stopButton.addActionListener(stopListener);
prevButton.addActionListener(prevListener);
nextButton.addActionListener(nextListener);
firstButton.addActionListener(firstListener);
lastButton.addActionListener(lastListener);
autoButton.addItemListener(autoButtonListener);
autoSlider.addChangeListener(autoSliderListener);
autoForwardButton.addItemListener(autoForwardListener);
autoBackwardButton.addItemListener(autoBackwardListener);
pauseButton.addActionListener(pauseButtonListener);
2024-07-08 22:17:31 +00:00
autoTimer = new Timer(1, nextListener);
2024-07-07 17:49:31 +00:00
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for starting the algorithm.
*/
class StartListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Component[] bComponents = methodButtons.getComponents();
2024-07-07 17:49:31 +00:00
setState(RUNSTATE);
2024-07-08 22:17:31 +00:00
for (Component c : bComponents) {
2024-07-07 17:49:31 +00:00
c.setEnabled(false);
}
2024-07-08 22:17:31 +00:00
logList = algorithm.run();
2024-07-07 17:49:31 +00:00
drawArea.setLogList(logList);
textArea.setLogList(logList);
}
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for stopping the algorithm.
*/
class StopListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Component[] bComponents = methodButtons.getComponents();
2024-07-07 17:49:31 +00:00
setState(PARAMETERSTATE);
2024-07-08 22:17:31 +00:00
for (Component c : bComponents) {
2024-07-07 17:49:31 +00:00
c.setEnabled(true);
}
logList.clear();
drawArea.clear();
textArea.clear();
}
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for moving to the previous step.
*/
class PrevListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (logList.isInitialized()) {
if (logList.get() == logList.firstElement()) {
2024-07-07 17:49:31 +00:00
autoButton.setSelected(false);
2024-07-08 22:17:31 +00:00
} else {
2024-07-07 17:49:31 +00:00
logList.prev();
drawArea.drawStep();
textArea.printStep();
}
2024-07-08 22:17:31 +00:00
} else {
2024-07-07 17:49:31 +00:00
logList.prev();
drawArea.drawStep();
textArea.printStep();
}
}
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for moving to the next step.
*/
class NextListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (logList.isInitialized()) {
if (logList.get() == logList.lastElement()) {
2024-07-07 17:49:31 +00:00
autoButton.setSelected(false);
2024-07-08 22:17:31 +00:00
} else {
2024-07-07 17:49:31 +00:00
logList.next();
drawArea.drawStep();
textArea.printStep();
}
2024-07-08 22:17:31 +00:00
} else {
2024-07-07 17:49:31 +00:00
logList.next();
drawArea.drawStep();
textArea.printStep();
}
}
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for moving to the first step.
*/
class FirstListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
2024-07-07 17:49:31 +00:00
logList.first();
drawArea.drawStep();
textArea.printStep();
}
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for moving to the last step.
*/
class LastListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
2024-07-07 17:49:31 +00:00
logList.last();
drawArea.drawStep();
textArea.printStep();
}
}
2024-07-08 22:17:31 +00:00
/**
* ItemListener implementation for handling auto button state changes.
*/
2024-07-07 17:49:31 +00:00
class AutoButtonListener implements ItemListener {
2024-07-08 22:17:31 +00:00
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
2024-07-07 17:49:31 +00:00
setState(AUTOSTATE);
2024-07-08 22:17:31 +00:00
autoTimer.setDelay(10000 / ((int) Math.pow(autoSlider.getValue(), 2)));
2024-07-07 17:49:31 +00:00
autoTimer.start();
2024-07-08 22:17:31 +00:00
} else {
2024-07-07 17:49:31 +00:00
setState(RUNSTATE);
autoTimer.stop();
}
}
}
2024-07-08 22:17:31 +00:00
/**
* ChangeListener implementation for handling auto slider changes.
*/
2024-07-07 17:49:31 +00:00
class AutoSliderListener implements ChangeListener {
2024-07-08 22:17:31 +00:00
public void stateChanged(ChangeEvent event) {
if (autoTimer.isRunning()) {
2024-07-07 17:49:31 +00:00
autoTimer.stop();
2024-07-08 22:17:31 +00:00
autoTimer.setInitialDelay(10000 / ((int) Math.pow(autoSlider.getValue(), 2)));
autoTimer.setDelay(10000 / ((int) Math.pow(autoSlider.getValue(), 2)));
2024-07-07 17:49:31 +00:00
autoTimer.start();
autoTimer.setInitialDelay(1);
}
}
}
2024-07-08 22:17:31 +00:00
/**
* ItemListener implementation for handling auto forward button state changes.
*/
class AutoForwardListener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
2024-07-07 17:49:31 +00:00
autoTimer.removeActionListener(prevListener);
autoTimer.addActionListener(nextListener);
}
}
}
2024-07-08 22:17:31 +00:00
/**
* ItemListener implementation for handling auto backward button state changes.
*/
class AutoBackwardListener implements ItemListener {
public void itemStateChanged(ItemEvent event) {
if (event.getStateChange() == ItemEvent.SELECTED) {
2024-07-07 17:49:31 +00:00
autoTimer.removeActionListener(nextListener);
autoTimer.addActionListener(prevListener);
}
}
}
2024-07-08 22:17:31 +00:00
/**
* ActionListener implementation for handling pause button clicks.
*/
class PauseButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
if (autoButton.isSelected()) {
2024-07-07 17:49:31 +00:00
setState(RUNSTATE);
autoTimer.stop();
autoButton.setSelected(false);
}
}
}
}