package stackprogramvisualiser.gui;

import javax.swing.*;
import java.awt.*;
import java.util.Stack;

public class StackGui extends JPanel {

	private JPanel mainPanel;
	private Stack<Integer> data = null;

	// empty label
	private static JLabel emptyLabel = new JLabel("Empty Stack", SwingConstants.CENTER);
	private static GridBagConstraints emptyLabelGBC = new GridBagConstraints();

	static {
		emptyLabel.setFont(emptyLabel.getFont().deriveFont(emptyLabel.getFont().getStyle() & ~Font.BOLD));
		GridBagConstraints emptyLabelGBC = new GridBagConstraints();
		emptyLabelGBC.anchor = GridBagConstraints.PAGE_START;
		emptyLabelGBC.gridx = 0;
		emptyLabelGBC.gridy = 0;
		emptyLabelGBC.fill = GridBagConstraints.HORIZONTAL;
		emptyLabelGBC.insets = new Insets(2, 2, 2, 2);
	}

	public StackGui() {
		// set layout
		setLayout(new GridLayout(1, 1));

		// set main panel
		mainPanel = new JPanel(new GridBagLayout());
		mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		add(new JScrollPane(mainPanel));

		// set default data
		mainPanel.add(emptyLabel, emptyLabelGBC);
		mainPanel.revalidate();
	}

	public void setDataSource(Stack<Integer> data) {
		this.data = data;
	}

	public void redraw() {
		// clear it out, then start again
		mainPanel.removeAll();
		mainPanel.revalidate();

		// empty?
		if (data == null || data.isEmpty()) {
			mainPanel.add(emptyLabel, emptyLabelGBC);
			return;
		}

		// make GBC for each cell
		GridBagConstraints cellGCB = new GridBagConstraints();
		cellGCB.anchor = GridBagConstraints.PAGE_START;
		cellGCB.gridx = 0;
		cellGCB.gridy = 0;
		cellGCB.weightx = 1;
		cellGCB.fill = GridBagConstraints.HORIZONTAL;
		cellGCB.insets = new Insets(2, 2, 2, 2);

		// fill stack, top to bottom
		for (int i = 0; i < data.size(); ++i) {
			cellGCB.gridy = i;
			mainPanel.add(new StackCell(data.get(data.size() - (i + 1))), cellGCB);
		}

		// fill remaining space
		GridBagConstraints glueGCB = new GridBagConstraints();
		glueGCB.anchor = GridBagConstraints.PAGE_START;
		glueGCB.gridx = 0;
		glueGCB.gridy = data.size();
		glueGCB.weightx = 1;
		glueGCB.weighty = 1;
		glueGCB.fill = GridBagConstraints.HORIZONTAL;
		glueGCB.insets = new Insets(2, 2, 2, 2);
		mainPanel.add(Box.createGlue(), glueGCB);

		mainPanel.revalidate();
	}

}
@@ -661,24 +661,82 @@
 .gridy = 0;%0A
+%09%09emptyLabelGBC.weightx = 1;%0A%09%09emptyLabelGBC.weighty = 1;%0A
 %09%09emptyLabel
@@ -1354,16 +1354,39 @@
 idate();
+%0A%09%09mainPanel.repaint();
 %0A%0A%09%09// e
@@ -2348,17 +2348,40 @@
 validate();%0A
+%09%09mainPanel.repaint();%0A
 %09%7D%0A%0A%7D

Fix bug with "empty stack" message staying on screen