|
| 1 | +package bab.ui; |
| 2 | + |
| 3 | +import java.awt.CardLayout; |
| 4 | +import java.awt.Dimension; |
| 5 | +import java.util.ArrayList; |
| 6 | +import javax.swing.JFrame; |
| 7 | +import javax.swing.JPanel; |
| 8 | + |
| 9 | +import bab.ui.panel.DisplayPanel; |
| 10 | +import bab.ui.panel.HomePanel; |
| 11 | +import bab.ui.panel.InputPanel; |
| 12 | +import bab.ui.panel.SelectPanel; |
| 13 | +import bab.util.Box; |
| 14 | + |
| 15 | +/** |
| 16 | + * Main frame of this application, the {@code Window} class consists of a |
| 17 | + * {@code JPanel} with the {@code CardLayout} where each card is a different |
| 18 | + * section. Each section is a JPanel with its own graphic interface. |
| 19 | + * |
| 20 | + * <p>The class also stores different values used by the software. Such as the |
| 21 | + * list of the different sections the list of possible algorithms, the desired |
| 22 | + * number of balls and boxes, the list of previously filled boxes and the |
| 23 | + * selected algorithm. |
| 24 | + */ |
| 25 | +public class Window extends JFrame { |
| 26 | + private static final long serialVersionUID = -2665608161311636325L; |
| 27 | + |
| 28 | + /** |
| 29 | + * The list of the different sections or panels with a {@code String} to be set as |
| 30 | + * a key when objects can't be stored. ({@code CardLayout} require a String) |
| 31 | + */ |
| 32 | + public static enum Panel { |
| 33 | + HOMEPANEL ("Home Panel"), |
| 34 | + INPUTPANEL ("Input Panel"), |
| 35 | + SELECTPANEL ("Select Panel"), |
| 36 | + DISPLAYPANEL ("Display Panel"); |
| 37 | + |
| 38 | + public final String txt; |
| 39 | + |
| 40 | + Panel(String txt) { |
| 41 | + this.txt = txt; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * The list of possible algorithms. |
| 47 | + */ |
| 48 | + public static enum Algo { |
| 49 | + CHAIN, |
| 50 | + DOUBLE, |
| 51 | + LINEAR, |
| 52 | + QUADRA |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Main panel of this frame, cards will use the CardLayout and display each |
| 57 | + * section. |
| 58 | + */ |
| 59 | + private final JPanel cards; |
| 60 | + |
| 61 | + /** |
| 62 | + * The {@code DisplayPanel} is the only panel stored directly in the class |
| 63 | + * because when the user sets different values the panel needs to be |
| 64 | + * refreshed. |
| 65 | + */ |
| 66 | + private final DisplayPanel d; |
| 67 | + |
| 68 | + /** |
| 69 | + * The number of boxes selected by the user. |
| 70 | + */ |
| 71 | + private Integer nbBoxes = 0; |
| 72 | + |
| 73 | + /** |
| 74 | + * The number of balls selected by the user. |
| 75 | + */ |
| 76 | + private Integer nbBalls = 0; |
| 77 | + |
| 78 | + /** |
| 79 | + * The list of processed boxes filled with balls. |
| 80 | + */ |
| 81 | + public ArrayList<Box> boxes; |
| 82 | + |
| 83 | + /** |
| 84 | + * The selected algorithm. |
| 85 | + */ |
| 86 | + private Algo algo; |
| 87 | + |
| 88 | + /** |
| 89 | + * Initializes a newly created {@code Window} object. Sets default values |
| 90 | + * and adds graphic components. Finally, displays a window to the user. |
| 91 | + */ |
| 92 | + public Window() { |
| 93 | + super("BoxAndBalls"); |
| 94 | + |
| 95 | + this.boxes = new ArrayList<Box>(); |
| 96 | + |
| 97 | + this.cards = new JPanel(new CardLayout()); |
| 98 | + this.d = new DisplayPanel(this); |
| 99 | + this.cards.add(new HomePanel(this), Panel.HOMEPANEL.txt); |
| 100 | + this.cards.add(new InputPanel(this), Panel.INPUTPANEL.txt); |
| 101 | + this.cards.add(new SelectPanel(this), Panel.SELECTPANEL.txt); |
| 102 | + this.cards.add(this.d, Panel.DISPLAYPANEL.txt); |
| 103 | + |
| 104 | + /* |
| 105 | + * this.setSize() takes into account the size of the title bar which is |
| 106 | + * different for windows and linux. this.pack() is better in our case. |
| 107 | + */ |
| 108 | + this.cards.setPreferredSize(new Dimension(250, 260)); |
| 109 | + this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 110 | + this.setLocationRelativeTo(null); |
| 111 | + this.setResizable(false); |
| 112 | + |
| 113 | + this.disp(Panel.HOMEPANEL); |
| 114 | + this.getContentPane().add(this.cards); |
| 115 | + |
| 116 | + this.pack(); |
| 117 | + this.setVisible(true); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Returns the number of boxes. |
| 122 | + */ |
| 123 | + public Integer getNbBoxes() { |
| 124 | + return nbBoxes; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Returns the number of balls. |
| 129 | + */ |
| 130 | + public Integer getNbBalls() { |
| 131 | + return nbBalls; |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Returns the selected algorithm. |
| 136 | + */ |
| 137 | + public Algo getAlgo() { |
| 138 | + return algo; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Sets the number of boxes. |
| 143 | + * |
| 144 | + * @param nbBoxes the number of boxes |
| 145 | + */ |
| 146 | + public void setNbBoxes(Integer nbBoxes) { |
| 147 | + this.nbBoxes = nbBoxes; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Sets the number of balls. |
| 152 | + * |
| 153 | + * @param nbBalls the number of balls |
| 154 | + */ |
| 155 | + public void setNbBalls(Integer nbBalls) { |
| 156 | + this.nbBalls = nbBalls; |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * Sets the selected algorithm. |
| 161 | + * |
| 162 | + * @param algo the selected algorithm |
| 163 | + */ |
| 164 | + public void setAlgo(Algo algo) { |
| 165 | + this.algo = algo; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Displays the selected panel on the window. Mostly used by panels to |
| 170 | + * switch between displays. |
| 171 | + * |
| 172 | + * <p>If the selected panel is the {@code DisplayPanel}, its refresh method |
| 173 | + * is called to update the graph. |
| 174 | + * |
| 175 | + * @param panel the selected panel |
| 176 | + * @see bab.ui.panel.DisplayPanel |
| 177 | + */ |
| 178 | + public void disp(Panel panel) { |
| 179 | + if (panel == Panel.DISPLAYPANEL) |
| 180 | + this.d.refresh(); |
| 181 | + |
| 182 | + ((CardLayout) this.cards.getLayout()).show(this.cards, panel.txt); |
| 183 | + } |
| 184 | +} |
0 commit comments