Merge pull request #4850 from salco/feature/new_ui_selection_type

Feature/Add Set Selection filtering
This commit is contained in:
LevelX2 2018-04-24 22:39:22 +02:00 committed by GitHub
commit e54980f6ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1662 additions and 10 deletions

View file

@ -57,6 +57,7 @@ import static mage.client.dialog.PreferencesDialog.KEY_DECK_EDITOR_SEARCH_TYPES;
import static mage.client.dialog.PreferencesDialog.KEY_DECK_EDITOR_SEARCH_UNIQUE;
import mage.client.util.GUISizeHelper;
import mage.client.util.gui.FastSearchUtil;
import mage.client.dialog.CheckBoxList;
import mage.client.util.sets.ConstructedFormats;
import mage.constants.CardType;
import mage.constants.Rarity;
@ -84,6 +85,8 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
private final SortSetting sortSetting;
private static final Map<String, Integer> pdAllowed = new HashMap<>();
private final String TEST_MULTI_SET="Multiple Sets selected";
private final ActionListener searchAction = evt -> jButtonSearchActionPerformed(evt);
/**
@ -97,6 +100,23 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
initListViewComponents();
setGUISize();
currentView = mainModel; // by default we use List View
listCodeSelected = new CheckBoxList();
// remove the all option
boolean is_removeFinish=false;
String[] setCodes = ConstructedFormats.getTypes();
java.util.List<String> result = new ArrayList<>();
for(int i=0; (i<setCodes.length)&&(!is_removeFinish);i++)
{
String item = setCodes[i];
if(!item.equals(ConstructedFormats.ALL))
{
result.add(item);
}
}
listCodeSelected.setListData(result.toArray());
}
private void makeTransparent() {
@ -356,14 +376,34 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
criteria.rarities(Rarity.SPECIAL);
criteria.rarities(Rarity.BONUS);
}
if (this.cbExpansionSet.isVisible()) {
String expansionSelection = this.cbExpansionSet.getSelectedItem().toString();
if (!expansionSelection.equals("- All Sets")) {
java.util.List<String> setCodes = ConstructedFormats.getSetsByFormat(expansionSelection);
criteria.setCodes(setCodes.toArray(new String[0]));
}
}
if(listCodeSelected.getCheckedIndices().length <= 1)
{
String expansionSelection = this.cbExpansionSet.getSelectedItem().toString();
if (!expansionSelection.equals("- All Sets")) {
java.util.List<String> setCodes = ConstructedFormats.getSetsByFormat(expansionSelection);
criteria.setCodes(setCodes.toArray(new String[0]));
}
}
else
{
java.util.List<String> setCodes = new ArrayList<>() ;
//java.util.List<String> listReceived=new ArrayList<>() ;
int[] choiseValue=listCodeSelected.getCheckedIndices();
ListModel x= listCodeSelected.getModel();
for(int itemIndex: choiseValue){
java.util.List<String> listReceived=ConstructedFormats.getSetsByFormat(x.getElementAt(itemIndex).toString());
listReceived.stream().filter((item) -> (setCodes.contains(item)==false)).forEachOrdered((item) -> {
setCodes.add(item);
});
}
criteria.setCodes(setCodes.toArray(new String[0]));
}
}
return criteria;
}
@ -1194,6 +1234,22 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
}// </editor-fold>//GEN-END:initComponents
private void cbExpansionSetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbExpansionSetActionPerformed
if(!cbExpansionSet.getSelectedItem().toString().contains(TEST_MULTI_SET))
{
int index=cbExpansionSet.getSelectedIndex();
if(cbExpansionSet.getItemAt(0).contains(TEST_MULTI_SET))
{
cbExpansionSet.removeItemAt(0);
index--;
}
listCodeSelected.uncheckAll();
if(index > 0)
{
//ofset because all sets is removed from the list
listCodeSelected.setChecked(index-1, true);
}
}
filterCards();
}//GEN-LAST:event_cbExpansionSetActionPerformed
@ -1375,7 +1431,53 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
}//GEN-LAST:event_chkRulesActionPerformed
private void btnExpansionSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExpansionSearchActionPerformed
FastSearchUtil.showFastSearchForStringComboBox(cbExpansionSet, "Select set or expansion");
FastSearchUtil.showFastSearchForStringComboBox(listCodeSelected, FastSearchUtil.DEFAULT_EXPANSION_SEARCH_MESSAGE);
//
int[] choiseValue=listCodeSelected.getCheckedIndices();
ListModel x= listCodeSelected.getModel();
if(choiseValue.length==0)//none
{
cbExpansionSet.setSelectedIndex(0);
}
else if(choiseValue.length==1)//one
{
String itemSelected=listCodeSelected.getModel().getElementAt(choiseValue[0]).toString();
for(int index=0;index < cbExpansionSet.getItemCount();index++)
{
if(cbExpansionSet.getItemAt(index).equals(itemSelected))
{
cbExpansionSet.setSelectedIndex(index);
}
}
}
else//many
{
String message=String.format("%s:%d",TEST_MULTI_SET,choiseValue.length);
cbExpansionSet.insertItemAt(message, 0);
cbExpansionSet.setSelectedIndex(0);
if(cbExpansionSet.getItemAt(1).contains(TEST_MULTI_SET))
{
cbExpansionSet.removeItemAt(1);
}
//listCodeSelected.setChecked(index-1, true);
//cbExpansionSet.
}
/*for(int itemIndex: choiseValue){
// LogLog.warn(String.format("%d:%s",itemIndex,x.getElementAt(itemIndex).toString()));
}
*/
//
filterCards();
}//GEN-LAST:event_btnExpansionSearchActionPerformed
private void tbCommonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tbCommonActionPerformed
@ -1435,6 +1537,8 @@ public class CardSelector extends javax.swing.JPanel implements ComponentListene
private TableModel mainModel;
private JTable mainTable;
private ICardGrid currentView;
private CheckBoxList listCodeSelected;
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.ButtonGroup bgView;

