Merge pull request #4567 from spjspj/master

Add a singleton search option for deck editor search
This commit is contained in:
spjspj 2018-03-04 17:06:06 +10:00 committed by GitHub
commit 41c43cb891
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 79 additions and 3 deletions

View file

@ -645,6 +645,8 @@
<EmptySpace max="-2" attributes="0"/>
<Component id="chkRules" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="5" max="-2" attributes="0"/>
<Component id="chkUnique" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" pref="5" max="-2" attributes="0"/>
<Component id="cardCountLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="cardCount" min="-2" pref="48" max="-2" attributes="0"/>
@ -659,6 +661,7 @@
<Group type="103" groupAlignment="0" attributes="0">
<Component id="chkTypes" max="32767" attributes="0"/>
<Component id="chkRules" alignment="1" max="32767" attributes="0"/>
<Component id="chkUnique" alignment="1" max="32767" attributes="0"/>
<Component id="chkNames" alignment="1" max="32767" attributes="0"/>
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
@ -846,6 +849,29 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="chkRulesActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JCheckBox" name="chkUnique">
<Properties>
<Property name="selected" type="boolean" value="true"/>
<Property name="text" type="java.lang.String" value="Unique"/>
<Property name="toolTipText" type="java.lang.String" value="Singleton rules."/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="4"/>
<Property name="maximumSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[69, 16]"/>
</Property>
<Property name="minimumSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[69, 16]"/>
</Property>
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[69, 16]"/>
</Property>
<Property name="verticalTextPosition" type="int" value="3"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="chkUniqueActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="jButtonSearch">
<Properties>
<Property name="text" type="java.lang.String" value="Search"/>

View file

@ -232,7 +232,7 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
FilterCard filter = new FilterCard();
String name = jTextFieldSearch.getText().trim();
filter.add(new CardTextPredicate(name, chkNames.isSelected(), chkTypes.isSelected(), chkRules.isSelected()));
filter.add(new CardTextPredicate(name, chkNames.isSelected(), chkTypes.isSelected(), chkRules.isSelected(), chkUnique.isSelected()));
if (limited) {
ArrayList<Predicate<MageObject>> predicates = new ArrayList<>();
@ -543,6 +543,7 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
chkNames = new javax.swing.JCheckBox();
chkTypes = new javax.swing.JCheckBox();
chkRules = new javax.swing.JCheckBox();
chkUnique = new javax.swing.JCheckBox();
jButtonSearch = new javax.swing.JButton();
jButtonClean = new javax.swing.JButton();
cardCountLabel = new javax.swing.JLabel();
@ -1062,6 +1063,22 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
chkRulesActionPerformed(evt);
}
});
chkUnique.setSelected(true);
chkUnique.setText("Unique");
chkUnique.setToolTipText("Singleton results only.");
chkUnique.setFocusable(false);
chkUnique.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
chkUnique.setMaximumSize(new java.awt.Dimension(69, 16));
chkUnique.setMinimumSize(new java.awt.Dimension(69, 16));
chkUnique.setPreferredSize(new java.awt.Dimension(69, 16));
chkUnique.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
chkUnique.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
chkUniqueActionPerformed(evt);
}
});
jButtonSearch.setText("Search");
jButtonSearch.setToolTipText("Performs the search.");
@ -1109,6 +1126,8 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
.addComponent(chkTypes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkRules, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(chkUnique, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(5, 5, 5)
.addComponent(cardCountLabel)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
@ -1122,6 +1141,7 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
.addGroup(cardSelectorBottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(chkTypes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(chkRules, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(chkUnique, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(chkNames, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(cardSelectorBottomPanelLayout.createSequentialGroup()
.addGroup(cardSelectorBottomPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@ -1341,6 +1361,10 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
// TODO add your handling code here:
}//GEN-LAST:event_chkRulesActionPerformed
private void chkUniqueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chkRulesActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_chkRulesActionPerformed
private void btnExpansionSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExpansionSearchActionPerformed
FastSearchUtil.showFastSearchForStringComboBox(cbExpansionSet, "Select set or expansion");
}//GEN-LAST:event_btnExpansionSearchActionPerformed
@ -1418,6 +1442,7 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
private javax.swing.JCheckBox chkPennyDreadful;
private javax.swing.JCheckBox chkPiles;
private javax.swing.JCheckBox chkRules;
private javax.swing.JCheckBox chkUnique;
private javax.swing.JCheckBox chkTypes;
private javax.swing.JButton jButtonAddToMain;
private javax.swing.JButton jButtonAddToSideboard;

View file

@ -27,6 +27,7 @@
*/
package mage.filter.predicate.other;
import java.util.HashMap;
import mage.cards.Card;
import mage.cards.SplitCard;
import mage.constants.SubType;
@ -44,21 +45,38 @@ public class CardTextPredicate implements Predicate<Card> {
private final boolean inNames;
private final boolean inTypes;
private final boolean inRules;
private final boolean isUnique;
private HashMap<String, Boolean> seenCards = null;
public CardTextPredicate(String text, boolean inNames, boolean inTypes, boolean inRules) {
public CardTextPredicate(String text, boolean inNames, boolean inTypes, boolean inRules, boolean isUnique) {
this.text = text;
this.inNames = inNames;
this.inTypes = inTypes;
this.inRules = inRules;
this.isUnique = isUnique;
seenCards = new HashMap<>();
}
@Override
public boolean apply(Card input, Game game) {
if (text.isEmpty()) {
if (text.isEmpty() && !isUnique) {
return true;
}
if (text.isEmpty() && isUnique) {
boolean found = !seenCards.keySet().contains(input.getName());
seenCards.put(input.getName(), true);
return found;
}
// first check in card name
if (inNames && input.getName().toLowerCase().contains(text.toLowerCase())) {
if (isUnique && seenCards.keySet().contains(input.getName())) {
return false;
}
if (isUnique) {
seenCards.put(input.getName(), true);
}
return true;
}
@ -105,11 +123,18 @@ public class CardTextPredicate implements Predicate<Card> {
}
}
}
if (found && isUnique && seenCards.keySet().contains(input.getName())) {
found = false;
}
if (!found) {
return false;
}
}
if (isUnique) {
seenCards.put(input.getName(), true);
}
return true;
}