File: UiReflectionHelper.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 (399 lines) | stat: -rw-r--r-- 14,783 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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// 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.util.*;
import java.util.Map;
import java.awt.Component;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.annotation.*;
import java.lang.reflect.*;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

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

import hughai.*;
import hughai.EnemyTracker.EnemyAdapter;
import hughai.basictypes.*;
import hughai.building.Workflows;
import hughai.mapping.*;
import hughai.packcoordinators.*;
import hughai.unitdata.*;
import hughai.unitdata.UnitController.UnitAdapter;
import hughai.utils.*;
import hughai.utils.ReflectionHelper.CustomClass;
import hughai.utils.ReflectionHelper.Exclude;
import hughai.utils.ReflectionHelper.ListTypeInfo;

public class UiReflectionHelper {
   PlayerObjects playerObjects;

   public UiReflectionHelper( PlayerObjects playerObjects ) {
      this.playerObjects = playerObjects;
   }
   
   public void dispose() {
      
   }

   JComponent objectToEditComponent( Object object, Annotation[] annotations ) {
      Class<?> objectClass = object.getClass();
      if( objectClass == String.class ) {
         JTextField textField = new JTextField();
         textField.setText( "" + object );
         return textField;
      }
      if( objectClass == boolean.class ) {
         JCheckBox checkBox = new JCheckBox();
         checkBox.setSelected( (Boolean)object );
         return checkBox;
      }
      if( objectClass == float.class ) {
         JTextField textField = new JTextField();
         textField.setText( "" + object );
         return textField;
      }
      if( objectClass == int.class ) {
         JTextField textField = new JTextField();
         textField.setText( "" + object );
         return textField;
      }
      if( List.class.isAssignableFrom( objectClass ) ) {

      }
      //      boolean foundCustomClassAttribute = false;
      //      for( Annotation annotation : annotations ) {
      //         if( CustomClass.class.isAssignableFrom( annotation.getClass() ) ) {
      //            foundCustomClassAttribute = true;
      //         }
      //      }
      //      if( foundCustomClassAttribute ) {
      // then, we put each field into its own column in a row?
      // and if one of them is a  list?
      for( Field field : objectClass.getDeclaredFields() ) {

      }
      //      }

      throw new RuntimeException("UiReflectionhelper: unhandled type: " + objectClass.getSimpleName() );
   }

   public enum DisplayContext {
      detailed,
      listrow,
      brief
   }

