Skip to content

Commit 97e43d1

Browse files
author
Arun Prasaad
committed
Refactor long constructor
1 parent 76a8587 commit 97e43d1

File tree

1 file changed

+42
-33
lines changed

1 file changed

+42
-33
lines changed

src/main/java/ihm/Editor.java

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,21 @@
1515
import logic.TileType;
1616
import logic.Controller;
1717
import logic.LevelFile;
18+
import org.jspecify.annotations.NullMarked;
1819

20+
@NullMarked
1921
public class Editor extends JFrame implements MouseListener, MouseMotionListener {
2022

2123
private final LevelFile levelFile;
2224
private final int rowCount;
2325
private final int columnCount;
2426

27+
private final int windowWidth;
28+
private final int windowHeight;
29+
private final Controller controller;
30+
31+
private TileType content = TileType.OUTSIDE;
32+
2533
@VisibleForTesting
2634
enum Component {
2735
// Tool buttons
@@ -50,11 +58,6 @@ enum Component {
5058
@VisibleForTesting
5159
static final int X_OFFSET = 10; // Horizontal offset from window edge to grid start
5260

53-
private int windowWidth = 0;
54-
private int windowHeight = 0;
55-
private final Controller controller;
56-
private TileType content = TileType.OUTSIDE;
57-
5861
@VisibleForTesting
5962
TileType getContent() {
6063
return content;
@@ -68,15 +71,33 @@ public Editor (int rowCount, int columnCount, String name) throws IOException {
6871
initializeEmptyLevel();
6972

7073
controller = new Controller(levelFile.getFilePath().toString());
71-
windowWidth = controller.warehouse.getColumns() * TILE_SIZE;
74+
windowWidth = controller.warehouse.getColumns() * TILE_SIZE;
7275
windowHeight = controller.warehouse.getLines() * TILE_SIZE;
76+
setupFrame();
7377

74-
this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
78+
JLabel errorLabel = createErrorLabel();
79+
createSaveButton(errorLabel);
80+
81+
this.pack();
82+
this.setVisible(true);
83+
}
84+
85+
private void setupFrame() {
86+
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
7587
this.setTitle("Sokoban v1.0 par Gabriel FARAGO");
7688
this.setPreferredSize(new Dimension(windowWidth + 150, Math.max(windowHeight + 150, 330)));
7789
this.setResizable(false);
7890
this.setLocationRelativeTo(null);
7991

92+
this.addMouseListener(this);
93+
this.addMouseMotionListener(this);
94+
95+
setupSokobanPanel();
96+
97+
// Action buttons
98+
createBackButton();
99+
createQuitButton();
100+
80101
// Tool buttons
81102
createPlayerButton();
82103
createBackgroundButton();
@@ -86,34 +107,13 @@ public Editor (int rowCount, int columnCount, String name) throws IOException {
86107
createWallButton();
87108
createTargetButton();
88109
createEmptyButton();
110+
}
89111

90-
JLabel errorLabel = createErrorLabel();
91-
92-
// Action buttons
93-
createBackButton();
94-
createQuitButton();
95-
JButton save = createSaveButton();
96-
97-
save.addActionListener(_ -> {
98-
if (isValidLevel()) {
99-
saveLevelToFile();
100-
dispose();
101-
new HomeWindow();
102-
} else {
103-
errorLabel.setText("Invalid level!");
104-
}
105-
});
106-
112+
private void setupSokobanPanel() {
107113
JPanel sokobanPanel = new SokobanPanel(controller);
108114
sokobanPanel.setName(Component.SOKOBAN_PANEL.name());
109115
this.add(sokobanPanel);
110-
111-
this.addMouseMotionListener(this);
112-
this.addMouseListener(this);
113-
this.addMouseMotionListener(this);
114-
this.pack();
115-
this.setVisible( true );
116-
}
116+
}
117117

118118
private void initializeEmptyLevel() {
119119
final var wall = TileType.WALL.codeAsString();
@@ -228,12 +228,21 @@ private void createTargetButton() {
228228
this.add(button);
229229
}
230230

231-
private JButton createSaveButton() {
231+
private void createSaveButton(JLabel errorLabel) {
232232
JButton save = new JButton("Save");
233233
save.setName(Component.SAVE_BUTTON.name());
234234
save.setBounds(windowWidth + 20, 170, 110, 30);
235+
236+
save.addActionListener(_ -> {
237+
if (isValidLevel()) {
238+
saveLevelToFile();
239+
dispose();
240+
new HomeWindow();
241+
} else {
242+
errorLabel.setText("Invalid level!");
243+
}
244+
});
235245
this.add(save);
236-
return save;
237246
}
238247

239248
private JLabel createErrorLabel() {

0 commit comments

Comments
 (0)