All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.andy2003.ui.rawview.RawViewTableModel Maven / Gradle / Ivy

/**
 * 	This file is part of Kayak.
 *
 *	Kayak is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Kayak 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 Lesser General Public License
 *	along with Kayak.  If not, see .
 *
 */

package com.github.andy2003.ui.rawview;

import com.github.andy2003.core.Frame;
import com.github.andy2003.core.FrameListener;
import com.github.andy2003.core.Util;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Jan-Niklas Meier 
 */
public class RawViewTableModel extends AbstractTableModel implements FrameListener {

    private final Map data = Collections.synchronizedMap(new TreeMap());    private Thread refreshThread;
    private boolean colorize = false;

    private Runnable refreshRunnable = new Runnable() {

        @Override
        public void run() {
            while (true) {
                synchronized (data) {
                    Set keys = data.keySet();
                    String[] keyArray = keys.toArray(new String[0]);
                    for(int i=0;i";

                            res += datString.substring(i, i + 2);
                            if (i != datString.length()) {
                                res += " ";
                            }
                            res += "";
                        }
                        res += "";
                        return res;
                    } else {
                        String res = "";
                        for (int i = 0; i < datString.length(); i += 2) {
                            res += datString.substring(i, i + 2);
                            if (i != datString.length()) {
                                res += " ";
                            }
                        }
                        return res;
                    }
                default:
                    return null;
            }
        }
    }

    public byte[] getData(int row) {
        synchronized(data) {
            String[] keys = data.keySet().toArray(new String[]{});
            FrameData frameData = data.get(keys[row]);
            return frameData.getData();
        }
    }

    public byte[] getDataForID(String id) {
        synchronized(data) {
            FrameData d = data.get(id);
            if(d != null)
                return d.getData();
            else
                return null;
        }
    }

    @Override
    public void newFrame(Frame frame) {
        synchronized(data) {
            String idString;
            if(frame.isExtended())
                idString = String.format("%08x", frame.getIdentifier());
            else
                idString = String.format("%03x", frame.getIdentifier());

            int row = getRowForKey(idString);
            if (row != -1) {
                FrameData old = data.get(idString);
                old.updateWith(frame);
            } else {
                data.put(idString, new FrameData(frame));
            }
        }
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        switch(columnIndex) {
            case 0:
                return String.class;
            case 1:
                return Integer.class;
            case 2:
                return String.class;
            case 3:
                return Integer.class;
            case 4:
                return String.class;
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch(column) {
            case 0:
                return "Timestamp [s]";
            case 1:
                return "Interval [ms]";
            case 2:
                return "Identifier [hex]";
            case 3:
                return "DLC";
            case 4:
                return "Data [hex]";
            default:
                return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy