~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to src/games/strategy/triplea/ui/TabbedProductionPanel.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This program is free software; you can redistribute it and/or modify
 
3
 * it under the terms of the GNU General Public License as published by
 
4
 * the Free Software Foundation; either version 2 of the License, or
 
5
 * (at your option) any later version.
 
6
 * This program is distributed in the hope that it will be useful,
 
7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
8
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
9
 * GNU General Public License for more details.
 
10
 * You should have received a copy of the GNU General Public License
 
11
 * along with this program; if not, write to the Free Software
 
12
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
13
 */
 
14
 
 
15
/*
 
16
 * TabbedProductionPanel.java
 
17
 *
 
18
 * Created on June 11, 2011
 
19
 */
 
20
 
 
21
package games.strategy.triplea.ui;
 
22
 
 
23
import games.strategy.engine.data.*;
 
24
import games.strategy.triplea.attatchments.UnitAttachment;
 
25
import games.strategy.util.IntegerMap;
 
26
 
 
27
import java.awt.*;
 
28
import java.math.BigDecimal;
 
29
import java.util.*;
 
30
 
 
31
import javax.swing.*;
 
32
 
 
33
/**
 
34
 * 
 
35
 * @author Edwin van der Wal
 
36
 * @version 1.0
 
37
 * 
 
38
 *  
 
39
 */
 
40
public class TabbedProductionPanel extends ProductionPanel
 
41
{
 
42
        private int m_maxColumns = 10;
 
43
    private int m_rows;
 
44
        private int m_columns;
 
45
 
 
46
        protected TabbedProductionPanel(UIContext uiContext) {
 
47
                super(uiContext);
 
48
        }
 
49
 
 
50
        public static IntegerMap<ProductionRule> getProduction(PlayerID id, JFrame parent, GameData data, boolean bid, IntegerMap<ProductionRule> initialPurchase, UIContext context)
 
51
    {
 
52
        return new TabbedProductionPanel(context).show(id, parent, data, bid, initialPurchase);
 
53
    }
 
54
       
 
55
    
 
56
    @Override
 
57
    protected void initLayout(PlayerID id)
 
58
    {
 
59
        this.removeAll();
 
60
        this.setLayout(new GridBagLayout());
 
61
        add(new JLabel("Attack/Defense/Movement"), new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.EAST, GridBagConstraints.HORIZONTAL, new Insets(8, 8, 8, 0), 0, 0));
 
62
 
 
63
        JTabbedPane tabs = new JTabbedPane();
 
64
        
 
65
        add(tabs,new GridBagConstraints(0,1,1,1,100,100,GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(8, 8, 8, 0), 0, 0));
 
66
 
 
67
        ArrayList<Rule> allRules = new ArrayList<Rule>();
 
68
        ArrayList<Rule> landRules = new ArrayList<Rule>();
 
69
        ArrayList<Rule> airRules = new ArrayList<Rule>();
 
70
        ArrayList<Rule> seaRules = new ArrayList<Rule>();
 
71
        ArrayList<Rule> constructRules = new ArrayList<Rule>();
 
72
        ArrayList<Rule> upgradeConsumesRules = new ArrayList<Rule>();
 
73
 
 
74
        for(Rule rule : m_rules) {
 
75
            UnitType type = (UnitType) rule.getProductionRule().getResults().keySet().iterator().next();
 
76
            UnitAttachment attach= UnitAttachment.get(type);
 
77
            
 
78
            allRules.add(rule);
 
79
            
 
80
            if (attach.getConsumesUnits() != null && attach.getConsumesUnits().totalValues() >= 1)
 
81
                upgradeConsumesRules.add(rule);
 
82
            
 
83
            if(attach.isConstruction() || attach.isFactory()) { // canproduceUnits isn't checked on purpose, since this category is for units that can be placed anywhere (placed without needing a factory).
 
84
                constructRules.add(rule);
 
85
            } else if(attach.isSea()) {
 
86
                seaRules.add(rule);
 
87
            } else if(attach.isAir()) {
 
88
                                airRules.add(rule);
 
89
            } else {
 
90
                landRules.add(rule);
 
91
            }
 
92
        }
 
93
        
 
94
        if (m_rules.size() <= 36) // 8 columns 2 rows is perfect for small screens, 12 columns 3 rows is perfect for mid-sized screens, while 16 columns and 5-8 rows is perfect for really big screens.
 
95
                m_maxColumns = Math.max(8, Math.min(12, new BigDecimal(m_rules.size()).divide(new BigDecimal(3),BigDecimal.ROUND_UP).intValue()));
 
96
        else if (m_rules.size() <= 64)
 
97
                m_maxColumns = Math.max(8, Math.min(16, new BigDecimal(m_rules.size()).divide(new BigDecimal(4),BigDecimal.ROUND_UP).intValue()));
 
98
        else
 
99
                m_maxColumns = Math.max(8, Math.min(16, new BigDecimal(m_rules.size()).divide(new BigDecimal(5),BigDecimal.ROUND_UP).intValue()));
 
100
        
 
101
        m_rows = Math.max(2, new BigDecimal(m_rules.size()).divide(new BigDecimal(m_maxColumns),BigDecimal.ROUND_UP).intValue());
 
102
        m_columns = Math.max(3, new BigDecimal(m_rules.size()).divide(new BigDecimal(m_rows), BigDecimal.ROUND_UP).intValue());
 
103
        
 
104
        
 
105
        if(allRules.size()>0) {
 
106
                tabs.addTab("All", new JScrollPane(getRulesPanel(allRules)));
 
107
        }
 
108
        if(landRules.size()>0){
 
109
                tabs.addTab("Land", new JScrollPane(getRulesPanel(landRules)));
 
110
        }
 
111
        if(airRules.size()>0) {
 
112
                tabs.addTab("Air", new JScrollPane(getRulesPanel(airRules)));
 
113
        }
 
114
        if(seaRules.size()>0) {
 
115
                tabs.addTab("Sea", new JScrollPane(getRulesPanel(seaRules)));
 
116
        }
 
117
        if(constructRules.size()>0) {
 
118
                tabs.addTab("Construction",new JScrollPane(getRulesPanel(constructRules)));
 
119
        }
 
120
        if(upgradeConsumesRules.size()>0) {
 
121
                tabs.addTab("Upgrades/Consumes",new JScrollPane(getRulesPanel(upgradeConsumesRules)));
 
122
        }
 
123
               
 
124
        add(m_left, new GridBagConstraints(0, 2, 1, 1, 1, 1, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(8, 8, 0, 12), 0, 0));
 
125
        m_done = new JButton(m_done_action);
 
126
        add(m_done, new GridBagConstraints(0, 3, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0,  0, 8, 0), 0, 0));
 
