Portfolio
  • Home
  • About
/**
 * @author Scott
 * @description
 * This class will be called whenever saving needs to be done via json
 */
package me.scotth0828.main.Data;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.JsonIOException;
import com.google.gson.reflect.TypeToken;

import me.scotth0828.main.Window.textareamanagement.textInfo;

public class data {

	private static String FILE_LOCATION = "save.json"; // file save location

	/**
	 * @author Scott
	 * @description Main constructor for the data file
	 */
	public data() {
		File f = new File(FILE_LOCATION); // a file location is set and a file is created if it is not available

		if (!f.exists()) {
			try {
				f.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * @author Scott
	 * @description This function will save all info into the json file when called
	 * @param list takes in all the information that will be saved with an object
	 *             arraylist
	 */
	public static void save(Map<String, ArrayList<textInfo>> list) {
		if (list == null) return;
		
		Gson g = new Gson();

		try {
			
			/*
			ArrayList<infoToSave> its = new ArrayList<>();
			infoToSave is = null;
			for (textInfo i : list) { // loops through adds each field that will be saved
				is = new infoToSave(i.getText());
				is.setFirstClick(i.getFirstClick());
				its.add(is);
			}
			*/
			
			
			infoToSave is = null;
			
			ArrayList<infoToSave> its = new ArrayList<>();
			
			for (Map.Entry<String, ArrayList<textInfo>> entry : list.entrySet()) {
				
				String key = entry.getKey();
				ArrayList<textInfo> val = entry.getValue();
				
				
				
				for (textInfo i : val) {
					is = new infoToSave();
					is.setCategory(key);
					is.setText(i.getText());
					is.setFirstClick(i.getFirstClick());
					its.add(is);
				}
			}
			
			
			
			
			

			FileWriter fw = new FileWriter(FILE_LOCATION); // saves info to the file
			g.toJson(its, fw);

			fw.close(); // closes the file

		} catch (JsonIOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @author Scott
	 * @description loads in all the infromation from the json file
	 * @return returns an arraylist of all the saved information so that it can be
	 *         loaded into the program on start
	 */
	public static Map<String, ArrayList<textInfo>> load() {

		ArrayList<infoToSave> its = null;

		try {
			FileReader fr = new FileReader(FILE_LOCATION); // file is read

			Gson gson = new Gson();

			Type type = new TypeToken<ArrayList<infoToSave>>() { // a file type is created
			}.getType();
			its = gson.fromJson(fr, type); // information is saved to a variable

			try {
				fr.close(); // reader is closed
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (its == null) return null;
		
		//** change to hashmap **//
		
		Map<String, ArrayList<textInfo>> m = new HashMap<>();
		
		for (infoToSave i : its) {
			
			if (m.get(i.getCategry()) == null) {
				m.put(i.getCategry(), new ArrayList<>());
			}
			
			textInfo inf = new textInfo();
			//System.out.println(i.getText()+"      ---\n\n"+i.getisFirstClick()+"\n");
			inf.setFirstClick(i.getisFirstClick());
			//System.out.println(inf.getFirstClick());
			inf.setText(i.getText());
			m.get(i.getCategry()).add(inf);
			
		}

		return m; // arraylist is returned
	}

}
package me.scotth0828.main.Data;
/**
 * @author Scott
 * @description
 * template for save object
 */

class infoToSave {

	private String category;
	
	private String text;
	
	private boolean isFirstClick = false;

	public infoToSave() { // constructor
		text = "";
	}
	
	public String getCategry() {
		return category;
	}
	
	public void setCategory(String cat) {
		this.category = cat;
	}
	
	public String getText() { // text getter
		return text;
	}
	
	public void setText(String text) {
		this.text = text;
	}
	
	public void setFirstClick(boolean val) { // first click setter
		isFirstClick = val;
	}
	
	public boolean getisFirstClick() { // first click getter
		return isFirstClick;
	}
}
© 2021 Copyright: scottsportfolio