View file

@ -0,0 +1,680 @@
/*
* 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/>.
*/
/*
* CheckBoxList.java
* Copyright (C) 2006-2012 University of Waikato, Hamilton, New Zealand
*/
package mage.client.dialog;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.NoSuchElementException;
import java.util.Vector;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
/**
* An extended JList that contains CheckBoxes. If necessary a CheckBoxListItem
* wrapper is added around the displayed object in any of the Model methods,
* e.g., addElement. For methods returning objects the opposite takes place, the
* wrapper is removed and only the payload object is returned.
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 10219 $
*/
public class CheckBoxList extends JList {
/** for serialization */
private static final long serialVersionUID = -4359573373359270258L;
/**
* represents an item in the CheckBoxListModel
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 10219 $
*/
protected class CheckBoxListItem {
/** whether item is checked or not */
private boolean m_Checked = false;
/** the actual object */
private Object m_Content = null;
/**
* initializes the item with the given object and initially unchecked
*
* @param o the content object
*/
public CheckBoxListItem(Object o) {
this(o, false);
}
/**
* initializes the item with the given object and whether it's checked
* initially
*
* @param o the content object
* @param checked whether the item should be checked initially
*/
public CheckBoxListItem(Object o, boolean checked) {
m_Checked = checked;
m_Content = o;
}
/**
* returns the content object
*/
public Object getContent() {
return m_Content;
}
/**
* sets the checked state of the item
*/
public void setChecked(boolean value) {
m_Checked = value;
}
/**
* returns the checked state of the item
*/
public boolean getChecked() {
return m_Checked;
}
/**
* returns the string representation of the content object
*/
@Override
public String toString() {
return m_Content.toString();
}
/**
* returns true if the "payload" objects of the current and the given
* CheckBoxListItem are the same.
*
* @param o the CheckBoxListItem to check
* @throws IllegalArgumentException if the provided object is not a
* CheckBoxListItem
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof CheckBoxListItem)) {
throw new IllegalArgumentException("Must be a CheckBoxListItem!");
}
return getContent().equals(((CheckBoxListItem) o).getContent());
}
}
/**
* A specialized model.
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 10219 $
*/
@SuppressWarnings("rawtypes")
public class CheckBoxListModel extends DefaultListModel {
/** for serialization */
private static final long serialVersionUID = 7772455499540273507L;
/**
* initializes the model with no data.
*/
public CheckBoxListModel() {
super();
}
/**
* Constructs a CheckBoxListModel from an array of objects and then applies
* setModel to it.
*
* @param listData the data to use
*/
public CheckBoxListModel(Object[] listData) {
for (Object element : listData) {
addElement(element);
}
}
/**
* Constructs a CheckBoxListModel from a Vector and then applies setModel to
* it.
*/
public CheckBoxListModel(Vector listData) {
for (int i = 0; i < listData.size(); i++) {
addElement(listData.get(i));
}
}
/**
* Inserts the specified element at the specified position in this list.
*
* @param index index at which the specified element is to be inserted
* @param element element to be inserted
*/
@Override
public void add(int index, Object element) {
if (!(element instanceof CheckBoxListItem)) {
super.add(index, new CheckBoxListItem(element));
} else {
super.add(index, element);
}
}
/**
* Adds the specified component to the end of this list.
*
* @param obj the component to be added
*/
@Override
public void addElement(Object obj) {
if (!(obj instanceof CheckBoxListItem)) {
super.addElement(new CheckBoxListItem(obj));
} else {
super.addElement(obj);
}
}
/**
* Tests whether the specified object is a component in this list.
*
* @param elem the element to check
* @return true if the element is in the list
*/
@Override
public boolean contains(Object elem) {
if (!(elem instanceof CheckBoxListItem)) {
return super.contains(new CheckBoxListItem(elem));
} else {
return super.contains(elem);
}
}
/**
* Copies the components of this list into the specified array.
*
* @param anArray the array into which the components get copied
* @throws IndexOutOfBoundsException if the array is not big enough
*/
@Override
public void copyInto(Object[] anArray) {
if (anArray.length < getSize()) {
throw new IndexOutOfBoundsException("Array not big enough!");
}
for (int i = 0; i < getSize(); i++) {
anArray[i] = ((CheckBoxListItem) getElementAt(i)).getContent();
}
}
/**
* Returns the component at the specified index. Throws an
* ArrayIndexOutOfBoundsException if the index is negative or not less than
* the size of the list.
*
* @param index an index into this list
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public Object elementAt(int index) {
return ((CheckBoxListItem) super.elementAt(index)).getContent();
}
/**
* Returns the first component of this list. Throws a NoSuchElementException
* if this vector has no components.
*
* @return the first component of this list
* @throws NoSuchElementException
*/
@Override
public Object firstElement() {
return ((CheckBoxListItem) super.firstElement()).getContent();
}
/**
* Returns the element at the specified position in this list.
*
* @param index of element to return
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public Object get(int index) {
return ((CheckBoxListItem) super.get(index)).getContent();
}
/**
* Returns the component at the specified index.
*
* @param index an index into this list
* @return the component at the specified index
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public Object getElementAt(int index) {
return ((CheckBoxListItem) super.getElementAt(index));//.getContent();
}
/**
* Searches for the first occurrence of elem.
*
* @param elem an object
* @return the index of the first occurrence of the argument in this list;
* returns -1 if the object is not found
*/
@Override
public int indexOf(Object elem) {
if (!(elem instanceof CheckBoxListItem)) {
return super.indexOf(new CheckBoxListItem(elem));
} else {
return super.indexOf(elem);
}
}
/**
* Searches for the first occurrence of elem, beginning the search at index.
*
* @param elem the desired component
* @param index the index from which to begin searching
* @return the index where the first occurrence of elem is found after
* index; returns -1 if the elem is not found in the list
*/
@Override
public int indexOf(Object elem, int index) {
if (!(elem instanceof CheckBoxListItem)) {
return super.indexOf(new CheckBoxListItem(elem), index);
} else {
return super.indexOf(elem, index);
}
}
/**
* Inserts the specified object as a component in this list at the specified
* index.
*
* @param obj the component to insert
* @param index where to insert the new component
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public void insertElementAt(Object obj, int index) {
if (!(obj instanceof CheckBoxListItem)) {
super.insertElementAt(new CheckBoxListItem(obj), index);
} else {
super.insertElementAt(obj, index);
}
}
/**
* Returns the last component of the list. Throws a NoSuchElementException
* if this vector has no components.
*
* @return the last component of the list
* @throws NoSuchElementException
*/
@Override
public Object lastElement() {
return ((CheckBoxListItem) super.lastElement()).getContent();
}
/**
* Returns the index of the last occurrence of elem.
*
* @param elem the desired component
* @return the index of the last occurrence of elem in the list; returns -1
* if the object is not found
*/
@Override
public int lastIndexOf(Object elem) {
if (!(elem instanceof CheckBoxListItem)) {
return super.lastIndexOf(new CheckBoxListItem(elem));
} else {
return super.lastIndexOf(elem);
}
}
/**
* Searches backwards for elem, starting from the specified index, and
* returns an index to it.
*
* @param elem the desired component
* @param index the index to start searching from
* @return the index of the last occurrence of the elem in this list at
* position less than index; returns -1 if the object is not found
*/
@Override
public int lastIndexOf(Object elem, int index) {
if (!(elem instanceof CheckBoxListItem)) {
return super.lastIndexOf(new CheckBoxListItem(elem), index);
} else {
return super.lastIndexOf(elem, index);
}
}
/**
* Removes the element at the specified position in this list. Returns the
* element that was removed from the list.
*
* @param index the index of the element to removed
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public Object remove(int index) {
return ((CheckBoxListItem) super.remove(index)).getContent();
}
/**
* Removes the first (lowest-indexed) occurrence of the argument from this
* list.
*
* @param obj the component to be removed
* @return true if the argument was a component of this list; false
* otherwise
*/
@Override
public boolean removeElement(Object obj) {
if (!(obj instanceof CheckBoxListItem)) {
return super.removeElement(new CheckBoxListItem(obj));
} else {
return super.removeElement(obj);
}
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
*
* @param index index of element to replace
* @param element element to be stored at the specified position
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public Object set(int index, Object element) {
if (!(element instanceof CheckBoxListItem)) {
return ((CheckBoxListItem) super.set(index, new CheckBoxListItem(
element))).getContent();
} else {
return ((CheckBoxListItem) super.set(index, element)).getContent();
}
}
/**
* Sets the component at the specified index of this list to be the
* specified object. The previous component at that position is discarded.
*
* @param obj what the component is to be set to
* @param index the specified index
* @throws ArrayIndexOutOfBoundsException
*/
@Override
public void setElementAt(Object obj, int index) {
if (!(obj instanceof CheckBoxListItem)) {
super.setElementAt(new CheckBoxListItem(obj), index);
} else {
super.setElementAt(obj, index);
}
}
/**
* Returns an array containing all of the elements in this list in the
* correct order.
*
* @return an array containing the elements of the list
*/
@Override
public Object[] toArray() {
Object[] result;
Object[] internal;
int i;
internal = super.toArray();
result = new Object[internal.length];
for (i = 0; i < internal.length; i++) {
result[i] = ((CheckBoxListItem) internal[i]).getContent();
}
return result;
}
/**
* returns the checked state of the element at the given index
*
* @param index the index of the element to return the checked state for
* @return the checked state of the specifed element
*/
public boolean getChecked(int index) {
return ((CheckBoxListItem) super.getElementAt(index)).getChecked();
}
/**
* sets the checked state of the element at the given index
*
* @param index the index of the element to set the checked state for
* @param checked the new checked state
*/
public void setChecked(int index, boolean checked) {
((CheckBoxListItem) super.getElementAt(index)).setChecked(checked);
}
}
/**
* A specialized CellRenderer for the CheckBoxList
*
* @author fracpete (fracpete at waikato dot ac dot nz)
* @version $Revision: 10219 $
* @see CheckBoxList
*/
public class CheckBoxListRenderer extends JCheckBox implements
ListCellRenderer {
/** for serialization */
private static final long serialVersionUID = 1059591605858524586L;
/**
* Return a component that has been configured to display the specified
* value.
*
* @param list The JList we're painting.
* @param value The value returned by list.getModel().getElementAt(index).
* @param index The cells index.
* @param isSelected True if the specified cell was selected.
* @param cellHasFocus True if the specified cell has the focus.
* @return A component whose paint() method will render the specified value.
*/
@Override
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {
setText(value.toString());
setSelected(((CheckBoxList) list).getChecked(index));
setBackground(isSelected ? list.getSelectionBackground() : list
.getBackground());
setForeground(isSelected ? list.getSelectionForeground() : list
.getForeground());
setFocusPainted(false);
return this;
}
}
/**
* initializes the list with an empty CheckBoxListModel
*/
public CheckBoxList() {
this(null);
}
/**
* initializes the list with the given CheckBoxListModel
*
* @param model the model to initialize with
*/
public CheckBoxList(CheckBoxListModel model) {
super();
if (model == null) {
model = this.new CheckBoxListModel();
}
setModel(model);
setCellRenderer(new CheckBoxListRenderer());
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
int index = locationToIndex(e.getPoint());
if (index != -1) {
setChecked(index, !getChecked(index));
repaint();
}
}
});
addKeyListener(new KeyAdapter() {
@Override
public void keyTyped(KeyEvent e) {
if ((e.getKeyChar() == ' ') && (e.getModifiers() == 0)) {
int index = getSelectedIndex();
setChecked(index, !getChecked(index));
e.consume();
repaint();
}
}
});
}
/**
* sets the model - must be an instance of CheckBoxListModel
*
* @param model the model to use
* @throws IllegalArgumentException if the model is not an instance of
* CheckBoxListModel
* @see CheckBoxListModel
*/
@Override
public void setModel(ListModel model) {
if (!(model instanceof CheckBoxListModel)) {
if (model instanceof javax.swing.DefaultListModel) {
super.setModel((CheckBoxListModel)model);
}
else {
throw new IllegalArgumentException(
"Model must be an instance of CheckBoxListModel!");
}
}
else {
super.setModel(model);
}
}
/*public void setModel(DefaultListModel model) {
throw new IllegalArgumentException(
"Model must be an ins12313tance of CheckBoxListModel!");
}*/
/**
* Constructs a CheckBoxListModel from an array of objects and then applies
* setModel to it.
*
* @param listData the data to use
*/
@Override
public void setListData(Object[] listData) {
setModel(new CheckBoxListModel(listData));
}
/**
* Constructs a CheckBoxListModel from a Vector and then applies setModel to
* it.
*/
@Override
public void setListData(@SuppressWarnings("rawtypes") Vector listData) {
setModel(new CheckBoxListModel(listData));
}
/**
* returns the checked state of the element at the given index
*
* @param index the index of the element to return the checked state for
* @return the checked state of the specifed element
*/
public boolean getChecked(int index) {
return ((CheckBoxListModel) getModel()).getChecked(index);
}
/**
* sets the checked state of the element at the given index
*
* @param index the index of the element to set the checked state for
* @param checked the new checked state
*/
public void setChecked(int index, boolean checked) {
((CheckBoxListModel) getModel()).setChecked(index, checked);
}
/**
* returns an array with the indices of all checked items
*
* @return the indices of all items that are currently checked
*/
public int[] getCheckedIndices() {
Vector<Integer> list;
int[] result;
int i;
// traverse over model
list = new Vector<Integer>();
for (i = 0; i < getModel().getSize(); i++) {
if (getChecked(i)) {
list.add(new Integer(i));
}
}
// generate result array
result = new int[list.size()];
for (i = 0; i < list.size(); i++) {
result[i] = list.get(i).intValue();
}
return result;
}
public void checkAll() {
for (int i = 0; i < getModel().getSize(); i++) {
this.setChecked(i,true);
}
}
public void uncheckAll() {
int[] choiceToUncheck = this.getCheckedIndices();
for(int itemIndex: choiceToUncheck){
this.setChecked(itemIndex,false);
}
}
}