127
        
 
128
        Dimension dtab = tabs.getPreferredSize();
 
129
        Dimension dthis = this.getPreferredSize();
 
130
        if (dtab != null && dthis != null)
 
131
        {
 
132
                tabs.setPreferredSize(new Dimension(dtab.width + 2, dtab.height + 2)); // for whatever dumb reason, the tabs need a couple extra height and width or else scroll bars will appear
 
133
            this.setPreferredSize(new Dimension(dthis.width + 4, dthis.height + 20)); // for whatever dumb reason, the window needs to be at least 16 pixels greater in height than normal, to accommodate the tabs
 
134
        }
 
135
        tabs.validate();
 
136
        this.validate();
 
137
    }
 
138
    
 
139
    private JPanel getRulesPanel(ArrayList<Rule> rules)
 
140
    {
 
141
        JPanel panel = new JPanel();
 
142
        panel.setLayout(new GridLayout(m_rows,m_columns));
 
143
        JPanel[][] panelHolder = new JPanel[m_rows][m_columns];
 
144
        
 
145
        for (int m = 0; m < m_rows; m++) {
 
146
                for (int n = 0; n < m_columns; n++) {
 
147
                        panelHolder[m][n] = new JPanel(new BorderLayout());
 
148
                        panel.add(panelHolder[m][n]);
 
149
                }
 
150
        }
 
151
        
 
152
        for (int x = 0; x < m_columns * m_rows; x++)
 
153
        {
 
154
                if (x < rules.size())
 
155
                        panelHolder[(x % m_rows)][(x / m_rows)].add(rules.get(x).getPanelComponent());
 
156
                //else
 
157
                        //panelHolder[(x % m_rows)][(x / m_rows)].add(new JPanel());
 
158
        }
 
159
        return panel;
 
160
    }
 
161
}
 
162