View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ******************************************************************************/
17  package net.sourceforge.jarduino.gui;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map.Entry;
22  
23  import javax.swing.JComponent;
24  import javax.swing.JTable;
25  import javax.swing.table.AbstractTableModel;
26  
27  import net.sourceforge.jarduino.message.ArduinoValues;
28  
29  /**
30   * Table for values display.
31   */
32  public class ArduinoTableValues {
33      /**
34       * The table.
35       */
36      private final JTable theTable;
37  
38      /**
39       * The model.
40       */
41      private final ArduinoValuesModel theModel;
42  
43      /**
44       * The selected values.
45       */
46      private final List<Number> theValues;
47  
48      /**
49       * The selected names.
50       */
51      private final List<String> theNames;
52  
53      /**
54       * The selected values.
55       */
56      private ArduinoValues theSelected;
57  
58      /**
59       * Constructor.
60       */
61      ArduinoTableValues() {
62          /* Create the lists */
63          theValues = new ArrayList<>();
64          theNames = new ArrayList<>();
65  
66          /* Create the table */
67          theTable = new JTable();
68          theTable.setAutoCreateRowSorter(true);
69  
70          /* Create the table model */
71          theModel = new ArduinoValuesModel();
72          theTable.setModel(theModel);
73      }
74  
75      /**
76       * Obtain the component.
77       * @return the component
78       */
79      JComponent getComponent() {
80          return theTable;
81      }
82  
83      /**
84       * Configure the table.
85       *
86       * @param pSelected the selected object
87       */
88      void configureTable(final ArduinoValues pSelected) {
89          /* store the details */
90          theSelected = pSelected;
91  
92          /* Refresh the table model */
93          theModel.refresh();
94      }
95  
96      /**
97       * Table Model.
98       */
99      private class ArduinoValuesModel
100             extends AbstractTableModel {
101         /**
102          * Serial Id.
103          */
104         private static final long serialVersionUID = -5981115401584506612L;
105 
106         /**
107          * The name column.
108          */
109         private static final int COL_NAME = 0;
110 
111         /**
112          * The value column.
113          */
114         private static final int COL_VALUE = COL_NAME + 1;
115 
116         /**
117          * Constructor.
118          */
119         ArduinoValuesModel() {
120         }
121 
122         /**
123          * Refresh.
124          */
125         void refresh() {
126             /* Clear the names */
127             theNames.clear();
128             theValues.clear();
129 
130             /* Loop through the values */
131             for (Entry<Number, String> myEntry : theSelected.getValues().entrySet()) {
132                 /* Add to lists */
133                 theNames.add(myEntry.getValue());
134                 theValues.add(myEntry.getKey());
135             }
136 
137             /* Refresh the table */
138             fireTableDataChanged();
139         }
140 
141         @Override
142         public int getRowCount() {
143             return theNames.size();
144         }
145 
146         @Override
147         public String getColumnName(final int pColIndex) {
148             switch (pColIndex) {
149                 case COL_NAME:
150                     return "Name";
151                 case COL_VALUE:
152                     return "Value";
153                 default:
154                     return null;
155             }
156         }
157 
158         @Override
159         public int getColumnCount() {
160             return COL_VALUE + 1;
161         }
162 
163         @Override
164         public boolean isCellEditable(final int pRowIndex,
165                                       final int pColIndex) {
166             return false;
167         }
168 
169         @Override
170         public Object getValueAt(final int pRowIndex,
171                                  final int pColIndex) {
172             switch (pColIndex) {
173                 case COL_NAME:
174                     return theNames.get(pRowIndex);
175                 case COL_VALUE:
176                     return theValues.get(pRowIndex);
177                 default:
178                     return null;
179             }
180         }
181     }
182 }