   // ok, so if we call with a primitive, it's easy.
   // if we call with a class, then we want either:
   //   fieldone: valueone
   //   fieldtwo: valuetwo
   //   fieldthree: valuethree
   // or:
   //   fieldone    fieldtwo    fieldthree
   //   valueone    valuetwo    valuethree
   // maybe the first is better?
   // next, if we call with a list, then we want something like:
   //   valueone
   //   valuetwo
   //   valuethree
   // what if it is a list of classes?  then something like:
   //   fieldone    fieldtwo   fieldthree
   //   itemonev1   itemonev2  itemonev3
   //   itemtwov1   itemtwov2  itemtwov3
   //
   // if we call with a class with a list in, then something like:
   //    fieldone: valueone
   //    fieldtwo: valuetwo
   //    fieldthree: valuethree
   //    listfield: [button here to see details]
   // or maybe:
   //    fieldone: valueone
   //    fieldtwo: valuetwo
   //    fieldthree: valuethree
   //    listfield:
   //    valueone
   //    valuetwo
   //    valuethree
   // or:
   //    fieldone: valueone
   //    fieldtwo: valuetwo
   //    fieldthree: valuethree
   //    listfield:  valueone
   //                valuetwo
   //                valuethree
   // first option: button to see details, is easiest
   //
   // what if list of classes, and the class contains a list?
   // maybe as before for a list of classes, with the button added?
   //
   // ok, so detailed can be true or false:
   // if true, then class fields will be listed vertically, and list in full
   // if false, then lists and classes will just become a button?
   // if true, and classasrowtable is true, then a class will be horizontal,
   // as a table row, otherwise vertically
   //
   // so much for layout... what about behavior when canModify is true?
   // - probably need a button 'apply', 'revert' for each dialog and sub-dialog, ie for classes, maybe also for lists?
   //   - apply and revert applies only to that dialog, not to modifications to children dialogs
   // - for lists, need button for each row, to delete it, and a button to add a new row
   // - for primitive types, nothing is applied until parent class or list applies it?
   // - maybe 'apply' and 'revert' apply at the level of each custom class?
   public JComponent objectToDisplayComponent( final Object object, final Annotation[] annotations, DisplayContext displayContext, final boolean canModify ) {
      try {
      if( object == null ) {
         return new JLabel("<null>");
      }
      
      final Class<?> objectClass = object.getClass();
      if( objectClass == String.class ) {
         if( canModify ) {
            JTextField textField = new JTextField();
            textField.setText( "" + object );
            return textField;
         } else {
            JLabel textField = new JLabel();
            textField.setText( "" + object );
            return textField;            
         }
      }
      if( objectClass.equals( Float.class ) ) {
         if( canModify ) {
            JTextField textField = new JTextField();
            textField.setText( "" + object );
            return textField;
         } else {
            JLabel textField = new JLabel();
            textField.setText( "" + object );
            return textField;
         }
      }
      if( objectClass.equals( Integer.class ) ) {
         if( canModify ) {
            JTextField textField = new JTextField();
            textField.setText( "" + object );
            return textField;
         } else {
            JLabel textField = new JLabel();
            textField.setText( "" + object );
            return textField;
         }
      }
      if( objectClass.equals( Boolean.class ) ) {
         JCheckBox checkbox = new JCheckBox();
         checkbox.setEnabled( canModify );
         checkbox.setSelected( (Boolean )object );
         return checkbox;
      }
      if( List.class.isAssignableFrom( objectClass ) ) {
         Class<?> listItemClass = getListItemClass( annotations );
         if( listItemClass == null ) {
            throw new RuntimeException("Missing ListTypeInfo annotation on list" );
         }
         
         if( displayContext.equals( DisplayContext.brief )
               || displayContext.equals( DisplayContext.listrow ) ) { // just return a button
            final JButton button = new JButton( "..." );
            button.addActionListener( new ActionListener() {
               @Override
               public void actionPerformed( ActionEvent e ) {
                  System.out.println("creating child dialog...");
                  JDialog childDialog = createDialog( button, "List", true );
                  JComponent childcomponents = objectToDisplayComponent(object, annotations, DisplayContext.detailed, canModify );
                  childDialog.add( childcomponents );
                  childDialog.setSize( 300, 300 );
                  childDialog.setVisible( true );
               }
            } );
            return button;
         }

         // otherwise show as a table
         List objectAsList = (List)object;
         GridLayout gridLayout = new GridLayout(0,1);
         JPanel panel = new JPanel( gridLayout );

         JComponent headers = listToHeaders( listItemClass );
         if( headers != null ) {
            addGridLayoutRow( gridLayout );
            panel.add( headers );
         }

         for( Object item : objectAsList ) {
            JComponent childcomponent = objectToDisplayComponent( item,
                  annotations, DisplayContext.listrow, canModify );
            addGridLayoutRow( gridLayout );
            panel.add( childcomponent );
         }         
         return panel;
      }
      //      boolean foundCustomClassAttribute = false;
      //      for( Annotation annotation : annotations ) {
      //         if( CustomClass.class.isAssignableFrom( annotation.getClass() ) ) {
      //            foundCustomClassAttribute = true;
      //         }
      //      }
      //      if( foundCustomClassAttribute ) {
      if( displayContext.equals( DisplayContext.brief ) ) { // just return a button
//         JPanel panel = new JPanel( new GridLayout( 1, 1 ));
         
//         final JLabel label = new JLabel( objectClass.getSimpleName() );
         
         final JButton button = new JButton( "..." );
         button.setSize( 50, 50 );
         
         button.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
               System.out.println("creating child dialog...");
               JDialog childDialog = createDialog( button, objectClass.getSimpleName(), true );
               JComponent childcomponents = objectToDisplayComponent(object, new Annotation[0], DisplayContext.detailed, canModify );
               childDialog.add( childcomponents );
               childDialog.setSize( 300, 300 );
               childDialog.setVisible( true );
            }
         } );
//         panel.add( label );
//         panel.add( button );
         return button;
//         return panel;
      }

      if( displayContext.equals( DisplayContext.listrow ) ) { // put each field into a column
         GridLayout gridLayout = new GridLayout(1,0);
         JPanel panel = new JPanel( gridLayout );
         for( Field field : objectClass.getDeclaredFields() ) {
            JComponent childcomponent = objectToDisplayComponent( field.get( object ), field.getAnnotations(), DisplayContext.brief, canModify );
            gridLayout.setColumns( gridLayout.getColumns() + 1 );
            panel.add( childcomponent );
         }
         return panel;            
      } else {
         // then, we put each field into its own row
         GridLayout gridLayout = new GridLayout(0,2);
         JPanel panel = new JPanel( gridLayout );
         for( Field field : objectClass.getDeclaredFields() ) {
            JLabel label = new JLabel( field.getName() );
            JComponent childcomponent = objectToDisplayComponent( field.get( object ), field.getAnnotations(), DisplayContext.brief, canModify );

            gridLayout.setRows( gridLayout.getRows() + 1 );

            panel.add(  label );
            panel.add( childcomponent );
         }
         return panel;
      }
      } catch( Exception e ) {
         e.printStackTrace();
         throw new RuntimeException( e );
      }

      //      }

