View Javadoc

1   package org.inigma.waragent.view;
2   
3   import java.lang.reflect.Field;
4   import java.sql.SQLException;
5   import java.text.NumberFormat;
6   import java.util.ArrayList;
7   import java.util.Calendar;
8   import java.util.Collections;
9   import java.util.Comparator;
10  import java.util.LinkedList;
11  import java.util.List;
12  
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.events.SelectionEvent;
17  import org.eclipse.swt.events.SelectionListener;
18  import org.eclipse.swt.graphics.Font;
19  import org.eclipse.swt.graphics.FontData;
20  import org.eclipse.swt.layout.FillLayout;
21  import org.eclipse.swt.layout.GridData;
22  import org.eclipse.swt.layout.GridLayout;
23  import org.eclipse.swt.widgets.Composite;
24  import org.eclipse.swt.widgets.Display;
25  import org.eclipse.swt.widgets.Group;
26  import org.eclipse.swt.widgets.Label;
27  import org.eclipse.swt.widgets.Shell;
28  import org.eclipse.swt.widgets.Table;
29  import org.eclipse.swt.widgets.TableColumn;
30  import org.eclipse.swt.widgets.TableItem;
31  import org.inigma.utopia.Army;
32  import org.inigma.utopia.Kingdom;
33  import org.inigma.utopia.Military;
34  import org.inigma.utopia.Province;
35  import org.inigma.utopia.utils.CalendarUtils;
36  import org.inigma.utopia.utils.I18NUtil;
37  import org.inigma.waragent.crud.AccountCrud;
38  
39  public class KingdomView {
40      private static final double NETWORTH_FACTOR = 0.25;
41  
42      private final class NetworthComparator implements Comparator<Province> {
43          public int compare(Province p1, Province p2) {
44              return p2.getNetworth() - p1.getNetworth();
45          }
46      }
47  
48      private final class NetworthDifferenceComparator implements Comparator<Province> {
49          private int targetNetworth;
50  
51          public NetworthDifferenceComparator(int targetNetworth) {
52              this.targetNetworth = targetNetworth;
53          }
54  
55          public int compare(Province p1, Province p2) {
56              int p1diff = targetNetworth - p1.getNetworth();
57              int p2diff = targetNetworth - p2.getNetworth();
58              return Math.abs(p1diff) - Math.abs(p2diff);
59          }
60      }
61  
62      private final class ProvinceSelectionListener implements SelectionListener {
63          private Table table;
64          private List<Province> list;
65  
66          public ProvinceSelectionListener(Table table, List<Province> list) {
67              this.table = table;
68              this.list = list;
69          }
70  
71          public void widgetSelected(SelectionEvent e) {}
72  
73          public void widgetDefaultSelected(SelectionEvent e) {
74              int index = this.table.getSelectionIndex();
75              new ProvinceView(this.list.get(index));
76          }
77      }
78  
79      private Log logger = LogFactory.getLog(KingdomView.class);
80  
81      private Shell shell;
82      private AccountCrud crud;
83      private Kingdom kingdom;
84  
85      private Label relationship;
86      private Label stance;
87      private Label warRecord;
88      private Label networth;
89      private Label acres;
90      private Label provinces;
91  
92      private Table targetTable;
93      private List<Province> targetList;
94  
95      private Table provinceTable;
96      private List<Province> provinceList;
97  
98      public KingdomView(AccountCrud crud, Kingdom kingdom) {
99          this.crud = crud;
100         this.kingdom = kingdom;
101         this.targetList = new LinkedList<Province>();
102         this.provinceList = new ArrayList<Province>();
103         this.shell = new Shell(Display.getDefault());
104         this.shell.setLayout(new GridLayout(1, true));
105 
106         Group kingdomPanel = new Group(shell, SWT.SHADOW_ETCHED_IN);
107         kingdomPanel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
108         kingdomPanel.setText(String.format("%s - (%s)",
109                 kingdom, CalendarUtils.getCalendarDiff(kingdom.getLastUpdate())));
110         kingdomPanel.setLayout(new GridLayout(4, false));
111         addLabel(kingdomPanel, "networth", "Networth:");
112         addLabel(kingdomPanel, "relationship", "Relationship:");
113         addLabel(kingdomPanel, "acres", "Acres:");
114         addLabel(kingdomPanel, "stance", "Stance:");
115         addLabel(kingdomPanel, "provinces", "Provinces:");
116         addLabel(kingdomPanel, "warRecord", "War Record:");
117         
118         Group targetPanel = new Group(shell, SWT.SHADOW_ETCHED_IN);
119         targetPanel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
120         targetPanel.setLayout(new FillLayout());
121         targetPanel.setText("Best Targets");
122         targetTable = new Table(targetPanel, SWT.SINGLE);
123         targetTable.setHeaderVisible(true);
124         targetTable.setLinesVisible(true);
125         targetTable.addSelectionListener(new ProvinceSelectionListener(targetTable, targetList));
126         addTableColumn(targetTable, "Province", 200, false);
127         addTableColumn(targetTable, "Networth", 100, true);
128         addTableColumn(targetTable, "Acres", 75, true);
129         addTableColumn(targetTable, "Offense", 75, true);
130         addTableColumn(targetTable, "Defense", 75, true);
131 
132         Group provinceListPanel = new Group(shell, SWT.SHADOW_ETCHED_IN);
133         provinceListPanel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, true));
134         provinceListPanel.setLayout(new FillLayout());
135         provinceListPanel.setText("Provinces");
136         provinceTable = new Table(provinceListPanel, SWT.SINGLE);
137         provinceTable.setHeaderVisible(true);
138         provinceTable.setLinesVisible(true);
139         provinceTable.addSelectionListener(new ProvinceSelectionListener(provinceTable, provinceList));
140         addTableColumn(provinceTable, "Province", 200, false);
141         addTableColumn(provinceTable, "Networth", 100, true);
142         addTableColumn(provinceTable, "Acres", 75, true);
143         addTableColumn(provinceTable, "NW / Acre", 75, true);
144         addTableColumn(provinceTable, "Updated", 100, false);
145 
146         update();
147         shell.setSize(600, 600);
148         shell.setVisible(true);
149     }
150 
151     private void addTableColumn(Table table, String title, int width, boolean leftAlign) {
152         TableColumn column = new TableColumn(table, SWT.NONE);
153         column.setText(title);
154         column.setWidth(width);
155         column.setAlignment(leftAlign ? SWT.LEFT : SWT.RIGHT);
156     }
157 
158     private void addLabel(Composite panel, String label, String text) {
159         Label display = new Label(panel, SWT.SHADOW_ETCHED_IN);
160         display.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
161         FontData[] fontData = display.getFont().getFontData();
162         for (FontData data : fontData) {
163             data.setStyle(SWT.BOLD);
164         }
165         display.setFont(new Font(display.getDisplay(), fontData));
166         display.setText(text);
167 
168         try {
169             Field field = getClass().getDeclaredField(label);
170             Label ref = new Label(panel, SWT.SHADOW_ETCHED_IN);
171             ref.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
172             field.set(this, ref);
173         } catch (Exception e) {
174             // should never happen
175         }
176     }
177 
178     public void setKingdom(Kingdom kingdom) {
179         this.kingdom = kingdom;
180         update();
181     }
182 
183     public void update() {
184         NumberFormat nf = I18NUtil.getGroupFormat();
185         NumberFormat pf = I18NUtil.getPrecisionFormat();
186 
187         shell.setText(kingdom.toString());
188         relationship.setText(kingdom.getRelation().toString());
189         stance.setText(kingdom.getStance().toString());
190         warRecord.setText(String.format("%s - %s - %s", kingdom.getWarWins(), kingdom.getWarCount(),
191                 kingdom.getWarNetworthDiff()));
192 //        int provCount = 0;
193 //        shell.update();
194 
195         Province your = null;
196         try {
197             your = crud.getProvince();
198             List<Province> list = crud.getProvinces(kingdom.getId());
199             provinceList.clear();
200             provinceList.addAll(list);
201         } catch (SQLException e) {
202             logger.error("TODO", e);
203             return;
204         }
205         Collections.sort(provinceList, new NetworthComparator());
206         provinceTable.removeAll();
207         for (Province province : provinceList) {
208             Military military = province.getMilitary();
209             boolean useMilitaryDefense = false;
210             for (Army army : military.getArmies()) {
211                 if (army.getReturnTime().after(CalendarUtils.getCalendar())) {
212                     useMilitaryDefense = true;
213                 }
214             }
215             if (((useMilitaryDefense &&  your.getOffense() * 1.09 > military.getDefense())
216                     || (province.getDefense() > 0 && your.getOffense() * 1.09 > province.getDefense()))
217                     && Math.abs(1.0 - (float) your.getNetworth() / (float) province.getNetworth()) < NETWORTH_FACTOR) {
218                 targetList.add(province);
219             }
220             TableItem item = new TableItem(provinceTable, SWT.NONE);
221             item.setText(0, province.toStringDetailed());
222             if (province.getAcres() == 0) {
223                 item.setText(1, "DEAD");
224                 item.setText(2, "DEAD");
225                 item.setText(3, "DEAD");
226             } else {
227                 item.setText(1, nf.format(province.getNetworth()));
228                 item.setText(2, nf.format(province.getAcres()));
229                 item.setText(3, pf.format((float) province.getNetworth() / (float) province.getAcres()));
230             }
231             item.setText(4, CalendarUtils.getCalendarDiff(province.getLastUpdate()));
232         }
233         provinceTable.update();
234 
235 
236         Collections.sort(targetList, new NetworthDifferenceComparator(your.getNetworth()));
237         targetTable.removeAll();
238         while (targetList.size() > 5) { // Don't overdo the best targets thing
239             targetList.remove(5);
240         }
241         for (Province province : targetList) {
242             TableItem item = new TableItem(targetTable, SWT.NONE);
243             item.setText(0, province.toStringDetailed());
244             item.setText(1, nf.format(province.getNetworth()));
245             item.setText(2, nf.format(province.getAcres()));
246             item.setText(3, nf.format(province.getCurrentOffense()));
247             item.setText(4, nf.format(province.getCurrentDefense()));
248         }
249         targetTable.update();
250 
251         networth.setText(String.format("%s (%s avg)", nf.format(kingdom.getTotalNetworth()),
252                 nf.format(kingdom.getTotalNetworth() / provinceList.size())));
253         acres.setText(String.format("%s (%s avg)", nf.format(kingdom.getTotalAcres()),
254                 nf.format(kingdom.getTotalAcres() / provinceList.size())));
255         provinces.setText("" + provinceList.size());
256     }
257 }