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
16
* TabbedProductionPanel.java
18
* Created on June 11, 2011
21
package games.strategy.triplea.ui;
23
import games.strategy.engine.data.*;
24
import games.strategy.triplea.attatchments.UnitAttachment;
25
import games.strategy.util.IntegerMap;
28
import java.math.BigDecimal;
35
* @author Edwin van der Wal
40
public class TabbedProductionPanel extends ProductionPanel
42
private int m_maxColumns = 10;
44
private int m_columns;
46
protected TabbedProductionPanel(UIContext uiContext) {
50
public static IntegerMap<ProductionRule> getProduction(PlayerID id, JFrame parent, GameData data, boolean bid, IntegerMap<ProductionRule> initialPurchase, UIContext context)
52
return new TabbedProductionPanel(context).show(id, parent, data, bid, initialPurchase);
57
protected void initLayout(PlayerID id)
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));
63
JTabbedPane tabs = new JTabbedPane();
65
add(tabs,new GridBagConstraints(0,1,1,1,100,100,GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(8, 8, 8, 0), 0, 0));
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>();
74
for(Rule rule : m_rules) {
75
UnitType type = (UnitType) rule.getProductionRule().getResults().keySet().iterator().next();
76
UnitAttachment attach= UnitAttachment.get(type);
80
if (attach.getConsumesUnits() != null && attach.getConsumesUnits().totalValues() >= 1)
81
upgradeConsumesRules.add(rule);
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()) {
87
} else if(attach.isAir()) {
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()));
99
m_maxColumns = Math.max(8, Math.min(16, new BigDecimal(m_rules.size()).divide(new BigDecimal(5),BigDecimal.ROUND_UP).intValue()));
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());
105
if(allRules.size()>0) {
106
tabs.addTab("All", new JScrollPane(getRulesPanel(allRules)));
108
if(landRules.size()>0){
109
tabs.addTab("Land", new JScrollPane(getRulesPanel(landRules)));
111
if(airRules.size()>0) {
112
tabs.addTab("Air", new JScrollPane(getRulesPanel(airRules)));
114
if(seaRules.size()>0) {
115
tabs.addTab("Sea", new JScrollPane(getRulesPanel(seaRules)));
117
if(constructRules.size()>0) {
118
tabs.addTab("Construction",new JScrollPane(getRulesPanel(constructRules)));
120
if(upgradeConsumesRules.size()>0) {
121
tabs.addTab("Upgrades/Consumes",new JScrollPane(getRulesPanel(upgradeConsumesRules)));
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));
128
Dimension dtab = tabs.getPreferredSize();
129
Dimension dthis = this.getPreferredSize();
130
if (dtab != null && dthis != null)
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
139
private JPanel getRulesPanel(ArrayList<Rule> rules)
141
JPanel panel = new JPanel();
142
panel.setLayout(new GridLayout(m_rows,m_columns));
143
JPanel[][] panelHolder = new JPanel[m_rows][m_columns];
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]);
152
for (int x = 0; x < m_columns * m_rows; x++)
154
if (x < rules.size())
155
panelHolder[(x % m_rows)][(x / m_rows)].add(rules.get(x).getPanelComponent());
157
//panelHolder[(x % m_rows)][(x / m_rows)].add(new JPanel());