package OurApplication; import logging.Algorithm; import logging.LogElement; import logging.LogElementList; 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; /** * This class provides an example for using visualization.HybridWindow. * It extends HybridWindow to integrate graphical components with algorithmic operations. * * @param Type of the draw area used in the hybrid window. * @param Type of the text area used in the hybrid window. * @param Type of the parameter area used in the hybrid window. * @param Type of the algorithm used in the hybrid window. * @param Type of logging elements used in the hybrid window. * @param Type of legend area used in the hybrid window. * @param Type of method buttons used in the hybrid window. * * @see HybridWindow * @see OurApplication */ public class OurHybridWindow extends HybridWindow { 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. * 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 */ public OurHybridWindow(DRAW drawArea, TEXT textArea, PARAM parameterArea, ALGORITHM algorithm, LogElementList logList, LEGEND legendArea, METHODBUTTONS methodButtons) { super(drawArea, textArea, parameterArea, algorithm, logList, legendArea); this.methodButtons = methodButtons; } /** * 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(); autoDirection.add(autoForwardButton); autoDirection.add(autoBackwardButton); autoForwardButton.setSelected(true); JPanel panelStartStopControls = new JPanel(); panelStartStopControls.setLayout(new GridLayout(4, 1, 5, 5)); panelStartStopControls.add(startButton); panelStartStopControls.add(stopButton); panelStartStopControls.add(pauseButton); panelStartStopControls.add(methodButtons); panelStartStopControls.setBorder(BorderFactory.createTitledBorder("Start / Stop")); JPanel panelStepwiseExecutionControls = new JPanel(); panelStepwiseExecutionControls.setLayout(new GridLayout(4, 1, 5, 5)); panelStepwiseExecutionControls.add(firstButton); panelStepwiseExecutionControls.add(nextButton); panelStepwiseExecutionControls.add(prevButton); panelStepwiseExecutionControls.add(lastButton); panelStepwiseExecutionControls.setBorder(BorderFactory.createTitledBorder("Stepwise execution")); JPanel panelAutomaticExecutionControls = new JPanel(); panelAutomaticExecutionControls.setLayout(new GridLayout(5, 1, 5, 5)); panelAutomaticExecutionControls.add(autoButton); panelAutomaticExecutionControls.add(autoSlider); panelAutomaticExecutionControls.add(autoForwardButton); panelAutomaticExecutionControls.add(autoBackwardButton); panelAutomaticExecutionControls.setBorder(BorderFactory.createTitledBorder("Automatic execution")); JPanel buttonLine = new JPanel(); buttonLine.setLayout(new GridLayout(3, 1, 5, 5)); buttonLine.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); buttonLine.add(panelStartStopControls); buttonLine.add(panelStepwiseExecutionControls); buttonLine.add(panelAutomaticExecutionControls); JLabel headline = new JLabel(algorithm.getTitle()); headline.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); headline.setFont(new Font("SansSerif", Font.PLAIN, 20)); JScrollPane parameterAreaScrollPane = parameterArea.getScrollPane(); JScrollPane legendAreaScrollPane = legendArea.getScrollPane(); parameterAreaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); legendAreaScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); JSplitPane hybridWindowSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, parameterAreaScrollPane, legendAreaScrollPane); hybridWindowSplitPane.setOneTouchExpandable(true); hybridWindowSplitPane.setResizeWeight(0.465); tabbedPane.insertTab(drawArea.getDrawAreaName(), null, drawArea.getScrollPane(), drawArea.getDrawAreaName(), 0); tabbedPane.setSelectedIndex(0); setLayout(new BorderLayout(10, 10)); add(headline, BorderLayout.NORTH); add(hybridWindowSplitPane, BorderLayout.WEST); add(tabbedPane, BorderLayout.CENTER); add(textArea.getScrollPane(), BorderLayout.SOUTH); add(buttonLine, BorderLayout.EAST); setState(PARAMETERSTATE); 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(); 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); autoTimer = new Timer(1, nextListener); } /** * ActionListener implementation for starting the algorithm. */ class StartListener implements ActionListener { public void actionPerformed(ActionEvent event) { Component[] bComponents = methodButtons.getComponents(); setState(RUNSTATE); for (Component c : bComponents) { c.setEnabled(false); } logList = algorithm.run(); drawArea.setLogList(logList); textArea.setLogList(logList); } } /** * ActionListener implementation for stopping the algorithm. */ class StopListener implements ActionListener { public void actionPerformed(ActionEvent event) { Component[] bComponents = methodButtons.getComponents(); setState(PARAMETERSTATE); for (Component c : bComponents) { c.setEnabled(true); } logList.clear(); drawArea.clear(); textArea.clear(); } } /** * 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()) { autoButton.setSelected(false); } else { logList.prev(); drawArea.drawStep(); textArea.printStep(); } } else { logList.prev(); drawArea.drawStep(); textArea.printStep(); } } } /** * 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()) { autoButton.setSelected(false); } else { logList.next(); drawArea.drawStep(); textArea.printStep(); } } else { logList.next(); drawArea.drawStep(); textArea.printStep(); } } } /** * ActionListener implementation for moving to the first step. */ class FirstListener implements ActionListener { public void actionPerformed(ActionEvent event) { logList.first(); drawArea.drawStep(); textArea.printStep(); } } /** * ActionListener implementation for moving to the last step. */ class LastListener implements ActionListener { public void actionPerformed(ActionEvent event) { logList.last(); drawArea.drawStep(); textArea.printStep(); } } /** * ItemListener implementation for handling auto button state changes. */ class AutoButtonListener implements ItemListener { public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { setState(AUTOSTATE); autoTimer.setDelay(10000 / ((int) Math.pow(autoSlider.getValue(), 2))); autoTimer.start(); } else { setState(RUNSTATE); autoTimer.stop(); } } } /** * ChangeListener implementation for handling auto slider changes. */ class AutoSliderListener implements ChangeListener { public void stateChanged(ChangeEvent event) { if (autoTimer.isRunning()) { autoTimer.stop(); autoTimer.setInitialDelay(10000 / ((int) Math.pow(autoSlider.getValue(), 2))); autoTimer.setDelay(10000 / ((int) Math.pow(autoSlider.getValue(), 2))); autoTimer.start(); autoTimer.setInitialDelay(1); } } } /** * ItemListener implementation for handling auto forward button state changes. */ class AutoForwardListener implements ItemListener { public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { autoTimer.removeActionListener(prevListener); autoTimer.addActionListener(nextListener); } } } /** * ItemListener implementation for handling auto backward button state changes. */ class AutoBackwardListener implements ItemListener { public void itemStateChanged(ItemEvent event) { if (event.getStateChange() == ItemEvent.SELECTED) { autoTimer.removeActionListener(nextListener); autoTimer.addActionListener(prevListener); } } } /** * ActionListener implementation for handling pause button clicks. */ class PauseButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { if (autoButton.isSelected()) { setState(RUNSTATE); autoTimer.stop(); autoButton.setSelected(false); } } } }