summaryrefslogtreecommitdiffstats
path: root/logo
diff options
context:
space:
mode:
authorMarko Živković <[email protected]>2015-02-26 20:31:17 +0000
committerMarko Živković <[email protected]>2015-02-26 20:31:17 +0000
commite5d505f0329e1ae048a1445c45b65cef8cd4e36b (patch)
tree324f0e59443344e73213599977a3e20ff623b51f /logo
parenta25b730c4d6679dae2a2a9b0489b70b0fc75d992 (diff)
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
Diffstat (limited to 'logo')
-rw-r--r--logo/src/xlogo/AppSettings.java4
-rw-r--r--logo/src/xlogo/Logo.java2
-rw-r--r--logo/src/xlogo/Popup.java3
-rw-r--r--logo/src/xlogo/gui/Traduc.java6
-rw-r--r--logo/src/xlogo/gui/translation/MyTable.java2
-rw-r--r--logo/src/xlogo/gui/translation/TranslateXLogo.java4
-rw-r--r--logo/src/xlogo/storage/JSONSerializer.java6
-rw-r--r--logo/src/xlogo/storage/StorableObject.java2
-rw-r--r--logo/src/xlogo/storage/WSManager.java58
-rw-r--r--logo/src/xlogo/storage/global/GlobalConfig.java72
-rw-r--r--logo/src/xlogo/storage/workspace/Language.java68
-rw-r--r--logo/src/xlogo/storage/workspace/LogoLanguage.java101
-rw-r--r--logo/src/xlogo/storage/workspace/NumberOfBackups.java15
-rw-r--r--logo/src/xlogo/storage/workspace/WorkspaceConfig.java17
-rw-r--r--logo/src/xlogo/storage/workspace/WorkspaceConfigJSONSerializer.java51
-rw-r--r--logo/src/xlogo/storage/workspace/WorkspaceSettingJSONMapper.java165
16 files changed, 215 insertions, 361 deletions
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<AppSettings.AppProperty>{
* 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<AppSettings.AppProperty>{
*/
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<jpopcar.length;i++) {
add(jpopcar[i]);
diff --git a/logo/src/xlogo/gui/Traduc.java b/logo/src/xlogo/gui/Traduc.java
index 93b6234..7fa1b3c 100644
--- a/logo/src/xlogo/gui/Traduc.java
+++ b/logo/src/xlogo/gui/Traduc.java
@@ -172,10 +172,10 @@ public class Traduc extends JFrame implements ActionListener {
}
// ajout des mots clés pour et fin
int id=combo_origine.getSelectedIndex();
- Locale locale=Language.getLanguage(id).getLocale();
+ Locale locale=Language.valueOf(id).getLocale();
ResourceBundle res1=ResourceBundle.getBundle("langage",locale);
id=combo_destination.getSelectedIndex();
- locale=Language.getLanguage(id).getLocale();
+ locale=Language.valueOf(id).getLocale();
ResourceBundle res2=ResourceBundle.getBundle("langage",locale);
tre.put(res1.getString("pour"),res2.getString("pour"));
tre.put(res1.getString("fin"),res2.getString("fin"));
@@ -210,7 +210,7 @@ public class Traduc extends JFrame implements ActionListener {
private ResourceBundle genere_langue(JComboBox jc){ // fixe la langue utilisée pour les messages
Locale locale=null;
int id=jc.getSelectedIndex();
- locale=Language.getLanguage(id).getLocale();
+ locale=Language.valueOf(id).getLocale();
return ResourceBundle.getBundle("primitives",locale);
}
}
diff --git a/logo/src/xlogo/gui/translation/MyTable.java b/logo/src/xlogo/gui/translation/MyTable.java
index cd8c410..d24dd2c 100644
--- a/logo/src/xlogo/gui/translation/MyTable.java
+++ b/logo/src/xlogo/gui/translation/MyTable.java
@@ -261,7 +261,7 @@ public class MyTable extends JPanel implements Searchable{
ResourceBundle[] rb=new ResourceBundle[getColumnCount()];
// initialize all ResourceBundle
for(int i=0;i<getColumnCount();i++){
- Locale locale = Language.getLanguage(i).getLocale();
+ Locale locale = Language.valueOf(i).getLocale();
// In CREATE Mode, when i=getColumnCount(), the last locale is null
if (null==locale) break;
rb[i] = ResourceBundle.getBundle(bundle, locale);
diff --git a/logo/src/xlogo/gui/translation/TranslateXLogo.java b/logo/src/xlogo/gui/translation/TranslateXLogo.java
index 2206f8d..db4b5ac 100644
--- a/logo/src/xlogo/gui/translation/TranslateXLogo.java
+++ b/logo/src/xlogo/gui/translation/TranslateXLogo.java
@@ -111,10 +111,10 @@ public class TranslateXLogo extends JFrame implements ActionListener {
try {
Locale locale=null;
if (action.equals(TranslateXLogo.CREATE)){
- locale = Language.getLanguage(0).getLocale();
+ locale = Language.valueOf(0).getLocale();
}
else if (!action.equals(TranslateXLogo.CONSULT)){
- locale = Language.getLanguage(Integer.parseInt(id)).getLocale();
+ locale = Language.valueOf(Integer.parseInt(id)).getLocale();
}
java.util.Vector<String> 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<T> implements Serializer<T>{
@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<T extends Observable<E>, E extends Enum<E>> 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<WorkspaceConfig, WorkspaceConfig.WorkspaceProperty> 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<WorkspaceConfig> serializer = new WorkspaceSettingJSONMapper();
- StorableObject<WorkspaceConfig, WorkspaceConfig.WorkspaceProperty> 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<WorkspaceConfig, WorkspaceProperty> createWorkspace(GlobalConfig gc, File wsDir){
StorableObject<WorkspaceConfig, WorkspaceConfig.WorkspaceProperty> 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<WorkspaceConfig, WorkspaceConfig.WorkspaceProperty> 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;
@@ -103,64 +102,6 @@ public class GlobalConfig implements Serializable, Observable<GlobalConfig.Globa
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
- * Physical Workspaces (stored on file system)
- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
-
- /**
- * Load the specified workspace from the file system.
- * @param workspaceName
- * @return the specified workspace or null if it does not exist.
- * @throws IOException
- */
- private StorableObject<WorkspaceConfig, WorkspaceProperty> retrieveWorkspace(String workspaceName) throws IOException {
- StorableObject<WorkspaceConfig, WorkspaceProperty> 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<String, StorableObject<WorkspaceConfig, WorkspaceProperty>> cachedWorkspaces;
-
- private StorableObject<WorkspaceConfig, WorkspaceProperty> getCachedWorkspace(String workspaceName) {
- if (cachedWorkspaces == null) {
- cachedWorkspaces = new TreeMap<>();
- }
- return cachedWorkspaces.get(workspaceName);
- }
-
- private void cacheWorkspace(String workspaceName, StorableObject<WorkspaceConfig, WorkspaceProperty> wsc) {
- if (cachedWorkspaces == null) {
- cachedWorkspaces = new TreeMap<>();
- }
- cachedWorkspaces.put(workspaceName, wsc);
- }
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Workspaces
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -183,7 +124,6 @@ public class GlobalConfig implements Serializable, Observable<GlobalConfig.Globa
public void addWorkspace(StorableObject<WorkspaceConfig, WorkspaceProperty> 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, Observable<GlobalConfig.Globa
}
workspaces.remove(workspaceName);
- cachedWorkspaces.remove(workspaceName);
publisher.publishEvent(GlobalProperty.WORKSPACES);
}
@@ -285,22 +224,21 @@ public class GlobalConfig implements Serializable, Observable<GlobalConfig.Globa
return currentWorkspace;
}
-
-
/**
* Load the workspace
* <p>Always 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<WorkspaceConfig, WorkspaceConfig.WorkspaceProperty> 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<Integer, Language> 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<Integer, Language>();
- 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<Integer, LogoLanguage> 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<Integer, LogoLanguage>();
- 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<WorkspaceConfig
private String lastActiveUser;
private NumberOfBackups numberOfBackups = NumberOfBackups.INFINITE;
private Language language;
+ private LogoLanguage logoLanguage = LogoLanguage.ENGLISH;
private boolean allowUserCreation = true;
private ContestConfig contestSettings; // Contest //TODO create options in workspace settings
private SyntaxHighlightConfig syntaxHighlightingStyles; // TODO = new SyntaxHighlightStyles();
@@ -335,10 +336,22 @@ public class WorkspaceConfig implements Serializable, Observable<WorkspaceConfig
public Language getLanguage() {
if (language == null)
- return Language.LANGUAGE_ENGLISH;
+ return Language.ENGLISH;
return language;
}
+ public LogoLanguage getLogoLanguage() {
+ if (logoLanguage == null)
+ return LogoLanguage.ENGLISH;
+ return logoLanguage;
+ }
+
+ public void setLogoLanguage(LogoLanguage logoLanguage) {
+ if (this.logoLanguage == logoLanguage) { return; }
+ this.logoLanguage = logoLanguage;
+ publisher.publishEvent(WorkspaceProperty.LOGO_LANGUAGE);
+ }
+
/*
* Allow users (children) to create new user accounts in workspaces?
*/
@@ -511,7 +524,7 @@ public class WorkspaceConfig implements Serializable, Observable<WorkspaceConfig
*/
public enum WorkspaceProperty {
- FONT, SYNTAX_HIGHLIGHTING, ALLOW_USER_CREATION, LANGUAGE, N_OF_BACKUPS, LAST_ACTIVE_USER, USER_LIST, CONTEST;
+ FONT, SYNTAX_HIGHLIGHTING, ALLOW_USER_CREATION, LANGUAGE, N_OF_BACKUPS, LAST_ACTIVE_USER, USER_LIST, CONTEST, LOGO_LANGUAGE;
}
private transient PropertyChangePublisher<WorkspaceProperty> 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<WorkspaceConfig> {
@@ -21,16 +25,34 @@ public class WorkspaceConfigJSONSerializer extends JSONSerializer<WorkspaceConfi
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 BRACE_COLOR = "braceColor"; // TODO enable syntax highlighting
+ private static final String IS_SYNTAX_HIGHLIGHTING_ENABLED = "isSyntaxHighlightingEnabled";
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 LOGO_LANGUAGE = "logoLanguage";
private static final String NUMBER_OF_BACKUPS = "numberOfBackups";
private static final String USER_LIST = "userList";
private static final String LAST_ACTIVE_USER = "lastActiveUser";
+
+ public static StorableObject<WorkspaceConfig, WorkspaceProperty> createOrLoad(File wsDir){
+ StorableObject<WorkspaceConfig, WorkspaceConfig.WorkspaceProperty> 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<WorkspaceConfi
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(NUMBER_OF_BACKUPS, ws.getNumberOfBackups().getValue());
+ json.put(LANGUAGE, ws.getLanguage().getValue());
+ json.put(LOGO_LANGUAGE, ws.getLogoLanguage().getValue());
json.put(IS_USER_CREATION_ALLOWED, ws.isUserCreationAllowed());
JSONObject jsonContestSettings = new JSONObject();
@@ -47,11 +70,12 @@ public class WorkspaceConfigJSONSerializer extends JSONSerializer<WorkspaceConfi
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());
+ 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())
+ .put(IS_SYNTAX_HIGHLIGHTING_ENABLED, ws.isSyntaxHighlightingEnabled());
json.put(SYNTAX_HIGHLIGHTING_STYLES, jsonSyntaxHighlightingStyles);
JSONObject jsonFont = new JSONObject().put(NAME, ws.getFont().getName()).put(STYLE, ws.getFont().getStyle())
@@ -77,11 +101,15 @@ public class WorkspaceConfigJSONSerializer extends JSONSerializer<WorkspaceConfi
}
if (json.has(NUMBER_OF_BACKUPS)) {
- ws.setNumberOfBackups(NumberOfBackups.valueOf(json.getString(NUMBER_OF_BACKUPS)));
+ ws.setNumberOfBackups(NumberOfBackups.valueOf(json.getInt(NUMBER_OF_BACKUPS)));
}
if (json.has(LANGUAGE)) {
- ws.setLanguage(Language.valueOf(json.getString(LANGUAGE)));
+ ws.setLanguage(Language.valueOf(json.getInt(LANGUAGE)));
+ }
+
+ if (json.has(LOGO_LANGUAGE)) {
+ ws.setLogoLanguage(LogoLanguage.valueOf(json.getInt(LOGO_LANGUAGE)));
}
if (json.has(IS_USER_CREATION_ALLOWED)) {
@@ -136,6 +164,9 @@ public class WorkspaceConfigJSONSerializer extends JSONSerializer<WorkspaceConfi
ws.setPrimitiveStyle(jsonContestSettings.getInt(PRIMITIVE_STYLE));
}
+ if (jsonContestSettings.has(IS_SYNTAX_HIGHLIGHTING_ENABLED)) {
+ ws.setSyntaxHighlightingEnabled(jsonContestSettings.getBoolean(IS_SYNTAX_HIGHLIGHTING_ENABLED));
+ }
}
if (json.has(FONT)) {
diff --git a/logo/src/xlogo/storage/workspace/WorkspaceSettingJSONMapper.java b/logo/src/xlogo/storage/workspace/WorkspaceSettingJSONMapper.java
deleted file mode 100644
index 022c288..0000000
--- a/logo/src/xlogo/storage/workspace/WorkspaceSettingJSONMapper.java
+++ /dev/null
@@ -1,165 +0,0 @@
-package xlogo.storage.workspace;
-
-import java.awt.Font;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-import xlogo.storage.JSONSerializer;
-
-public class WorkspaceSettingJSONMapper extends JSONSerializer<WorkspaceConfig> {
-
- 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;
- }
-
-}