From 7449f9f6f6af965bb5a1b7abcceaf5d72bc3127f Mon Sep 17 00:00:00 2001
From: Peter Hormanns <peter.hormanns@jalin.de>
Date: Tue, 18 Dec 2012 13:26:59 +0100
Subject: [PATCH] new edit widget pacprefixed-name

---
 hsarweb/src/de/hsadmin/web/UnixUserModule.java                             |   35 ++---------
 hsarweb/src/de/hsadmin/web/config/PropertyConfig.java                      |    2 
 hsarweb/src/texts/home_de.properties                                       |    1 
 hsarweb/src/de/hsadmin/web/vaadin/PasswordPropertyFieldFactory.java        |    3 
 hsarweb/src/texts/user_de.properties                                       |    5 +
 hsarweb/src/de/hsadmin/web/config/LocaleConfig.java                        |    7 +
 hsarweb/src/de/hsadmin/web/vaadin/GenericForm.java                         |    5 +
 hsarweb/src/de/hsadmin/web/vaadin/PacPrefixedNamePropertyFieldFactory.java |  100 +++++++++++++++++++++++++++++++++
 hsarweb/src/texts/user.properties                                          |    3 +
 9 files changed, 126 insertions(+), 35 deletions(-)

diff --git a/hsarweb/src/de/hsadmin/web/UnixUserModule.java b/hsarweb/src/de/hsadmin/web/UnixUserModule.java
index 7cc59e8..243c0c5 100644
--- a/hsarweb/src/de/hsadmin/web/UnixUserModule.java
+++ b/hsarweb/src/de/hsadmin/web/UnixUserModule.java
@@ -9,6 +9,7 @@
 import de.hsadmin.web.config.PropertyDefaultValue;
 import de.hsadmin.web.config.PropertySelectValues;
 import de.hsadmin.web.config.PropertyTableColumn;
+import de.hsadmin.web.vaadin.PacPrefixedNamePropertyFieldFactory;
 import de.hsadmin.web.vaadin.PasswordPropertyFieldFactory;
 import de.hsadmin.web.vaadin.SelectPropertyFieldFactory;
 
@@ -22,10 +23,12 @@
 	protected void initModule() {
 		MainApplication application = getApplication();
 		moduleConfig = new ModuleConfig("user", application.getLocale());
-		String login = application.getRunAs();
-		final String pac = login.length() >= 5 ? login.substring(0, 5) : "";
-		PropertyConfig pacProp = new PropertyConfig(moduleConfig, "pac", String.class, PropertyTableColumn.HIDDEN, new SelectPropertyFieldFactory());
-		pacProp.setSelectValues(new PropertySelectValues() {
+		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
+		idProp.setReadOnly(true);
+		PropertyConfig useridProp = new PropertyConfig(moduleConfig, "userid", Long.class, PropertyTableColumn.HIDDEN);
+		useridProp.setReadOnly(true);
+		PropertyConfig nameProp = new PropertyConfig(moduleConfig, "name", String.class, new PacPrefixedNamePropertyFieldFactory(this));
+		nameProp.setSelectValues(new PropertySelectValues() {
 			@Override
 			public boolean newItemsAllowed() {
 				return false;
@@ -34,6 +37,7 @@
 			public boolean hasSelectList() {
 				return true;
 			}
+			
 			@Override
 			public Map<String, String> getSelectValues() {
 				List<String> list = getPackets();
@@ -44,28 +48,7 @@
 				return map;
 			}
 		});
-		pacProp.setDefaultValue(new PropertyDefaultValue() {
-			@Override
-			public String getDefaultValue() {
-				return pac;
-			}
-		});
-		pacProp.setWriteOnce(true);
-		PropertyConfig idProp = new PropertyConfig(moduleConfig, "id", Long.class, PropertyTableColumn.INTERNAL_KEY);
-		idProp.setReadOnly(true);
-		PropertyConfig useridProp = new PropertyConfig(moduleConfig, "userid", Long.class, PropertyTableColumn.HIDDEN);
-		useridProp.setReadOnly(true);
-		PropertyConfig nameProp = new PropertyConfig(moduleConfig, "name", String.class);
 		nameProp.setWriteOnce(true);
-		nameProp.setDefaultValue(new PropertyDefaultValue() {
-			@Override
-			public String getDefaultValue() {
-				if (pac.length() > 0) {
-					return pac + "-";
-				}
-				return "";
-			}
-		});
 		PropertyConfig passwordProp = new PropertyConfig(moduleConfig, "password", String.class, PropertyTableColumn.NONE, new PasswordPropertyFieldFactory(this));
 		PropertyConfig commentProp = new PropertyConfig(moduleConfig, "comment", String.class);
 		commentProp.setExpandRatio(0.7f);
@@ -116,10 +99,8 @@
 				return "0";
 			}
 		});
-		pacProp.setShowInForm(false);
 		idProp.setShowInForm(false);
 		useridProp.setShowInForm(false);
-		moduleConfig.addProperty(pacProp);
 		moduleConfig.addProperty(idProp);
 		moduleConfig.addProperty(useridProp);
 		moduleConfig.addProperty(nameProp);
diff --git a/hsarweb/src/de/hsadmin/web/config/LocaleConfig.java b/hsarweb/src/de/hsadmin/web/config/LocaleConfig.java
index 82500a8..f1afc31 100644
--- a/hsarweb/src/de/hsadmin/web/config/LocaleConfig.java
+++ b/hsarweb/src/de/hsadmin/web/config/LocaleConfig.java
@@ -1,11 +1,14 @@
 package de.hsadmin.web.config;
 
+import java.io.Serializable;
 import java.util.Locale;
 import java.util.ResourceBundle;
 
-public class LocaleConfig {
+public class LocaleConfig implements Serializable {
 
-	private ResourceBundle bundle;
+	private static final long serialVersionUID = 1L;
+
+	private final ResourceBundle bundle;
 
 	public LocaleConfig(Locale locale, String moduleName) {
 		bundle = ResourceBundle.getBundle("texts." + moduleName, locale);
diff --git a/hsarweb/src/de/hsadmin/web/config/PropertyConfig.java b/hsarweb/src/de/hsadmin/web/config/PropertyConfig.java
index cfe95e9..9d61a8f 100644
--- a/hsarweb/src/de/hsadmin/web/config/PropertyConfig.java
+++ b/hsarweb/src/de/hsadmin/web/config/PropertyConfig.java
@@ -147,5 +147,5 @@
 	public void setExpandRatio(float expandRatio) {
 		this.expandRatio = expandRatio;
 	}
-	
+
 }
diff --git a/hsarweb/src/de/hsadmin/web/vaadin/GenericForm.java b/hsarweb/src/de/hsadmin/web/vaadin/GenericForm.java
index 3c3a308..2124996 100644
--- a/hsarweb/src/de/hsadmin/web/vaadin/GenericForm.java
+++ b/hsarweb/src/de/hsadmin/web/vaadin/GenericForm.java
@@ -89,8 +89,9 @@
 				f.setData(entityId);
 				Layout layout = f.getLayout();
 				for (PropertyConfig prop : config.getPropertyList()) {
-					if (prop.getPropFieldFactory().getClass().equals(DefaultPropertyFieldFactory.class)
-							&& prop.getPropTableColumn().equals(PropertyTableColumn.DISPLAY)) {
+					if (( DefaultPropertyFieldFactory.class.equals(prop.getPropFieldFactory().getClass()) ||
+							PacPrefixedNamePropertyFieldFactory.class.equals(prop.getPropFieldFactory().getClass()))
+								&& prop.getPropTableColumn().equals(PropertyTableColumn.DISPLAY)) {
 						PropertyFieldFactory propFieldFactory = prop.getPropFieldFactory();
 						Object value = row.get(prop.getId());
 						Component component = (Component) propFieldFactory.createFieldComponent(prop, value);
diff --git a/hsarweb/src/de/hsadmin/web/vaadin/PacPrefixedNamePropertyFieldFactory.java b/hsarweb/src/de/hsadmin/web/vaadin/PacPrefixedNamePropertyFieldFactory.java
new file mode 100644
index 0000000..5fb268d
--- /dev/null
+++ b/hsarweb/src/de/hsadmin/web/vaadin/PacPrefixedNamePropertyFieldFactory.java
@@ -0,0 +1,100 @@
+package de.hsadmin.web.vaadin;
+
+import java.util.Map;
+
+import com.vaadin.terminal.Sizeable;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Select;
+import com.vaadin.ui.TextField;
+
+import de.hsadmin.web.HsarwebException;
+import de.hsadmin.web.Module;
+import de.hsadmin.web.config.ModuleConfig;
+import de.hsadmin.web.config.PropertyConfig;
+import de.hsadmin.web.config.PropertyFieldFactory;
+
+
+public class PacPrefixedNamePropertyFieldFactory implements PropertyFieldFactory {
+
+	final private Module module;
+
+	private boolean readOnly = false;
+	private boolean writeOnce = false;
+
+	public PacPrefixedNamePropertyFieldFactory(Module module) {
+		this.module = module;
+	}
+
+	@Override
+	public Object createFieldComponent(PropertyConfig prop, Object value) {
+		ModuleConfig config = module.getModuleConfig();
+		HorizontalLayout layout = new HorizontalLayout();
+		layout.setCaption(prop.getLabel());
+		layout.setData(prop.getId());
+		Select sel = new Select(config.getLabel(prop.getId() + ".pacprefix"));
+		sel.setData(prop.getId());
+		sel.setNullSelectionAllowed(false);
+		sel.setNewItemsAllowed(prop.newItemsAllowed());
+		Map<String, String> selectValues = prop.getSelectValues();
+		for (Object key : selectValues.keySet()) {
+			sel.addItem(key);
+			sel.setItemCaption(key, selectValues.get(key));
+		}
+		sel.setWidth(80.0f, Sizeable.UNITS_PIXELS);
+		sel.setInvalidAllowed(prop.newItemsAllowed());
+		layout.addComponent(sel);
+		
+		TextField separator = new TextField(" ", config.getLabel(prop.getId() + ".separator"));
+		separator.setWidth(18.0f, Sizeable.UNITS_PIXELS);
+		separator.setReadOnly(true);
+		layout.addComponent(separator);
+		
+		TextField tf = new TextField(config.getLabel(prop.getId() + ".postfix"));
+		tf.setData(prop.getId());
+		tf.setWidth(384.0f, Sizeable.UNITS_PIXELS);
+		layout.addComponent(tf);
+		String valueOrDefault = (value != null && value instanceof String) ? ((String) value) : prop.getDefaultValue();
+		if (valueOrDefault.length() >= 5) {
+			sel.setValue(valueOrDefault.substring(0, 5));
+			tf.setValue(valueOrDefault.length() > 6 ? valueOrDefault.substring(6) : "");
+			sel.setReadOnly(readOnly || writeOnce);
+			tf.setReadOnly(readOnly || writeOnce);
+		} else {
+			sel.setReadOnly(readOnly);
+			tf.setReadOnly(readOnly);
+		}
+		return layout;
+	}
+
+	@Override
+	public String getValue(PropertyConfig prop, Object component) throws HsarwebException {
+		ModuleConfig config = module.getModuleConfig();
+		HorizontalLayout layout = (HorizontalLayout) component;
+		Select sel = (Select) layout.getComponent(0);
+		TextField tf = (TextField) layout.getComponent(2);
+		String pacPart = (String) sel.getValue();
+		String userPart = (String) tf.getValue();
+		return pacPart + config.getLabel(prop.getId() + ".separator") + userPart;
+	}
+
+	@Override
+	public void setReadOnly(boolean readOnly) {
+		this.readOnly = readOnly;
+	}
+
+	@Override
+	public boolean isReadOnly() {
+		return readOnly;
+	}
+
+	@Override
+	public void setWriteOnce(boolean writeOnce) {
+		this.writeOnce = writeOnce;
+	}
+
+	@Override
+	public boolean isWriteOnce() {
+		return writeOnce;
+	}
+
+}
diff --git a/hsarweb/src/de/hsadmin/web/vaadin/PasswordPropertyFieldFactory.java b/hsarweb/src/de/hsadmin/web/vaadin/PasswordPropertyFieldFactory.java
index e5ccbf3..f9c32a7 100644
--- a/hsarweb/src/de/hsadmin/web/vaadin/PasswordPropertyFieldFactory.java
+++ b/hsarweb/src/de/hsadmin/web/vaadin/PasswordPropertyFieldFactory.java
@@ -12,7 +12,8 @@
 
 public class PasswordPropertyFieldFactory implements PropertyFieldFactory {
 
-	private Module module;
+	final private Module module;
+
 	private boolean readOnly = false;
 	private boolean writeOnce = false;
 
diff --git a/hsarweb/src/texts/home_de.properties b/hsarweb/src/texts/home_de.properties
index 820fe6e..a950946 100644
--- a/hsarweb/src/texts/home_de.properties
+++ b/hsarweb/src/texts/home_de.properties
@@ -11,7 +11,6 @@
 moduletitle=Passwort
 moduleicon=../runo/icons/16/lock.png
 change_password_icon=../runo/icons/16/lock.png
-hello=Hallo Welt!
 change_password=Passwort �ndern
 update=Passwort �ndern
 save=Passwort speichern
diff --git a/hsarweb/src/texts/user.properties b/hsarweb/src/texts/user.properties
index dae491c..876396e 100644
--- a/hsarweb/src/texts/user.properties
+++ b/hsarweb/src/texts/user.properties
@@ -1,5 +1,8 @@
 id=identifier
 name=username
+name.pacprefix=packet
+name.separator=-
+name.postfix=userpart
 password=password
 password1=new password
 password2=repeat password
diff --git a/hsarweb/src/texts/user_de.properties b/hsarweb/src/texts/user_de.properties
index 424ef35..ccaa229 100644
--- a/hsarweb/src/texts/user_de.properties
+++ b/hsarweb/src/texts/user_de.properties
@@ -1,5 +1,8 @@
 id=Schl�ssel
-name=Benutzername
+name=Benutzer
+name.pacprefix=Paket
+name.separator=-
+name.postfix=Benutzer-Postfix
 password=Passwort
 password1=neues Passwort
 password2=Passwort-Wiederholung

--
Gitblit v1.9.0-SNAPSHOT