From e5d505f0329e1ae048a1445c45b65cef8cd4e36b Mon Sep 17 00:00:00 2001 From: Marko Živković Date: Thu, 26 Feb 2015 20:31:17 +0000 Subject: Finished mapping of workspace config to JSON and back, applied further refactorings git-svn-id: https://svn.code.sf.net/p/xlogo4schools/svn/trunk@28 3b0d7934-f7ef-4143-9606-b51f2e2281fd --- logo/src/xlogo/AppSettings.java | 4 +- logo/src/xlogo/Logo.java | 2 +- logo/src/xlogo/Popup.java | 3 +- logo/src/xlogo/gui/Traduc.java | 6 +- logo/src/xlogo/gui/translation/MyTable.java | 2 +- logo/src/xlogo/gui/translation/TranslateXLogo.java | 4 +- logo/src/xlogo/storage/JSONSerializer.java | 6 +- logo/src/xlogo/storage/StorableObject.java | 2 +- logo/src/xlogo/storage/WSManager.java | 58 +++----- logo/src/xlogo/storage/global/GlobalConfig.java | 72 +-------- logo/src/xlogo/storage/workspace/Language.java | 68 ++++----- logo/src/xlogo/storage/workspace/LogoLanguage.java | 101 ++++++++----- .../xlogo/storage/workspace/NumberOfBackups.java | 15 ++ .../xlogo/storage/workspace/WorkspaceConfig.java | 17 ++- .../workspace/WorkspaceConfigJSONSerializer.java | 51 +++++-- .../workspace/WorkspaceSettingJSONMapper.java | 165 --------------------- 16 files changed, 215 insertions(+), 361 deletions(-) delete mode 100644 logo/src/xlogo/storage/workspace/WorkspaceSettingJSONMapper.java diff --git a/logo/src/xlogo/AppSettings.java b/logo/src/xlogo/AppSettings.java index 9e94efb..8a8d2da 100644 --- a/logo/src/xlogo/AppSettings.java +++ b/logo/src/xlogo/AppSettings.java @@ -90,7 +90,7 @@ public class AppSettings implements Observable{ * LANGUAGE * * * * * * */ - private Language language = Language.LANGUAGE_ENGLISH; + private Language language = Language.ENGLISH; public Language getLanguage() { return language; @@ -113,7 +113,7 @@ public class AppSettings implements Observable{ */ public String translate(String key) { if (Logo.messages == null) { - Logo.generateLanguage(Language.LANGUAGE_ENGLISH); // TODO this is a temporary bug fix + Logo.generateLanguage(Language.ENGLISH); // TODO this is a temporary bug fix } return Logo.messages.getString(key); } diff --git a/logo/src/xlogo/Logo.java b/logo/src/xlogo/Logo.java index 34945fb..907aa79 100644 --- a/logo/src/xlogo/Logo.java +++ b/logo/src/xlogo/Logo.java @@ -111,7 +111,7 @@ public class Logo { /**Builds Application with the valid Config*/ public Logo() { WorkspaceConfig wc = WSManager.getWorkspaceConfig(); - Language lang = Language.LANGUAGE_ENGLISH; + Language lang = Language.ENGLISH; if (wc != null){ lang = WSManager.getInstance().getWorkspaceConfigInstance().getLanguage(); } diff --git a/logo/src/xlogo/Popup.java b/logo/src/xlogo/Popup.java index 8eda3d9..480b869 100644 --- a/logo/src/xlogo/Popup.java +++ b/logo/src/xlogo/Popup.java @@ -39,6 +39,7 @@ import javax.swing.JPopupMenu; import xlogo.storage.WSManager; import xlogo.storage.workspace.Language; +import xlogo.storage.workspace.LogoLanguage; import java.awt.event.*; /** @@ -121,7 +122,7 @@ public class Popup extends JPopupMenu implements ActionListener { jpopcouper.setText(Logo.messages.getString("menu.edition.cut")); jpopcopier.setText(Logo.messages.getString("menu.edition.copy")); // Si le langage choisie est l'esperanto, on rajoute les caractères accentués spéciaux au menu - if (WSManager.getWorkspaceConfig().getLanguage()==Language.LANGUAGE_ESPERANTO) { + if (WSManager.getWorkspaceConfig().getLogoLanguage()==LogoLanguage.ESPERANTO) { add(separ); for (int i=0;i v=bottom.getPrimTable().getKeys(); ResourceBundle rb = ResourceBundle.getBundle("primitives", locale); diff --git a/logo/src/xlogo/storage/JSONSerializer.java b/logo/src/xlogo/storage/JSONSerializer.java index 2766da4..0711b8d 100644 --- a/logo/src/xlogo/storage/JSONSerializer.java +++ b/logo/src/xlogo/storage/JSONSerializer.java @@ -11,11 +11,13 @@ public abstract class JSONSerializer implements Serializer{ @Override public T deserialize(String json) { - return deserialize(new JSONObject(json)); + return deserialize(new JSONObject(json.trim())); } @Override public String serialize2String(T target) { - return serialize2JSON(target).toString(); + JSONObject json = serialize2JSON(target); + String serialized = json.toString(); + return serialized; } } diff --git a/logo/src/xlogo/storage/StorableObject.java b/logo/src/xlogo/storage/StorableObject.java index 4742673..2d6fa5f 100644 --- a/logo/src/xlogo/storage/StorableObject.java +++ b/logo/src/xlogo/storage/StorableObject.java @@ -194,7 +194,7 @@ public class StorableObject, E extends Enum> extends if (!isVirtual()) { if (getSerializer() != null) { - Utils.store(file, getSerializer().serialize2String(get())); + Utils.storeFile(file, getSerializer().serialize2String(get())); } else { Utils.store(file, get()); diff --git a/logo/src/xlogo/storage/WSManager.java b/logo/src/xlogo/storage/WSManager.java index 3be37b4..0e5f832 100644 --- a/logo/src/xlogo/storage/WSManager.java +++ b/logo/src/xlogo/storage/WSManager.java @@ -45,6 +45,7 @@ import xlogo.storage.user.UserConfig; import xlogo.storage.user.UserConfig.UserProperty; import xlogo.storage.workspace.WorkspaceConfig; import xlogo.storage.workspace.WorkspaceConfig.WorkspaceProperty; +import xlogo.storage.workspace.WorkspaceConfigJSONSerializer; import xlogo.utils.Utils; /** @@ -305,29 +306,7 @@ public class WSManager { /* * Config Creation */ - - /* * - * - * @param dir - where the workspace should be located - * @param workspaceName - * @param deferred - if true, the workspace is not persisted until changes are made to it - * @return - * @throws IOException - * / - public static StorableObject getWorkspace(File dir, String workspaceName, boolean deferred) - throws IOException { - if (!Storable.checkLegalName(workspaceName)) { - DialogMessenger.getInstance().dispatchError(Logo.messages.getString(MessageKeys.NAME_ERROR_TITLE), - Logo.messages.getString(MessageKeys.ILLEGAL_NAME)); - return null; - } - File wsd = StorableObject.getDirectory(dir, workspaceName); - Serializer serializer = new WorkspaceSettingJSONMapper(); - StorableObject wsc = new StorableObject<>(WorkspaceConfig.class, wsd, deferred).withSerializer(serializer); - wsc.get().setLocation(wsd); - return wsc; - } - */ + /** * Load the specified user's settings from the current workspace * or create the user if it does not exist yet or if it was deleted for unknown reasons. @@ -421,17 +400,16 @@ public class WSManager { */ protected StorableObject createWorkspace(GlobalConfig gc, File wsDir){ StorableObject wc; - wc = new StorableObject<>(WorkspaceConfig.class, wsDir); - try { - wc.createOrLoad(); - wc.get().setDirectory(wsDir); - gc.addWorkspace(wc); - enterWorkspace(gc, wsDir.getName()); - return wc; - } - catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException | IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + wc = WorkspaceConfigJSONSerializer.createOrLoad(wsDir); + if (wc != null){ + try { + gc.addWorkspace(wc); + enterWorkspace(gc, wsDir.getName()); + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } return wc; } @@ -484,7 +462,17 @@ public class WSManager { logger.trace("Retrieving USB workspace: " + workspaceName); initUSBDrive(gc, workspaceName); } - gc.enterWorkspace(workspaceName); + File wsDir = gc.getWorkspaceDirectory(workspaceName); + if (wsDir == null){ + logger.error("Can't find workspace " + workspaceName); + return; + } + StorableObject wc = WorkspaceConfigJSONSerializer.createOrLoad(wsDir); + if (wc == null){ + logger.error("Can't enter workspace because creation or laod failed for " + workspaceName); + return; + } + gc.enterWorkspace(wc); } /* diff --git a/logo/src/xlogo/storage/global/GlobalConfig.java b/logo/src/xlogo/storage/global/GlobalConfig.java index 1c5a27d..6195142 100644 --- a/logo/src/xlogo/storage/global/GlobalConfig.java +++ b/logo/src/xlogo/storage/global/GlobalConfig.java @@ -28,7 +28,6 @@ import java.io.File; import java.io.IOException; import java.io.Serializable; import java.io.UnsupportedEncodingException; -import java.lang.reflect.InvocationTargetException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -102,64 +101,6 @@ public class GlobalConfig implements Serializable, Observable retrieveWorkspace(String workspaceName) throws IOException { - StorableObject wsc = getCachedWorkspace(workspaceName); - if (wsc != null) { - logger.trace("Retrieved cached workspace: " + workspaceName); - return wsc; - } - - if (!existsWorkspace(workspaceName)) { - logger.warn("Attempting to load an inexistent workspace: " + workspaceName); - return null; - } - - File wsDir = getWorkspaceDirectory(workspaceName); - - logger.trace("Retrieving workspace: " + workspaceName + " from " + wsDir.toString()); - try { - wsc = new StorableObject<>(WorkspaceConfig.class, wsDir).createOrLoad(); - wsc.get().setDirectory(wsDir); - cacheWorkspace(workspaceName, wsc); - } - catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException ignore) { } - - return wsc; - } - - /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * - * Workspace Cache - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ - - /** - * Workspace Objects that have already been created or loaded from disk. - */ - private transient Map> cachedWorkspaces; - - private StorableObject getCachedWorkspace(String workspaceName) { - if (cachedWorkspaces == null) { - cachedWorkspaces = new TreeMap<>(); - } - return cachedWorkspaces.get(workspaceName); - } - - private void cacheWorkspace(String workspaceName, StorableObject wsc) { - if (cachedWorkspaces == null) { - cachedWorkspaces = new TreeMap<>(); - } - cachedWorkspaces.put(workspaceName, wsc); - } - /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Workspaces * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -183,7 +124,6 @@ public class GlobalConfig implements Serializable, Observable wc) { File location = wc.getLocation(); String name = wc.get().getWorkspaceName(); - cacheWorkspace(name, wc); addWorkspace(name, location.getParentFile().toString()); } @@ -200,7 +140,6 @@ public class GlobalConfig implements Serializable, ObservableAlways succeeds if workspaceName equals {@link WorkspaceConfig#VIRTUAL_WORKSPACE} * @param workspaceName - the workspace to load and enter * @throws IOException - if the workspace could not be loaded */ - public void enterWorkspace(String workspaceName) throws IOException { - logger.trace("Entering workspace: " + workspaceName); + public void enterWorkspace(StorableObject wc) throws IOException { + String name = wc.get().getWorkspaceName(); + logger.trace("Entering workspace: " + name); if (currentWorkspace != null) { leaveWorkspace(); } - currentWorkspace = retrieveWorkspace(workspaceName); + currentWorkspace = wc; - setLastUsedWorkspace(workspaceName); + setLastUsedWorkspace(name); publisher.publishEvent(GlobalProperty.CURRENT_WORKSPACE); diff --git a/logo/src/xlogo/storage/workspace/Language.java b/logo/src/xlogo/storage/workspace/Language.java index df32f70..d1c3469 100644 --- a/logo/src/xlogo/storage/workspace/Language.java +++ b/logo/src/xlogo/storage/workspace/Language.java @@ -27,33 +27,30 @@ package xlogo.storage.workspace; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; public enum Language { - LANGUAGE_FRENCH(0, "French", "fr", "FR"), - LANGUAGE_ENGLISH(1, "English", "en", "US"), - LANGUAGE_ARABIC(2, "Arabic", "ar", "MA"), - LANGUAGE_SPANISH(3, "Spanish", "es", "ES"), - LANGUAGE_PORTUGAL(4, "Portuguese", "pt", "BR"), - LANGUAGE_ESPERANTO(5, "Esperanto", "eo", "EO"), - LANGUAGE_GERMAN(6, "German", "de", "DE"), - LANGUAGE_GALICIAN(7, "Galician", "gl", "ES"), - LANGUAGE_ASTURIAN(8, "Asturian", "al", "ES"), - LANGUAGE_GREEK(9, "Greek", "el", "GR"), - LANGUAGE_ITALIAN(10, "Italian", "it", "IT"), - LANGUAGE_CATALAN(11, "Catalan", "ca", "ES"), - LANGUAGE_HUNGARIAN(12, "Hungarian", "hu", "HU"), - LANGUAGE_ENGLISH_GERMAN(13, "ABZ German/English", "en", "DE"); + FRENCH(0, "French", "fr", "FR"), + ENGLISH(1, "English", "en", "US"), +// ARABIC(2, "Arabic", "ar", "MA"), +// SPANISH(3, "Spanish", "es", "ES"), +// PORTUESE(4, "Portuguese", "pt", "BR"), +// ESPERANTO(5, "Esperanto", "eo", "EO"), + GERMAN(6, "German", "de", "DE"), +// GALICIAN(7, "Galician", "gl", "ES"), +// ASTURIAN(8, "Asturian", "al", "ES"), +// GREEK(9, "Greek", "el", "GR"), + ITALIAN(10, "Italian", "it", "IT") +// CATALAN(11, "Catalan", "ca", "ES"), +// HUNGARIAN(12, "Hungarian", "hu", "HU"), +// ENGLISH_GERMAN(13, "ABZ German/English", "en", "DE") // TODO remove this + ; private int value; private String englishName; private String languageCode; private String countryCode; - private static Map valueToLanguage; - private Language(int value, String englishName, String languageCode, String countryCode) { this.value = value; this.englishName = englishName; @@ -61,26 +58,30 @@ public enum Language { this.countryCode = countryCode; } - public static Language getLanguage(int i) - { - if (valueToLanguage == null) - initMapping(); - return valueToLanguage.get(i); - } - - public static void initMapping() - { - valueToLanguage = new HashMap(); - for (Language lang : values()) { - valueToLanguage.put(lang.value, lang); - } - } - public int getValue() { return value; } + public static Language valueOf(int value){ + switch(value){ + case 0: return Language.FRENCH; + case 1: return Language.ENGLISH; +// case 2: return Language.ARABIC; +// case 3: return Language.SPANISH; +// case 4: return Language.PORTUESE; +// case 5: return Language.ESPERANTO; + case 6: return Language.GERMAN; +// case 7: return Language.GALICIAN; +// case 8: return Language.ASTURIAN; +// case 9: return Language.GREEK; + case 10: return Language.ITALIAN; +// case 11: return Language.CATALAN; +// case 12: return Language.HUNGARIAN; + default: return Language.ENGLISH; + } + } + public String getEnglishName() { return englishName; @@ -108,4 +109,5 @@ public enum Language { { return countryCode; } + } diff --git a/logo/src/xlogo/storage/workspace/LogoLanguage.java b/logo/src/xlogo/storage/workspace/LogoLanguage.java index 342aa52..cab5513 100644 --- a/logo/src/xlogo/storage/workspace/LogoLanguage.java +++ b/logo/src/xlogo/storage/workspace/LogoLanguage.java @@ -1,54 +1,83 @@ +/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq + * Copyright (C) 2013 Marko Zivkovic + * + * Contact Information: marko88zivkovic at gmail dot com + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 2 of the License, or (at your option) + * any later version. 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, write to the Free + * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + * + * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic + * during his Bachelor thesis at the computer science department of ETH Zurich, + * in the year 2013 and/or during future work. + * + * It is a reengineered version of XLogo written by Loic Le Coq, published + * under the GPL License at http://xlogo.tuxfamily.org/ + * + * Contents of this file were entirely written by Marko Zivkovic + */ + package xlogo.storage.workspace; -import java.util.HashMap; import java.util.Locale; -import java.util.Map; public enum LogoLanguage { - LANGUAGE_FRENCH(0, "French", "fr", "FR"), - LANGUAGE_ENGLISH(1, "English", "en", "US"), - LANGUAGE_ARABIC(2, "Arabic", "ar", "MA"), - LANGUAGE_SPANISH(3, "Spanish", "es", "ES"), - LANGUAGE_PORTUGAL(4, "Portuguese", "pt", "BR"), - LANGUAGE_ESPERANTO(5, "Esperanto", "eo", "EO"), - LANGUAGE_GERMAN(6, "German", "de", "DE"), - LANGUAGE_GALICIAN(7, "Galician","gl", "ES"), - LANGUAGE_ASTURIAN(8, "Asturian", "al", "ES"), - LANGUAGE_GREEK(9, "Greek", "el", "GR"), - LANGUAGE_ITALIAN(10, "Italian", "it", "IT"), - LANGUAGE_CATALAN(11, "Catalan", "ca", "ES"), - LANGUAGE_HUNGARIAN(12,"Hungarian", "hu", "HU"); - - private int value; - private String englishName; - private String languageCode; - private String countryCode; - - private static Map valueToLanguage; + FRENCH(0, "French", "fr", "FR"), + ENGLISH(1, "English", "en", "US"), + ARABIC(2, "Arabic", "ar", "MA"), + SPANISH(3, "Spanish", "es", "ES"), + PORTUESE(4, "Portuguese", "pt", "BR"), + ESPERANTO(5, "Esperanto", "eo", "EO"), + GERMAN(6, "German", "de", "DE"), + GALICIAN(7, "Galician","gl", "ES"), + ASTURIAN(8, "Asturian", "al", "ES"), + GREEK(9, "Greek", "el", "GR"), + ITALIAN(10, "Italian", "it", "IT"), + CATALAN(11, "Catalan", "ca", "ES"), + HUNGARIAN(12,"Hungarian", "hu", "HU"); + private int value; + private String englishName; + private String languageCode; + private String countryCode; + private LogoLanguage(int value, String englishName, String languageCode, String countryCode) { this.value = value; this.englishName = englishName; this.languageCode = languageCode; this.countryCode = countryCode; } - - public static LogoLanguage getLanguage(int i) { - if (valueToLanguage == null) - initMapping(); - return valueToLanguage.get(i); - } - - public static void initMapping() { - valueToLanguage = new HashMap(); - for (LogoLanguage lang : values()) { - valueToLanguage.put(lang.value, lang); - } - } - + public int getValue() { return value; } + + public static LogoLanguage valueOf(int value){ + switch(value){ + case 0: return LogoLanguage.FRENCH; + case 1: return LogoLanguage.ENGLISH; + case 2: return LogoLanguage.ARABIC; + case 3: return LogoLanguage.SPANISH; + case 4: return LogoLanguage.PORTUESE; + case 5: return LogoLanguage.ESPERANTO; + case 6: return LogoLanguage.GERMAN; + case 7: return LogoLanguage.GALICIAN; + case 8: return LogoLanguage.ASTURIAN; + case 9: return LogoLanguage.GREEK; + case 10: return LogoLanguage.ITALIAN; + case 11: return LogoLanguage.CATALAN; + case 12: return LogoLanguage.HUNGARIAN; + default: return LogoLanguage.ENGLISH; + } + } public String getEnglishName() { return englishName; diff --git a/logo/src/xlogo/storage/workspace/NumberOfBackups.java b/logo/src/xlogo/storage/workspace/NumberOfBackups.java index 3aa3534..6b5bf46 100644 --- a/logo/src/xlogo/storage/workspace/NumberOfBackups.java +++ b/logo/src/xlogo/storage/workspace/NumberOfBackups.java @@ -54,4 +54,19 @@ public enum NumberOfBackups { } return Integer.toString(number); } + + public int getValue(){ + return number; + } + + public static NumberOfBackups valueOf(int value){ + switch(value){ + case 0: return NumberOfBackups.NO_BACKUPS; + case 1: return NumberOfBackups.ONE; + case 3: return NumberOfBackups.THREE; + case 10: return NumberOfBackups.TEN; + case 50: return NumberOfBackups.FIFTY; + default: return NumberOfBackups.INFINITE; + } + } } diff --git a/logo/src/xlogo/storage/workspace/WorkspaceConfig.java b/logo/src/xlogo/storage/workspace/WorkspaceConfig.java index 800270b..7ee4f26 100644 --- a/logo/src/xlogo/storage/workspace/WorkspaceConfig.java +++ b/logo/src/xlogo/storage/workspace/WorkspaceConfig.java @@ -109,6 +109,7 @@ public class WorkspaceConfig implements Serializable, Observable publisher = new PropertyChangePublisher<>(); diff --git a/logo/src/xlogo/storage/workspace/WorkspaceConfigJSONSerializer.java b/logo/src/xlogo/storage/workspace/WorkspaceConfigJSONSerializer.java index b78d3f0..4bb07fe 100644 --- a/logo/src/xlogo/storage/workspace/WorkspaceConfigJSONSerializer.java +++ b/logo/src/xlogo/storage/workspace/WorkspaceConfigJSONSerializer.java @@ -1,11 +1,15 @@ package xlogo.storage.workspace; import java.awt.Font; +import java.io.File; +import java.lang.reflect.InvocationTargetException; import org.json.JSONArray; import org.json.JSONObject; import xlogo.storage.JSONSerializer; +import xlogo.storage.StorableObject; +import xlogo.storage.workspace.WorkspaceConfig.WorkspaceProperty; public class WorkspaceConfigJSONSerializer extends JSONSerializer { @@ -21,16 +25,34 @@ public class WorkspaceConfigJSONSerializer extends JSONSerializer createOrLoad(File wsDir){ + StorableObject wc; + wc = new StorableObject<>(WorkspaceConfig.class, wsDir).withSerializer(new WorkspaceConfigJSONSerializer()); + try { + wc.createOrLoad(); + wc.get().setDirectory(wsDir); + return wc; + } + catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return wc; + } + @Override public JSONObject serialize2JSON(WorkspaceConfig ws) { @@ -38,8 +60,9 @@ public class WorkspaceConfigJSONSerializer extends JSONSerializer { - - private static final String FONT = "font"; - private static final String SIZE = "size"; - private static final String STYLE = "style"; - private static final String NAME = "name"; - private static final String SYNTAX_HIGHLIGHTING_STYLES = "syntaxHighlightingStyles"; - private static final String PRIMITIVE_STYLE = "primitiveStyle"; - private static final String PRIMITIVE_COLOR = "primitiveColor"; - private static final String OPERAND_STYLE = "operandStyle"; - private static final String OPERAND_COLOR = "operandColor"; - private static final String COMMENT_STYLE = "commentStyle"; - private static final String COMMENT_COLOR = "commentColor"; - private static final String BRACE_STYLE = "braceStyle"; - private static final String BRACE_COLOR = "braceColor"; - private static final String CONTEST_SETTINGS = "contestSettings"; - private static final String N_OF_CONTEST_BONUS_FILES = "nOfContestBonusFiles"; - private static final String N_OF_CONTEST_FILES = "nOfContestFiles"; - private static final String IS_USER_CREATION_ALLOWED = "isUserCreationAllowed"; - private static final String LANGUAGE = "language"; - private static final String NUMBER_OF_BACKUPS = "numberOfBackups"; - private static final String USER_LIST = "userList"; - private static final String LAST_ACTIVE_USER = "lastActiveUser"; - - @Override - public JSONObject serialize2JSON(WorkspaceConfig ws) { - - JSONObject json = new JSONObject(); - - json.put(USER_LIST, new JSONArray(ws.getUserList())); - json.put(LAST_ACTIVE_USER, ws.getLastActiveUser()); - json.put(NUMBER_OF_BACKUPS, ws.getNumberOfBackups().toString()); - json.put(LANGUAGE, ws.getLanguage().toString()); - json.put(IS_USER_CREATION_ALLOWED, ws.isUserCreationAllowed()); - - JSONObject jsonContestSettings = new JSONObject(); - jsonContestSettings.put(N_OF_CONTEST_FILES, ws.getNOfContestFiles()); - jsonContestSettings.put(N_OF_CONTEST_BONUS_FILES, ws.getNOfContestBonusFiles()); - json.put(CONTEST_SETTINGS, jsonContestSettings); - - JSONObject jsonSyntaxHighlightingStyles = new JSONObject().put(BRACE_COLOR, ws.getBraceColor()) - .put(BRACE_STYLE, ws.getBraceStyle()).put(COMMENT_COLOR, ws.getCommentColor()) - .put(COMMENT_STYLE, ws.getCommentStyle()).put(OPERAND_COLOR, ws.getOperandColor()) - .put(OPERAND_STYLE, ws.getOperandStyle()).put(PRIMITIVE_COLOR, ws.getPrimitiveColor()) - .put(PRIMITIVE_STYLE, ws.getPrimitiveStyle()); - json.put(SYNTAX_HIGHLIGHTING_STYLES, jsonSyntaxHighlightingStyles); - - JSONObject jsonFont = new JSONObject().put(NAME, ws.getFont().getName()).put(STYLE, ws.getFont().getStyle()) - .put(SIZE, ws.getFont().getSize()); - json.put(FONT, jsonFont); - - return json; - } - - @Override - public WorkspaceConfig deserialize(JSONObject json) { - WorkspaceConfig ws = new WorkspaceConfig(); - - if (json.has(USER_LIST)) { - JSONArray jsonUserList = json.getJSONArray(USER_LIST); - for (int i = 0; i < jsonUserList.length(); i++) { - ws.addUser(jsonUserList.getString(i)); - } - } - - if (json.has(LAST_ACTIVE_USER)) { - ws.setLastActiveUser(json.getString(LAST_ACTIVE_USER)); - } - - if (json.has(NUMBER_OF_BACKUPS)) { - ws.setNumberOfBackups(NumberOfBackups.valueOf(json.getString(NUMBER_OF_BACKUPS))); - } - - if (json.has(LANGUAGE)) { - ws.setLanguage(Language.valueOf(json.getString(LANGUAGE))); - } - - if (json.has(IS_USER_CREATION_ALLOWED)) { - ws.setUserCreationAllowed(json.getBoolean(IS_USER_CREATION_ALLOWED)); - } - - if (json.has(CONTEST_SETTINGS)) { - JSONObject jsonContestSettings = json.getJSONObject(CONTEST_SETTINGS); - - if (jsonContestSettings.has(N_OF_CONTEST_FILES)) { - ws.setNOfContestFiles(jsonContestSettings.getInt(N_OF_CONTEST_FILES)); - - } - if (jsonContestSettings.has(N_OF_CONTEST_BONUS_FILES)) { - ws.setNOfContestBonusFiles(jsonContestSettings.getInt(N_OF_CONTEST_BONUS_FILES)); - } - - } - - if (json.has(SYNTAX_HIGHLIGHTING_STYLES)) { - JSONObject jsonContestSettings = json.getJSONObject(SYNTAX_HIGHLIGHTING_STYLES); - - if (jsonContestSettings.has(BRACE_COLOR)) { - ws.setBraceColor(jsonContestSettings.getInt(BRACE_COLOR)); - } - - if (jsonContestSettings.has(BRACE_STYLE)) { - ws.setBraceStyle(jsonContestSettings.getInt(BRACE_STYLE)); - } - - if (jsonContestSettings.has(COMMENT_COLOR)) { - ws.setCommentColor(jsonContestSettings.getInt(COMMENT_COLOR)); - } - - if (jsonContestSettings.has(COMMENT_STYLE)) { - ws.setCommentStyle(jsonContestSettings.getInt(COMMENT_STYLE)); - } - - if (jsonContestSettings.has(OPERAND_COLOR)) { - ws.setOperandColor(jsonContestSettings.getInt(OPERAND_COLOR)); - } - - if (jsonContestSettings.has(OPERAND_STYLE)) { - ws.setOperandStyle(jsonContestSettings.getInt(OPERAND_STYLE)); - } - - if (jsonContestSettings.has(PRIMITIVE_COLOR)) { - ws.setPrimitiveColor(jsonContestSettings.getInt(PRIMITIVE_COLOR)); - } - - if (jsonContestSettings.has(PRIMITIVE_STYLE)) { - ws.setPrimitiveStyle(jsonContestSettings.getInt(PRIMITIVE_STYLE)); - } - - } - - if (json.has(FONT)) { - JSONObject jsonFont = json.getJSONObject(FONT); - - String name = ws.getFont().getName(); - if (jsonFont.has(NAME)) { - name = jsonFont.getString(NAME); - } - - int style = ws.getFont().getStyle(); - if (jsonFont.has(STYLE)) { - style = jsonFont.getInt(STYLE); - } - - int size = ws.getFont().getSize(); - if (jsonFont.has(SIZE)) { - size = jsonFont.getInt(SIZE); - } - - ws.setFont(new Font(name, style, size)); - } - - return ws; - } - -} -- cgit v1.2.3