View Javadoc

1   package org.inigma.waragent.view;
2   
3   import java.lang.reflect.Field;
4   import java.text.NumberFormat;
5   import java.util.Calendar;
6   import java.util.Collection;
7   
8   import org.eclipse.swt.SWT;
9   import org.eclipse.swt.graphics.Font;
10  import org.eclipse.swt.graphics.FontData;
11  import org.eclipse.swt.layout.GridData;
12  import org.eclipse.swt.layout.GridLayout;
13  import org.eclipse.swt.widgets.Composite;
14  import org.eclipse.swt.widgets.Label;
15  import org.inigma.utopia.Army;
16  import org.inigma.utopia.Province;
17  import org.inigma.utopia.utils.CalendarUtils;
18  import org.inigma.utopia.utils.I18NUtil;
19  
20  /**
21   * @author <a href="mailto:sejal@inigma.org">Sejal Patel</a>
22   * @version $Revision: 1 $
23   */
24  class ThroneComposite extends Composite {
25      private Label name;
26      private Label racePerso;
27      private Label networth;
28      private Label peasants;
29      private Label thieves;
30      private Label wizards;
31      private Label gold;
32      private Label food;
33      private Label runes;
34      private Label land;
35      private Label militaryOffense;
36      private Label soldiers;
37      private Label offspecs;
38      private Label defspecs;
39      private Label elites;
40      private Label horses;
41      private Label prisoners;
42      private Label militaryDefense;
43  
44      private Province province;
45  
46      public ThroneComposite(Composite parent, Province province) {
47          super(parent, SWT.NONE);
48          setLayout(new GridLayout(4, false));
49  
50          addLabel("name", "Title:");
51          addLabel("racePerso", "Details:");
52          addLabel("land", "Acres:");
53          addLabel("soldiers", "Soldiers:");
54          addLabel("networth", "Networth:");
55          addLabel("offspecs", "Off Specs:");
56          addLabel("peasants", "Peasants:");
57          addLabel("defspecs", "Def Specs:");
58          addLabel("gold", "Gold:");
59          addLabel("elites", "Elites:");
60          addLabel("food", "Food:");
61          addLabel("horses", "Horses:");
62          addLabel("runes", "Runes:");
63          addLabel("prisoners", "Prisoners:");
64          addLabel("thieves", "Thieves:");
65          addLabel("militaryOffense", "Offense:");
66          addLabel("wizards", "Wizards:");
67          addLabel("militaryDefense", "Defense:");
68  
69          setProvince(province);
70      }
71  
72      private void addLabel(String label, String text) {
73          Label display = new Label(this, SWT.SHADOW_ETCHED_IN);
74          display.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
75          FontData[] fontData = display.getFont().getFontData();
76          for (FontData data : fontData) {
77              data.setStyle(SWT.BOLD);
78          }
79          display.setFont(new Font(display.getDisplay(), fontData));
80          display.setText(text);
81          try {
82              Field field = getClass().getDeclaredField(label);
83              Label ref = new Label(this, SWT.SHADOW_ETCHED_IN);
84              ref.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));
85              field.set(this, ref);
86          } catch (Exception e) {
87              // should never happen
88          }
89      }
90  
91      public void setProvince(Province province) {
92          this.province = province;
93          update();
94      }
95  
96      @Override
97      public void update() {
98          NumberFormat nf = I18NUtil.getGroupFormat();
99          NumberFormat df = I18NUtil.getPrecisionFormat();
100         NumberFormat pf = I18NUtil.getPercentageFormat();
101 
102         name.setText(String.format("%s %s of %s", province.getRank(), province.getLeader(), province.getName()));
103         racePerso.setText(String.format("%s / %s", province.getRace().getDisplayName(),
104                 province.getPersonality().getDisplayName()));
105         networth.setText(String.format("%s (%s nwpa)", nf.format(province.getNetworth()),
106                 df.format(province.getNetworth() / (double) province.getAcres())));
107         float popTotal = province.getPeasants() + province.getThieves() + province.getWizards() + province.getSoldiers()
108             + province.getOffspecs() + province.getDefspecs() + province.getElites();
109         peasants.setText(String.format("%s (%s ppa)", nf.format(province.getPeasants()),
110                 nf.format(popTotal / province.getAcres())));
111         thieves.setText(String.format("%s (%s tpa)", nf.format(province.getThieves()),
112                 nf.format((float) province.getThieves() / (float) province.getAcres())));
113         wizards.setText(String.format("%s (%s wpa)", nf.format(province.getWizards()),
114                 nf.format((float) province.getWizards() / (float) province.getAcres())));
115         gold.setText(nf.format(province.getGold()));
116         food.setText(nf.format(province.getFood()));
117         runes.setText(nf.format(province.getRunes()));
118         land.setText(String.format("%s (%s BE)", nf.format(province.getAcres()),
119                 pf.format(province.getSurvey().getEfficiency())));
120 
121         int soldierCount = province.getSoldiers();
122         int offspecCount = province.getOffspecs();
123         int defspecCount = province.getDefspecs();
124         int eliteCount = province.getElites();
125         int horseCount = province.getHorses();
126         Calendar now = CalendarUtils.getCalendar();
127         Collection<Army> armies = province.getMilitary().getArmies();
128         boolean away = false;
129         if (!province.getMilitary().isRaw()) {
130             for (Army army : armies) {
131                 if (army.getReturnTime().after(now)) {
132                     soldierCount -= army.getSoldiers();
133                     offspecCount -= army.getOffspecs();
134                     defspecCount -= army.getDefspecs();
135                     eliteCount -= army.getElites();
136                     horseCount -= army.getHorses();
137                     away = true;
138                 }
139             }
140         }
141         if (away) {
142             soldiers.setText(String.format("%s / %s", nf.format(soldierCount), nf.format(province.getSoldiers())));
143             offspecs.setText(String.format("%s / %s", nf.format(offspecCount), nf.format(province.getOffspecs())));
144             defspecs.setText(String.format("%s / %s", nf.format(defspecCount), nf.format(province.getDefspecs())));
145             elites.setText(String.format("%s / %s", nf.format(eliteCount), nf.format(province.getElites())));
146             horses.setText(String.format("%s / %s", nf.format(horseCount), nf.format(province.getHorses())));
147             militaryOffense.setText(String.format("%s / %s [%s eff]",
148                     nf.format(Math.round(province.getCurrentOffense() * 1.09)),
149                     nf.format(Math.round(province.getOffense() * 1.09)),
150                     pf.format(province.getOffme())));
151             militaryDefense.setText(String.format("%s / %s [%s eff]",
152                     nf.format(province.getCurrentDefense()),
153                     nf.format(province.getDefense()),
154                     pf.format(province.getDefme())));
155         } else {
156             soldiers.setText(nf.format(province.getSoldiers()));
157             offspecs.setText(nf.format(province.getOffspecs()));
158             defspecs.setText(nf.format(province.getDefspecs()));
159             elites.setText(nf.format(province.getElites()));
160             horses.setText(nf.format(province.getHorses()));
161             militaryOffense.setText(String.format("%s (%s opa) [%s eff]",
162                     nf.format(Math.round(province.getOffense() * 1.09)),
163                     nf.format((float) province.getOffense() / (float) province.getAcres()),
164                     pf.format(province.getOffme())));
165             militaryDefense.setText(String.format("%s (%s dpa) [%s eff]",
166                     nf.format(province.getDefense()),
167                     nf.format((float) province.getDefense() / (float) province.getAcres()),
168                     pf.format(province.getDefme())));
169         }
170         prisoners.setText(nf.format(province.getPrisoners()));
171         super.update();
172     }
173 }