View file

@ -0,0 +1,223 @@
<?xml version="1.0" encoding="UTF-8" ?>
<Form version="1.9" maxVersion="1.9" type="org.netbeans.modules.form.forminfo.JInternalFrameFormInfo">
<SyntheticProperties>
<SyntheticProperty name="formSizePolicy" type="int" value="1"/>
</SyntheticProperties>
<AuxValues>
<AuxValue name="FormSettings_autoResourcing" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_autoSetComponentName" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_generateFQN" type="java.lang.Boolean" value="true"/>
<AuxValue name="FormSettings_generateMnemonicsCode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_i18nAutoMode" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
<AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
<AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
<AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
</AuxValues>
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="scrollList" alignment="1" max="32767" attributes="0"/>
<Component id="panelCommands" alignment="0" max="32767" attributes="0"/>
<Component id="panelHeader" alignment="0" max="32767" attributes="0"/>
<Component id="panelSearch" alignment="1" max="32767" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="1" attributes="0">
<EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="panelHeader" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="panelSearch" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="scrollList" pref="240" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="panelCommands" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Container class="javax.swing.JPanel" name="panelHeader">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="labelMessage" alignment="1" pref="0" max="32767" attributes="0"/>
<Component id="labelSubMessage" alignment="1" max="32767" attributes="0"/>
</Group>
<EmptySpace min="0" pref="0" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
<Component id="labelMessage" max="32767" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="labelSubMessage" pref="30" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="labelMessage">
<Properties>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="&lt;html&gt;&lt;div style=&apos;text-align: center;&apos;&gt;example long message example long message example long message example long message example long message&lt;/div&gt;&lt;/html&gt;"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="labelSubMessage">
<Properties>
<Property name="font" type="java.awt.Font" editor="org.netbeans.modules.form.editors2.FontEditor">
<FontInfo relative="true">
<Font bold="true" component="labelSubMessage" italic="true" property="font" relativeSize="true" size="0"/>
</FontInfo>
</Property>
<Property name="horizontalAlignment" type="int" value="0"/>
<Property name="text" type="java.lang.String" value="&lt;html&gt;&lt;div style=&apos;text-align: center;&apos;&gt;example long message example long&lt;/div&gt;&lt;/html&gt;"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="panelSearch">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="0" pref="0" max="-2" attributes="0"/>
<Component id="labelSearch" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="editSearch" max="32767" attributes="0"/>
<EmptySpace min="0" pref="0" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<EmptySpace min="-2" pref="3" max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="labelSearch" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="editSearch" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace min="-2" pref="3" max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JLabel" name="labelSearch">
<Properties>
<Property name="text" type="java.lang.String" value="Search:"/>
</Properties>
</Component>
<Component class="javax.swing.JTextField" name="editSearch">
<Properties>
<Property name="text" type="java.lang.String" value="sample search text"/>
</Properties>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JScrollPane" name="scrollList">
<Layout class="org.netbeans.modules.form.compat2.layouts.support.JScrollPaneSupportLayout"/>
<SubComponents>
<Component class="javax.swing.JList" name="listChoices">
<Properties>
<Property name="model" type="javax.swing.ListModel" editor="org.netbeans.modules.form.editors2.ListModelEditor">
<StringArray count="3">
<StringItem index="0" value="item1"/>
<StringItem index="1" value="item2"/>
<StringItem index="2" value="item3"/>
</StringArray>
</Property>
</Properties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value=""/>
</AuxValues>
</Component>
</SubComponents>
</Container>
<Container class="javax.swing.JPanel" name="panelCommands">
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="32767" attributes="0"/>
<Component id="btClear" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="btOK" min="-2" max="-2" attributes="0"/>
<EmptySpace min="-2" max="-2" attributes="0"/>
<Component id="btCancel" min="-2" pref="83" max="-2" attributes="0"/>
<EmptySpace max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" alignment="0" attributes="0">
<EmptySpace max="32767" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="btCancel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="btOK" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="btClear" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
</Layout>
<SubComponents>
<Component class="javax.swing.JButton" name="btOK">
<Properties>
<Property name="text" type="java.lang.String" value="Choose"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btOKActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_AddingCodePost" type="java.lang.String" value="getRootPane().setDefaultButton(btOK);"/>
</AuxValues>
</Component>
<Component class="javax.swing.JButton" name="btCancel">
<Properties>
<Property name="text" type="java.lang.String" value="Cancel"/>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btCancelActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JButton" name="btClear">
<Properties>
<Property name="text" type="java.lang.String" value="Clear"/>
<Property name="minimumSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
<Dimension value="[30, 25]"/>
</Property>
</Properties>
<Events>
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="btClearActionPerformed"/>
</Events>
<AuxValues>
<AuxValue name="JavaCodeGenerator_AddingCodePost" type="java.lang.String" value="getRootPane().setDefaultButton(btOK);"/>
</AuxValues>
</Component>
</SubComponents>
</Container>
</SubComponents>
</Form>

