/* * UFO Saved Game Editor * Copyright (C) 2010 Christopher Davoren * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package net.rubikscomplex.ufosge.gui; import net.rubikscomplex.ufosge.gui.table.ResearchTableModel; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.Point; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.util.ArrayList; import java.util.Iterator; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import net.rubikscomplex.ufosge.data.Facility; import net.rubikscomplex.ufosge.data.ProducibleItem; import net.rubikscomplex.ufosge.data.Research; import net.rubikscomplex.ufosge.data.SavedGame; import net.rubikscomplex.ufosge.data.UPItem; import net.rubikscomplex.ufosge.gui.table.UPItemListTable; import net.rubikscomplex.ufosge.resources.ResourceManager; import net.rubikscomplex.ufosge.util.Util; /** * * @author Chris Davoren */ public class ResearchEditDialog extends JDialog implements ActionListener { protected static final String ACTION_APPLY = "Apply"; protected static final String ACTION_CLOSE = "Close"; protected static final String ACTION_CANCEL = "Cancel"; protected static final String ACTION_RESEARCHALL = "Research All"; protected ResearchTableModel researchModel; protected ArrayList<Research> rCache; protected ArrayList<Facility> fCache; protected ArrayList<ProducibleItem> pCache; protected ArrayList<UPItem> uCache; protected UPItemListTable upItemListTable; protected SavedGame game; public ResearchEditDialog(Window owner, SavedGame game) { super(owner, "Edit Research"); setIconImage(ResourceManager.getDefaultFrameIcon().getImage()); setModal(true); this.game = game; updateCache(false); researchModel = new ResearchTableModel(game, rCache, fCache, pCache, uCache); JTable researchTable = new JTable(researchModel); researchTable.setAutoCreateRowSorter(true); JScrollPane tableScrollPane = new JScrollPane(researchTable); tableScrollPane.setAlignmentY(Component.TOP_ALIGNMENT); upItemListTable = new UPItemListTable(game, uCache); JScrollPane UPListScrollPane = new JScrollPane(upItemListTable); UPListScrollPane.setAlignmentY(Component.TOP_ALIGNMENT); JPanel researchItemTablePanel = new JPanel(); researchItemTablePanel.setLayout(new BoxLayout(researchItemTablePanel, BoxLayout.X_AXIS)); researchItemTablePanel.setBorder(BorderFactory.createTitledBorder("Research")); researchItemTablePanel.add(tableScrollPane); researchItemTablePanel.add(Box.createHorizontalStrut(10)); researchItemTablePanel.add(UPListScrollPane); researchItemTablePanel.setAlignmentX(Component.RIGHT_ALIGNMENT); researchItemTablePanel.setPreferredSize(new Dimension(500, 500)); JButton applyButton = new JButton("Apply"); applyButton.addActionListener(this); applyButton.setMnemonic(KeyEvent.VK_A); applyButton.setActionCommand(ACTION_APPLY); Util.setButtonSize(applyButton, 100, 25); JButton cancelButton = new JButton("Cancel"); cancelButton.addActionListener(this); cancelButton.setActionCommand(ACTION_CANCEL); Util.setButtonSize(cancelButton, 100, 25); JButton closeButton = new JButton("Apply & Close"); closeButton.addActionListener(this); closeButton.setMnemonic(KeyEvent.VK_C); closeButton.setActionCommand(ACTION_CLOSE); Util.setButtonSize(closeButton, 100, 25); JButton researchAllButton = new JButton("Research All..."); researchAllButton.addActionListener(this); researchAllButton.setMnemonic(KeyEvent.VK_R); researchAllButton.setActionCommand(ACTION_RESEARCHALL); Util.setButtonSize(researchAllButton, 120, 25); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.X_AXIS)); buttonPanel.add(researchAllButton); buttonPanel.add(Box.createRigidArea(new Dimension(10, 10))); buttonPanel.add(applyButton); buttonPanel.add(Box.createRigidArea(new Dimension(10, 10))); buttonPanel.add(closeButton); buttonPanel.add(Box.createRigidArea(new Dimension(10, 10))); buttonPanel.add(cancelButton); buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 10)); buttonPanel.setAlignmentX(Component.RIGHT_ALIGNMENT); Container contentPane = getContentPane(); contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.Y_AXIS)); contentPane.add(researchItemTablePanel); contentPane.add(buttonPanel); pack(); setMinimumSize(getSize()); int locx = ResourceManager.getUserPreferenceInt(ResourceManager.PREF_RESEARCHEDITDIALOG_POSX); int locy = ResourceManager.getUserPreferenceInt(ResourceManager.PREF_RESEARCHEDITDIALOG_POSY); if (locx != -1) { setLocation(locx, locy); } else { setLocationRelativeTo(owner); } getRootPane().setDefaultButton(closeButton); } public void updateCache() { updateCache(true); } protected void updateCache(boolean updateChildren) { rCache = new ArrayList<Research>(); fCache = new ArrayList<Facility>(); pCache = new ArrayList<ProducibleItem>(); uCache = new ArrayList<UPItem>(); Iterator<Research> ri = game.researchItems.iterator(); while(ri.hasNext()) { rCache.add(new Research(ri.next())); } Iterator<Facility> fi = game.facilities.iterator(); while(fi.hasNext()) { fCache.add(new Facility(fi.next())); } Iterator<ProducibleItem> pii = game.producibleItems.iterator(); while(pii.hasNext()) { pCache.add(new ProducibleItem(pii.next())); } Iterator<UPItem> uii = game.UPItems.iterator(); while(uii.hasNext()) { uCache.add(new UPItem(uii.next())); } if (updateChildren) { researchModel.setCache(rCache, fCache, pCache, uCache); upItemListTable.setCache(uCache); } } public void update() { for (Iterator<Research> it = rCache.iterator(); it.hasNext();) { Research research = it.next(); game.researchItems.get(research.index).copy(research); } for (Iterator<Facility> it = fCache.iterator(); it.hasNext();) { Facility facility = it.next(); game.facilities.get(facility.index).copy(facility); } for (Iterator<ProducibleItem> it = pCache.iterator(); it.hasNext();) { ProducibleItem producibleItem = it.next(); game.producibleItems.get(producibleItem.index).copy(producibleItem); } for (Iterator<UPItem> it = uCache.iterator(); it.hasNext();) { UPItem uPItem = it.next(); game.UPItems.get(uPItem.index).copy(uPItem); } } @Override public void dispose() { Point loc = getLocation(); ResourceManager.setUserPreferenceInt(ResourceManager.PREF_RESEARCHEDITDIALOG_POSX, loc.x); ResourceManager.setUserPreferenceInt(ResourceManager.PREF_RESEARCHEDITDIALOG_POSY, loc.y); super.dispose(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals(ACTION_APPLY)) { update(); } else if (e.getActionCommand().equals(ACTION_CANCEL)) { this.dispose(); } else if (e.getActionCommand().equals(ACTION_CLOSE)) { update(); this.dispose(); } else if (e.getActionCommand().equals(ACTION_RESEARCHALL)) { String[] options = { "Research All", "Cancel" }; // int confirmResult = JOptionPane.showConfirmDialog(this, "This will make all facilities and items buildable and unlock all UFOpaedia entries. Are you sure?", "Warning", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); int confirmResult = JOptionPane.showOptionDialog(this, "This will make all facilities and items buildable and unlock all UFOpaedia entries. Are you sure?", "Research All", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if (confirmResult == JOptionPane.YES_OPTION) { researchModel.completeAll(); upItemListTable.revalidate(); } } } }