1 package org.woehlke.computer.kurzweil.tabs.simulatedevolution;
2
3 import lombok.EqualsAndHashCode;
4 import lombok.Getter;
5 import lombok.ToString;
6 import lombok.extern.log4j.Log4j2;
7 import org.woehlke.computer.kurzweil.commons.Updateable;
8 import org.woehlke.computer.kurzweil.tabs.ComputerKurzweilTabbedPane;
9 import org.woehlke.computer.kurzweil.tabs.TabPanel;
10 import org.woehlke.computer.kurzweil.tabs.Tab;
11 import org.woehlke.computer.kurzweil.tabs.simulatedevolution.model.population.SimulatedEvolutionPopulation;
12
13 import java.awt.event.ActionEvent;
14
15
16 @Log4j2
17 @Getter
18 @ToString(callSuper = true, exclude = {"tabCtx"})
19 @EqualsAndHashCode(callSuper=true, exclude = {"tabCtx"})
20 public class SimulatedEvolutionTab extends TabPanel implements Tab, SimulatedEvolution, Updateable {
21
22 private final SimulatedEvolutionCanvas canvas;
23 private final SimulatedEvolutionContext tabCtx;
24 private final SimulatedEvolutionModel tabModel;
25 private final SimulatedEvolutionTabPane tabPane;
26
27 private SimulatedEvolutionPopulation population;
28
29 public SimulatedEvolutionTab(ComputerKurzweilTabbedPane tabbedPane) {
30 super(tabbedPane, TAB_TYPE);
31 this.tabCtx = new SimulatedEvolutionContext(this, this.getCtx());
32 this.canvas = this.tabCtx.getCanvas();
33 this.tabModel = this.canvas.getTabModel();
34 this.tabPane = new SimulatedEvolutionTabPane(this);
35 this.add(this.panelSubtitle);
36 this.add(this.canvas);
37 this.add(this.tabPane);
38 this.tabModel.stop();
39 this.tabPane.stop();
40 this.ctx.getFrame().pack();
41 }
42
43 @Override
44 public void start() {
45 log.info("start");
46 this.tabPane.start();
47 this.tabModel.start();
48 this.getTabCtx().startController();
49 this.getTabCtx().getController().start();
50 this.ctx.getFrame().pack();
51 int x = this.canvas.getWidth();
52 int y = this.canvas.getHeight();
53 log.info("start with canvas x="+x+" y="+y);
54 log.info("started");
55 }
56
57 @Override
58 public void stop() {
59 log.info("stop");
60 this.tabModel.stop();
61 this.tabPane.stop();
62 this.getTabCtx().stopController();
63 int x = this.canvas.getWidth();
64 int y = this.canvas.getHeight();
65 log.info("stop with canvas x="+x+" y="+y);
66 log.info("stopped");
67 }
68
69 @Override
70 public void actionPerformed(ActionEvent ae) {
71 if (ae.getSource() == this.tabPane.getFoodPerDayPanel().getFoodPerDayIncreaseButton()) {
72 log.info("actionPerformed: increaseFoodPerDay");
73 this.tabModel.increaseFoodPerDay();
74 this.update();
75 } else if (ae.getSource() == this.tabPane.getFoodPerDayPanel().getFoodPerDayDecreaseButton()) {
76 log.info("actionPerformed: decreaseFoodPerDay");
77 this.tabModel.decreaseFoodPerDay();
78 this.update();
79 } else if (ae.getSource() == this.tabPane.getGardenOfEdenPanel().getButtonToggleGardenOfEden()) {
80 log.info("actionPerformed: toggleGardenOfEden");
81 this.tabModel.toggleGardenOfEden();
82 this.update();
83 }
84 if(ae.getSource() == this.tabPane.getStartStopButtonsPanel().getStartButton()){
85 super.ctx.getFrame().start();
86 }
87 if(ae.getSource() == this.tabPane.getStartStopButtonsPanel().getStopButton()){
88 super.ctx.getFrame().stop();
89 }
90 }
91
92 public void update(){
93 if(this.population!= null){
94 this.tabPane.update();
95 }
96 }
97
98 public void update(SimulatedEvolutionPopulation population) {
99 this.population = population;
100 this.tabPane.update();
101 }
102 }