2222import javax .swing .table .DefaultTableModel ;
2323import javax .swing .table .TableCellRenderer ;
2424import javax .swing .table .TableColumn ;
25-
2625import org .json .JSONObject ;
2726import simulator .control .Controller ;
2827import simulator .model .BodiesGroup ;
2928import simulator .model .Body ;
3029import simulator .model .SimulatorObserver ;
3130
3231class ForceLawsDialog extends JDialog implements SimulatorObserver {
32+
33+ private static final long serialVersionUID = 1L ;
34+
3335 private DefaultComboBoxModel <String > _lawsModel ;
3436 private DefaultComboBoxModel <String > _groupsModel ;
3537 private DefaultTableModel _dataTableModel ;
3638 private Controller _ctrl ;
3739 private List <JSONObject > _forceLawsInfo ;
3840 private String [] _headers = { "Key" , "Value" , "Description" };
3941
40- // TODO en caso de ser necesario, añadir los atributos aquí…
42+ // en caso de ser necesario, añadir los atributos aquí…
4143 int _status = 0 ;
44+ int _selectedLawsIndex = 0 ;
45+ int _selectedGroupsIndex = 0 ;
4246 private JFrame _parent ;
4347 private JComboBox <String > _lawscomb ;
4448 private JComboBox <String > _groupscomb ;
@@ -70,11 +74,16 @@ private void initGUI() {
7074 mainPanel .add (Box .createRigidArea (new Dimension (0 , 20 )));
7175
7276 _dataTableModel = new DefaultTableModel () {
77+
78+ private static final long serialVersionUID = 1L ;
79+
7380 @ Override
7481 public boolean isCellEditable (int row , int column ) {
7582 return column == 1 ;
7683 }
7784 };
85+ _dataTableModel .setColumnIdentifiers (_headers );
86+ updateTableModel (0 );
7887 JTable dataTable = new JTable (_dataTableModel ) {
7988 private static final long serialVersionUID = 1L ;
8089
@@ -100,15 +109,14 @@ public Component prepareRenderer(TableCellRenderer renderer, int row, int column
100109 comboBoxPanel .setAlignmentX (CENTER_ALIGNMENT );
101110 mainPanel .add (comboBoxPanel );
102111
103- _dataTableModel .setColumnIdentifiers (_headers );
104112 _lawsModel = new DefaultComboBoxModel <>();
113+ _lawscomb = new JComboBox <>(_lawsModel );
105114 _forceLawsInfo .forEach (fl -> {
106- if (! fl .has ("desc" ))
107- throw new IllegalArgumentException ( );
108-
109- _lawsModel . addElement ( fl . getString ( "desc" ));
115+ _lawscomb . addItem ( fl .getString ("desc" ));
116+ } );
117+ _lawscomb . addActionListener (( e ) -> {
118+ updateTableModel ( _lawscomb . getSelectedIndex ( ));
110119 });
111- _lawscomb = new JComboBox <>(_lawsModel );
112120 comboBoxPanel .add (_lawscomb );
113121
114122 _groupsModel = new DefaultComboBoxModel <>();
@@ -138,8 +146,23 @@ public void actionPerformed(ActionEvent e) {
138146
139147 @ Override
140148 public void actionPerformed (ActionEvent e ) {
141- _status = 1 ;
142- ForceLawsDialog .this .setVisible (false );
149+ try {
150+ JSONObject aux = new JSONObject ();
151+ for (int i = 0 ; i < _dataTableModel .getRowCount (); i ++) {
152+ // TODO
153+ aux .put ((String ) _dataTableModel .getValueAt (i , 0 ), (String ) _dataTableModel .getValueAt (i , 1 ));
154+ }
155+
156+ JSONObject jo = new JSONObject ();
157+ jo .put ("data" , aux );
158+ jo .put ("type" , _forceLawsInfo .get (_selectedLawsIndex ).getString ("type" ));
159+
160+ _ctrl .setForcesLaws ((String ) _groupscomb .getSelectedItem (), jo );
161+ _status = 1 ;
162+ ForceLawsDialog .this .setVisible (false );
163+ } catch (Exception ex ) {
164+ Utils .showErrorMsg (ex .getMessage ());
165+ }
143166 }
144167 });
145168 buttonPanel .add (okButton );
@@ -161,24 +184,37 @@ public int open() {
161184 return _status ;
162185 }
163186
164- // TODO el resto de métodos van aquí…
187+ // el resto de métodos van aquí…
188+ private void updateTableModel (int _dataIdx ) {
189+ this ._selectedLawsIndex = _dataIdx ;
190+ JSONObject data = _forceLawsInfo .get (_dataIdx ).getJSONObject ("data" );
191+ _dataTableModel .setNumRows (data .length ());
192+
193+ int i = 0 ;
194+ for (String key : data .keySet ()) {
195+ _dataTableModel .setValueAt (key , i , 0 );
196+ _dataTableModel .setValueAt (data .getString (key ), i , 2 );
197+ i ++;
198+ }
199+ }
200+
165201 @ Override
166202 public void onAdvance (Map <String , BodiesGroup > groups , double time ) {
167203 }
168204
169205 @ Override
170206 public void onReset (Map <String , BodiesGroup > groups , double time , double dt ) {
171- _groupsModel . removeAllElements ();
207+ _groupscomb . removeAllItems ();
172208 }
173209
174210 @ Override
175211 public void onRegister (Map <String , BodiesGroup > groups , double time , double dt ) {
176- groups .values ().forEach (bg -> _groupsModel . addElement (bg .getId ()));
212+ groups .values ().forEach (bg -> _groupscomb . addItem (bg .getId ()));
177213 }
178214
179215 @ Override
180216 public void onGroupAdded (Map <String , BodiesGroup > groups , BodiesGroup g ) {
181- _groupsModel . addElement (g .getId ());
217+ _groupscomb . addItem (g .getId ());
182218 }
183219
184220 @ Override
0 commit comments