⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camochoicedialog.java

📁 MegaMek is a networked Java clone of BattleTech, a turn-based sci-fi boardgame for 2+ players. Fight
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * MegaMek - Copyright (C) 2004 Ben Mazur (bmazur@sev.org) * *  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 2 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. */package megamek.client.ui.swing;import megamek.client.ui.swing.util.ImageFileFactory;import megamek.client.ui.swing.util.PlayerColors;import megamek.common.Player;import megamek.common.util.DirectoryItems;import javax.swing.DefaultListModel;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JList;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.ListSelectionModel;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Image;import java.awt.ItemSelectable;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.ItemListener;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.File;import java.util.Enumeration;import java.util.Vector;/** * This dialog allows players to select the camo pattern (or color) used by * their units during the game.  It automatically fills itself with all the * color choices in <code>Settings</code> and all the camo patterns in the * "data/iamges/camo" directory tree. * <p/> * Created on January 19, 2004 * * @author James Damour * @version 1 */public class CamoChoiceDialog extends JDialog implements ActionListener,        ItemListener,        ItemSelectable, ListSelectionListener {    /**     * The parent <code>Frame</code> of this dialog.     */    private JFrame frame;    /**     * The categorized camo patterns.     */    private DirectoryItems camos;    /**     * The menu containing the category names.     */    private JComboBox categories;    /**     * The list containing the item names.     */    private JList items;    /**     * The "keep old camo" button.     */    private JButton keep;    /**     * The "select new camo" button.     */    private JButton select;    /**     * The previously selected category.     */    private String prevCat;    /**     * The previously selected item.     */    private String prevItem;    /**     * The registered camo selection listeners.     */    private Vector listeners = new Vector();    /**     * A helper function to close the dialog.     */    /* package */    private void close() {        // Make sure the previous selection is the current selection.        setCategory(prevCat);        setItemName(prevItem);        // And hide the dialog.        setVisible(false);    }    /**     * A helper function to fill the list with items in the selected category.     *     * @param category - the <code>String</code> name of the category     *                 whose items should be displayed.     */    /* package */    private void fillList(String category) {        // Clear the list of items.        ((DefaultListModel) items.getModel()).removeAllElements();        // If this is the "no camos" category, then        // fill the item list with the colors.        if (Player.NO_CAMO.equals(category)) {            for (String color : Player.colorNames) {                ((DefaultListModel) items.getModel()).addElement(color);            }        }        // Otherwise, fill the list with the camo names.        else {            // Translate the "root camo" category name.            Enumeration camoNames;            if (Player.ROOT_CAMO.equals(category)) {                camoNames = camos.getItemNames(""); //$NON-NLS-1$            } else {                camoNames = camos.getItemNames(category);            }            // Get the camo names for this category.            while (camoNames.hasMoreElements()) {                ((DefaultListModel) items.getModel()).addElement(camoNames.nextElement());            }        }        // If this is the previous selection, then        // select the previous item in the category.        // Otherwise, select the first item in the list.        if (prevCat.equals(category)) {            setItemName(prevItem);        } else {            setItemName((String) items.getModel().getElementAt(0));        }    }    /**     * A helper function to assign values for the previously selected camo.     * This function will also set the "keep old camo" button's image.     * Please note, if the specified selection does not exist, or if there     * is an error when generating the selection's image, the values won't     * change.     *     * @param category - the <code>String</code> category name.     *                 This value must be one of the categories from the     *                 <code>DirectoryItems</code>.     * @param item     - the <code>String</code> name of the item.     *                 This value must be one of the items in the named     *                 category from <code>DirectoryItems</code>.     */    /* package */    private void setPrevSelection(String category, String item) {        // If a "no camo" item is selected, clear the image.        if (Player.NO_CAMO.equals(category)) {            keep.setIcon(null);            // Find the correct background color.            for (int color = 0; color < Player.colorNames.length; color++) {                if (Player.colorNames[color].equals(item)) {                    keep.setBackground(PlayerColors.getColor(color));                    prevCat = category;                    prevItem = item;                    break;                }            }        }        // Otherwise, clear the background color and try to        // set the camo image for the "keep old camo" button.        else {            try {                // Don't forget to translate the ROOT_CAMO.                String curCat = category;                if (Player.ROOT_CAMO.equals(curCat)) {                    curCat = ""; //$NON-NLS-1$                }                // We need to copy the image to make it appear.                Image image = (Image) camos.getItem(curCat, item);                // Now, we're ready.                keep.setBackground(categories.getBackground());                keep.setIcon(new ImageIcon(image));                prevCat = category;                prevItem = item;            } catch (Exception err) {                // Print the stack trace and display the message.                err.printStackTrace();                JOptionPane.showMessageDialog(frame, err.getMessage(), Messages.getString("CamoChoiceDialog.error_getting_camo"), JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$            }        }    }    /**     * Create a dialog that allows players to choose a camo pattern.     *     * @param parent - the <code>Frame</code> that displays this dialog.     */    public CamoChoiceDialog(JFrame parent) {        // Initialize our superclass and record our parent frame.        super(parent, Messages.getString("CamoChoiceDialog.select_camo_pattern"), true); //$NON-NLS-1$        frame = parent;        // Declare local variables.        Enumeration names;        String name;        // Parse the camo directory.        try {            camos = new DirectoryItems(new File("data/images/camo"), "", //$NON-NLS-1$ //$NON-NLS-2$                    ImageFileFactory.getInstance());        } catch (Exception e) {            camos = null;        }        // Close the window, when the WM says to.        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e) {                close();            }        });                // Use a border layout.        getContentPane().setLayout(new BorderLayout());        // Create a pulldown menu for the categories.        JPanel panel = new JPanel();        getContentPane().add(panel, BorderLayout.NORTH);        panel.setLayout(new GridBagLayout());        GridBagConstraints layout = new GridBagConstraints();        layout.anchor = GridBagConstraints.CENTER;        categories = new JComboBox();        panel.add(categories, layout);        // Fill the pulldown.  Include the "no camo" category.        // Make sure the "no camo" and "root camo" are at top.        // Only add the "root camo" category if it contains items.        categories.addItem(Player.NO_CAMO);        if (camos != null) {            if (camos.getItemNames("").hasMoreElements()) { //$NON-NLS-1$                categories.addItem(Player.ROOT_CAMO);            }            names = camos.getCategoryNames();            while (names.hasMoreElements()) {                name = (String) names.nextElement();                if (!"".equals(name)) { //$NON-NLS-1$                    categories.addItem(name);                }            }        }        // Refill the item list when a new category is selected.        // Make sure that the "select new camo" button is updated.        categories.addItemListener(new ItemListener() {            public void itemStateChanged(ItemEvent event) {                fillList((String) event.getItem());                updateButton();            }        });        // Create a list to hold the items in the category.        items = new JList(new DefaultListModel());        items.setPreferredSize(new Dimension(150, 200));        getContentPane().add(new JScrollPane(items), BorderLayout.CENTER);        // Update the "select new camo" when an item is selected.        items.addListSelectionListener(this);        items.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);        // Create a panel to hold our buttons.        // Use a grid bag layout.        panel = new JPanel();        panel.setLayout(new GridBagLayout());        getContentPane().add(panel, BorderLayout.EAST);        layout = new GridBagConstraints();        layout.anchor = GridBagConstraints.EAST;        layout.gridx = 0;        layout.gridy = 0;        layout.gridwidth = 1;        layout.gridheight = 1;        layout.fill = GridBagConstraints.NONE;        layout.ipadx = 4;        layout.ipady = 4;        layout.weightx = 0.0;        layout.weighty = 0.0;        // Add a "spacer" label to push everything else to the bottom.        layout.weighty = 1.0;        panel.add(new JLabel(), layout);        layout.weighty = 0.0;        layout.gridy++;        // Add a label for the "keep old camo" button.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -