18
18
import java .awt .BorderLayout ;
19
19
import java .awt .Dimension ;
20
20
import java .awt .FlowLayout ;
21
+ import java .awt .Insets ;
21
22
import java .awt .event .ActionEvent ;
22
23
import java .awt .event .ActionListener ;
23
24
import java .awt .event .ComponentAdapter ;
55
56
*
56
57
*/
57
58
public class LayoutPanel extends JPanel {
58
-
59
+
59
60
private static final long serialVersionUID = 2275543202224445302L ;
60
-
61
+
61
62
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
+
63
70
public LayoutPanel () {
64
71
this (false );
65
72
}
66
-
73
+
67
74
public LayoutPanel (boolean readonly ) {
68
75
this .readonly = readonly ;
69
76
initComponents ();
70
77
postInit ();
71
78
}
72
-
79
+
73
80
private void postInit () {
74
81
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
+
80
87
if (readonly ) {
81
- this .canvas .setDrawGrid (!readonly );
82
-
88
+ //this.canvas.setDrawGrid(!readonly);
89
+ this .canvas .setGridType (0 );
90
+
83
91
this .loadBtn .setEnabled (!readonly );
84
92
this .loadBtn .setVisible (!readonly );
85
93
this .toolBar .remove (this .loadBtn );
86
-
94
+
87
95
this .routeBtn .setEnabled (readonly );
88
96
this .routeBtn .setVisible (readonly );
89
-
97
+
90
98
this .selectBtn .setEnabled (!readonly );
91
99
this .selectBtn .setVisible (!readonly );
92
100
this .toolBar .remove (this .selectBtn );
93
-
101
+
94
102
this .addBtn .setEnabled (!readonly );
95
103
this .addBtn .setVisible (!readonly );
96
-
104
+
97
105
this .deleteBtn .setEnabled (!readonly );
98
106
this .deleteBtn .setVisible (!readonly );
99
-
107
+
100
108
this .gridBtn .setEnabled (!readonly );
101
109
this .gridBtn .setVisible (!readonly );
102
-
110
+
103
111
this .straightBtn .setEnabled (!readonly );
104
112
this .straightBtn .setVisible (!readonly );
105
-
113
+
106
114
this .curvedBtn .setEnabled (!readonly );
107
115
this .curvedBtn .setVisible (!readonly );
108
-
116
+
109
117
this .blockBtn .setEnabled (!readonly );
110
118
this .blockBtn .setVisible (!readonly );
111
-
119
+
112
120
this .sensorBtn .setEnabled (!readonly );
113
121
this .sensorBtn .setVisible (!readonly );
114
-
122
+
115
123
this .signalBtn .setEnabled (!readonly );
116
124
this .signalBtn .setVisible (!readonly );
117
-
125
+
118
126
this .leftSwitchBtn .setEnabled (!readonly );
119
127
this .leftSwitchBtn .setVisible (!readonly );
120
-
128
+
121
129
this .rightSwitchBtn .setEnabled (!readonly );
122
130
this .rightSwitchBtn .setVisible (!readonly );
123
-
131
+
124
132
this .crossLBtn .setEnabled (!readonly );
125
133
this .crossLBtn .setVisible (!readonly );
126
-
134
+
127
135
this .crossRBtn .setEnabled (!readonly );
128
136
this .crossRBtn .setVisible (!readonly );
129
-
137
+
130
138
this .endTrackBtn .setEnabled (!readonly );
131
139
this .endTrackBtn .setVisible (!readonly );
132
-
140
+
133
141
this .straightDirectionBtn .setEnabled (!readonly );
134
142
this .straightDirectionBtn .setVisible (!readonly );
135
-
143
+
136
144
this .crossingBtn .setEnabled (!readonly );
137
145
this .crossingBtn .setVisible (!readonly );
138
-
146
+
139
147
this .flipVerticalBtn .setEnabled (!readonly );
140
148
this .flipVerticalBtn .setVisible (!readonly );
141
-
149
+
142
150
this .flipHorizontalBtn .setEnabled (!readonly );
143
151
this .flipHorizontalBtn .setVisible (!readonly );
152
+ } else {
153
+ gridType = 1 ;
154
+ gridBtn .setIcon (new ImageIcon (getClass ().getResource (GRID_1 )));
155
+ canvas .setGridType (gridType );
144
156
}
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
+
149
161
if (readonly ) {
150
162
loadLayout ();
151
163
Powerlistener powerlistener = new Powerlistener ();
152
164
JCS .getJcsCommandStation ().addPowerEventListener (powerlistener );
153
165
}
154
-
155
166
}
156
-
167
+
157
168
public void loadLayout () {
158
169
canvas .loadLayoutInBackground ();
159
170
}
160
-
171
+
161
172
public void rotateSelectedTile () {
162
173
canvas .rotateSelectedTile ();
163
174
}
164
-
175
+
165
176
public void flipSelectedTileHorizontal () {
166
177
canvas .flipSelectedTileHorizontal ();
167
178
}
168
-
179
+
169
180
public void flipSelectedTileVerical () {
170
181
canvas .flipSelectedTileVertical ();
171
182
}
172
-
183
+
173
184
public void deleteSelectedTile () {
174
185
canvas .deleteSelectedTile ();
175
186
}
@@ -204,7 +215,7 @@ private void initComponents() {
204
215
startAllLocomotivesBtn = new JToggleButton ();
205
216
resetAutopilotBtn = new JButton ();
206
217
filler1 = new Box .Filler (new Dimension (20 , 0 ), new Dimension (20 , 0 ), new Dimension (20 , 32767 ));
207
- gridBtn = new JToggleButton ();
218
+ gridBtn = new JButton ();
208
219
filler2 = new Box .Filler (new Dimension (20 , 0 ), new Dimension (20 , 0 ), new Dimension (20 , 32767 ));
209
220
selectBtn = new JButton ();
210
221
addBtn = new JButton ();
@@ -420,11 +431,13 @@ public void actionPerformed(ActionEvent evt) {
420
431
toolBar .add (resetAutopilotBtn );
421
432
toolBar .add (filler1 );
422
433
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 );
426
436
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 ));
428
441
gridBtn .setVerticalTextPosition (SwingConstants .BOTTOM );
429
442
gridBtn .addActionListener (new ActionListener () {
430
443
public void actionPerformed (ActionEvent evt ) {
@@ -845,10 +858,6 @@ private void sensorBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_sensor
845
858
setTileType (TileBean .TileType .SENSOR );
846
859
}//GEN-LAST:event_sensorBtnActionPerformed
847
860
848
- private void gridBtnActionPerformed (ActionEvent evt ) {//GEN-FIRST:event_gridBtnActionPerformed
849
- this .canvas .setDrawGrid (this .gridBtn .isSelected ());
850
- }//GEN-LAST:event_gridBtnActionPerformed
851
-
852
861
private void formComponentResized (ComponentEvent evt ) {//GEN-FIRST:event_formComponentResized
853
862
//TODO!
854
863
//Logger.debug(evt.getComponent().getSize());// TODO add your handling code here:
@@ -895,7 +904,7 @@ private void endTrackBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_endT
895
904
896
905
private void autoPilotBtnActionPerformed (ActionEvent evt ) {//GEN-FIRST:event_autoPilotBtnActionPerformed
897
906
Logger .trace (evt .getActionCommand () + (autoPilotBtn .isSelected () ? " Enable" : " Disable" ) + " Auto mode" );
898
-
907
+
899
908
if (autoPilotBtn .isSelected ()) {
900
909
startAllLocomotivesBtn .setEnabled (true );
901
910
} else {
@@ -904,7 +913,7 @@ private void autoPilotBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:event_aut
904
913
//}
905
914
startAllLocomotivesBtn .setEnabled (false );
906
915
}
907
-
916
+
908
917
AutoPilot .runAutoPilot (autoPilotBtn .isSelected ());
909
918
}//GEN-LAST:event_autoPilotBtnActionPerformed
910
919
@@ -923,18 +932,38 @@ private void resetAutopilotBtnActionPerformed(ActionEvent evt) {//GEN-FIRST:even
923
932
AutoPilot .reset ();
924
933
}//GEN-LAST:event_resetAutopilotBtnActionPerformed
925
934
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
+
926
955
private void setTileType (TileBean .TileType tileType ) {
927
956
canvas .setTileType (tileType );
928
957
}
929
-
958
+
930
959
private void setDirection (Direction direction ) {
931
960
canvas .setDirection (direction );
932
961
}
933
-
962
+
934
963
public void showRoutes () {
935
964
canvas .showRoutesDialog ();
936
965
}
937
-
966
+
938
967
public void setMode (LayoutCanvas .Mode mode ) {
939
968
switch (mode ) {
940
969
case SELECT -> {
@@ -958,12 +987,12 @@ public void setMode(LayoutCanvas.Mode mode) {
958
987
deleteBtn .setIcon (new ImageIcon (getClass ().getResource ("/media/delete-24.png" )));
959
988
}
960
989
}
961
-
990
+
962
991
canvas .setMode (mode );
963
992
}
964
-
993
+
965
994
private class Powerlistener implements PowerEventListener {
966
-
995
+
967
996
@ Override
968
997
public void onPowerChange (PowerEvent event ) {
969
998
//Logger.info("Track Power is " + (event.isPower() ? "on" : "off"));
@@ -998,7 +1027,7 @@ public void onPowerChange(PowerEvent event) {
998
1027
private JMenuItem flipHorizontalMI ;
999
1028
private JButton flipVerticalBtn ;
1000
1029
private JMenuItem flipVerticalMI ;
1001
- private JToggleButton gridBtn ;
1030
+ private JButton gridBtn ;
1002
1031
private JMenuItem horizontalMI ;
1003
1032
private JMenuItem leftMI ;
1004
1033
private JToggleButton leftSwitchBtn ;
0 commit comments