0
# Data Management
1
2
The `PanamaHitek_DataBuffer` class provides comprehensive data organization, table visualization, and export functionality. It handles structured data storage with type safety, Excel export capabilities, and Swing GUI integration for data display.
3
4
## PanamaHitek_DataBuffer Class
5
6
```java { .api }
7
public class PanamaHitek_DataBuffer {
8
public PanamaHitek_DataBuffer();
9
}
10
```
11
12
Creates an empty data buffer for storing and organizing data.
13
14
## Column Management
15
16
### Add Data Column
17
18
```java { .api }
19
public void addColumn(int index, String variableName, Object dataType);
20
```
21
22
Adds a new data column with a specified data type at the given index.
23
24
**Parameters:**
25
- `index` - Column position (zero-based)
26
- `variableName` - Name of the column
27
- `dataType` - Data type class (e.g., `String.class`, `Integer.class`, `Double.class`)
28
29
### Add Time Column
30
31
```java { .api }
32
public void addTimeColumn(int index, String variableName);
33
```
34
35
Adds a timestamp column that automatically captures current date/time when data is added.
36
37
**Parameters:**
38
- `index` - Column position
39
- `variableName` - Name of the time column
40
41
### Date Format Configuration
42
43
```java { .api }
44
public void setDateFormat(String format);
45
public void setDateFormat(SimpleDateFormat format);
46
```
47
48
Configures the date format for timestamp columns.
49
50
**Parameters:**
51
- `format` - Date format string (e.g., "yyyy-MM-dd HH:mm:ss") or `SimpleDateFormat` object
52
53
## Data Manipulation
54
55
### Add Data Values
56
57
```java { .api }
58
public void addValue(int column, Object value) throws Exception;
59
```
60
61
Adds a value to the specified column in the current row.
62
63
**Parameters:**
64
- `column` - Column index
65
- `value` - Value to add (must match column data type)
66
67
**Throws:**
68
- `Exception` - If value type doesn't match column type or column doesn't exist
69
70
### Complete Row Entry
71
72
```java { .api }
73
public void printRow();
74
```
75
76
Finishes the current row and prepares for the next row entry.
77
78
### Clear Data
79
80
```java { .api }
81
public void clearBuffer();
82
```
83
84
Removes all data from the buffer, keeping column structure intact.
85
86
## Data Access
87
88
### Buffer Information
89
90
```java { .api }
91
public int getRowCount();
92
public int getColumnCount();
93
public List<Object> getClassList();
94
public List<String> getVariableList();
95
public List<List<Object>> getMainBuffer();
96
public int getTimeColumn();
97
```
98
99
Methods for accessing buffer metadata and structure.
100
101
### Sheet Configuration
102
103
```java { .api }
104
public String getSheetName();
105
public void setSheetName(String sheetName);
106
```
107
108
Configure Excel sheet name for export operations.
109
110
## Export Functionality
111
112
### Excel Export
113
114
```java { .api }
115
public void exportExcelFile() throws FileNotFoundException, IOException;
116
public void exportExcelFile(String path);
117
```
118
119
Export buffer data to Excel format. The first method shows a file dialog, while the second exports to a specific path.
120
121
**Parameters:**
122
- `path` - Full file path for Excel export
123
124
**Throws:**
125
- `FileNotFoundException` - If the export path is invalid
126
- `IOException` - If file writing fails
127
128
## GUI Integration
129
130
### Table Display
131
132
```java { .api }
133
public void insertToPanel(JPanel panel);
134
public JTable getTable();
135
public JScrollPane getScrollPane();
136
```
137
138
Methods for integrating the data buffer into Swing GUIs.
139
140
### Table Operations
141
142
```java { .api }
143
public void sortColumn(int column, boolean ascending);
144
```
145
146
Sorts the data table by the specified column.
147
148
**Parameters:**
149
- `column` - Column index to sort by
150
- `ascending` - `true` for ascending, `false` for descending order
151
152
## Event Handling
153
154
### Data Insertion Events
155
156
```java { .api }
157
public void addEventListener(DataInsertionListener listener);
158
public void removeEventListener();
159
```
160
161
Add or remove event listeners for data insertion notifications.
162
163
## Usage Examples
164
165
### Basic Data Buffer Setup
166
167
```java
168
import com.panamahitek.PanamaHitek_DataBuffer;
169
import java.text.SimpleDateFormat;
170
171
public class BasicDataBufferExample {
172
173
public void setupDataBuffer() {
174
PanamaHitek_DataBuffer buffer = new PanamaHitek_DataBuffer();
175
176
// Configure sheet name
177
buffer.setSheetName("Sensor_Data");
178
179
// Set date format for timestamps
180
buffer.setDateFormat("yyyy-MM-dd HH:mm:ss");
181
182
try {
183
// Add columns
184
buffer.addTimeColumn(0, "Timestamp");
185
buffer.addColumn(1, "Temperature", Double.class);
186
buffer.addColumn(2, "Humidity", Double.class);
187
buffer.addColumn(3, "SensorID", String.class);
188
189
// Add some data rows
190
buffer.addValue(1, 23.5);
191
buffer.addValue(2, 65.2);
192
buffer.addValue(3, "DHT22_01");
193
buffer.printRow(); // Complete first row
194
195
buffer.addValue(1, 24.1);
196
buffer.addValue(2, 63.8);
197
buffer.addValue(3, "DHT22_01");
198
buffer.printRow(); // Complete second row
199
200
System.out.println("Rows: " + buffer.getRowCount());
201
System.out.println("Columns: " + buffer.getColumnCount());
202
203
} catch (Exception ex) {
204
System.err.println("Data buffer error: " + ex.getMessage());
205
}
206
}
207
}
208
```
209
210
### Arduino Data Logging
211
212
```java
213
import com.panamahitek.PanamaHitek_Arduino;
214
import com.panamahitek.PanamaHitek_DataBuffer;
215
import com.panamahitek.ArduinoException;
216
import jssc.*;
217
218
public class ArduinoDataLogger {
219
220
private PanamaHitek_Arduino arduino;
221
private PanamaHitek_DataBuffer dataLogger;
222
223
public void setupLogging() throws ArduinoException {
224
arduino = new PanamaHitek_Arduino();
225
dataLogger = new PanamaHitek_DataBuffer();
226
227
// Setup data buffer structure
228
dataLogger.setSheetName("Arduino_Sensor_Log");
229
dataLogger.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
230
231
try {
232
// Define logging columns
233
dataLogger.addTimeColumn(0, "Timestamp");
234
dataLogger.addColumn(1, "Temperature", Double.class);
235
dataLogger.addColumn(2, "Light_Level", Integer.class);
236
dataLogger.addColumn(3, "Motion_Detected", Boolean.class);
237
dataLogger.addColumn(4, "Battery_Voltage", Double.class);
238
239
} catch (Exception ex) {
240
System.err.println("Logger setup error: " + ex.getMessage());
241
}
242
243
// Setup Arduino connection
244
arduino.arduinoRX("COM3", 9600, new SerialPortEventListener() {
245
@Override
246
public void serialEvent(SerialPortEvent event) {
247
if (event.isRXCHAR()) {
248
processArduinoData();
249
}
250
}
251
});
252
}
253
254
private void processArduinoData() {
255
try {
256
if (arduino.isMessageAvailable()) {
257
String data = arduino.printMessage();
258
parseAndLogData(data);
259
}
260
} catch (SerialPortException | ArduinoException ex) {
261
System.err.println("Data processing error: " + ex.getMessage());
262
}
263
}
264
265
private void parseAndLogData(String rawData) {
266
try {
267
// Expected format: "TEMP:25.3,LIGHT:1023,MOTION:1,BATTERY:3.7"
268
String[] parts = rawData.split(",");
269
270
for (String part : parts) {
271
String[] keyValue = part.split(":");
272
String key = keyValue[0].trim();
273
String value = keyValue[1].trim();
274
275
switch (key) {
276
case "TEMP":
277
dataLogger.addValue(1, Double.parseDouble(value));
278
break;
279
case "LIGHT":
280
dataLogger.addValue(2, Integer.parseInt(value));
281
break;
282
case "MOTION":
283
dataLogger.addValue(3, value.equals("1"));
284
break;
285
case "BATTERY":
286
dataLogger.addValue(4, Double.parseDouble(value));
287
break;
288
}
289
}
290
291
// Complete the row
292
dataLogger.printRow();
293
294
// Auto-export every 100 rows
295
if (dataLogger.getRowCount() % 100 == 0) {
296
autoExportData();
297
}
298
299
} catch (Exception ex) {
300
System.err.println("Data parsing error: " + ex.getMessage());
301
}
302
}
303
304
private void autoExportData() {
305
try {
306
String filename = "arduino_log_" + System.currentTimeMillis() + ".xlsx";
307
dataLogger.exportExcelFile(filename);
308
System.out.println("Auto-exported to: " + filename);
309
} catch (Exception ex) {
310
System.err.println("Auto-export error: " + ex.getMessage());
311
}
312
}
313
314
public void exportLogData() {
315
try {
316
dataLogger.exportExcelFile(); // Shows file dialog
317
} catch (Exception ex) {
318
System.err.println("Export error: " + ex.getMessage());
319
}
320
}
321
}
322
```
323
324
### GUI Integration with Event Handling
325
326
```java
327
import com.panamahitek.events.*;
328
import javax.swing.*;
329
import java.awt.*;
330
331
public class DataBufferGUIExample extends JFrame {
332
333
private PanamaHitek_DataBuffer buffer;
334
private JLabel statusLabel;
335
336
public DataBufferGUIExample() {
337
setupGUI();
338
setupDataBuffer();
339
}
340
341
private void setupGUI() {
342
setTitle("Data Buffer Display");
343
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
344
setLayout(new BorderLayout());
345
346
// Status label
347
statusLabel = new JLabel("Rows: 0");
348
add(statusLabel, BorderLayout.NORTH);
349
350
// Export button
351
JButton exportButton = new JButton("Export to Excel");
352
exportButton.addActionListener(e -> exportData());
353
add(exportButton, BorderLayout.SOUTH);
354
355
setSize(600, 400);
356
setLocationRelativeTo(null);
357
}
358
359
private void setupDataBuffer() {
360
buffer = new PanamaHitek_DataBuffer();
361
362
try {
363
// Setup columns
364
buffer.addTimeColumn(0, "Time");
365
buffer.addColumn(1, "Value1", Double.class);
366
buffer.addColumn(2, "Value2", Integer.class);
367
buffer.addColumn(3, "Description", String.class);
368
369
// Add event listener for data changes
370
buffer.addEventListener(new DataInsertionListener() {
371
@Override
372
public void onDataInsertion(DataInsertionEvent ev) {
373
SwingUtilities.invokeLater(() -> {
374
statusLabel.setText("Rows: " + buffer.getRowCount());
375
});
376
}
377
});
378
379
// Insert table into GUI
380
JPanel centerPanel = new JPanel(new BorderLayout());
381
buffer.insertToPanel(centerPanel);
382
add(centerPanel, BorderLayout.CENTER);
383
384
// Add some sample data
385
addSampleData();
386
387
} catch (Exception ex) {
388
JOptionPane.showMessageDialog(this, "Setup error: " + ex.getMessage());
389
}
390
}
391
392
private void addSampleData() {
393
try {
394
for (int i = 0; i < 10; i++) {
395
buffer.addValue(1, Math.random() * 100);
396
buffer.addValue(2, (int) (Math.random() * 1000));
397
buffer.addValue(3, "Sample " + (i + 1));
398
buffer.printRow();
399
}
400
} catch (Exception ex) {
401
JOptionPane.showMessageDialog(this, "Data error: " + ex.getMessage());
402
}
403
}
404
405
private void exportData() {
406
try {
407
buffer.exportExcelFile();
408
JOptionPane.showMessageDialog(this, "Data exported successfully!");
409
} catch (Exception ex) {
410
JOptionPane.showMessageDialog(this, "Export error: " + ex.getMessage());
411
}
412
}
413
414
public static void main(String[] args) {
415
SwingUtilities.invokeLater(() -> {
416
new DataBufferGUIExample().setVisible(true);
417
});
418
}
419
}
420
```
421
422
### Advanced Data Processing
423
424
```java
425
import java.util.List;
426
427
public class AdvancedDataProcessor {
428
429
private PanamaHitek_DataBuffer buffer;
430
431
public void performAdvancedOperations() {
432
buffer = new PanamaHitek_DataBuffer();
433
434
try {
435
// Setup complex data structure
436
buffer.addTimeColumn(0, "Timestamp");
437
buffer.addColumn(1, "Sensor1", Double.class);
438
buffer.addColumn(2, "Sensor2", Double.class);
439
buffer.addColumn(3, "Calculated", Double.class);
440
buffer.addColumn(4, "Status", String.class);
441
442
// Add data with calculations
443
for (int i = 0; i < 50; i++) {
444
double sensor1 = Math.random() * 100;
445
double sensor2 = Math.random() * 50;
446
double calculated = (sensor1 + sensor2) / 2;
447
String status = calculated > 50 ? "HIGH" : "LOW";
448
449
buffer.addValue(1, sensor1);
450
buffer.addValue(2, sensor2);
451
buffer.addValue(3, calculated);
452
buffer.addValue(4, status);
453
buffer.printRow();
454
}
455
456
// Access raw data for analysis
457
List<List<Object>> rawData = buffer.getMainBuffer();
458
System.out.println("Total data points: " + rawData.size());
459
460
// Get column information
461
List<String> columnNames = buffer.getVariableList();
462
List<Object> columnTypes = buffer.getClassList();
463
464
System.out.println("Column structure:");
465
for (int i = 0; i < columnNames.size(); i++) {
466
System.out.println(columnNames.get(i) + " (" +
467
columnTypes.get(i).toString() + ")");
468
}
469
470
// Sort by calculated values (descending)
471
buffer.sortColumn(3, false);
472
473
// Export with custom sheet name
474
buffer.setSheetName("Advanced_Analysis_" + System.currentTimeMillis());
475
buffer.exportExcelFile("advanced_data_analysis.xlsx");
476
477
} catch (Exception ex) {
478
System.err.println("Advanced processing error: " + ex.getMessage());
479
}
480
}
481
}
482
```
483
484
## Event System
485
486
### DataInsertionEvent
487
488
```java { .api }
489
public class DataInsertionEvent {
490
public DataInsertionEvent(Object source, PanamaHitek_DataBuffer buffer);
491
public PanamaHitek_DataBuffer getBuffer();
492
}
493
```
494
495
### DataInsertionListener Interface
496
497
```java { .api }
498
public interface DataInsertionListener {
499
void onDataInsertion(DataInsertionEvent ev);
500
}
501
```
502
503
## Best Practices
504
505
1. **Define column structure first** before adding any data
506
2. **Use appropriate data types** for columns to ensure type safety
507
3. **Handle exceptions properly** when adding values or exporting
508
4. **Call `printRow()`** after adding all values for a complete row
509
5. **Configure date formats** before adding timestamp data
510
6. **Use event listeners** for real-time GUI updates
511
7. **Sort data** before exporting for better organization
512
8. **Clean up resources** by removing event listeners when done