File: ConfigDialog.java

package info (click to toggle)
spring 104.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 47,512 kB
  • sloc: cpp: 391,093; ansic: 79,943; python: 12,356; java: 12,201; awk: 5,889; sh: 1,826; xml: 655; makefile: 486; perl: 405; php: 211; objc: 194; sed: 2
file content (318 lines) | stat: -rw-r--r-- 12,626 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright Hugh Perkins 2009
// hughperkins@gmail.com http://manageddreams.com
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
//  more details.
//
// You should have received a copy of the GNU General Public License along
// with this program in the file licence.txt; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
// 1307 USA
// You can find the licence also on the web at:
// http://www.opensource.org/licenses/gpl-license.php
//
// ======================================================================================
//

package hughai.ui;

import java.awt.*;
import java.awt.event.*;

import java.lang.reflect.*;

import javax.swing.*;

import java.util.*;
import java.util.Map;
import java.lang.annotation.*;

import com.springrts.ai.*;
import com.springrts.ai.oo.clb.*;

import hughai.*;
import hughai.EnemyTracker.EnemyAdapter;
import hughai.basictypes.*;
import hughai.mapping.*;
import hughai.packcoordinators.*;
import hughai.unitdata.*;
import hughai.unitdata.UnitController.UnitAdapter;
import hughai.utils.*;
import hughai.utils.ConfigController.ConfigSource;

// builds the configuration tab in the gui
// using reflection
// supports most primitive types
// todo: lists, custom classes, lists of custom classes..
public class ConfigDialog {
   PlayerObjects playerObjects;

   JPanel configPanel;
   GridLayout configGridLayout;

   public ConfigDialog( PlayerObjects playerObjects ) {
      this.playerObjects = playerObjects;
      Init();
   }
   
   public void Init() {
      configGridLayout = new GridLayout(0,2);
      configPanel = new JPanel( configGridLayout );
      
      playerObjects.getMainUI().addPanelToTabbedPanel( "Config", configPanel );

      buildConfigPanel();      
   }
   Method getGetMethod( Class<?> targetClass, Class<?> fieldType, String fieldName ) {
      fieldName = fieldName.substring( 0, 1 ).toUpperCase()
      + fieldName.substring( 1 );
      String methodname = "get" + fieldName;
      if( fieldType == boolean.class || fieldType == Boolean.class ) {
         methodname = "is" + fieldName;
      }
      Method method = null;
      try {
         method = targetClass.getMethod( methodname, new Class<?>[0] );
      } catch( Exception e ) {
         playerObjects.getLogFile().WriteLine( Formatting.exceptionToStackTrace( e ) );
      }
      return method;
   }

   Method getSetMethod( Class<?> targetClass, Class<?> fieldType, String fieldName ) {
      fieldName = fieldName.substring( 0, 1 ).toUpperCase()
      + fieldName.substring( 1 );
      String methodname = "set" + fieldName;
      Method method = null;
      try {
         method = targetClass.getMethod( methodname, new Class<?>[]{ fieldType } );
      } catch( Exception e ) {
         playerObjects.getLogFile().WriteLine( Formatting.exceptionToStackTrace( e ) );
      }
      return method;
   }

   void buildConfigPanel() {
      try {
         Config config = playerObjects.getConfig();
         for( Field field : config.getClass().getDeclaredFields() ) {
            Annotation excludeAnnotation = field.getAnnotation( ReflectionHelper.Exclude.class );
            if( excludeAnnotation == null ) { // so, this field is not excluded
               Class<?> fieldType = field.getType();
               Method getMethod = getGetMethod( config.getClass(), field.getType(), field.getName() );
               if( getMethod != null ) {
                  Object value = getMethod.invoke( config ); 
                  if( fieldType == String.class ) {
                     addTextBox( field.getName(), (String)value );
                  }
                  if( fieldType == boolean.class || fieldType == Boolean.class ) {
                     addBooleanComponent( field.getName(), (Boolean)value );
                  }
                  if( fieldType == float.class || fieldType == Float.class ) {
                     addTextBox( field.getName(), "" + value );
                  }
                  if( fieldType == int.class || fieldType == Integer.class ) {
                     addTextBox( field.getName(), "" + value );
                  }
               } else {
                  playerObjects.getLogFile().WriteLine( "No get accessor method for config field " + field.getName() );
               }
            }
         }
      } catch( Exception e ) {
         playerObjects.getLogFile().WriteLine( Formatting.exceptionToStackTrace( e ) );
      }

      configGridLayout.setRows( configGridLayout.getRows() + 2 );

      configRevertButton = new JButton( "Revert" );
      configReloadButton = new JButton( "Reload" );
      configApplyButton = new JButton( "Apply" );
      configSaveButton = new JButton( "Save" );
      
      configRevertButton.addActionListener( new ConfigRevert() );
      configReloadButton.addActionListener( new ConfigReload() );
      configApplyButton.addActionListener( new ConfigApply() );
      configSaveButton.addActionListener( new ConfigSave() );

      configPanel.add( configRevertButton );
      configPanel.add( configReloadButton );
      configPanel.add( configApplyButton );
      configPanel.add( configSaveButton );
   }

   JButton configRevertButton;
   JButton configReloadButton;
   JButton configApplyButton;
   JButton configSaveButton;
   HashMap< String, JComponent > componentByName = new HashMap<String, JComponent>();

