I implemented the focused cells changing color, still a lots of work :)
[puzzles.git] / src / com / github / puzzles / gui / FlippingPuzzleDialog.java
blobb11ea6214cb9c5b7372447118f689c562d42a349
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.FlippingPuzzle;
17 public class FlippingPuzzleDialog 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 rowsNotANumberErrorMessage;
28 private JLabel colsNumberMessage;
29 private JLabel colsNotANumberErrorMessage;
30 private JLabel colsNegativeNumberErrorMessage;
31 private JLabel rowsNegativeNumberErrorMessage;
33 /**
34 * Launch the application.
37 * public static void main(String[] args) { try { FlippingPuzzleDialog
38 * dialog = new FlippingPuzzleDialog();
39 * dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
40 * dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } }
41 * //
44 /**
45 * Create the dialog.
47 public FlippingPuzzleDialog(final MainWindow mainWindow) {
48 setBounds(100, 100, 450, 300);
49 getContentPane().setLayout(new BorderLayout());
50 contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
51 getContentPane().add(contentPanel, BorderLayout.CENTER);
52 contentPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
54 JPanel panel = new JPanel();
55 contentPanel.add(panel);
57 rowsNumberMessage = new JLabel("Rows number");
58 panel.add(rowsNumberMessage);
61 rowsText = new JTextField();
62 panel.add(rowsText);
63 rowsText.setColumns(10);
65 rowsNotANumberErrorMessage = new JLabel("* should be a number.");
66 panel.add(rowsNotANumberErrorMessage);
68 rowsNegativeNumberErrorMessage = new JLabel(
69 "* should be a positif value.");
70 rowsNegativeNumberErrorMessage.setVisible(false);
71 panel.add(rowsNegativeNumberErrorMessage);
72 rowsNotANumberErrorMessage.setVisible(false);
75 JPanel panel = new JPanel();
76 contentPanel.add(panel);
78 colsNumberMessage = new JLabel("Cols number");
79 panel.add(colsNumberMessage);
82 colsText = new JTextField();
83 panel.add(colsText);
84 colsText.setColumns(10);
87 colsNotANumberErrorMessage = new JLabel("* should be a number");
88 colsNotANumberErrorMessage.setVisible(false);
89 panel.add(colsNotANumberErrorMessage);
92 colsNegativeNumberErrorMessage = new JLabel(
93 "* should be a positif value.");
94 colsNegativeNumberErrorMessage.setVisible(false);
95 panel.add(colsNegativeNumberErrorMessage);
98 JPanel buttonPane = new JPanel();
99 buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
100 getContentPane().add(buttonPane, BorderLayout.SOUTH);
102 JButton okButton = new JButton("OK");
103 okButton.addMouseListener(new MouseAdapter() {
104 @Override
105 public void mouseReleased(MouseEvent e) {
106 int rows = 0;
107 int cols = 0;
108 boolean error = false;
110 rowsNotANumberErrorMessage.setVisible(false);
111 rowsNegativeNumberErrorMessage.setVisible(false);
112 colsNotANumberErrorMessage.setVisible(false);
113 colsNegativeNumberErrorMessage.setVisible(false);
114 try {
115 rows = Integer.parseInt(rowsText.getText());
116 if (rows <= 0) {
117 error = true;
118 rowsNegativeNumberErrorMessage.setVisible(true);
120 } catch (NumberFormatException e1) {
121 rowsNotANumberErrorMessage.setVisible(true);
122 error = true;
125 try {
126 cols = Integer.parseInt(colsText.getText());
127 if (cols <= 0) {
128 error = true;
129 colsNegativeNumberErrorMessage.setVisible(true);
131 } catch (NumberFormatException e1) {
132 colsNotANumberErrorMessage.setVisible(true);
133 error = true;
136 if (!error) {
137 mainWindow.reset();
138 mainWindow.setFlippingPuzzle(new FlippingPuzzle(
139 rows, cols));
140 // puzzlePanel.add(new
141 // FlippingPuzzlePanel(MainWindow.this));
142 mainWindow.getPuzzlePanel().add(
143 new FlippingPuzzlePanel(mainWindow));
144 mainWindow.getFrame().revalidate();
145 mainWindow.getFrame().repaint();
147 FlippingPuzzleDialog.this.dispose();
151 okButton.setActionCommand("OK");
152 buttonPane.add(okButton);
153 getRootPane().setDefaultButton(okButton);
156 JButton cancelButton = new JButton("Cancel");
157 cancelButton.addMouseListener(new MouseAdapter() {
158 @Override
159 public void mouseReleased(MouseEvent e) {
160 FlippingPuzzleDialog.this.dispose();
163 cancelButton.setActionCommand("Cancel");
164 buttonPane.add(cancelButton);
169 public JLabel getColsNegativeNumberErrorMessage() {
170 return colsNegativeNumberErrorMessage;
173 public JLabel getRowsNegativeNumberErrorMessage() {
174 return rowsNegativeNumberErrorMessage;