This repository contains MadHelix itself, along with compiled libraries with its dependencies.

[[ 🗃 ^ZEkyo madhelix ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Commits]

Clone

HTTPS: git clone https://vervis.peers.community/repos/ZEkyo

SSH: git clone USERNAME@vervis.peers.community:ZEkyo

Branches

Tags

master :: src / org / ultrasonicmadness / madhelix / models /

HistoryTableModel.java

/*
 * MadHelix is a Java Swing-based GUI frontend for SoundHelix.
 * Copyright (C) 2018 UltrasonicMadness
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 only,
 * as published by the Free Software Foundation.
 *
 * 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.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.ultrasonicmadness.madhelix.models;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import org.ultrasonicmadness.madhelix.utils.PathUtils;

public class HistoryTableModel extends AbstractTableModel
{
    private final String HISTORY_PATH = PathUtils.getHistoryPath();
    private JSONArray jsonHistory = null;
    private ArrayList<SongLogEntry> history = new ArrayList<>();
    
    public HistoryTableModel() throws IOException, ParseException
    {
        try
        {
            FileReader historyReader = new FileReader(HISTORY_PATH);
            JSONParser historyParser = new JSONParser();
            
            jsonHistory = (JSONArray)historyParser.parse(historyReader);
            
            for (Object entry : jsonHistory)
            {
                history.add(new SongLogEntry((JSONObject)entry));
            }
        }
        catch (FileNotFoundException x)
        {
            jsonHistory = new JSONArray();
        }
    }
     
    @Override
    public int getRowCount()
    {
        return history.size();
    }
    
    @Override
    public int getColumnCount()
    {
        return 4;
    }
    
    @Override
    public String getColumnName(int col)
    {
        switch(col)
        {
            case 0:
                return "Song name";
                
            case 1:
                return "Style";
            
            case 2:
                return "Length";
            
            case 3:
                return "Time played";
            
            default:
                return null;
        }
    }
    
    @Override
    public Object getValueAt(int row, int col)
    {
        switch(col)
        {
            case 0:
                return history.get(row).SONG_NAME;
                
            case 1:
                return history.get(row).STYLE_NAME;
            
            case 2:
                return history.get(row).getLengthAsString();
            
            case 3:
                return history.get(row).getTimePlayedAsString();
            
            default:
                return null;
        }
    }
    
    public void logSong(String songName, String style, int length) throws IOException
    {
        FileWriter writer = new FileWriter(PathUtils.getHistoryPath());
        
        LocalDateTime timePlayed = LocalDateTime.now();
        
        JSONObject jsonTime = new JSONObject();
        jsonTime.put("year", timePlayed.getYear());
        jsonTime.put("month", timePlayed.getMonthValue());
        jsonTime.put("day", timePlayed.getDayOfMonth());
        jsonTime.put("hour", timePlayed.getHour());
        jsonTime.put("minute", timePlayed.getMinute());
        jsonTime.put("second", timePlayed.getSecond());
        
        JSONObject jsonEntry = new JSONObject();
        jsonEntry.put("name", songName);
        jsonEntry.put("style", style);
        jsonEntry.put("length", length);
        jsonEntry.put("date", jsonTime);
        
        jsonHistory.add(0, jsonEntry);
        
        jsonHistory.writeJSONString(writer);
        
        writer.close();
        
        history.add(0, new SongLogEntry(songName, style, length, timePlayed));
        
        fireTableDataChanged();
    }
    
    public void clear()
    {
        history.clear();
        fireTableDataChanged();
    }
}

[See repo JSON]