   void addTextBox( String name, String currentValue ) {
      configGridLayout.setRows( configGridLayout.getRows() + 1 );
      addConfigLabel( name );

      JTextField textField = new JTextField();
      textField.setText( currentValue );

      componentByName.put(  name, textField );
      configPanel.add( textField );
   }

   void addConfigLabel( String labelName ) {
      JLabel label = new JLabel( labelName );
      configPanel.add( label );      
   }

   void addBooleanComponent( String name, boolean currentValue ) {
      configGridLayout.setRows( configGridLayout.getRows() + 1 );
      addConfigLabel( name );

      JCheckBox checkBox = new JCheckBox();
      checkBox.setSelected( currentValue );

      componentByName.put(  name, checkBox );
      configPanel.add( checkBox );
   }

   void debug( Object message ) {
      playerObjects.getLogFile().WriteLine( "" + message );
   }
   
   class ConfigRevert implements ActionListener {
      @Override
      public void actionPerformed( ActionEvent event ){
         revertConfig();
      }
   }
      
   void revertConfig() {
      try {
         debug("reverting config panel");
         Config config = playerObjects.getConfig();
         for( Field field : config.getClass().getDeclaredFields() ) {
            Annotation excludeAnnotation = field.getAnnotation( ReflectionHelper.Exclude.class );
            if( excludeAnnotation == null ) { // so, this field is not excluded
               debug("field " + field.getName() );
               Class<?> fieldType = field.getType();
               Method getMethod = getGetMethod( config.getClass(), field.getType(), field.getName() );
               if( getMethod != null ) {
                  debug(" ... found accessor method" );
                  Object value = getMethod.invoke( config );
                  String fieldname = field.getName();
                  Component component = componentByName.get( fieldname );
                  if( component != null ) {
                     debug(" ... found component" );
                     if( fieldType == String.class ) {
                        ((JTextField)component).setText( (String )value );
                     }
                     if( fieldType == boolean.class || fieldType == Boolean.class ) {
                        ((JCheckBox)component).setSelected( (Boolean )value );
                     }
                     if( fieldType == float.class || fieldType == Float.class ) {
                        ((JTextField)component).setText( "" + value );
                     }
                     if( fieldType == int.class || fieldType == Integer.class ) {
                        ((JTextField)component).setText( "" + value );
                     }
                  }
               } else {
                  playerObjects.getLogFile().WriteLine( "No get accessor method for config field " + field.getName() );
               }
            }
         }
      } catch( Exception e ) {
         playerObjects.getLogFile().WriteLine( Formatting.exceptionToStackTrace( e ) );
      }
   }

   class ConfigApply implements ActionListener {
      @Override
      public void actionPerformed( ActionEvent event ){
         applyConfig();
      }
   }
   
   void applyConfig() {
      debug("applying config from panel");
      Config config = playerObjects.getConfig();
      for( Field field : config.getClass().getDeclaredFields() ) {
         Annotation excludeAnnotation = field.getAnnotation( ReflectionHelper.Exclude.class );
         if( excludeAnnotation == null ) { // so, this field is not excluded
            debug("field " + field.getName() );
            Class<?> fieldType = field.getType();
            Method setMethod = getSetMethod( config.getClass(), field.getType(), field.getName() );
            if( setMethod != null ) {
               debug(" ... found accessor method" );
               String fieldname = field.getName();
               Component component = componentByName.get( fieldname );
               if( component != null ) {
                  debug(" ... found component" );
                  Object value = null;
                  if( fieldType == String.class ) {
                     value = ((JTextField)component).getText();
                  }
                  if( fieldType == boolean.class || fieldType == Boolean.class ) {
                     value = ((JCheckBox)component).isSelected();
                  }
                  if( fieldType == float.class || fieldType == Float.class ) {
                     String stringvalue = (String)((JTextField)component).getText();
                     try {
                        value = Float.parseFloat( stringvalue );
                     } catch( Exception e ) {
                     }
                  }
                  if( fieldType == int.class || fieldType == Integer.class ) {
                     String stringvalue = (String)((JTextField)component).getText();
                     try {
                        value = Integer.parseInt( stringvalue );
                     } catch( Exception e ) {
                     }
                  }
                  if( value != null ) {
                     try {
                        setMethod.invoke( config, value );
                     } catch( Exception e ) {
                        playerObjects.getLogFile().WriteLine( Formatting.exceptionToStackTrace( e ) );
                     }
                  }
               }
               if( fieldType == boolean.class || fieldType == Boolean.class ) {
                  //addBooleanComponent( field.getName(), (Boolean)value );
               }
            } else {
               playerObjects.getLogFile().WriteLine( "No get accessor method for config field " + field.getName() );
            }
         }
      }
      revertConfig(); // in case some parses and stuff didn't work, so 
                      // user can see what is actually being read.
      playerObjects.getMainUI().showInfo( "Config updated.  Note that most changes require an AI restart.  You can click on 'reloadAI' in 'Actions' tab to do so." );
      playerObjects.getConfig().configUpdated();
   }

   class ConfigSave implements ActionListener {
      @Override
      public void actionPerformed( ActionEvent e ){
         applyConfig();
         playerObjects.getConfigController().writeConfigBackToSource( ConfigSource.XmlFile );
//         playerObjects.getConfig().save();
      }
   }
   
   class ConfigReload implements ActionListener {
      @Override
      public void actionPerformed( ActionEvent e ){
         //playerObjects.getConfig().reload();
         playerObjects.getConfigController().restoreFromSource( ConfigSource.WorkingCopy );
         playerObjects.getConfig().configUpdated();
         revertConfig();
      }
   }
}