diff --git a/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java b/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java index 6f11993a74..59eacfaa88 100644 --- a/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java +++ b/Mage.Client/src/main/java/mage/client/dialog/NewTournamentDialog.java @@ -35,17 +35,15 @@ package mage.client.dialog; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.UUID; import javax.swing.ComboBoxModel; import javax.swing.DefaultComboBoxModel; -import javax.swing.DefaultListModel; -import javax.swing.DefaultListSelectionModel; +import javax.swing.JButton; import javax.swing.JComboBox; -import javax.swing.JList; import javax.swing.JOptionPane; -import javax.swing.JScrollPane; -import static javax.swing.ListSelectionModel.SINGLE_SELECTION; +import javax.swing.JTextArea; import javax.swing.SpinnerNumberModel; import mage.cards.decks.importer.DeckImporterUtil; import mage.cards.repository.ExpansionInfo; @@ -80,12 +78,12 @@ public class NewTournamentDialog extends MageDialog { private UUID roomId; private final Session session; private String lastSessionId; - private JList randomList = new JList(); + private RandomPacksSelectorDialog randomPackSelector; + private JTextArea txtRandomPacks; private final List players = new ArrayList<>(); private final List packs = new ArrayList<>(); private final int CONSTRUCTION_TIME_MIN = 6; private final int CONSTRUCTION_TIME_MAX = 30; - private final String randomDraftDescription = ("The selected packs will be randomly distributed to players. Each player may open different packs. Duplicates will be avoided."); private boolean automaticChange = false; @@ -534,15 +532,8 @@ public class NewTournamentDialog extends MageDialog { if (tournamentType.isCubeBooster()) { tOptions.getLimitedOptions().setDraftCubeName(this.cbDraftCube.getSelectedItem().toString()); } else if (tournamentType.isRandom()) { - for (Object pack : randomList.getSelectedValuesList()) { - String packStr = (String) pack; - String code = packStr.substring(0, 3); - tOptions.getLimitedOptions().getSetCodes().add(code); - } - if (tOptions.getLimitedOptions().getSetCodes().size() < 2) { - // At least two sets must be chosen. - return; - } + tOptions.getLimitedOptions().getSetCodes().clear(); + tOptions.getLimitedOptions().getSetCodes().addAll(randomPackSelector.getSelectedPacks()); } else { for (JComboBox pack: packs) { tOptions.getLimitedOptions().getSetCodes().add(((ExpansionInfo) pack.getSelectedItem()).getCode()); @@ -724,60 +715,54 @@ public class NewTournamentDialog extends MageDialog { private void createRandomPacks() { if (pnlRandomPacks.getComponentCount() == 0) { - - DefaultListModel randomListModel = new DefaultListModel(); - randomList = new JList(randomListModel); - randomList.setToolTipText(randomDraftDescription); - ExpansionInfo[] allExpansions = ExpansionRepository.instance.getWithBoostersSortedByReleaseDate(); - for (ExpansionInfo expansion : allExpansions) { - String exp = expansion.getCode() + " - " + expansion.getName(); - randomListModel.addElement(exp); + if (randomPackSelector == null) { + randomPackSelector = new RandomPacksSelectorDialog(); } - randomList.setSelectionModel(new DefaultListSelectionModel() { - private boolean mGestureStarted; - - @Override - public void setSelectionInterval(int index0, int index1) { - // Toggle only one element while the user is dragging the mouse - if (!mGestureStarted) { - if (isSelectedIndex(index0)) { - super.removeSelectionInterval(index0, index1); - } else { - if (getSelectionMode() == SINGLE_SELECTION) { - super.setSelectionInterval(index0, index1); - } else { - super.addSelectionInterval(index0, index1); - } - } - } - // Disable toggling till the adjusting is over, or keep it - // enabled in case setSelectionInterval was called directly. - mGestureStarted = getValueIsAdjusting(); - } - - @Override - public void setValueIsAdjusting(boolean isAdjusting) { - super.setValueIsAdjusting(isAdjusting); - - if (isAdjusting == false) { - // Enable toggling - mGestureStarted = false; - } - } - }); - + txtRandomPacks = new JTextArea(); + txtRandomPacks.setEnabled(false); + txtRandomPacks.setLineWrap(true); String randomPrefs = PreferencesDialog.getCachedValue(PreferencesDialog.KEY_NEW_TOURNAMENT_PACKS_RANDOM_DRAFT, ""); if (randomPrefs.length() > 0) { - for (String exp : randomPrefs.split(";")) { - randomList.setSelectedValue(exp, false); - } + txtRandomPacks.setText(randomPrefs); + ArrayList theList = new ArrayList<>(); + theList.addAll(Arrays.asList(randomPrefs.split(";"))); + randomPackSelector.setSelectedPacks(theList); } else { - randomList.setSelectionInterval(0, randomListModel.size() - 1); + ExpansionInfo[] allExpansions = ExpansionRepository.instance.getWithBoostersSortedByReleaseDate(); + StringBuilder packList = new StringBuilder(); + for (ExpansionInfo exp : allExpansions) { + packList.append(exp.getCode()); + packList.append(";"); + } + txtRandomPacks.setText(packList.toString()); } - JScrollPane list1scr = new JScrollPane(randomList); - randomList.setVisibleRowCount(4); - pnlRandomPacks.add(list1scr); + + pnlRandomPacks.add(txtRandomPacks); + JButton btnSelectRandomPacks = new JButton(); + btnSelectRandomPacks.setText("Select packs to be included in the pool"); + btnSelectRandomPacks.setToolTipText(RandomPacksSelectorDialog.randomDraftDescription); + btnSelectRandomPacks.addActionListener(new java.awt.event.ActionListener() { + @Override + public void actionPerformed(java.awt.event.ActionEvent evt) { + showRandomPackSelectorDialog(); + + } + }); + pnlRandomPacks.add(btnSelectRandomPacks); + } + this.pack(); + this.revalidate(); + this.repaint(); + } + + private void showRandomPackSelectorDialog() { + randomPackSelector.showDialog(); + StringBuilder packList = new StringBuilder(); + for (String str : randomPackSelector.getSelectedPacks()) { + packList.append(str); + packList.append(";"); } + this.txtRandomPacks.setText(packList.toString()); this.pack(); this.revalidate(); this.repaint(); @@ -1006,8 +991,8 @@ public class NewTournamentDialog extends MageDialog { if (tOptions.getLimitedOptions().getIsRandom()){ // save random boosters to prefs StringBuilder packlist = new StringBuilder(); - for (Object pack: randomList.getSelectedValuesList()){ - packlist.append((String)pack); + for (String pack : this.randomPackSelector.getSelectedPacks()){ + packlist.append(pack); packlist.append(";"); } PreferencesDialog.saveValue(PreferencesDialog.KEY_NEW_TOURNAMENT_PACKS_RANDOM_DRAFT, packlist.toString()); diff --git a/Mage.Client/src/main/java/mage/client/dialog/RandomPacksSelectorDialog.form b/Mage.Client/src/main/java/mage/client/dialog/RandomPacksSelectorDialog.form new file mode 100644 index 0000000000..d486cab047 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/dialog/RandomPacksSelectorDialog.form @@ -0,0 +1,106 @@ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/Mage.Client/src/main/java/mage/client/dialog/RandomPacksSelectorDialog.java b/Mage.Client/src/main/java/mage/client/dialog/RandomPacksSelectorDialog.java new file mode 100644 index 0000000000..5b9b3cc0d0 --- /dev/null +++ b/Mage.Client/src/main/java/mage/client/dialog/RandomPacksSelectorDialog.java @@ -0,0 +1,199 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package mage.client.dialog; + +import java.awt.Component; +import java.util.ArrayList; +import javax.swing.JCheckBox; +import mage.cards.repository.ExpansionInfo; +import mage.cards.repository.ExpansionRepository; + +/** + * + * @author BrodyL + */ +public class RandomPacksSelectorDialog extends javax.swing.JDialog { + + /** + * Creates new form RandomPacksSelectorDialog + */ + private boolean boxesCreated; + public final static String randomDraftDescription = ("The selected packs will be randomly distributed to players. Each player may open different packs. Duplicates will be avoided."); + + public RandomPacksSelectorDialog() { + initComponents(); + this.pnlApply.setToolTipText(randomDraftDescription); + this.pnlSelect.setToolTipText(randomDraftDescription); + boxesCreated = false; + } + + public void showDialog() { + createCheckboxes(); + pnlPacks.setVisible(true); + pnlPacks.revalidate(); + pnlPacks.repaint(); + this.pack(); + this.revalidate(); + this.repaint(); + this.setVisible(true); + this.setModal(true); + } + + public void setSelectedPacks(ArrayList packs){ + if (!boxesCreated){ + createCheckboxes(); + } + for (Component pack : pnlPacks.getComponents()) { + JCheckBox thePack = (JCheckBox) pack; + if (packs.contains(thePack.getText())) { + thePack.setSelected(true); + } else{ + thePack.setSelected(false); + } + } + } + + public ArrayList getSelectedPacks() { + ArrayList returnVal = new ArrayList<>(); + for (Component pack: pnlPacks.getComponents()){ + JCheckBox thePack = (JCheckBox) pack; + if (thePack.isSelected()){ + returnVal.add(thePack.getText()); + } + } + return returnVal; + } + + private void createCheckboxes() { + if (!boxesCreated) { + ExpansionInfo[] allExpansions = ExpansionRepository.instance.getWithBoostersSortedByReleaseDate(); + for (ExpansionInfo exp : allExpansions) { + JCheckBox pack = new JCheckBox(); + pack.setSelected(true); + pack.setText(exp.getCode()); + pack.setToolTipText(exp.getName()); + pnlPacks.add(pack); + } + pnlPacks.setVisible(true); + this.pack(); + boxesCreated = true; + pnlPacks.validate(); + } + } + + + /** + * This method is called from within the constructor to initialize the form. + * WARNING: Do NOT modify this code. The content of this method is always + * regenerated by the Form Editor. + */ + @SuppressWarnings("unchecked") + // //GEN-BEGIN:initComponents + private void initComponents() { + + pnlPacks = new java.awt.Panel(); + pnlSelect = new javax.swing.JPanel(); + btnNone = new javax.swing.JButton(); + btnAll = new javax.swing.JButton(); + pnlApply = new javax.swing.JPanel(); + btnApply = new javax.swing.JButton(); + + setTitle("Random Booster Draft Packs Selector"); + setModal(true); + setModalExclusionType(java.awt.Dialog.ModalExclusionType.APPLICATION_EXCLUDE); + setPreferredSize(new java.awt.Dimension(600, 450)); + setResizable(false); + + pnlPacks.setLayout(new java.awt.GridLayout(11, 12)); + + pnlSelect.setLayout(new javax.swing.BoxLayout(pnlSelect, javax.swing.BoxLayout.LINE_AXIS)); + + btnNone.setText("Select none"); + btnNone.setActionCommand("none"); + btnNone.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + btnNoneActionPerformed(evt); + } + }); + pnlSelect.add(btnNone); + + btnAll.setText("Select all"); + btnAll.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + btnAllActionPerformed(evt); + } + }); + pnlSelect.add(btnAll); + + pnlApply.setLayout(new javax.swing.BoxLayout(pnlApply, javax.swing.BoxLayout.LINE_AXIS)); + + btnApply.setText("Apply"); + btnApply.setToolTipText("At least two packs must be selected"); + btnApply.addActionListener(new java.awt.event.ActionListener() { + public void actionPerformed(java.awt.event.ActionEvent evt) { + btnApplyActionPerformed(evt); + } + }); + pnlApply.add(btnApply); + + javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); + getContentPane().setLayout(layout); + layout.setHorizontalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(pnlSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 241, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 300, Short.MAX_VALUE) + .addComponent(pnlApply, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addGroup(layout.createSequentialGroup() + .addComponent(pnlPacks, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addContainerGap()) + ); + layout.setVerticalGroup( + layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addGroup(layout.createSequentialGroup() + .addComponent(pnlPacks, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE) + .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) + .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) + .addComponent(pnlApply, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE) + .addComponent(pnlSelect, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)) + .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + ); + + pack(); + }// //GEN-END:initComponents + + private void btnAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAllActionPerformed + setAllCheckBoxes(true); + }//GEN-LAST:event_btnAllActionPerformed + + private void btnNoneActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNoneActionPerformed + setAllCheckBoxes(false); + }//GEN-LAST:event_btnNoneActionPerformed + + private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyActionPerformed + if (getSelectedPacks().size() < 2) { + // at least 2 packs must be selected. + } else { + this.setVisible(false); + } + }//GEN-LAST:event_btnApplyActionPerformed + + private void setAllCheckBoxes(boolean value) { + for (Component pack : pnlPacks.getComponents()) { + JCheckBox thePack = (JCheckBox) pack; + thePack.setSelected(value); + } + } + + // Variables declaration - do not modify//GEN-BEGIN:variables + private javax.swing.JButton btnAll; + private javax.swing.JButton btnApply; + private javax.swing.JButton btnNone; + private javax.swing.JPanel pnlApply; + private java.awt.Panel pnlPacks; + private javax.swing.JPanel pnlSelect; + // End of variables declaration//GEN-END:variables +}