View file

@ -0,0 +1,594 @@
/*
* 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.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.DefaultListModel;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.KeyStroke;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import mage.choices.Choice;
import mage.client.MageFrame;
import mage.client.util.SettingsManager;
import mage.client.util.gui.GuiDisplayUtil;
import mage.client.util.gui.MageDialogState;
/**
*
* @author JayDi85
* @author Salco
*/
public class PickCheckBoxDialog extends MageDialog {
Choice choice;
ArrayList<KeyValueItem> allItems = new ArrayList<>();
DefaultListModel<KeyValueItem> dataModel = new DefaultListModel();
CheckBoxList.CheckBoxListModel m_dataModel;
CheckBoxList tList;
final private static String HTML_TEMPLATE = "<html><div style='text-align: center;'>%s</div></html>";
private void setFocus(CheckBoxList obj){
if (!(obj instanceof java.awt.Component)) {
throw new IllegalArgumentException("Must be a java.awt.Component!");
}
this.scrollList.setViewportView((java.awt.Component)obj);
}
private javax.swing.JList get_a_Jlist_from_ScrollListView(){
return ((javax.swing.JList)this.scrollList.getViewport().getView());
}
private void restoreData(Object dataFrom){
this.allItems.forEach((item) -> {
((CheckBoxList.CheckBoxListModel)dataFrom).addElement(item.getObjectValue());
});
}
public void showDialog(Choice choice) {
showDialog(choice, null, null, null);
}
public void showDialog(Choice choice, String startSelectionValue) {
showDialog(choice, null, null, startSelectionValue);
}
public void showDialog(Choice choice, UUID objectId, MageDialogState mageDialogState) {
showDialog(choice, objectId, mageDialogState, null);
}
public void showDialog(Choice choice, UUID objectId, MageDialogState mageDialogState, String startSelectionValue) {
this.choice = choice;
KeyValueItem tempKeyValue;
int indexInTList;
setLabelText(this.labelMessage, choice.getMessage());
setLabelText(this.labelSubMessage, choice.getSubMessage());
btCancel.setEnabled(!choice.isRequired());
// 2 modes: string or key-values
// sore data in allItems for inremental filtering
// http://logicbig.com/tutorials/core-java-tutorial/swing/list-filter/
this.allItems.clear();
if (choice.isKeyChoice()){
for (Map.Entry<String, String> entry: choice.getKeyChoices().entrySet()) {
if(tList != null){
indexInTList = m_dataModel.indexOf(entry.getKey());
tempKeyValue=new KeyValueItem(entry.getKey(), entry.getValue(),(CheckBoxList.CheckBoxListItem) this.tList.getModel().getElementAt(indexInTList));
}
else{
tempKeyValue=new KeyValueItem(entry.getKey(), entry.getValue());
}
this.allItems.add(tempKeyValue);
}
} else {
for (String value: choice.getChoices()){
if(tList != null){
indexInTList = m_dataModel.indexOf(value);
tempKeyValue=new KeyValueItem(value, value,(CheckBoxList.CheckBoxListItem) tList.getModel().getElementAt(indexInTList));
}
else{
tempKeyValue=new KeyValueItem(value, value);
}
this.allItems.add(tempKeyValue);
}
}
// sorting
if(choice.isSortEnabled()){
Collections.sort(this.allItems, new Comparator<KeyValueItem>() {
@Override
public int compare(KeyValueItem o1, KeyValueItem o2) {
Integer n1 = choice.getSortData().get(o1.getKey());
Integer n2 = choice.getSortData().get(o2.getKey());
return n1.compareTo(n2);
}
});
}
// search
if(choice.isSearchEnabled())
{
panelSearch.setVisible(true);
this.editSearch.setText(choice.getSearchText());
}else{
panelSearch.setVisible(false);
this.editSearch.setText("");
}
// listeners for inremental filtering
editSearch.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void insertUpdate(DocumentEvent e) {
choice.setSearchText(editSearch.getText());
loadData();
}
@Override
public void removeUpdate(DocumentEvent e) {
choice.setSearchText(editSearch.getText());
loadData();
}
@Override
public void changedUpdate(DocumentEvent e) {
choice.setSearchText(editSearch.getText());
loadData();
}
});
// listeners for select up and down without edit focus lost
editSearch.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
//System.out.println("types");
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_UP){
doPrevSelect();
}else if(e.getKeyCode() == KeyEvent.VK_DOWN){
doNextSelect();
}
}
@Override
public void keyReleased(KeyEvent e) {
//System.out.println("released");
}
});
// listeners double click choose
listChoices.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if(e.getClickCount() == 2){
doChoose();
}
}
});
// listeners for ESC close
if(!choice.isRequired()){
String cancelName = "cancel";
InputMap inputMap = getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), cancelName);
ActionMap actionMap = getRootPane().getActionMap();
actionMap.put(cancelName, new AbstractAction() {
public void actionPerformed(ActionEvent e) {
doCancel();
}
});
}
// window settings
if (this.isModal()){
MageFrame.getDesktop().add(this, JLayeredPane.MODAL_LAYER);
}else{
MageFrame.getDesktop().add(this, JLayeredPane.PALETTE_LAYER);
}
if (mageDialogState != null) {
mageDialogState.setStateToDialog(this);
} else {
Point centered = SettingsManager.instance.getComponentPosition(getWidth(), getHeight());
this.setLocation(centered.x, centered.y);
GuiDisplayUtil.keepComponentInsideScreen(centered.x, centered.y, this);
}
// final load
loadData();
// start selection
if((startSelectionValue != null)){
javax.swing.JList currentlistChoices;// = new javax.swing.JList();
currentlistChoices=this.get_a_Jlist_from_ScrollListView();
/*currentlistChoices = this.listChoices;*/
int selectIndex = -1;
for(int i = 0; i < this.listChoices.getModel().getSize(); i++){
//KeyValueItem listItem = (KeyValueItem)currentlistChoices.getModel().getElementAt(i);
String elementOfList = currentlistChoices.getModel().getElementAt(i).toString();
if (elementOfList.equals(startSelectionValue)){
selectIndex = i;
break;
}
}
if(selectIndex >= 0){
// currentlistChoices=this.get_a_Jlist_from_ScrollListView();
/*currentlistChoices = this.listChoices;*/
currentlistChoices.setSelectedIndex(selectIndex);
currentlistChoices.ensureIndexIsVisible(selectIndex);
}
}
this.setVisible(true);
}
public void setWindowSize(int width, int heigth){
this.setSize(new Dimension(width, heigth));
}
private void loadData(){
// load data to datamodel after filter or on startup
String filter = choice.getSearchText();
if (filter == null){ filter = ""; }
filter = filter.toLowerCase();
this.dataModel.clear();
this.m_dataModel.clear();
for(KeyValueItem item: this.allItems){
if(!choice.isSearchEnabled() || item.Value.toLowerCase().contains(filter)){
this.dataModel.addElement(item);
this.m_dataModel.addElement(item.getObjectValue());
}
}
}
private void setLabelText(JLabel label, String text){
if ((text != null) && !text.equals("")){
label.setText(String.format(HTML_TEMPLATE, text));
label.setVisible(true);
}else{
label.setText("");
label.setVisible(false);
}
}
private void doNextSelect(){
int newSel = this.listChoices.getSelectedIndex() + 1;
int maxSel = this.listChoices.getModel().getSize() - 1;
if(newSel <= maxSel){
this.listChoices.setSelectedIndex(newSel);
this.listChoices.ensureIndexIsVisible(newSel);
}
}
private void doPrevSelect(){
int newSel = this.listChoices.getSelectedIndex() - 1;
if(newSel >= 0){
this.listChoices.setSelectedIndex(newSel);
this.listChoices.ensureIndexIsVisible(newSel);
}
}
private void doChoose(){
if((tList != null)||(setChoice())){
this.m_dataModel.clear();
restoreData(this.m_dataModel);
this.hideDialog();
}
}
private void doCancel(){
this.listChoices.clearSelection();
this.choice.clearChoice();
hideDialog();
}
/**
* Creates new form PickChoiceDialog
* @param list
*/
public PickCheckBoxDialog(CheckBoxList list) {
initComponents();
tList=list;
this.listChoices.setModel(dataModel);
this.setModal(true);
if(tList != null)
{
this.listChoices.setVisible(false);
m_dataModel= ( CheckBoxList.CheckBoxListModel )tList.getModel();
tList.setSelectionForeground(Color.BLUE);
if(this.tList instanceof javax.swing.JList){
setFocus(tList);
}
}
}
/**
* Creates new form PickChoiceDialog
*/
public PickCheckBoxDialog() {
this(null);
}
public boolean setChoice() {
KeyValueItem item = (KeyValueItem)this.listChoices.getSelectedValue();
// auto select one item (after incemental filtering)
if((item == null) && (this.listChoices.getModel().getSize() == 1)){
this.listChoices.setSelectedIndex(0);
item = (KeyValueItem)this.listChoices.getSelectedValue();
}
if(item != null){
if(choice.isKeyChoice()){
choice.setChoiceByKey(item.getKey());
}else{
choice.setChoice(item.getKey());
}
return true;
}else{
choice.clearChoice();
return false;
}
}
class KeyValueItem
{
private final String Key;
private final String Value;
private final CheckBoxList.CheckBoxListItem objectValue;
public KeyValueItem(String value) {
this(value,null,null);
}
public KeyValueItem(String value, String label) {
this(value,label,null);
}
public KeyValueItem(String value, String label,CheckBoxList.CheckBoxListItem object) {
this.Key = value;
this.Value = label;
this.objectValue = object;
}
public String getKey() {
return this.Key;
}
public String getValue() {
return this.Value;
}
public Object getObjectValue(){
return (CheckBoxList.CheckBoxListItem)this.objectValue;
}
@Override
public String toString() {
return this.Value;
}
}
/**
* 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")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
panelHeader = new javax.swing.JPanel();
labelMessage = new javax.swing.JLabel();
labelSubMessage = new javax.swing.JLabel();
panelSearch = new javax.swing.JPanel();
labelSearch = new javax.swing.JLabel();
editSearch = new javax.swing.JTextField();
scrollList = new javax.swing.JScrollPane();
listChoices = new javax.swing.JList();
panelCommands = new javax.swing.JPanel();
btOK = new javax.swing.JButton();
btCancel = new javax.swing.JButton();
btClear = new javax.swing.JButton();
labelMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
labelMessage.setText("<html><div style='text-align: center;'>example long message example long message example long message example long message example long message</div></html>");
labelSubMessage.setFont(labelSubMessage.getFont().deriveFont((labelSubMessage.getFont().getStyle() | java.awt.Font.ITALIC) | java.awt.Font.BOLD));
labelSubMessage.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
labelSubMessage.setText("<html><div style='text-align: center;'>example long message example long</div></html>");
javax.swing.GroupLayout panelHeaderLayout = new javax.swing.GroupLayout(panelHeader);
panelHeader.setLayout(panelHeaderLayout);
panelHeaderLayout.setHorizontalGroup(
panelHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelHeaderLayout.createSequentialGroup()
.addGroup(panelHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(labelMessage, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
.addComponent(labelSubMessage, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(0, 0, 0))
);
panelHeaderLayout.setVerticalGroup(
panelHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelHeaderLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addComponent(labelMessage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelSubMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
);
labelSearch.setText("Search:");
editSearch.setText("sample search text");
javax.swing.GroupLayout panelSearchLayout = new javax.swing.GroupLayout(panelSearch);
panelSearch.setLayout(panelSearchLayout);
panelSearchLayout.setHorizontalGroup(
panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelSearchLayout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(labelSearch)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(editSearch)
.addGap(0, 0, 0))
);
panelSearchLayout.setVerticalGroup(
panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelSearchLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addGroup(panelSearchLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(labelSearch)
.addComponent(editSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(3, 3, 3))
);
listChoices.setModel(new javax.swing.AbstractListModel() {
String[] strings = { "item1", "item2", "item3" };
public int getSize() { return strings.length; }
public Object getElementAt(int i) { return strings[i]; }
});
scrollList.setViewportView(listChoices);
btOK.setText("Choose");
btOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btOKActionPerformed(evt);
}
});
btCancel.setText("Cancel");
btCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btCancelActionPerformed(evt);
}
});
btClear.setText("Clear");
btClear.setMinimumSize(new java.awt.Dimension(30, 25));
btClear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btClearActionPerformed(evt);
}
});
javax.swing.GroupLayout panelCommandsLayout = new javax.swing.GroupLayout(panelCommands);
panelCommands.setLayout(panelCommandsLayout);
panelCommandsLayout.setHorizontalGroup(
panelCommandsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelCommandsLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(btClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btOK)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
panelCommandsLayout.setVerticalGroup(
panelCommandsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelCommandsLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(panelCommandsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(btCancel)
.addComponent(btOK)
.addComponent(btClear, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap())
);
getRootPane().setDefaultButton(btOK);
getRootPane().setDefaultButton(btOK);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollList, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(panelCommands, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelHeader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelSearch, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(panelHeader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(scrollList, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelCommands, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void btOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btOKActionPerformed
doChoose();
}//GEN-LAST:event_btOKActionPerformed
private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
doCancel();
}//GEN-LAST:event_btCancelActionPerformed
/**
* Closes the dialog
*/
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
doCancel();
}//GEN-LAST:event_closeDialog
private void btClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btClearActionPerformed
// TODO add your handling code here:
this.tList.uncheckAll();
//this.tList.repaint();
scrollList.repaint();
}//GEN-LAST:event_btClearActionPerformed
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton btCancel;
private javax.swing.JButton btClear;
private javax.swing.JButton btOK;
private javax.swing.JTextField editSearch;
private javax.swing.JLabel labelMessage;
private javax.swing.JLabel labelSearch;
private javax.swing.JLabel labelSubMessage;
private javax.swing.JList listChoices;
private javax.swing.JPanel panelCommands;
private javax.swing.JPanel panelHeader;
private javax.swing.JPanel panelSearch;
private javax.swing.JScrollPane scrollList;
// End of variables declaration//GEN-END:variables
}

