Support input FOptionPane

This commit is contained in:
drdev
2013-12-28 20:23:41 +00:00
parent 2a9b59f935
commit a592a44647
3 changed files with 93 additions and 45 deletions

View File

@@ -1,7 +1,5 @@
package forge.gui.deckeditor; package forge.gui.deckeditor;
import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import forge.Singletons; import forge.Singletons;
@@ -50,27 +48,20 @@ public class SEditorIO {
} }
// Confirm if overwrite // Confirm if overwrite
else if (controller.fileExists(name)) { else if (controller.fileExists(name)) {
int confirmResult = JOptionPane.YES_OPTION; boolean confirmResult = true;
if ( !StringUtils.equals(name, controller.getModelName()) ) { // prompt only if name was changed if ( !StringUtils.equals(name, controller.getModelName()) ) { // prompt only if name was changed
confirmResult = JOptionPane.showConfirmDialog(JOptionPane.getRootFrame(), confirmResult = FOptionPane.showConfirmDialog(
limitedDeckMode ? "Would you like to save changes to your deck?" limitedDeckMode ? "Would you like to save changes to your deck?"
: "There is already a deck named '" + name + "'. Overwrite?", : "There is already a deck named '" + name + "'. Overwrite?",
limitedDeckMode ? "Save changes?" : "Overwrite Deck?", limitedDeckMode ? "Save changes?" : "Overwrite Deck?");
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
} }
if (confirmResult == JOptionPane.YES_OPTION) { controller.save(); } if (confirmResult) { controller.save(); }
} }
// Confirm if a new deck will be created // Confirm if a new deck will be created
else { else if (FOptionPane.showConfirmDialog("This will create a new deck named '" +
final int m = JOptionPane.showConfirmDialog(JOptionPane.getRootFrame(), name + "'. Continue?", "Create Deck?")) {
"This will create a new deck named '" + name + "'. Continue?", controller.saveAs(name);
"Create Deck?",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if (m == JOptionPane.YES_OPTION) { controller.saveAs(name); }
} }
return true; return true;

View File

@@ -19,8 +19,6 @@ package forge.gui.deckeditor.controllers;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.swing.JOptionPane;
import forge.Singletons; import forge.Singletons;
import forge.card.CardEdition; import forge.card.CardEdition;
import forge.card.MagicColor; import forge.card.MagicColor;
@@ -36,6 +34,7 @@ import forge.gui.deckeditor.views.VDeckgen;
import forge.gui.framework.DragCell; import forge.gui.framework.DragCell;
import forge.gui.framework.FScreen; import forge.gui.framework.FScreen;
import forge.gui.home.sanctioned.CSubmenuDraft; import forge.gui.home.sanctioned.CSubmenuDraft;
import forge.gui.toolbox.FOptionPane;
import forge.gui.toolbox.itemmanager.CardManager; import forge.gui.toolbox.itemmanager.CardManager;
import forge.gui.toolbox.itemmanager.views.SColumnUtil; import forge.gui.toolbox.itemmanager.views.SColumnUtil;
import forge.item.PaperCard; import forge.item.PaperCard;
@@ -171,10 +170,7 @@ public class CEditorDraftingProcess extends ACEditorBase<PaperCard, DeckGroup> {
* </p> * </p>
*/ */
private void saveDraft() { private void saveDraft() {
String s = JOptionPane.showInputDialog(JOptionPane.getRootFrame(), String s = FOptionPane.showInputDialog("Save this draft as:", "Save Draft", FOptionPane.QUESTION_ICON);
"Save this draft as:",
"Save draft",
JOptionPane.QUESTION_MESSAGE);
// Cancel button will be null; OK will return string. // Cancel button will be null; OK will return string.
// Must check for null value first, then string length. // Must check for null value first, then string length.
@@ -187,14 +183,10 @@ public class CEditorDraftingProcess extends ACEditorBase<PaperCard, DeckGroup> {
// Check for overwrite case // Check for overwrite case
for (DeckGroup d : Singletons.getModel().getDecks().getDraft()) { for (DeckGroup d : Singletons.getModel().getDecks().getDraft()) {
if (s.equalsIgnoreCase(d.getName())) { if (s.equalsIgnoreCase(d.getName())) {
final int m = JOptionPane.showConfirmDialog(JOptionPane.getRootFrame(), if (!FOptionPane.showConfirmDialog(
"There is already a deck named '" + s + "'. Overwrite?", "There is already a deck named '" + s + "'. Overwrite?",
"Overwrite Deck?", "Overwrite Deck?", false)) {
JOptionPane.YES_NO_OPTION, // If no overwrite, recurse.
JOptionPane.QUESTION_MESSAGE);
// If no overwrite, recurse.
if (m == JOptionPane.NO_OPTION) {
saveDraft(); saveDraft();
return; return;
} }

View File

@@ -1,5 +1,6 @@
package forge.gui.toolbox; package forge.gui.toolbox;
import java.awt.Component;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.FontMetrics; import java.awt.FontMetrics;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
@@ -7,7 +8,6 @@ import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter; import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import javax.swing.JComponent;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
@@ -56,27 +56,81 @@ public class FOptionPane extends FDialog {
} }
public static int showOptionDialog(String message, String title, SkinImage icon, String[] options, int defaultOption) { public static int showOptionDialog(String message, String title, SkinImage icon, String[] options, int defaultOption) {
FTextArea txtMessage = new FTextArea(message);
FSkin.get(txtMessage).setFont(FSkin.getFont(14)); final FOptionPane optionPane = new FOptionPane(message, title, icon, null, options, defaultOption);
txtMessage.setAutoSize(true);
Dimension parentSize = JOptionPane.getRootFrame().getSize();
txtMessage.setMaximumSize(new Dimension(parentSize.width / 2, parentSize.height - 100));
FOptionPane optionPane = new FOptionPane(title, icon, txtMessage, options, defaultOption);
optionPane.setVisible(true); optionPane.setVisible(true);
int dialogResult = optionPane.result; int dialogResult = optionPane.result;
optionPane.dispose(); optionPane.dispose();
return dialogResult; return dialogResult;
} }
public static String showInputDialog(String message, String title, SkinImage icon) {
return showInputDialog(message, title, icon, "", null);
}
public static String showInputDialog(String message, String title, SkinImage icon, String initialInput) {
return showInputDialog(message, title, icon, initialInput, null);
}
@SuppressWarnings("unchecked")
public static <T> T showInputDialog(String message, String title, SkinImage icon, T initialInput, T[] inputOptions) {
final Component inputField;
FTextField txtInput = null;
FComboBox<T> cbInput = null;
if (inputOptions == null) {
txtInput = new FTextField.Builder().text(initialInput.toString()).build();
inputField = txtInput;
}
else {
cbInput = new FComboBox<T>(inputOptions);
cbInput.setSelectedItem(initialInput);
inputField = cbInput;
}
final FOptionPane optionPane = new FOptionPane(message, title, icon, inputField, new String[] {"OK", "Cancel"}, -1);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
inputField.requestFocusInWindow();
}
});
inputField.addKeyListener(new KeyAdapter() { //hook so pressing Enter on field accepts dialog
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
SwingUtilities.invokeLater(new Runnable() { //delay so enter can confirm input choice first
@Override
public void run() {
optionPane.result = 0;
optionPane.setVisible(false);
}
});
}
}
});
optionPane.setVisible(true);
int dialogResult = optionPane.result;
optionPane.dispose();
if (dialogResult == 0) {
if (inputOptions == null) {
return (T)txtInput.getText();
}
else {
return (T)cbInput.getSelectedItem();
}
}
return null;
}
private int result = -1; //default result to -1, indicating dialog closed without choosing option private int result = -1; //default result to -1, indicating dialog closed without choosing option
private FOptionPane(String title, SkinImage icon, JComponent comp, String[] options, int defaultOption) { private FOptionPane(String message, String title, SkinImage icon, Component comp, String[] options, int defaultOption) {
this.setTitle(title); this.setTitle(title);
int padX = 10; int padding = 10;
int gapBottom = 2 * padX; int x = padding;
int x = padX; int gapAboveButtons = padding * 3 / 2;
int gapBottom = comp == null ? gapAboveButtons: padding;
if (icon != null) { if (icon != null) {
FLabel lblIcon = new FLabel.Builder().icon(icon).build(); FLabel lblIcon = new FLabel.Builder().icon(icon).build();
@@ -84,7 +138,18 @@ public class FOptionPane extends FDialog {
this.add(lblIcon, "x " + (x - 3) + ", ay top, w " + labelWidth + ", h " + icon.getHeight() + ", gapbottom " + gapBottom); this.add(lblIcon, "x " + (x - 3) + ", ay top, w " + labelWidth + ", h " + icon.getHeight() + ", gapbottom " + gapBottom);
x += labelWidth; x += labelWidth;
} }
this.add(comp, "x " + x + ", wrap, gapbottom " + gapBottom); if (message != null) {
FTextArea prompt = new FTextArea(message);
FSkin.get(prompt).setFont(FSkin.getFont(14));
prompt.setAutoSize(true);
Dimension parentSize = JOptionPane.getRootFrame().getSize();
prompt.setMaximumSize(new Dimension(parentSize.width / 2, parentSize.height - 100));
this.add(prompt, "x " + x + ", ay top, wrap, gaptop 7, gapbottom " + gapBottom);
x = padding;
}
if (comp != null) {
this.add(comp, "x " + x + ", w 100%-" + (x + padding) + ", wrap, gapbottom " + gapAboveButtons);
}
//determine size of buttons //determine size of buttons
int optionCount = options.length; int optionCount = options.length;
@@ -113,9 +178,9 @@ public class FOptionPane extends FDialog {
//add buttons //add buttons
x = (width - totalButtonWidth) / 2; x = (width - totalButtonWidth) / 2;
if (x < padX) { if (x < padding) {
width = totalButtonWidth + 2 * padX; //increase width to make room for buttons width = totalButtonWidth + 2 * padding; //increase width to make room for buttons
x = padX; x = padding;
} }
for (int i = 0; i < optionCount; i++) { for (int i = 0; i < optionCount; i++) {
final int option = i; final int option = i;