<commit_before>/*******************************************************************************
 * Copyright 2014 <NAME>.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.bladecoder.engineeditor.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.bladecoder.engine.util.Config;
import org.bladecoder.engineeditor.Ctx;
import org.bladecoder.engineeditor.utils.DinamicClassPath;
import org.bladecoder.engineeditor.utils.EditorLogger;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.resolvers.ResolutionFileResolver.Resolution;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Project extends PropertyChange {
	public static final String PROP_PROJECTFILE = "projectFile";
	public static final String NOTIFY_SCENE_SELECTED = "SCENE_SELECTED";
	public static final String NOTIFY_ACTOR_SELECTED = "ACTOR_SELECTED";
	public static final String NOTIFY_FA_SELECTED = "FA_SELECTED";
	public static final String NOTIFY_VERB_SELECTED = "VERB_SELECTED";
	public static final String NOTIFY_PROJECT_LOADED = "PROJECT_LOADED";

	public static final String TMPL_JAR_FILENAME = "/res/data/project_tmpl.jar";

	public static final String ASSETS_PATH = "/assets";
	public static final String MODEL_PATH = ASSETS_PATH + "/model";
	public static final String ATLASES_PATH = ASSETS_PATH + "/atlases";
	public static final String BACKGROUNDS_PATH = ASSETS_PATH + "/backgrounds";
	public static final String FONTS_PATH = ASSETS_PATH + "/fonts";
	public static final String MUSIC_PATH = ASSETS_PATH + "/music";
	public static final String SOUND_PATH = ASSETS_PATH + "/sounds";
	public static final String IMAGE_PATH = ASSETS_PATH + "/images";
	public static final String SPRITE3D_PATH = ASSETS_PATH + "/3d";
	public static final String SPINE_PATH = ASSETS_PATH + "/spine";
	public static final String UI_PATH = ASSETS_PATH + "/ui";

	private static final String CONFIG_DIR = System.getProperty("user.home") + "/.AdventureComposer";
	private static final String CONFIG_FILENAME = "config.properties";
	
	public static final String LAST_PROJECT_PROP = "last_project";

	private final Properties editorConfig = new Properties();

	private File projectFile;

	private final WorldDocument world = new WorldDocument();	
	private Properties projectConfig;

	private ChapterDocument selectedChapter;
	private Element selectedScene;
	private Element selectedActor;
	private String selectedFA;
	
	private HashMap<String, TextureRegion> bgIconCache = new HashMap<String, TextureRegion>();

	final PropertyChangeListener modelChangeListener = new PropertyChangeListener() {
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
		}

	};

	public Project() {
		world.addPropertyChangeListener(modelChangeListener);
		loadConfig();
	}
	
	public TextureRegion getBgIcon(String s) {
		TextureRegion icon = bgIconCache.get(s);
		
		if(icon == null) {
			try {
				bgIconCache.put(s, createBgIcon(s));
			} catch (Exception e) {
				return null;
			}
			
			icon = bgIconCache.get(s);
		}
		
		return icon;
	}
	
	private TextureRegion createBgIcon(String bg) {
		return new TextureRegion(new Texture(Gdx.files.absolute(getProjectPath() + "/" + BACKGROUNDS_PATH + 
				"/" + getResolutions().get(0).suffix + "/" + bg)));
	}

	private void loadConfig() {
		File dir = new File(CONFIG_DIR);
		File f = new File(CONFIG_DIR + "/" + CONFIG_FILENAME);

		if (!dir.exists())
			dir.mkdirs();

		try {
			if (!f.exists()) {
				f.createNewFile();
			} else {
				editorConfig.load(new FileInputStream(f));
			}
		} catch (IOException e) {
			EditorLogger.error(e.getMessage());
		}
	}
	
	public void saveConfig() {
		File f = new File(CONFIG_DIR + "/" + CONFIG_FILENAME);
		
		try {
			editorConfig.store(new FileOutputStream(f), null);
		} catch (IOException e) {
			EditorLogger.error(e.getMessage());
		}
	}
	
	public Properties getConfig() {
		return editorConfig;
	}

	public void setSelectedScene(Element scn) {
		Element old = null;

		old = selectedScene;

		selectedScene = scn;
		selectedActor = null;
		selectedFA = null;
		EditorLogger.error("XXX SCENE SELECTED ACTOR NULLLLLLL");
		
		firePropertyChange(NOTIFY_SCENE_SELECTED, old, selectedScene);
	}

	public void setSelectedActor(Element a) {
		Element old = null;

		old = selectedActor;

		selectedActor = a;

		selectedFA = null;
		
		if(selectedActor==null)
			EditorLogger.error("XXX SELECTED ACTOR NULLLLLLL");
		else
			EditorLogger.error("XXX SELECTED ACTOR: " + selectedActor.getAttribute("id"));

		firePropertyChange(NOTIFY_ACTOR_SELECTED, old, selectedActor);
	}
	
	public ChapterDocument getSelectedChapter() {
		return selectedChapter;
	}

	public Element getSelectedScene() {
		return selectedScene;
	}

	public Element getSelectedActor() {
		return selectedActor;
	}

	public String getSelectedFA() {
		return selectedFA;
	}

	public void setSelectedFA(String id) {
		String old = selectedFA;

		selectedFA = id;

		firePropertyChange(NOTIFY_FA_SELECTED, old, selectedFA);
	}

	public String getModelPath() {
		return projectFile.getAbsolutePath() + MODEL_PATH;
	}
<commit_msg>deleted some debug prints<commit_after>/*******************************************************************************
 * Copyright 2014 <NAME>.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package org.bladecoder.engineeditor.model;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.bladecoder.engine.util.Config;
import org.bladecoder.engineeditor.Ctx;
import org.bladecoder.engineeditor.utils.DinamicClassPath;
import org.bladecoder.engineeditor.utils.EditorLogger;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.loaders.resolvers.ResolutionFileResolver.Resolution;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;

public class Project extends PropertyChange {
	public static final String PROP_PROJECTFILE = "projectFile";
	public static final String NOTIFY_SCENE_SELECTED = "SCENE_SELECTED";
	public static final String NOTIFY_ACTOR_SELECTED = "ACTOR_SELECTED";
	public static final String NOTIFY_FA_SELECTED = "FA_SELECTED";
	public static final String NOTIFY_VERB_SELECTED = "VERB_SELECTED";
	public static final String NOTIFY_PROJECT_LOADED = "PROJECT_LOADED";

	public static final String TMPL_JAR_FILENAME = "/res/data/project_tmpl.jar";

	public static final String ASSETS_PATH = "/assets";
	public static final String MODEL_PATH = ASSETS_PATH + "/model";
	public static final String ATLASES_PATH = ASSETS_PATH + "/atlases";
	public static final String BACKGROUNDS_PATH = ASSETS_PATH + "/backgrounds";
	public static final String FONTS_PATH = ASSETS_PATH + "/fonts";
	public static final String MUSIC_PATH = ASSETS_PATH + "/music";
	public static final String SOUND_PATH = ASSETS_PATH + "/sounds";
	public static final String IMAGE_PATH = ASSETS_PATH + "/images";
	public static final String SPRITE3D_PATH = ASSETS_PATH + "/3d";
	public static final String SPINE_PATH = ASSETS_PATH + "/spine";
	public static final String UI_PATH = ASSETS_PATH + "/ui";

	private static final String CONFIG_DIR = System.getProperty("user.home") + "/.AdventureComposer";
	private static final String CONFIG_FILENAME = "config.properties";
	
	public static final String LAST_PROJECT_PROP = "last_project";

	private final Properties editorConfig = new Properties();

	private File projectFile;

	private final WorldDocument world = new WorldDocument();	
	private Properties projectConfig;

	private ChapterDocument selectedChapter;
	private Element selectedScene;
	private Element selectedActor;
	private String selectedFA;
	
	private HashMap<String, TextureRegion> bgIconCache = new HashMap<String, TextureRegion>();

	final PropertyChangeListener modelChangeListener = new PropertyChangeListener() {
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			firePropertyChange(evt.getPropertyName(), evt.getOldValue(), evt.getNewValue());
		}

	};

	public Project() {
		world.addPropertyChangeListener(modelChangeListener);
		loadConfig();
	}
	
	public TextureRegion getBgIcon(String s) {
		TextureRegion icon = bgIconCache.get(s);
		
		if(icon == null) {
			try {
				bgIconCache.put(s, createBgIcon(s));
			} catch (Exception e) {
				return null;
			}
			
			icon = bgIconCache.get(s);
		}
		
		return icon;
	}
	
	private TextureRegion createBgIcon(String bg) {
		return new TextureRegion(new Texture(Gdx.files.absolute(getProjectPath() + "/" + BACKGROUNDS_PATH + 
				"/" + getResolutions().get(0).suffix + "/" + bg)));
	}

	private void loadConfig() {
		File dir = new File(CONFIG_DIR);
		File f = new File(CONFIG_DIR + "/" + CONFIG_FILENAME);

		if (!dir.exists())
			dir.mkdirs();

		try {
			if (!f.exists()) {
				f.createNewFile();
			} else {
				editorConfig.load(new FileInputStream(f));
			}
		} catch (IOException e) {
			EditorLogger.error(e.getMessage());
		}
	}
	
	public void saveConfig() {
		File f = new File(CONFIG_DIR + "/" + CONFIG_FILENAME);
		
		try {
			editorConfig.store(new FileOutputStream(f), null);
		} catch (IOException e) {
			EditorLogger.error(e.getMessage());
		}
	}
	
	public Properties getConfig() {
		return editorConfig;
	}

	public void setSelectedScene(Element scn) {
		Element old = null;

		old = selectedScene;

		selectedScene = scn;
		selectedActor = null;
		selectedFA = null;
		
		firePropertyChange(NOTIFY_SCENE_SELECTED, old, selectedScene);
	}

	public void setSelectedActor(Element a) {
		Element old = null;

		old = selectedActor;

		selectedActor = a;

		selectedFA = null;
		
		firePropertyChange(NOTIFY_ACTOR_SELECTED, old, selectedActor);
	}
	
	public ChapterDocument getSelectedChapter() {
		return selectedChapter;
	}

	public Element getSelectedScene() {
		return selectedScene;
	}

	public Element getSelectedActor() {
		return selectedActor;
	}

	public String getSelectedFA() {
		return selectedFA;
	}

	public void setSelectedFA(String id) {
		String old = selectedFA;

		selectedFA = id;

		firePropertyChange(NOTIFY_FA_SELECTED, old, selectedFA);
	}

	public String getModelPath() {
		return projectFile.getAbsolutePath() + MODEL_PATH;
	}

{'fraction_non_alphanumeric': 0.12043062585361934, 'fraction_numerical': 0.0020888567526311562, 'mean_word_length': 4.87724268177526, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6079581', 'n_tokens_mistral': 4027, 'n_tokens_neox': 3639, 'n_words': 984}