View file

@ -203,4 +203,4 @@
</SubComponents>
</Container>
</SubComponents>
</Form>
</Form>

View file

@ -491,4 +491,4 @@ public class PickChoiceDialog extends MageDialog {
private javax.swing.JPanel panelSearch;
private javax.swing.JScrollPane scrollList;
// End of variables declaration//GEN-END:variables
}
}

View file

@ -1,8 +1,11 @@
package mage.client.util.gui;
import mage.choices.ChoiceImpl;
import mage.client.dialog.PickCheckBoxDialog;
import mage.client.dialog.PickChoiceDialog;
import mage.client.dialog.CheckBoxList;
import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
@ -61,4 +64,52 @@ public class FastSearchUtil {
}
}
}
/**
* Show fast choice modal dialog with incremental searching for any string CheckBoxList components
* @param combo CheckBoxList control with default data model
* @param chooseMessage caption message for dialog
*/
public static void showFastSearchForStringComboBox(CheckBoxList combo, String chooseMessage){
// fast search/choice dialog for string combobox
mage.choices.Choice choice = new ChoiceImpl(false);
// collect data from expansion combobox (String)
DefaultListModel comboModel = (DefaultListModel)combo.getModel();
Map<String, String> choiceItems = new HashMap<>(comboModel.getSize());
Map<String, Integer> choiceSorting = new HashMap<>(comboModel.getSize());
String item;
for(int i = 0; i < comboModel.size(); i++){
item = comboModel.getElementAt(i).toString();
choiceItems.put(item, item);
choiceSorting.put(item, i); // need so sorting
}
choice.setKeyChoices(choiceItems);
choice.setSortData(choiceSorting);
choice.setMessage(chooseMessage);
// current selection value restore
String needSelectValue;
needSelectValue = comboModel.firstElement().toString();
// ask for new value
PickCheckBoxDialog dlg = new PickCheckBoxDialog(combo);
dlg.setWindowSize(300, 500);
dlg.showDialog(choice, needSelectValue);
if(choice.isChosen()){
item = choice.getChoiceKey();
// compatible select for object's models (use setSelectedIndex instead setSelectedObject)
for(int i = 0; i < comboModel.getSize(); i++){
if(comboModel.getElementAt(i).toString().equals(item)){
combo.setSelectedIndex(i);
}
}
}
}
}