I'm making the GUI for flipping puzzle :)
[puzzles.git] / src / com / github / puzzles / gui / FlippingPuzzleDialog.java
blob9fc81db1f02ddedc2c28c79a3f6aa9ab57479756
1 package com.github.puzzles.gui;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
6 import javax.swing.JButton;
7 import javax.swing.JDialog;
8 import javax.swing.JPanel;
9 import javax.swing.border.EmptyBorder;
10 import javax.swing.JTextField;
11 import javax.swing.JLabel;
13 import com.github.puzzles.core.FlippingPuzzle;
15 import java.awt.event.MouseAdapter;
16 import java.awt.event.MouseEvent;
18 public class FlippingPuzzleDialog extends JDialog {
20 /**
23 private static final long serialVersionUID = 4729972122941718936L;
24 private final JPanel contentPanel = new JPanel();
25 private JTextField rowsText;
26 private JTextField colsText;
27 private JLabel rowsNumberMessage;
28 private JLabel rowsErrorMessage;
29 private JLabel colsNumberMessage;
30 private JLabel colsErrorMessage;
32 /**
33 * Launch the application.
36 * public static void main(String[] args) { try { FlippingPuzzleDialog
37 * dialog = new FlippingPuzzleDialog();
38 * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
39 * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
40 * //
43 /**
44 * Create the dialog.
46 public FlippingPuzzleDialog(final MainWindow mainWindow) {
47 setBounds(100, 100, 450, 300);
48 getContentPane().setLayout(new BorderLayout());
49 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
50 getContentPane().add(contentPanel, BorderLayout.CENTER);
51 contentPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
53 JPanel panel = new JPanel();
54 contentPanel.add(panel);
56 rowsNumberMessage = new JLabel("Rows number");
57 panel.add(rowsNumberMessage);
60 rowsText = new JTextField();
61 panel.add(rowsText);
62 rowsText.setColumns(10);
65 rowsErrorMessage = new JLabel("* should be a number.");
66 rowsErrorMessage.setVisible(false);
67 panel.add(rowsErrorMessage);
71 JPanel panel = new JPanel();
72 contentPanel.add(panel);
74 colsNumberMessage = new JLabel("Cols number");
75 panel.add(colsNumberMessage);
78 colsText = new JTextField();
79 panel.add(colsText);
80 colsText.setColumns(10);
83 colsErrorMessage = new JLabel("* should be a number");
84 colsErrorMessage.setVisible(false);
85 panel.add(colsErrorMessage);
88 JPanel buttonPane = new JPanel();
89 buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
90 getContentPane().add(buttonPane, BorderLayout.SOUTH);
92 JButton okButton = new JButton("OK");
93 okButton.addMouseListener(new MouseAdapter() {
94 @Override
95 public void mouseReleased(MouseEvent e) {
96 int rows = 0;
97 int cols = 0;
98 boolean error = false;
99 rowsErrorMessage.setVisible(false);
100 colsErrorMessage.setVisible(false);
101 try {
102 rows = Integer.parseInt(rowsText.getText());
103 error = true;
104 } catch (NumberFormatException e1) {
105 rowsErrorMessage.setVisible(true);
107 try {
108 cols = Integer.parseInt(colsText.getText());
109 error = true;
110 } catch (NumberFormatException e1) {
111 colsErrorMessage.setVisible(true);
114 if (error) {
115 mainWindow.setFlippingPuzzle(new FlippingPuzzle(
116 rows, cols));
117 FlippingPuzzleDialog.this.dispose();
120 FlippingPuzzleDialog.this.dispose();
124 okButton.setActionCommand("OK");
125 buttonPane.add(okButton);
126 getRootPane().setDefaultButton(okButton);
129 JButton cancelButton = new JButton("Cancel");
130 cancelButton.addMouseListener(new MouseAdapter() {
131 @Override
132 public void mouseReleased(MouseEvent e) {
133 FlippingPuzzleDialog.this.dispose();
136 cancelButton.setActionCommand("Cancel");
137 buttonPane.add(cancelButton);