View Javadoc

1   package org.inigma.waragent.view;
2   
3   import java.text.NumberFormat;
4   
5   import org.eclipse.swt.SWT;
6   import org.eclipse.swt.layout.FillLayout;
7   import org.eclipse.swt.widgets.Composite;
8   import org.eclipse.swt.widgets.Table;
9   import org.eclipse.swt.widgets.TableColumn;
10  import org.eclipse.swt.widgets.TableItem;
11  import org.inigma.utopia.Science;
12  import org.inigma.utopia.utils.I18NUtil;
13  
14  /**
15   * @author <a href="mailto:sejal@inigma.org">Sejal Patel</a>
16   */
17  class ScienceComposite extends Composite {
18      private Table table;
19  
20      private Science science;
21      private int acres;
22  
23      public ScienceComposite(Composite parent, Science science) {
24          super(parent, SWT.NONE);
25          setLayout(new FillLayout());
26  
27          table = new Table(this, SWT.SINGLE);
28          table.setHeaderVisible(true);
29          table.setLinesVisible(true);
30          TableColumn col = new TableColumn(table, SWT.NONE);
31          col.setText("Knowledge");
32          col.setWidth(125);
33          col = new TableColumn(table, SWT.NONE);
34          col.setText("Effect %");
35          col.setWidth(75);
36          col.setAlignment(SWT.RIGHT);
37          col = new TableColumn(table, SWT.NONE);
38          col.setText("Books");
39          col.setWidth(100);
40          col.setAlignment(SWT.RIGHT);
41          col = new TableColumn(table, SWT.NONE);
42          col.setText("Description");
43          col.setWidth(300);
44  
45          setScience(science);
46      }
47  
48      public void setScience(Science science) {
49          this.science = science;
50          this.acres = science.getProvince().getAcres();
51          update();
52      }
53  
54      @Override
55      public void update() {
56          NumberFormat pf = I18NUtil.getPrecisionFormat();
57          NumberFormat nf = I18NUtil.getGroupFormat();
58          table.removeAll();
59  
60          TableItem item = new TableItem(table, SWT.NONE);
61          item.setText(0, "Alchemy");
62          item.setText(1, pf.format(getAlchemyBonus()));
63          item.setText(2, nf.format(science.getAlchemy()));
64          item.setText(3, "Income");
65  
66          item = new TableItem(table, SWT.NONE);
67          item.setText(0, "Tools");
68          item.setText(1, pf.format(getToolsBonus()));
69          item.setText(2, nf.format(science.getTools()));
70          item.setText(3, "Building Effectiveness");
71  
72          item = new TableItem(table, SWT.NONE);
73          item.setText(0, "Housing");
74          item.setText(1, pf.format(getHousingBonus()));
75          item.setText(2, nf.format(science.getHousing()));
76          item.setText(3, "Population Limits");
77          
78          item = new TableItem(table, SWT.NONE);
79          item.setText(0, "Food");
80          item.setText(1, pf.format(getFoodBonus()));
81          item.setText(2, nf.format(science.getFood()));
82          item.setText(3, "Food Production");
83          
84          item = new TableItem(table, SWT.NONE);
85          item.setText(0, "Military");
86          item.setText(1, pf.format(getMilitaryBonus()));
87          item.setText(2, nf.format(science.getMilitary()));
88          item.setText(3, "Gains in Combat");
89          
90          item = new TableItem(table, SWT.NONE);
91          item.setText(0, "Crime");
92          item.setText(1, pf.format(getCrimeBonus()));
93          item.setText(2, nf.format(science.getCrime()));
94          item.setText(3, "Thievery Effectiveness");
95          
96          item = new TableItem(table, SWT.NONE);
97          item.setText(0, "Channeling");
98          item.setText(1, pf.format(getChannelingBonus()));
99          item.setText(2, nf.format(science.getChanneling()));
100         item.setText(3, "Magic Effectiveness & Rune Production");
101         
102         table.update();
103         super.update();
104     }
105 
106     private double getBonus(double value, double factor) {
107         if (acres == 0) {
108             return 0;
109         }
110         return Math.sqrt(value / acres) * factor;
111     }
112 
113     public double getAlchemyBonus() {
114         return getBonus(science.getAlchemy(), Science.ALCHEMY_FACTOR);
115     }
116 
117     public double getToolsBonus() {
118         return getBonus(science.getTools(), Science.TOOLS_FACTOR);
119     }
120 
121     public double getHousingBonus() {
122         return getBonus(science.getHousing(), Science.HOUSING_FACTOR);
123     }
124 
125     public double getFoodBonus() {
126         return getBonus(science.getFood(), Science.FOOD_FACTOR);
127     }
128 
129     public double getMilitaryBonus() {
130         return getBonus(science.getMilitary(), Science.MILITARY_FACTOR);
131     }
132 
133     public double getCrimeBonus() {
134         return getBonus(science.getCrime(), Science.CRIME_FACTOR);
135     }
136 
137     public double getChannelingBonus() {
138         return getBonus(science.getChanneling(), Science.CHANNELING_FACTOR);
139     }
140 }