/****************************************************************************
 * Copyright (C) 2013 HS Coburg.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.richclient.gui.manage.addon;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import org.openecard.addon.manifest.AddonSpecification;
import org.openecard.addon.manifest.ConfigurationEntry;
import org.openecard.addon.manifest.EnumEntry;
import org.openecard.addon.manifest.EnumListEntry;
import org.openecard.addon.manifest.ScalarEntry;
import org.openecard.addon.manifest.ScalarListEntry;
import org.openecard.common.util.FileUtils;
import org.openecard.richclient.gui.manage.SettingsGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * SettingsGroup that can be used as default.
 * For every ConfigurationEntry in the given AddonSpecification an according item will be added.
 *
 * @author Dirk Petrautzki <dirk.petrautzki@hs-coburg.de>
 */
public class DefaultSettingsGroup extends SettingsGroup {

    private static final long serialVersionUID = 1L;
    private static final Logger logger = LoggerFactory.getLogger(DefaultSettingsGroup.class);

    private static final String LANGUAGE_CODE = System.getProperty("user.language");

    private AddonSpecification desc;

    public DefaultSettingsGroup(String title, Properties properties, AddonSpecification desc) {
	super(title, properties);
	this.desc = desc;
	for (ConfigurationEntry entry : desc.getConfigDescription().getEntries()) {
	    String name = entry.getLocalizedName(LANGUAGE_CODE);
	    String description = entry.getLocalizedDescription(LANGUAGE_CODE);

	    // match entry types with class, else the type hierarchy is implicit in the if an that is a bad thing
	    if (ScalarEntry.class.equals(entry.getClass())) {
		ScalarEntry scalarEntry = (ScalarEntry) entry;
		if (scalarEntry.getType().equalsIgnoreCase("string")) {
		    addInputItem(name, description, entry.getKey());
		} else if (scalarEntry.getType().equalsIgnoreCase("boolean")) {
		    addBoolItem(name, description, entry.getKey());
		} else {
		    logger.error("Untreated ScalarEntry type: {}", scalarEntry.getType());
		}
	    } else if (ScalarListEntry.class.equals(entry.getClass())) {
		addScalarListItem(name, description, entry.getKey());
	    } else if (EnumEntry.class.equals(entry.getClass())) {
		EnumEntry enumEntry = (EnumEntry) entry;
		List<String> values = enumEntry.getValues();
		// TODO: implement function
		//addMultiSelectionItem(name, description, entry.getKey(), values);
	    } else if (EnumListEntry.class.equals(entry.getClass())) {
		EnumListEntry enumEntry = (EnumListEntry) entry;
		List<String> values = enumEntry.getValues();
		// TODO: use correct function
		// addMultiSelectionItem(name, description, entry.getKey(), values);
		logger.error("Yet unsupported entry type: {}", entry.getClass().getName());
	    } else {
		logger.error("Untreated entry type: {}", entry.getClass().getName());
	    }
	    // TODO: implement missing types EnumEntry, EnumListEntry
	}
    }

    @Override
    protected void saveProperties() throws IOException, SecurityException {
	File home = FileUtils.getHomeConfigDir();
	File path = new File(home, "addons");
	path = new File(path, desc.getId());
	File config = new File(path, "settings" + ".properties");
	FileWriter writer = new FileWriter(config);
	properties.store(writer, null);
    }

}

@@ -92,16 +92,21 @@
 (C) 2013
+-2014
  HS Cobu
@@ -954,56 +954,8 @@
 n;%0A%0A
-import java.io.File;%0Aimport java.io.FileWriter;%0A
 impo
@@ -1012,80 +1012,48 @@
 ort 
-java.util.Properties;%0Aimport org.openecard.addon.manifest.AddonSpecifica
+org.openecard.addon.AddonPropertiesExcep
 tion
@@ -1335,28 +1335,53 @@
 ard.
-common.util.FileUtil
+richclient.gui.manage.SettingsFactory.Setting
 s;%0Ai
@@ -1704,16 +1704,74 @@
 urg.de%3E%0A
+ * @author Hans-Martin Haase %3Chans-martin.haase@ecsec.de%3E%0A
  */%0Apubl
@@ -1972,17 +1972,16 @@
 class);%0A
-%0A
     priv
@@ -2062,46 +2062,8 @@
 );%0A%0A
-    private AddonSpecification desc;%0A%0A
     
@@ -2108,54 +2108,65 @@
 le, 
-Properties properties, AddonSpecification d
+Settings settings, List%3CConfigurationEntry%3E configEntri
 es
-c
 ) %7B%0A
@@ -2183,38 +2183,17 @@
 le, 
-properties);%0A%09this.desc = desc
+settings)
 ;%0A%09f
@@ -2227,48 +2227,21 @@
 y : 
-desc.getConfigDescription().get
+config
 Entries
-()
 ) %7B%0A
@@ -3185,82 +3185,123 @@
 ;%0A%09%09
-// TODO: implement function%0A%09%09//addMultiSelectionItem(name, description, e
+addMultiSelectionItem(entry.getLocalizedName(LANGUAGE_CODE), entry.getLocalizedDescription(LANGUAGE_CODE),%0A%09%09%09enumE
 ntry
@@ -3488,85 +3488,131 @@
 ;%0A%09%09
-// TODO: use correct function%0A%09%09// addMultiSelectionItem(name, description, e
+addMultiSelectionItem(enumEntry.getLocalizedName(LANGUAGE_CODE), enumEntry.getLocalizedDescription(LANGUAGE_CODE),%0A%09%09%09enumE
 ntry
@@ -3806,71 +3806,8 @@
   %7D%0A
-%09    // TODO: implement missing types EnumEntry, EnumListEntry%0A
 %09%7D%0A 
@@ -3903,265 +3903,59 @@
 tion
- %7B%0A%09File home = FileUtils.getHomeConfigDir();%0A%09File path = new File(home, %22addons%22);%0A%09path = new File(path, desc.getId());%0A%09File config = new File(path, %22settings%22 + %22.properties%22);%0A%09FileWriter writer = new FileWriter(config);%0A%09properties.store(writer, null
+, AddonPropertiesException %7B%0A%09super.saveProperties(
 );%0A 

Fix the creation of the DefaultSettingsGroup.