/* * 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 java.awt.Component; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import net.rubikscomplex.ufosge.data.SavedGame; import net.rubikscomplex.ufosge.gui.table.SoldierListTableModel; import net.rubikscomplex.ufosge.util.Util; /** * * @author Chris Davoren */ public class SoldierListPanel extends JPanel implements ActionListener, MouseListener { SavedGame game; JTable soldierListTable; SoldierListTableModel tableModel; public SoldierListPanel(SavedGame game) { this.game = game; tableModel = new SoldierListTableModel(game); soldierListTable = new JTable(); soldierListTable.setModel(tableModel); soldierListTable.setAutoCreateRowSorter(true); soldierListTable.setFillsViewportHeight(true); soldierListTable.getRowSorter().toggleSortOrder(SoldierListTableModel.COLUMN_BASE); soldierListTable.addMouseListener(this); JScrollPane scrollPane = new JScrollPane(soldierListTable); scrollPane.setPreferredSize(new Dimension(400, 300)); JPanel tablePanel = new JPanel(); tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS)); tablePanel.add(scrollPane); tablePanel.setAlignmentY(Component.TOP_ALIGNMENT); JPanel buttonPanel = new JPanel(); buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); buttonPanel.setAlignmentY(Component.TOP_ALIGNMENT); buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); JButton editSoldierButton = new JButton("Edit..."); editSoldierButton.setMnemonic(KeyEvent.VK_E); editSoldierButton.addActionListener(this); Util.setButtonSize(editSoldierButton, 100, 25); editSoldierButton.setAlignmentX(Component.RIGHT_ALIGNMENT); buttonPanel.add(editSoldierButton); setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); add(tablePanel); add(buttonPanel); } public void editSelectedSoldier() { int selectedRow = soldierListTable.getSelectedRow(); if (selectedRow == -1) { return; } int modelRow = soldierListTable.convertRowIndexToModel(selectedRow); SoldierEditDialog soldierDetailsFrame = new SoldierEditDialog(null, game, tableModel.getSoldierAt(modelRow)); soldierDetailsFrame.setVisible(true); updateValues(); } public void updateValues() { tableModel.fireTableDataChanged(); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("Edit...")) { editSelectedSoldier(); } } public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { editSelectedSoldier(); } } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mousePressed(MouseEvent e) { } }