Made dialogs (FreeColDialog) focusable and modal.

This commit is contained in:
Stian Grenborgen 2004-05-11 22:39:56 +00:00
parent 89fcf80b77
commit c93cb9c717
3 changed files with 52 additions and 14 deletions

View File

@ -265,13 +265,12 @@ public final class Canvas extends JLayeredPane {
FreeColDialog confirmDialog = FreeColDialog.createConfirmDialog(text, okText, cancelText);
confirmDialog.setLocation(getWidth() / 2 - confirmDialog.getWidth() / 2, getHeight() / 2 - confirmDialog.getHeight() / 2);
setEnabled(false);
add(confirmDialog, new Integer(POPUP_LAYER.intValue() - 1));
confirmDialog.requestFocus();
boolean response = confirmDialog.getResponseBoolean();
remove(confirmDialog);
setEnabled(true);
return response;
}
@ -298,13 +297,12 @@ public final class Canvas extends JLayeredPane {
FreeColDialog inputDialog = FreeColDialog.createInputDialog(text, defaultValue, okText, cancelText);
inputDialog.setLocation(getWidth() / 2 - inputDialog.getWidth() / 2, getHeight() / 2 - inputDialog.getHeight() / 2);
setEnabled(false);
add(inputDialog, new Integer(POPUP_LAYER.intValue() - 1));
inputDialog.requestFocus();
String response = (String) inputDialog.getResponse();
remove(inputDialog);
setEnabled(true);
return response;
}
@ -371,10 +369,19 @@ public final class Canvas extends JLayeredPane {
* @param comp The component to remove from this Container.
*/
public void remove(Component comp) {
boolean takeFocus = true;
if (comp == statusPanel) {
takeFocus = false;
}
Rectangle bounds = comp.getBounds();
setEnabled(true);
super.remove(comp);
takeFocus();
if (takeFocus) {
takeFocus();
}
repaint(bounds.x, bounds.y, bounds.width, bounds.height);
}
@ -777,5 +784,5 @@ public final class Canvas extends JLayeredPane {
}
}
}
}

View File

@ -729,7 +729,11 @@ public final class GUI {
for (int cX=-1; cX <=1; cX++) {
for (int cY=-1; cY <=1; cY++) {
if (biX+cX >= 0 && biY+cY >= 0 && biX+cX < bi.getWidth() && biY+cY < bi.getHeight() && bi.getRGB(biX + cX, biY + cY) == playerColor) {
bi.setRGB(biX, biY, Color.BLACK.getRGB());
if (playerColor != Color.BLACK.getRGB()) {
bi.setRGB(biX, biY, Color.BLACK.getRGB());
} else {
bi.setRGB(biX, biY, Color.WHITE.getRGB());
}
continue;
}
}

View File

@ -65,11 +65,21 @@ public class FreeColDialog extends JPanel {
try {
if (SwingUtilities.isEventDispatchThread()) {
EventQueue theQueue = getToolkit().getSystemEventQueue();
while (!responseGiven) {
// This is essentially the body of EventDispatchThread
AWTEvent event = theQueue.getNextEvent();
Object src = event.getSource();
// Block 'MouseEvent' beeing sent to other components:
if (event instanceof MouseEvent) {
MouseEvent me = (MouseEvent) event;
Component dc = SwingUtilities.getDeepestComponentAt(((ComponentEvent) event).getComponent(), me.getX(), me.getY());
if (!SwingUtilities.isDescendingFrom(dc, this)) {
continue;
}
}
// We cannot call theQueue.dispatchEvent, so I pasted its body here:
if (event instanceof ActiveEvent) {
((ActiveEvent) event).dispatch();
@ -95,7 +105,7 @@ public class FreeColDialog extends JPanel {
return tempResponse;
}
/**
* Convenience method for {@link #getResponse}.
*/
@ -110,7 +120,7 @@ public class FreeColDialog extends JPanel {
public int getResponseInt() {
return ((Integer) getResponse()).intValue();
}
/**
* Creates a new <code>FreeColDialog</code> with a text and a ok/cancel option.
@ -123,7 +133,14 @@ public class FreeColDialog extends JPanel {
* @return The <code>FreeColDialog</code>.
*/
public static FreeColDialog createConfirmDialog(String text, String okText, String cancelText) {
final FreeColDialog confirmDialog = new FreeColDialog();
final JButton okButton = new JButton(okText);
final FreeColDialog confirmDialog = new FreeColDialog() {
public void requestFocus() {
okButton.requestFocus();
}
};
confirmDialog.setLayout(new BorderLayout());
JPanel labelPanel = new JPanel(new FlowLayout());
@ -131,7 +148,6 @@ public class FreeColDialog extends JPanel {
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
JButton okButton = new JButton(okText);
JButton cancelButton = new JButton(cancelText);
okButton.addActionListener(new ActionListener() {
@ -173,7 +189,14 @@ public class FreeColDialog extends JPanel {
* @return The <code>FreeColDialog</code>.
*/
public static FreeColDialog createInputDialog(String text, String defaultValue, String okText, String cancelText) {
final FreeColDialog inputDialog = new FreeColDialog();
final JTextField input = new JTextField(defaultValue);
final FreeColDialog inputDialog = new FreeColDialog() {
public void requestFocus() {
input.requestFocus();
}
};
inputDialog.setLayout(new GridLayout(3, 1));
JPanel buttons = new JPanel(new GridLayout(1, 2));
@ -181,7 +204,11 @@ public class FreeColDialog extends JPanel {
JButton okButton = new JButton(okText);
JButton cancelButton = new JButton(cancelText);
final JTextField input = new JTextField(defaultValue);
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
inputDialog.setResponse(input.getText());
}
});
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {