|
| 1 | +package mekhq.gui.dialog; |
| 2 | + |
| 3 | +import java.awt.Component; |
| 4 | +import java.awt.Container; |
| 5 | +import java.awt.Dimension; |
| 6 | +import java.awt.Frame; |
| 7 | +import java.awt.GridBagConstraints; |
| 8 | +import java.awt.GridBagLayout; |
| 9 | +import java.awt.event.ActionEvent; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Objects; |
| 13 | +import java.util.ResourceBundle; |
| 14 | +import java.util.TreeMap; |
| 15 | + |
| 16 | +import javax.swing.AbstractAction; |
| 17 | +import javax.swing.AbstractListModel; |
| 18 | +import javax.swing.DefaultListCellRenderer; |
| 19 | +import javax.swing.JButton; |
| 20 | +import javax.swing.JDialog; |
| 21 | +import javax.swing.JList; |
| 22 | +import javax.swing.JScrollPane; |
| 23 | + |
| 24 | +import org.joda.time.DateTime; |
| 25 | + |
| 26 | +import megamek.common.util.EncodeControl; |
| 27 | +import mekhq.campaign.universe.Era; |
| 28 | +import mekhq.campaign.universe.Faction; |
| 29 | + |
| 30 | +public class ChooseFactionsDialog extends JDialog { |
| 31 | + private static final long serialVersionUID = 805616085217507489L; |
| 32 | + |
| 33 | + private DateTime date; |
| 34 | + |
| 35 | + ResourceBundle resourceMap; |
| 36 | + private JList<Faction> factionList; |
| 37 | + private List<String> result; |
| 38 | + private boolean changed; |
| 39 | + |
| 40 | + public ChooseFactionsDialog(Frame parent, DateTime date, List<String> defaults) { |
| 41 | + this(parent, date, defaults, true); |
| 42 | + } |
| 43 | + |
| 44 | + public ChooseFactionsDialog(Frame parent, DateTime date, List<String> defaults, boolean modal) { |
| 45 | + super(parent, modal); |
| 46 | + this.date = Objects.requireNonNull(date); |
| 47 | + this.result = defaults; |
| 48 | + this.changed = false; |
| 49 | + initComponents(); |
| 50 | + setLocationRelativeTo(parent); |
| 51 | + } |
| 52 | + |
| 53 | + protected void initComponents() { |
| 54 | + resourceMap = ResourceBundle.getBundle("mekhq.resources.ChooseFactionsDialog", new EncodeControl()); //$NON-NLS-1$ |
| 55 | + setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); |
| 56 | + setName("form"); //$NON-NLS-1$ |
| 57 | + setTitle(resourceMap.getString("Form.title")); //$NON-NLS-1$ |
| 58 | + setMinimumSize(new Dimension(400, 500)); |
| 59 | + |
| 60 | + final Container content = getContentPane(); |
| 61 | + content.setLayout(new GridBagLayout()); |
| 62 | + GridBagConstraints gbc = new GridBagConstraints(); |
| 63 | + gbc.gridx = 0; |
| 64 | + gbc.gridy = 0; |
| 65 | + gbc.gridwidth = 2; |
| 66 | + gbc.weightx = 1.0; |
| 67 | + gbc.weighty = 1.0; |
| 68 | + gbc.fill = GridBagConstraints.BOTH; |
| 69 | + JScrollPane scrollPane = new JScrollPane(); |
| 70 | + factionList = new JList<>(new FactionListModel(date)); |
| 71 | + factionList.setCellRenderer(new DefaultListCellRenderer(){ |
| 72 | + private static final long serialVersionUID = -2504011562223561964L; |
| 73 | + |
| 74 | + @Override |
| 75 | + public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, |
| 76 | + boolean cellHasFocus) { |
| 77 | + DefaultListCellRenderer result = (DefaultListCellRenderer) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| 78 | + if(value instanceof Faction) { |
| 79 | + result.setText(((Faction)value).getFullName(Era.getEra(date.getYear()))); |
| 80 | + } |
| 81 | + return result; |
| 82 | + } |
| 83 | + |
| 84 | + }); |
| 85 | + scrollPane.setViewportView(factionList); |
| 86 | + content.add(scrollPane, gbc); |
| 87 | + |
| 88 | + gbc.gridy = 1; |
| 89 | + gbc.gridwidth = 1; |
| 90 | + gbc.weighty = 0.0; |
| 91 | + gbc.anchor = GridBagConstraints.WEST; |
| 92 | + gbc.fill = GridBagConstraints.NONE; |
| 93 | + content.add(new JButton(new AbstractAction(resourceMap.getString("ok.label")) { //$NON-NLS-1$ |
| 94 | + private static final long serialVersionUID = -8920630119126015954L; |
| 95 | + |
| 96 | + @Override |
| 97 | + public void actionPerformed(ActionEvent e) { |
| 98 | + result = new ArrayList<>(); |
| 99 | + for(Faction faction : factionList.getSelectedValuesList()) { |
| 100 | + result.add(faction.getShortName()); |
| 101 | + } |
| 102 | + changed = true; |
| 103 | + setVisible(false); |
| 104 | + } |
| 105 | + }), gbc); |
| 106 | + |
| 107 | + gbc.gridx = 1; |
| 108 | + gbc.anchor = GridBagConstraints.EAST; |
| 109 | + content.add(new JButton(new AbstractAction(resourceMap.getString("cancel.label")){ //$NON-NLS-1$ |
| 110 | + private static final long serialVersionUID = -8920630119126015955L; |
| 111 | + |
| 112 | + @Override |
| 113 | + public void actionPerformed(ActionEvent e) { |
| 114 | + setVisible(false); |
| 115 | + } |
| 116 | + }), gbc); |
| 117 | + pack(); |
| 118 | + } |
| 119 | + |
| 120 | + public List<String> getResult() { |
| 121 | + return result; |
| 122 | + } |
| 123 | + |
| 124 | + public boolean isChanged() { |
| 125 | + return changed; |
| 126 | + } |
| 127 | + |
| 128 | + private static class FactionListModel extends AbstractListModel<Faction> { |
| 129 | + private static final long serialVersionUID = 2779479232585980171L; |
| 130 | + |
| 131 | + private TreeMap<String, Faction> factionMap = new TreeMap<>(); |
| 132 | + private List<String> names; |
| 133 | + |
| 134 | + public FactionListModel(DateTime date) { |
| 135 | + int era = Era.getEra(date.getYear()); |
| 136 | + for(Faction faction : Faction.factions.values()) { |
| 137 | + factionMap.put(faction.getFullName(era), faction); |
| 138 | + } |
| 139 | + names = new ArrayList<>(factionMap.navigableKeySet()); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public int getSize() { |
| 144 | + return names.size(); |
| 145 | + } |
| 146 | + |
| 147 | + @Override |
| 148 | + public Faction getElementAt(int index) { |
| 149 | + return factionMap.get(names.get(index)); |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments