30
30
import javax .swing .AbstractAction ;
31
31
import javax .swing .DefaultListModel ;
32
32
import javax .swing .ImageIcon ;
33
- import javax .swing .JFrame ;
33
+ import javax .swing .JButton ;
34
34
import javax .swing .JLabel ;
35
35
import javax .swing .JList ;
36
36
import javax .swing .JPanel ;
46
46
47
47
import net .miginfocom .swing .MigLayout ;
48
48
import net .sf .freecol .client .FreeColClient ;
49
+ import net .sf .freecol .client .gui .panel .FreeColButton .ButtonStyle ;
50
+ import net .sf .freecol .client .gui .panel .FreeColPanel ;
49
51
import net .sf .freecol .client .gui .panel .MigPanel ;
50
52
import net .sf .freecol .client .gui .panel .Utility ;
51
53
import net .sf .freecol .client .gui .plaf .FreeColSelectedPanelUI ;
52
54
import net .sf .freecol .common .i18n .Messages ;
53
- import net .sf .freecol .common .model .Player ;
54
55
import net .sf .freecol .common .model .StringTemplate ;
55
56
import net .sf .freecol .common .model .Unit ;
56
57
57
58
58
59
/**
59
- * Centers the map on a known settlement or colony. Pressing ENTER
60
- * opens a panel if appropriate.
60
+ * Displays units that have no orders and allows the player to
61
+ * either end turn immediately or stop to give them orders.
61
62
*/
62
- public final class EndTurnDialog extends FreeColConfirmDialog {
63
+ public final class EndTurnDialog extends FreeColPanel {
63
64
64
65
@ SuppressWarnings ("unused" )
65
66
private static final Logger logger = Logger .getLogger (EndTurnDialog .class .getName ());
66
67
68
+ /** The list of units to display. */
69
+ private final JList <UnitWrapper > unitList ;
70
+
71
+
72
+ /**
73
+ * Creates a new dialog.
74
+ *
75
+ * @param freeColClient The {@code FreeColClient}
76
+ * @param units The list of units that still have moves left.
77
+ */
78
+ public EndTurnDialog (FreeColClient freeColClient , List <Unit > units ) {
79
+ super (freeColClient , null , new MigLayout ("wrap 1, fill" , "[align center]" , "[][][growprio 200][]" ));
80
+
81
+ final JLabel header = Utility .localizedHeader (Messages .nameKey ("endTurnDialog" ), Utility .FONTSPEC_TITLE );
82
+
83
+ JTextArea text = Utility .localizedTextArea (StringTemplate
84
+ .template ("endTurnDialog.areYouSure" )
85
+ .addAmount ("%number%" , units .size ()));
86
+
87
+ DefaultListModel <UnitWrapper > model = new DefaultListModel <>();
88
+ for (Unit unit : units ) {
89
+ model .addElement (new UnitWrapper (unit ));
90
+ }
91
+
92
+ final int numUnitRows = Math .min (4 , units .size ());
93
+
94
+ this .unitList = new JList <>(model );
95
+ this .unitList .setVisibleRowCount (numUnitRows );
96
+ this .unitList .setCellRenderer (new UnitCellRenderer ());
97
+ this .unitList .getInputMap ().put (KeyStroke .getKeyStroke ("ENTER" ), "select" );
98
+ this .unitList .getActionMap ().put ("select" , new AbstractAction () {
99
+ @ Override
100
+ public void actionPerformed (ActionEvent ae ) {
101
+ selectUnit ();
102
+ }
103
+ });
104
+
105
+ this .unitList .addListSelectionListener (new ListSelectionListener () {
106
+ @ Override
107
+ public void valueChanged (ListSelectionEvent e ) {
108
+ if (e .getValueIsAdjusting ()) return ;
109
+ selectUnit ();
110
+ }
111
+ });
112
+
113
+ JScrollPane listScroller = new JScrollPane (this .unitList );
114
+
115
+ add (header , "growx, shrinkx" );
116
+ add (text , "newline 20, growx, shrinkx, wmin 100" );
117
+ add (listScroller , "newline 10, grow, shrink" );
118
+
119
+ final JButton okButton = Utility .localizedButton ("ok" ).withButtonStyle (ButtonStyle .IMPORTANT );
120
+ okButton .addActionListener (ae -> {
121
+ getGUI ().removeComponent (this );
122
+ getFreeColClient ().getInGameController ().endTurn (false );
123
+ });
124
+ add (okButton , "newline, split 2, tag ok" );
125
+
126
+ final JButton cancelButton = Utility .localizedButton ("cancel" );
127
+ cancelButton .addActionListener (ae -> {
128
+ getGUI ().removeComponent (this );
129
+ });
130
+ add (cancelButton , "tag cancel" );
131
+
132
+ setEscapeAction (new AbstractAction () {
133
+ @ Override
134
+ public void actionPerformed (ActionEvent ae ) {
135
+ cancelButton .doClick ();
136
+ }
137
+ });
138
+ }
139
+
140
+ /**
141
+ * Select the current unit in the list.
142
+ */
143
+ private void selectUnit () {
144
+ final UnitWrapper wrapper = this .unitList .getSelectedValue ();
145
+ if (wrapper != null && wrapper .unit != null ) {
146
+ if (wrapper .unit .isInEurope ()) {
147
+ getGUI ().showEuropePanel ();
148
+ } else {
149
+ getGUI ().changeView (wrapper .unit , false );
150
+ if (wrapper .unit .getColony () != null ) {
151
+ getGUI ().showColonyPanel (wrapper .unit .getColony (), wrapper .unit );
152
+ }
153
+ }
154
+ }
155
+ }
156
+
157
+
67
158
/**
68
159
* We need to wrap the Unit class in order to make the JList
69
160
* support keystroke navigation. JList.getNextMatch uses the
@@ -84,12 +175,6 @@ public UnitWrapper(Unit unit) {
84
175
.getLocationLabelFor (unit .getOwner ()));
85
176
}
86
177
87
-
88
- // Override Object
89
-
90
- /**
91
- * {@inheritDoc}
92
- */
93
178
@ Override
94
179
public String toString () {
95
180
return name ;
@@ -102,16 +187,8 @@ public UnitCellRenderer() {
102
187
103
188
}
104
189
105
-
106
- /**
107
- * {@inheritDoc}
108
- */
109
- @ Override
110
190
public Component getListCellRendererComponent (JList <? extends UnitWrapper > list ,
111
- UnitWrapper value ,
112
- int index ,
113
- boolean isSelected ,
114
- boolean cellHasFocus ) {
191
+ UnitWrapper value , int index , boolean isSelected , boolean cellHasFocus ) {
115
192
final JLabel imageLabel = new JLabel ();
116
193
imageLabel .setIcon (new ImageIcon (getImageLibrary ().getSmallerUnitImage (value .unit )));
117
194
imageLabel .setHorizontalAlignment (SwingConstants .CENTER );
@@ -158,89 +235,4 @@ private Dimension largestIconSize(JList<? extends UnitWrapper> list) {
158
235
return new Dimension (largestWidth , largestHeight );
159
236
}
160
237
}
161
-
162
-
163
- /** The list of units to display. */
164
- private final JList <UnitWrapper > unitList ;
165
-
166
-
167
- /**
168
- * The constructor to use.
169
- *
170
- * @param freeColClient The freecol client.
171
- * @param frame The owner frame.
172
- * @param units The unit list.
173
- */
174
- public EndTurnDialog (FreeColClient freeColClient , JFrame frame , List <Unit > units ) {
175
- super (freeColClient , frame );
176
-
177
- final Player player = getMyPlayer ();
178
-
179
- JLabel header = Utility .localizedHeader (Messages .nameKey ("endTurnDialog" ),
180
- Utility .FONTSPEC_TITLE );
181
- JTextArea text = Utility .localizedTextArea (StringTemplate
182
- .template ("endTurnDialog.areYouSure" )
183
- .addAmount ("%number%" , units .size ()));
184
-
185
- DefaultListModel <UnitWrapper > model = new DefaultListModel <>();
186
- for (Unit unit : units ) {
187
- model .addElement (new UnitWrapper (unit ));
188
- }
189
-
190
- this .unitList = new JList <>(model );
191
- this .unitList .setCellRenderer (new UnitCellRenderer ());
192
- this .unitList .getInputMap ().put (KeyStroke .getKeyStroke ("ENTER" ),
193
- "select" );
194
- this .unitList .getActionMap ().put ("select" , new AbstractAction () {
195
- @ Override
196
- public void actionPerformed (ActionEvent ae ) {
197
- selectUnit ();
198
- }
199
- });
200
- this .unitList .getInputMap ().put (KeyStroke .getKeyStroke ("ESCAPE" ),
201
- "quit" );
202
- this .unitList .getActionMap ().put ("quit" , new AbstractAction () {
203
- @ Override
204
- public void actionPerformed (ActionEvent ae ) {
205
- EndTurnDialog .this .setValue (options .get (1 ));
206
- }
207
- });
208
- this .unitList .addListSelectionListener (new ListSelectionListener () {
209
- @ Override
210
- public void valueChanged (ListSelectionEvent e ) {
211
- if (e .getValueIsAdjusting ()) return ;
212
- selectUnit ();
213
- }
214
- });
215
- JScrollPane listScroller = new JScrollPane (this .unitList );
216
-
217
- JPanel panel = new MigPanel (new MigLayout ("wrap 1, fill" ,
218
- "[align center]" ));
219
- panel .add (header );
220
- panel .add (text , "newline 20" );
221
- panel .add (listScroller , "newline 10" );
222
- panel .setSize (panel .getPreferredSize ());
223
-
224
- ImageIcon icon = new ImageIcon (
225
- getImageLibrary ().getScaledNationImage (player .getNation ()));
226
- initializeConfirmDialog (frame , false , panel , icon , "ok" , "cancel" );
227
- }
228
-
229
- /**
230
- * Select the current unit in the list.
231
- */
232
- private void selectUnit () {
233
- UnitWrapper wrapper = this .unitList .getSelectedValue ();
234
- if (wrapper != null && wrapper .unit != null ) {
235
- if (wrapper .unit .isInEurope ()) {
236
- getGUI ().showEuropePanel ();
237
- } else {
238
- getGUI ().changeView (wrapper .unit , false );
239
- if (wrapper .unit .getColony () != null ) {
240
- getGUI ().showColonyPanel (wrapper .unit .getColony (),
241
- wrapper .unit );
242
- }
243
- }
244
- }
245
- }
246
238
}
0 commit comments