rename my PickChoiceDialog to PickCheckBoxDialog and bring back the correct one

This commit is contained in:
Salco 2018-04-23 09:58:28 -04:00
parent e53a9d5f39
commit d782b69f0f
4 changed files with 861 additions and 161 deletions

View file

@ -5,7 +5,6 @@
*/
package mage.client.dialog;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.ActionEvent;
@ -33,7 +32,6 @@ import mage.client.util.gui.MageDialogState;
/**
*
* @author JayDi85
* @author Salco
*/
public class PickChoiceDialog extends MageDialog {
@ -41,29 +39,9 @@ public class PickChoiceDialog 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);
}
@ -78,9 +56,7 @@ public class PickChoiceDialog extends MageDialog {
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());
@ -92,25 +68,11 @@ public class PickChoiceDialog extends MageDialog {
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);
this.allItems.add(new KeyValueItem(entry.getKey(), entry.getValue()));
}
} 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);
this.allItems.add(new KeyValueItem(value, value));
}
}
@ -119,8 +81,8 @@ public class PickChoiceDialog extends MageDialog {
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());
Integer n1 = choice.getSortData().get(o1.Key);
Integer n2 = choice.getSortData().get(o2.Key);
return n1.compareTo(n2);
}
});
@ -222,24 +184,18 @@ public class PickChoiceDialog extends MageDialog {
// 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)){
KeyValueItem listItem = (KeyValueItem)this.listChoices.getModel().getElementAt(i);
if (listItem.Key.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.listChoices.setSelectedIndex(selectIndex);
this.listChoices.ensureIndexIsVisible(selectIndex);
}
}
@ -252,16 +208,14 @@ public class PickChoiceDialog extends MageDialog {
private void loadData(){
// load data to datamodel after filter or on startup
String filter = choice.getSearchText();
String filter = choice.getSearchText();
if (filter == null){ filter = ""; }
filter = filter.toLowerCase();
filter = filter.toLowerCase(Locale.ENGLISH);
this.dataModel.clear();
this.m_dataModel.clear();
for(KeyValueItem item: this.allItems){
if(!choice.isSearchEnabled() || item.Value.toLowerCase().contains(filter)){
if(!choice.isSearchEnabled() || item.Value.toLowerCase(Locale.ENGLISH).contains(filter)){
this.dataModel.addElement(item);
this.m_dataModel.addElement(item.getObjectValue());
}
}
}
@ -294,9 +248,7 @@ public class PickChoiceDialog extends MageDialog {
}
private void doChoose(){
if((tList != null)||(setChoice())){
this.m_dataModel.clear();
restoreData(this.m_dataModel);
if(setChoice()){
this.hideDialog();
}
}
@ -307,35 +259,13 @@ public class PickChoiceDialog extends MageDialog {
hideDialog();
}
/**
* Creates new form PickChoiceDialog
* @param list
*/
public PickChoiceDialog(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 PickChoiceDialog() {
this(null);
initComponents();
this.listChoices.setModel(dataModel);
this.setModal(true);
}
public boolean setChoice() {
@ -364,31 +294,19 @@ public class PickChoiceDialog extends MageDialog {
{
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) {
public KeyValueItem(String value, String label) {
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() {
@ -416,7 +334,6 @@ public class PickChoiceDialog extends MageDialog {
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>");
@ -431,17 +348,17 @@ public class PickChoiceDialog extends MageDialog {
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))
.addComponent(labelMessage, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
.addComponent(labelSubMessage, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE))
.addGap(0, 0, 0))
);
panelHeaderLayout.setVerticalGroup(
panelHeaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(panelHeaderLayout.createSequentialGroup()
.addGap(0, 0, Short.MAX_VALUE)
.addGap(0, 0, 0)
.addComponent(labelMessage)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(labelSubMessage, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE))
.addGap(0, 0, 0)
.addComponent(labelSubMessage))
);
labelSearch.setText("Search:");
@ -490,39 +407,30 @@ public class PickChoiceDialog extends MageDialog {
}
});
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))
.addComponent(btCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
panelCommandsLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btCancel, btOK});
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))
.addComponent(btOK))
.addContainerGap())
);
getRootPane().setDefaultButton(btOK);
getRootPane().setDefaultButton(btOK);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
@ -546,7 +454,7 @@ public class PickChoiceDialog extends MageDialog {
.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)
.addComponent(scrollList, javax.swing.GroupLayout.DEFAULT_SIZE, 246, 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())
@ -570,16 +478,8 @@ public class PickChoiceDialog extends MageDialog {
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;
@ -591,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
}
}