2323import org .json .JSONObject ;
2424
2525import javax .swing .*;
26+ import javax .swing .table .DefaultTableModel ;
2627import java .awt .*;
2728import java .io .BufferedReader ;
2829import java .io .IOException ;
@@ -44,8 +45,11 @@ public class InvasionTracker {
4445
4546 public final HashMap <String , Invasion > invasions = new HashMap <>();
4647 public final Logger logger = LogManager .getLogger (InvasionTracker .class );
47- public ScheduledExecutorService scheduler ;
48+ public ScheduledExecutorService schedulerGUI ;
49+ public ScheduledExecutorService schedulerAPI ;
4850 public boolean showDurations ;
51+ public JTable invasionTable ;
52+ public DefaultTableModel invasionTableModel ;
4953
5054 /**
5155 * Open the invasion window.
@@ -71,26 +75,53 @@ public void showWindow(boolean showDurations) {
7175 invasionsLabel .setAlignmentX (Component .CENTER_ALIGNMENT );
7276 panel .add (invasionsLabel );
7377
74- JTextArea textArea = new JTextArea ();
75- textArea .setEditable (false );
76- textArea .setHighlighter (null );
77- scheduler .scheduleAtFixedRate (() -> textArea .setText (updateInvasionListGUI ()), 0 , 500 , TimeUnit .MILLISECONDS );
78- panel .add (textArea );
78+ invasionTable = new JTable ();
79+
80+ String [] columns ;
81+ if (showDurations ) {
82+ columns = new String [] {"District" , "Cog Type" , "Time Left" };
83+ } else {
84+ columns = new String [] {"District" , "Cog Type" , "Cogs" };
85+ }
86+
87+ invasionTableModel = (DefaultTableModel ) invasionTable .getModel ();
88+ invasionTableModel .setColumnIdentifiers (columns );
89+ invasionTable .setModel (invasionTableModel );
90+ invasionTable .setDefaultEditor (Object .class , null );
91+ invasionTable .getTableHeader ().setReorderingAllowed (false );
92+ invasionTable .setFocusable (false );
93+ JScrollPane scrollPane = new JScrollPane (invasionTable );
94+ scrollPane .setVisible (true );
95+ panel .add (scrollPane );
96+
97+ schedulerGUI = Executors .newScheduledThreadPool (0 );
98+ schedulerGUI .scheduleAtFixedRate (this ::updateInvasionListGUI , 0 , 500 , TimeUnit .MILLISECONDS );
99+
100+ startInvasionRefresh ();
79101
80102 frame .pack ();
81103 frame .setSize (500 , 400 );
82104 frame .add (panel );
83105 frame .setLocationRelativeTo (null );
84106 frame .setVisible (true );
107+
108+ frame .addWindowListener (new java .awt .event .WindowAdapter () {
109+ @ Override
110+ public void windowClosing (java .awt .event .WindowEvent windowEvent ) {
111+ schedulerAPI .shutdown ();
112+ schedulerGUI .shutdown ();
113+ }
114+ });
85115 }
86116
87117 /**
88118 * Updates the invasion list on the actual GUI.
89119 */
90- private String updateInvasionListGUI () {
120+ private void updateInvasionListGUI () {
121+ invasionTableModel .setRowCount (0 );
91122 // create a separate list of all the invasions
92123 List <Invasion > sortedInvasions = new ArrayList <>();
93- StringBuilder finalText = new StringBuilder () ;
124+ String [] data ;
94125 for (Map .Entry <String , Invasion > entry : invasions .entrySet ()) {
95126 sortedInvasions .add (entry .getValue ());
96127 }
@@ -109,36 +140,22 @@ private String updateInvasionListGUI() {
109140 } else {
110141 timeLeft = convertTime (ChronoUnit .SECONDS .between (LocalDateTime .now (), invasion .endTime ));
111142 }
112- finalText
113- .append (district )
114- .append (" - " )
115- .append (cogType )
116- .append (" - " )
117- .append (timeLeft )
118- .append ("\n " );
143+
144+ data = new String [] {district , cogType , timeLeft };
119145 } else {
120- finalText
121- .append (district )
122- .append (" - " )
123- .append (cogType )
124- .append (" - " )
125- .append ("(" )
126- .append (invasion .getCogsDefeated ())
127- .append ("/" )
128- .append (invasion .getCogsTotal ())
129- .append (")" )
130- .append ("\n " );
146+ data = new String [] {district , cogType , invasion .getCogsDefeated () + "/" + invasion .getCogsTotal ()};
131147 }
148+ invasionTableModel .addRow (data );
149+ invasionTableModel .fireTableDataChanged ();
132150 }
133- return finalText .toString ();
134151 }
135152
136153 /**
137154 * Read invasion API every 5 seconds.
138155 */
139156 public void startInvasionRefresh () {
140- scheduler = Executors .newScheduledThreadPool (0 );
141- scheduler .scheduleAtFixedRate (
157+ schedulerAPI = Executors .newScheduledThreadPool (0 );
158+ schedulerAPI .scheduleAtFixedRate (
142159 () -> {
143160 try {
144161 readInvasionAPI ();
@@ -150,7 +167,7 @@ public void startInvasionRefresh() {
150167 // clear the invasions JUST to be safe
151168 invasions .clear ();
152169 // restart the scheduler
153- scheduler .shutdown ();
170+ schedulerAPI .shutdown ();
154171 startInvasionRefresh ();
155172 }
156173 },
@@ -221,10 +238,11 @@ public void readInvasionAPI() throws IOException {
221238 JSONObject temp = invasionsObject .getJSONObject (key );
222239 String progress = temp .getString ("progress" );
223240 int cogsDefeated = Integer .parseInt (progress .substring (0 , progress .indexOf ('/' )));
241+ int oldCount = tempInv .getCogsDefeated ();
242+ tempInv .updateCogsDefeated (cogsDefeated );
224243 // if we want to show invasions, then calculate the cogs per min
225244 if (showDurations ) {
226- int difference = cogsDefeated - tempInv .getCogsDefeated ();
227- tempInv .updateCogsDefeated (cogsDefeated );
245+ int difference = cogsDefeated - oldCount ;
228246 logger .info (tempInv .getDistrict () + " - " + tempInv .getCogsDefeated () + " cogs" );
229247 logger .info (tempInv .getDistrict () + " - " + difference + " new" );
230248 tempInv .cogsPerMinute = tempInv .cogsPerMinute + difference ;
@@ -256,8 +274,6 @@ public void readInvasionAPI() throws IOException {
256274 logger .info ("Invasion gone! " + pair .getKey ());
257275 }
258276 }
259-
260- updateInvasionListGUI ();
261277 }
262278
263279 private String convertTime (long totalSecs ) {
0 commit comments