I'm making the GUI for flipping puzzle :)
[puzzles.git] / src / com / github / puzzles / gui / SlidingPuzzleDialog.java
blob94c16df22bdf20ebcc941f7b4a09c30b00a1febb
1 package com.github.puzzles.gui;
3 import java.awt.BorderLayout;
4 import java.awt.FlowLayout;
5 import java.awt.event.MouseAdapter;
6 import java.awt.event.MouseEvent;
8 import javax.swing.JButton;
9 import javax.swing.JDialog;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 import javax.swing.JTextField;
13 import javax.swing.border.EmptyBorder;
15 import com.github.puzzles.core.SlidingPuzzle;
17 public class SlidingPuzzleDialog extends JDialog {
19 /**
22 private static final long serialVersionUID = 4729972122941718936L;
23 private final JPanel contentPanel = new JPanel();
24 private JTextField rowsText;
25 private JTextField colsText;
26 private JLabel rowsNumberMessage;
27 private JLabel rowsErrorMessage;
28 private JLabel colsNumberMessage;
29 private JLabel colsErrorMessage;
31 /**
32 * Launch the application.
35 * public static void main(String[] args) { try { FlippingPuzzleDialog
36 * dialog = new FlippingPuzzleDialog();
37 * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
38 * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
39 * //
42 /**
43 * Create the dialog.
45 public SlidingPuzzleDialog(final MainWindow mainWindow) {
46 setBounds(100, 100, 450, 300);
47 getContentPane().setLayout(new BorderLayout());
48 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
49 getContentPane().add(contentPanel, BorderLayout.CENTER);
50 contentPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
52 JPanel panel = new JPanel();
53 contentPanel.add(panel);
55 rowsNumberMessage = new JLabel("Rows number");
56 panel.add(rowsNumberMessage);
59 rowsText = new JTextField();
60 panel.add(rowsText);
61 rowsText.setColumns(10);
64 rowsErrorMessage = new JLabel("* should be a number.");
65 rowsErrorMessage.setVisible(false);
66 panel.add(rowsErrorMessage);
70 JPanel panel = new JPanel();
71 contentPanel.add(panel);
73 colsNumberMessage = new JLabel("Cols number");
74 panel.add(colsNumberMessage);
77 colsText = new JTextField();
78 panel.add(colsText);
79 colsText.setColumns(10);
82 colsErrorMessage = new JLabel("* should be a number");
83 colsErrorMessage.setVisible(false);
84 panel.add(colsErrorMessage);
87 JPanel buttonPane = new JPanel();
88 buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
89 getContentPane().add(buttonPane, BorderLayout.SOUTH);
91 JButton okButton = new JButton("OK");
92 okButton.addMouseListener(new MouseAdapter() {
93 @Override
94 public void mouseReleased(MouseEvent e) {
95 int rows = 0;
96 int cols = 0;
97 boolean error = false;
98 rowsErrorMessage.setVisible(false);
99 colsErrorMessage.setVisible(false);
100 try {
101 rows = Integer.parseInt(rowsText.getText());
102 error = true;
103 } catch (NumberFormatException e1) {
104 rowsErrorMessage.setVisible(true);
106 try {
107 cols = Integer.parseInt(colsText.getText());
108 error = true;
109 } catch (NumberFormatException e1) {
110 colsErrorMessage.setVisible(true);
113 if (error) {
114 mainWindow.setSliddingPuzzle(new SlidingPuzzle(
115 rows, cols));
116 SlidingPuzzleDialog.this.dispose();
118 SlidingPuzzleDialog.this.dispose();
121 okButton.setActionCommand("OK");
122 buttonPane.add(okButton);
123 getRootPane().setDefaultButton(okButton);
126 JButton cancelButton = new JButton("Cancel");
127 cancelButton.addMouseListener(new MouseAdapter() {
128 @Override
129 public void mouseReleased(MouseEvent e) {
130 SlidingPuzzleDialog.this.dispose();
133 cancelButton.setActionCommand("Cancel");
134 buttonPane.add(cancelButton);