Skip to content

Commit 7e783f7

Browse files
committed
Fix grid button
1 parent 7d4c340 commit 7e783f7

File tree

4 files changed

+114
-86
lines changed

4 files changed

+114
-86
lines changed

src/main/java/jcs/ui/layout/LayoutCanvas.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ public enum Mode {
8888
CONTROL
8989
}
9090

91-
static final int LINE_GRID = 0;
92-
static final int DOT_GRID = 1;
91+
static final int LINE_GRID = 1;
9392

9493
private int gridType = LINE_GRID;
9594

@@ -120,8 +119,8 @@ public LayoutCanvas(boolean readonly) {
120119
setDoubleBuffered(true);
121120

122121
this.readonly = readonly;
122+
drawGrid = !readonly;
123123
this.executor = Executors.newSingleThreadExecutor();
124-
//this.executor = Executors.newCachedThreadPool();
125124

126125
this.mode = Mode.SELECT;
127126
this.orientation = Orientation.EAST;
@@ -145,12 +144,14 @@ public void paint(Graphics g) {
145144
super.paint(g);
146145

147146
if (drawGrid) {
148-
if (this.gridType == LINE_GRID) {
149-
paintLineGrid(g);
150-
} else {
151-
paintDotGrid(g);
147+
switch (gridType) {
148+
case 1 ->
149+
paintLineGrid(g);
150+
case 2 ->
151+
paintDotGrid(g);
152+
//default -> no grid
152153
}
153-
}
154+
}
154155

155156
//long now = System.currentTimeMillis();
156157
//Logger.trace("Duration: " + (now - started) + " ms.");
@@ -216,19 +217,9 @@ void setMode(LayoutCanvas.Mode mode) {
216217
Logger.trace("Mode: " + mode);
217218
}
218219

219-
void setDrawGrid(boolean flag) {
220-
if (flag) {
221-
switch (gridType) {
222-
case LINE_GRID ->
223-
gridType = DOT_GRID;
224-
case DOT_GRID ->
225-
gridType = LINE_GRID;
226-
default ->
227-
gridType = LINE_GRID;
228-
}
229-
}
230-
drawGrid = flag;
231-
repaint();
220+
void setGridType(int gridType) {
221+
this.gridType = gridType;
222+
executor.execute((() -> repaint()));
232223
}
233224

234225
void setTileType(TileBean.TileType tileType) {

src/main/java/jcs/ui/layout/LayoutPanel.form

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -317,16 +317,24 @@
317317
<AuxValue name="classDetails" type="java.lang.String" value="Box.Filler.HorizontalStrut"/>
318318
</AuxValues>
319319
</Component>
320-
<Component class="javax.swing.JToggleButton" name="gridBtn">
320+
<Component class="javax.swing.JButton" name="gridBtn">
321321
<Properties>
322322
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
323-
<Image iconType="3" name="/media/grid-2-24.png"/>
323+
<Image iconType="3" name="/media/square-grid-24.png"/>
324324
</Property>
325-
<Property name="selected" type="boolean" value="true"/>
326-
<Property name="toolTipText" type="java.lang.String" value="Show Grid"/>
325+
<Property name="focusable" type="boolean" value="false"/>
327326
<Property name="horizontalTextPosition" type="int" value="0"/>
328-
<Property name="selectedIcon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
329-
<Image iconType="3" name="/media/grid-dot-24.png"/>
327+
<Property name="margin" type="java.awt.Insets" editor="org.netbeans.beaninfo.editors.InsetsEditor">
328+
<Insets value="[2, 2, 2, 2]"/>
329+
</Property>
330+
<Property name="maximumSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
331+
<Dimension value="[38, 38]"/>
332+
</Property>
333+
<Property name="minimumSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
334+
<Dimension value="[38, 38]"/>
335+
</Property>
336+
<Property name="preferredSize" type="java.awt.Dimension" editor="org.netbeans.beaninfo.editors.DimensionEditor">
337+
<Dimension value="[38, 38]"/>
330338
</Property>
331339
<Property name="verticalTextPosition" type="int" value="3"/>
332340
</Properties>

src/main/java/jcs/ui/layout/LayoutPanel.java

Lines changed: 88 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.awt.BorderLayout;
1919
import java.awt.Dimension;
2020
import java.awt.FlowLayout;
21+
import java.awt.Insets;
2122
import java.awt.event.ActionEvent;
2223
import java.awt.event.ActionListener;
2324
import java.awt.event.ComponentAdapter;
@@ -55,121 +56,131 @@
5556
*
5657
*/
5758
public class LayoutPanel extends JPanel {
58-
59+
5960
private static final long serialVersionUID = 2275543202224445302L;
60-
61+
6162
private final boolean readonly;
62-
63+
64+
private int gridType;
65+
66+
private static final String GRID_0 = "/media/square-grid-24.png";
67+
private static final String GRID_1 = "/media/grid-2-24.png";
68+
private static final String GRID_2 = "/media/grid-dot-24.png";
69+
6370
public LayoutPanel() {
6471
this(false);
6572
}
66-
73+
6774
public LayoutPanel(boolean readonly) {
6875
this.readonly = readonly;
6976
initComponents();
7077
postInit();
7178
}
72-
79+
7380
private void postInit() {
7481
RunUtil.loadProperties();
75-
76-
this.straightBtn.setSelected(true);
77-
this.canvas.setTileType(TileType.STRAIGHT);
78-
this.setMode(readonly ? LayoutCanvas.Mode.CONTROL : LayoutCanvas.Mode.SELECT);
79-
82+
83+
straightBtn.setSelected(true);
84+
canvas.setTileType(TileType.STRAIGHT);
85+
setMode(readonly ? LayoutCanvas.Mode.CONTROL : LayoutCanvas.Mode.SELECT);
86+
8087
if (readonly) {
81-
this.canvas.setDrawGrid(!readonly);
82-
88+
//this.canvas.setDrawGrid(!readonly);
89+
this.canvas.setGridType(0);
90+
8391
this.loadBtn.setEnabled(!readonly);
8492
this.loadBtn.setVisible(!readonly);
8593
this.toolBar.remove(this.loadBtn);
86-
94+
8795
this.routeBtn.setEnabled(readonly);
8896
this.routeBtn.setVisible(readonly);
89-
97+
9098
this.selectBtn.setEnabled(!readonly);
9199
this.selectBtn.setVisible(!readonly);
92100
this.toolBar.remove(this.selectBtn);
93-
101+
94102
this.addBtn.setEnabled(!readonly);
95103
this.addBtn.setVisible(!readonly);
96-
104+
97105
this.deleteBtn.setEnabled(!readonly);
98106
this.deleteBtn.setVisible(!readonly);
99-
107+
100108
this.gridBtn.setEnabled(!readonly);
101109
this.gridBtn.setVisible(!readonly);
102-
110+
103111
this.straightBtn.setEnabled(!readonly);
104112
this.straightBtn.setVisible(!readonly);
105-
113+
106114
this.curvedBtn.setEnabled(!readonly);
107115
this.curvedBtn.setVisible(!readonly);
108-
116+
109117
this.blockBtn.setEnabled(!readonly);
110118
this.blockBtn.setVisible(!readonly);
111-
119+
112120
this.sensorBtn.setEnabled(!readonly);
113121
this.sensorBtn.setVisible(!readonly);
114-
122+
115123
this.signalBtn.setEnabled(!readonly);
116124
this.signalBtn.setVisible(!readonly);
117-
125+
118126
this.leftSwitchBtn.setEnabled(!readonly);
119127
this.leftSwitchBtn.setVisible(!readonly);
120-
128+
121129
this.rightSwitchBtn.setEnabled(!readonly);
122130
this.rightSwitchBtn.setVisible(!readonly);
123-
131+
124132
this.crossLBtn.setEnabled(!readonly);
125133
this.crossLBtn.setVisible(!readonly);
126-
134+
127135
this.crossRBtn.setEnabled(!readonly);
128136
this.crossRBtn.setVisible(!readonly);
129-
137+
130138
this.endTrackBtn.setEnabled(!readonly);
131139
this.endTrackBtn.setVisible(!readonly);
132-
140+
133141
this.straightDirectionBtn.setEnabled(!readonly);
134142
this.straightDirectionBtn.setVisible(!readonly);
135-
143+
136144
this.crossingBtn.setEnabled(!readonly);
137145
this.crossingBtn.setVisible(!readonly);
138-
146+
139147
this.flipVerticalBtn.setEnabled(!readonly);
140148
this.flipVerticalBtn.setVisible(!readonly);
141-
149+
142150
this.flipHorizontalBtn.setEnabled(!readonly);
143151
this.flipHorizontalBtn.setVisible(!readonly);
152+
} else {
153+
gridType = 1;
154+
gridBtn.setIcon(new ImageIcon(getClass().getResource(GRID_1)));
155+
canvas.setGridType(gridType);
144156
}
145-
this.toolBar.remove(this.autoPilotBtn);
146-
this.toolBar.remove(this.resetAutopilotBtn);
147-
this.toolBar.remove(this.startAllLocomotivesBtn);
148-
157+
toolBar.remove(autoPilotBtn);
158+
toolBar.remove(resetAutopilotBtn);
159+
toolBar.remove(startAllLocomotivesBtn);
160+
149161
if (readonly) {
150162
loadLayout();
151163
Powerlistener powerlistener = new Powerlistener();
152164
JCS.getJcsCommandStation().addPowerEventListener(powerlistener);
153165
}
154-
155166
}
156-
167+
157168
public void loadLayout() {
158169
canvas.loadLayoutInBackground();
159170
}
160-
171+
161172
public void rotateSelectedTile() {
162173
canvas.rotateSelectedTile();
163174
}
164-
175+
165176
public void flipSelectedTileHorizontal() {
166177
canvas.flipSelectedTileHorizontal();
167178
}
168-
179+
169180
public void flipSelectedTileVerical() {
170181
canvas.flipSelectedTileVertical();
171182
}
172-
183+
173184
public void deleteSelectedTile() {
174185
canvas.deleteSelectedTile();
175186
}
@@ -204,7 +215,7 @@ private void initComponents() {
204215
startAllLocomotivesBtn = new JToggleButton();
205216
resetAutopilotBtn = new JButton();
206217
filler1 = new Box.Filler(new Dimension(20, 0), new Dimension(20, 0), new Dimension(20, 32767));
207-
gridBtn = new JToggleButton();
218+
gridBtn = new JButton();
208219
filler2 = new Box.Filler(new Dimension(20, 0), new Dimension(20, 0), new Dimension(20, 32767));
209220
selectBtn = new JButton();
210221
addBtn = new JButton();
@@ -420,11 +431,13 @@ public void actionPerformed(ActionEvent evt) {
420431
toolBar.add(resetAutopilotBtn);
421432
toolBar.add(filler1);
422433

423-
gridBtn.setIcon(new ImageIcon(getClass().getResource("/media/grid-2-24.png"))); // NOI18N
424-
gridBtn.setSelected(true);
425-
gridBtn.setToolTipText("Show Grid");
434+
gridBtn.setIcon(new ImageIcon(getClass().getResource("/media/square-grid-24.png"))); // NOI18N
435+
gridBtn.setFocusable(false);
426436
gridBtn.setHorizontalTextPosition(SwingConstants.CENTER);
427-
gridBtn.setSelectedIcon(new ImageIcon(getClass().getResource("/media/grid-dot-24.png"))); // NOI18N
437+
gridBtn.setMargin(new Insets(2, 2, 2, 2));
438+
gridBtn.setMaximumSize(new Dimension(38, 38));
439+
gridBtn.setMinimumSize(new Dimension(38, 38));
440+
gridBtn.setPreferredSize(new Dimension(38, 38));
428441
gridBtn.setVerticalTextPosition(SwingConstants.BOTTOM);
429442
gridBtn.addActionListener(new ActionListener() {
430443
public void actionPerformed(ActionEvent evt) {
@@ -845,10 +858,6 @@ private void sensorBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_sensor
845858
setTileType(TileBean.TileType.SENSOR);
846859
}//GEN-LAST:event_sensorBtnActionPerformed
847860

848-
private void gridBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_gridBtnActionPerformed
849-
this.canvas.setDrawGrid(this.gridBtn.isSelected());
850-
}//GEN-LAST:event_gridBtnActionPerformed
851-
852861
private void formComponentResized(ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
853862
//TODO!
854863
//Logger.debug(evt.getComponent().getSize());// TODO add your handling code here:
@@ -895,7 +904,7 @@ private void endTrackBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_endT
895904

896905
private void autoPilotBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_autoPilotBtnActionPerformed
897906
Logger.trace(evt.getActionCommand() + (autoPilotBtn.isSelected() ? " Enable" : " Disable") + " Auto mode");
898-
907+
899908
if (autoPilotBtn.isSelected()) {
900909
startAllLocomotivesBtn.setEnabled(true);
901910
} else {
@@ -904,7 +913,7 @@ private void autoPilotBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_aut
904913
//}
905914
startAllLocomotivesBtn.setEnabled(false);
906915
}
907-
916+
908917
AutoPilot.runAutoPilot(autoPilotBtn.isSelected());
909918
}//GEN-LAST:event_autoPilotBtnActionPerformed
910919

@@ -923,18 +932,38 @@ private void resetAutopilotBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:even
923932
AutoPilot.reset();
924933
}//GEN-LAST:event_resetAutopilotBtnActionPerformed
925934

935+
private void gridBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_gridBtnActionPerformed
936+
gridType++;
937+
if (gridType > 2) {
938+
gridType = 0;
939+
}
940+
switch (gridType) {
941+
942+
case 0 -> {
943+
gridBtn.setIcon(new ImageIcon(getClass().getResource(GRID_0)));
944+
}
945+
case 1 -> {
946+
gridBtn.setIcon(new ImageIcon(getClass().getResource(GRID_1)));
947+
}
948+
case 2 -> {
949+
gridBtn.setIcon(new ImageIcon(getClass().getResource(GRID_2)));
950+
}
951+
}
952+
canvas.setGridType(gridType);
953+
}//GEN-LAST:event_gridBtnActionPerformed
954+
926955
private void setTileType(TileBean.TileType tileType) {
927956
canvas.setTileType(tileType);
928957
}
929-
958+
930959
private void setDirection(Direction direction) {
931960
canvas.setDirection(direction);
932961
}
933-
962+
934963
public void showRoutes() {
935964
canvas.showRoutesDialog();
936965
}
937-
966+
938967
public void setMode(LayoutCanvas.Mode mode) {
939968
switch (mode) {
940969
case SELECT -> {
@@ -958,12 +987,12 @@ public void setMode(LayoutCanvas.Mode mode) {
958987
deleteBtn.setIcon(new ImageIcon(getClass().getResource("/media/delete-24.png")));
959988
}
960989
}
961-
990+
962991
canvas.setMode(mode);
963992
}
964-
993+
965994
private class Powerlistener implements PowerEventListener {
966-
995+
967996
@Override
968997
public void onPowerChange(PowerEvent event) {
969998
//Logger.info("Track Power is " + (event.isPower() ? "on" : "off"));
@@ -998,7 +1027,7 @@ public void onPowerChange(PowerEvent event) {
9981027
private JMenuItem flipHorizontalMI;
9991028
private JButton flipVerticalBtn;
10001029
private JMenuItem flipVerticalMI;
1001-
private JToggleButton gridBtn;
1030+
private JButton gridBtn;
10021031
private JMenuItem horizontalMI;
10031032
private JMenuItem leftMI;
10041033
private JToggleButton leftSwitchBtn;
200 Bytes
Loading

0 commit comments

Comments
 (0)