Jump to content

Saving gui setting Snippet


Joseph

Recommended Posts

Support enums for those who like using them.

ps. im still fucking around with Loading method.

 

Snippet

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * @author Josedpay
 */
public class Saving {

	public Properties prop = new Properties();
	private File filePath, textPath;
	
	public Saving(String folder, String text)	{
		this.filePath = new File(System.getProperty("user.home") +File.separator +"OSBot" +File.separator +"data" +File.separator +folder);
		this.textPath = new File(System.getProperty("user.home") +File.separator +"OSBot" +File.separator +"data" +File.separator +folder +File.separator  +text +".txt");
	}
		
	public File getTextPath()	{
		return this.textPath;
	}
	
	/**
	 * @return - all necessary folder are made
	 */
	public boolean foldersAreReady()	{
		if (filePath.mkdirs())
			filePath.mkdirs();		
		return !filePath.mkdirs();
	}

	/**
	 * 
	 * @return - the necessary text file
	 * @throws IOException
	 */
	public boolean containsText() throws IOException	{
		if (textPath.createNewFile())
			textPath.createNewFile();
		return !textPath.createNewFile();
	}

	/** Recommended:
	 *  Make void methods that set your gui component, with a argument.
	 *  Also, use the same name as your void methods, the same as the setting Key 
	 *  example: "setExampleComboBox" with out the "()"
	 */
	public void save(String comment, Setting...settings) throws FileNotFoundException, IOException	{
		if (foldersAreReady())	{
			if (containsText())	{
				prop.load(new FileInputStream(this.textPath));
				
				for (Setting setting: settings)	{
					if (setting != null)
						prop.put(setting.getKey(), setting.getValue());
				}
				
				prop.store(new FileOutputStream(this.textPath), (comment != null ? comment: ""));				
			}
		}
	}
		
	/**
	 * A Simple Map class
	 */
	public static class Setting	{
		private Object key, value;

		public Setting(Object key, Object value)	{
			this.key = key;
			this.value = value;
		}
		
		public Object getKey()	{
			return this.key;
		}
		
		public Object getValue()	{
			return this.value;
		}
	}	
}

  
Picture example: (my construction )
87612babef.png
 
Results:

#my new gui setting
#Tue Jul 08 16:45:53 EDT 2014
setToHouse=RUNES
setConStaff=LAVA_BATTLESTAFF
setToBank=EDGEVILLE_1_GLORY
setNail=NONE
setServant=DEMON_BUTLER
setMouseSpeed=10

Edited by josedpay
  • Like 1
Link to comment
Share on other sites

Use a Hash Map instead of the class 'setting'. Since that's what it is smile.png

 

Good job, will be useful for quite some people. 

i could but the struggle to add variables into the map, unless there is an easier way.

 

I created the class because its easier to create a setting class

new Setting("setToBank", toBank().name()),

  • Like 1
Link to comment
Share on other sites

Personally I would create a wrapper class that I could pass my properties file to when loading. This way I can have a generic class which handles all of the logic in the properties file, making it easy for later use.

 

For example this is what I use for saving paths in a x;y;z;x;y;z format:

import org.osbot.rs07.api.map.Position;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * User: Anthony
 * Date: 6/3/2014
 */

public class Path {

	private String name;
	private List<Position> path;

	public Path(Properties properties) {
		this.name = properties.getProperty("name");
		this.path = getPathFromProperties(properties);
	}

	public String getName() {
		return name;
	}

	public List<Position> getPath() {
		return path;
	}

	@Override
	public String toString() {
		return name;
	}

	private List<Position> getPathFromProperties(Properties properties) {
		List<Position> returnPath = new ArrayList<>();
		for (String s : properties.getProperty("path").split(";")) {
			String[] coords = s.split(",");
			returnPath.add(new Position(Integer.parseInt(coords[0]), Integer.parseInt(coords[1]), Integer.parseInt(coords[2])));
		}
		return returnPath;
	}

}
Link to comment
Share on other sites

Personally I would create a wrapper class that I could pass my properties file to when loading. This way I can have a generic class which handles all of the logic in the properties file, making it easy for later use.

 

For example this is what I use for saving paths in a x;y;z;x;y;z format:

import org.osbot.rs07.api.map.Position;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA
 * User: Anthony
 * Date: 6/3/2014
 */

public class Path {

	private String name;
	private List<Position> path;

	public Path(Properties properties) {
		this.name = properties.getProperty("name");
		this.path = getPathFromProperties(properties);
	}

	public String getName() {
		return name;
	}

	public List<Position> getPath() {
		return path;
	}

	@Override
	public String toString() {
		return name;
	}

	private List<Position> getPathFromProperties(Properties properties) {
		List<Position> returnPath = new ArrayList<>();
		for (String s : properties.getProperty("path").split(";")) {
			String[] coords = s.split(",");
			returnPath.add(new Position(Integer.parseInt(coords[0]), Integer.parseInt(coords[1]), Integer.parseInt(coords[2])));
		}
		return returnPath;
	}

}

I Made my snippet generic, the different between yours and mine would be that my contains a key, your are separated with comas. It would be very difficult for me, because I could save all my setting but when it come to my furniture list I'll be fucked.

And the reason why I created a map class was so people could name there key exactly as there method (Their SETTERS). I look through all decleard method in the GUI class. Looking the identical one. And just like I asked the question before, execute the method using the value key as the argument.

Link to comment
Share on other sites

I Made my snippet generic, the different between yours and mine would be that my contains a key, your are separated with comas. It would be very difficult for me, because I could save all my setting but when it come to my furniture list I'll be fucked.

And the reason why I created a map class was so people could name there key exactly as there method (Their SETTERS). I look through all decleard method in the GUI class. Looking the identical one. And just like I asked the question before, execute the method using the value key as the argument.

The Properties Object stores key/value pairs. All I'm saying is creating a wrapper class to grab each value based on it's key could prove to be beneficial :p

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...