//      throw new RuntimeException("UiReflectionhelper: unhandled type: " + objectClass.getSimpleName() );
   }
   
   JComponent listToHeaders( Class<?> listClass ) {
      if( listClass.isPrimitive() ) {
         return null;
      }
      
      if( listClass.equals( String.class ) ) {
         return null;
      }
      
      GridLayout gridLayout = new GridLayout( 1, 0 );
      JPanel panel = new JPanel( gridLayout );
      
      // assume custom class, for now
      for( Field field : listClass.getDeclaredFields() ) {
         if( isExcluded( field ) ) {
            continue;
         }
         
         JLabel label = new JLabel( field.getName() );
         addGridLayoutColumn( gridLayout );
         panel.add( label );
      }
      return panel;
   }
   
   void addGridLayoutRow( GridLayout gridLayout ) {
      gridLayout.setRows( gridLayout.getRows() + 1 );
   }
   
   void addGridLayoutColumn( GridLayout gridLayout ) {
      gridLayout.setColumns( gridLayout.getColumns() + 1 );
   }
   
   boolean isExcluded(Annotation[] annotations ) {
      for( Annotation annotation : annotations ) {
         if( Exclude.class.isAssignableFrom( annotation.getClass() ) ) {
            return true;
         }
      }
      return false;
   }
   
   boolean isExcluded( Field field ) {
      return isExcluded( field.getAnnotations() ); // not that efficient, but do we care?
   }
   
   Class<?> getListItemClass( Annotation[] annotations ) {
      for( Annotation annotation : annotations ) {
         if( ListTypeInfo.class.isAssignableFrom( annotation.getClass() ) ) {
            ListTypeInfo listTypeInfo = (ListTypeInfo)annotation;
            return listTypeInfo.value();
         }
      }
      return null;
   }
   
   JDialog createDialog( Component lockedtoparentofthiscomponent, String title, boolean modal ) {
      if( JFrame.class.isAssignableFrom( lockedtoparentofthiscomponent.getClass() ) ) {
         JDialog dialog = new JDialog( (JFrame)lockedtoparentofthiscomponent,
               title, modal );
         return dialog;
      }
      if( JDialog.class.isAssignableFrom( lockedtoparentofthiscomponent.getClass() ) ) {
         JDialog dialog = new JDialog( (JDialog)lockedtoparentofthiscomponent,
               title, modal );
         return dialog;
      }
      if( lockedtoparentofthiscomponent == null ) {
         throw new RuntimeException("UiReflectionHelper: couldn't find parent frame or dialog" );
      }
      return createDialog( lockedtoparentofthiscomponent.getParent(), title, modal );
   }

//   JComponent objectToTableHeaders( Object object, Annotation[] annotations ) {
//      return null; // placeholder
//   }
}