From 13513067806bf85fa8cfd3e8702a9f9485e04c00 Mon Sep 17 00:00:00 2001 From: "Eric J. Bowersox" Date: Thu, 22 Jul 2004 09:06:16 +0000 Subject: [PATCH] bugfix to display of community hide mode --- etc/ui-config.xml | 22 +- scripts/comm/create.js | 8 +- scripts/comm/profile.js | 7 +- .../venice/core/CommunityContext.java | 166 ++++---- .../venice/core/CommunityHideMode.java | 165 ++++++++ .../silverwrist/venice/core/UserContext.java | 2 +- .../core/impl/CommunityUserContextImpl.java | 31 +- .../venice/core/impl/UserContextImpl.java | 6 +- .../venice/ui/dlg/DialogElementLoader.java | 8 +- .../venice/ui/dlg/EnumPickListField.java | 363 ++++++++++++++++++ .../venice/ui/dlg/PickListField.java | 41 +- .../venice/ui/dlg/StaticPickListField.java | 31 +- 12 files changed, 704 insertions(+), 146 deletions(-) create mode 100644 src/com/silverwrist/venice/core/CommunityHideMode.java create mode 100644 src/com/silverwrist/venice/ui/dlg/EnumPickListField.java diff --git a/etc/ui-config.xml b/etc/ui-config.xml index 4210f0c..4a39799 100644 --- a/etc/ui-config.xml +++ b/etc/ui-config.xml @@ -652,11 +652,12 @@ the community's host, or via an invitation e-mail message. Please enter it in th Private - - Show in both directory and search - Hide in directory, but not in search - Hide in both directory and search - + + Show in both directory and search + Hide in directory, but not in search + Hide in both directory and search + @@ -688,11 +689,12 @@ the community's host, or via an invitation e-mail message. Please enter it in th - - Show in both directory and search - Hide in directory, but not in search - Hide in both directory and search - + + Show in both directory and search + Hide in directory, but not in search + Hide in both directory and search + diff --git a/scripts/comm/create.js b/scripts/comm/create.js index d69b9e3..c7e6423 100644 --- a/scripts/comm/create.js +++ b/scripts/comm/create.js @@ -8,9 +8,9 @@ // // The Original Code is the Venice Web Communities System. // -// The Initial Developer of the Original Code is Eric J. Bowersox , +// The Initial Developer of the Original Code is Eric J. Bowersox , // for Silverwrist Design Studios. Portions created by Eric J. Bowersox are -// Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. +// Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. // // Contributor(s): @@ -76,12 +76,10 @@ if (op=="create") } // end if - my_hidemode = parseInt(dlg.getValue("hidemode"),10); // get the hide mode - // Create the new community context. new_comm = rinput.user.createCommunity(dlg.getValue("name"),my_alias,dlg.getValue("language"), dlg.getValue("synopsis"),dlg.getValue("rules"),my_joinkey, - my_hidemode); + dlg.getValue("hidemode")); // Get the new community's contact record and fill in the pieces of info we know. ci = new_comm.getContactInfo(); diff --git a/scripts/comm/profile.js b/scripts/comm/profile.js index 8b1cbab..091de3d 100644 --- a/scripts/comm/profile.js +++ b/scripts/comm/profile.js @@ -8,9 +8,9 @@ // // The Original Code is the Venice Web Communities System. // -// The Initial Developer of the Original Code is Eric J. Bowersox , +// The Initial Developer of the Original Code is Eric J. Bowersox , // for Silverwrist Design Studios. Portions created by Eric J. Bowersox are -// Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. +// Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. // // Contributor(s): @@ -102,6 +102,7 @@ if ("GET"==rinput.verb) if (comm.membersOnly) dlg.setValue("membersonly",1); + logger.debug("Community's hidemode is " + comm.hideMode); dlg.setValue("hidemode",comm.hideMode); dlg.setValue("read_lvl",comm.readLevel); dlg.setValue("write_lvl",comm.writeLevel); @@ -193,7 +194,7 @@ if (op=="update") comm.joinKey = null; comm.membersOnly = dlg.getValue("membersonly").booleanValue(); - comm.setHideMode(parseInt(dlg.getValue("hidemode"),10)); + comm.setHideMode(dlg.getValue("hidemode")); comm.setSecurityLevels(parseInt(dlg.getValue("read_lvl"),10),parseInt(dlg.getValue("write_lvl"),10), parseInt(dlg.getValue("create_lvl"),10),parseInt(dlg.getValue("delete_lvl"),10), parseInt(dlg.getValue("join_lvl"),10)); diff --git a/src/com/silverwrist/venice/core/CommunityContext.java b/src/com/silverwrist/venice/core/CommunityContext.java index fc46507..db9e6b4 100644 --- a/src/com/silverwrist/venice/core/CommunityContext.java +++ b/src/com/silverwrist/venice/core/CommunityContext.java @@ -9,9 +9,9 @@ * * The Original Code is the Venice Web Communities System. * - * The Initial Developer of the Original Code is Eric J. Bowersox , + * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are - * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ @@ -27,167 +27,159 @@ import com.silverwrist.venice.except.EmailException; public interface CommunityContext extends SearchMode { - public static final int HIDE_NONE = 0; - public static final int HIDE_DIRECTORY = 1; - public static final int HIDE_BOTH = 2; + public int getCommunityID(); - public abstract int getCommunityID(); + public String getName(); - public abstract String getName(); + public String getAlias(); - public abstract String getAlias(); + public boolean isMember(); - public abstract boolean isMember(); + public boolean isAdmin(); - public abstract boolean isAdmin(); + public boolean isPublicCommunity(); - public abstract boolean isPublicCommunity(); + public int getCategoryID(); - public abstract int getCategoryID(); + public CategoryDescriptor getCategory() throws DataException; - public abstract CategoryDescriptor getCategory() throws DataException; + public String getSynopsis(); - public abstract String getSynopsis(); + public int getHostUID(); - public abstract int getHostUID(); + public UserProfile getHostProfile() throws DataException; - public abstract UserProfile getHostProfile() throws DataException; + public String getLanguageCode(); - public abstract String getLanguageCode(); + public String getLanguageFullName(); - public abstract String getLanguageFullName(); + public String getRules(); - public abstract String getRules(); + public Date getCreationDate(); - public abstract Date getCreationDate(); + public Date getLastAccessDate(); - public abstract Date getLastAccessDate(); + public Date getLastUpdateDate(); - public abstract Date getLastUpdateDate(); + public ContactInfo getContactInfo() throws DataException; - public abstract ContactInfo getContactInfo() throws DataException; + public void putContactInfo(ContactInfo ci) throws DataException, AccessError; - public abstract void putContactInfo(ContactInfo ci) throws DataException, AccessError; + public Set getServices(); - public abstract Set getServices(); + public Set getConfiguredServices(); - public abstract Set getConfiguredServices(); + public void setServiceEnable(ServiceToken token, boolean enable) throws AccessError, DataException; - public abstract void setServiceEnable(ServiceToken token, boolean enable) throws AccessError, DataException; + public void setName(String name) throws DataException, AccessError; - public abstract void setName(String name) throws DataException, AccessError; + public void setAlias(String alias) throws DataException, AccessError; - public abstract void setAlias(String alias) throws DataException, AccessError; + public void setCategoryID(int catid) throws DataException, AccessError; - public abstract void setCategoryID(int catid) throws DataException, AccessError; + public void setCategory(CategoryDescriptor cat) throws DataException, AccessError; - public abstract void setCategory(CategoryDescriptor cat) throws DataException, AccessError; + public void setSynopsis(String synopsis) throws DataException, AccessError; - public abstract void setSynopsis(String synopsis) throws DataException, AccessError; + public void setLanguage(String language) throws DataException, AccessError; - public abstract void setLanguage(String language) throws DataException, AccessError; + public void setRules(String rules) throws DataException, AccessError; - public abstract void setRules(String rules) throws DataException, AccessError; + public CommunityHideMode getHideMode(); - public abstract int getHideMode(); + public void setHideMode(CommunityHideMode mode) throws DataException, AccessError; - public abstract void setHideMode(int mode) throws DataException, AccessError; + public boolean getMembersOnly(); - public abstract boolean getMembersOnly(); + public void setMembersOnly(boolean flag) throws DataException, AccessError; - public abstract void setMembersOnly(boolean flag) throws DataException, AccessError; + public ServiceToken getDefaultService(); - public abstract ServiceToken getDefaultService(); + public void setDefaultService(ServiceToken token) throws AccessError, DataException; - public abstract void setDefaultService(ServiceToken token) throws AccessError, DataException; + public String getJoinKey() throws DataException, AccessError; - public abstract String getJoinKey() throws DataException, AccessError; + public void setJoinKey(String key) throws DataException, AccessError; - public abstract void setJoinKey(String key) throws DataException, AccessError; + public int getReadLevel() throws DataException, AccessError; - public abstract int getReadLevel() throws DataException, AccessError; + public int getWriteLevel() throws DataException, AccessError; - public abstract int getWriteLevel() throws DataException, AccessError; + public int getCreateLevel() throws DataException, AccessError; - public abstract int getCreateLevel() throws DataException, AccessError; + public int getDeleteLevel() throws DataException, AccessError; - public abstract int getDeleteLevel() throws DataException, AccessError; + public int getJoinLevel() throws DataException, AccessError; - public abstract int getJoinLevel() throws DataException, AccessError; - - public abstract void setSecurityLevels(int read, int write, int create, int delete, int join) + public void setSecurityLevels(int read, int write, int create, int delete, int join) throws DataException, AccessError; - public abstract boolean canAdministerCommunity(); + public boolean canAdministerCommunity(); - public abstract boolean canModifyProfile(); + public boolean canModifyProfile(); - public abstract boolean isAdminCommunity(); + public boolean isAdminCommunity(); - public abstract void join(String joinkey) throws DataException, AccessError; + public void join(String joinkey) throws DataException, AccessError; - public abstract void unjoin() throws DataException, AccessError; + public void unjoin() throws DataException, AccessError; - public abstract int getMemberCount() throws DataException; + public int getMemberCount() throws DataException; - public abstract boolean canUnjoin(); + public boolean canUnjoin(); - public abstract boolean canJoin(); + public boolean canJoin(); - public abstract List getConferences() throws DataException, AccessError; + public List getConferences() throws DataException, AccessError; - public abstract ConferenceContext getConferenceContext(int confid) throws DataException, AccessError; + public ConferenceContext getConferenceContext(int confid) throws DataException, AccessError; - public abstract ConferenceContext getConferenceContext(String alias) throws DataException, AccessError; + public ConferenceContext getConferenceContext(String alias) throws DataException, AccessError; - public abstract ConferenceContext createConference(String name, String alias, String description, - boolean pvt, boolean hide_list) - throws DataException, AccessError; + public ConferenceContext createConference(String name, String alias, String description, + boolean pvt, boolean hide_list) throws DataException, AccessError; - public abstract boolean canCreateConference(); + public boolean canCreateConference(); - public abstract List searchForMembers(int field, int mode, String term, int offset, int count) - throws DataException; + public List searchForMembers(int field, int mode, String term, int offset, int count) throws DataException; - public abstract int getSearchMemberCount(int field, int mode, String term) throws DataException; + public int getSearchMemberCount(int field, int mode, String term) throws DataException; - public abstract List getMemberList() throws DataException; + public List getMemberList() throws DataException; - public abstract boolean canManageConferences(); + public boolean canManageConferences(); - public abstract int getMemberLevel(int uid) throws DataException, AccessError; + public int getMemberLevel(int uid) throws DataException, AccessError; - public abstract void setMembership(int uid, int new_level) - throws DataException, AccessError; + public void setMembership(int uid, int new_level) throws DataException, AccessError; - public abstract boolean canDelete(); + public boolean canDelete(); - public abstract void delete() throws DataException, AccessError; + public void delete() throws DataException, AccessError; - public abstract void deleteCommunity() throws DataException, AccessError; + public void deleteCommunity() throws DataException, AccessError; - public abstract void sendInvitation(String address, String personal_message) + public void sendInvitation(String address, String personal_message) throws AccessError, DataException, EmailException; - public abstract boolean canSendInvitation(); + public boolean canSendInvitation(); - public abstract List getAuditRecords(int offset, int count) throws AccessError, DataException; + public List getAuditRecords(int offset, int count) throws AccessError, DataException; - public abstract int getAuditRecordCount() throws AccessError, DataException; + public int getAuditRecordCount() throws AccessError, DataException; - public abstract CommunityProperties getProperties() throws DataException, AccessError; + public CommunityProperties getProperties() throws DataException, AccessError; - public abstract void setProperties(CommunityProperties props) throws DataException, AccessError; + public void setProperties(CommunityProperties props) throws DataException, AccessError; - public abstract SecurityInfo getSecurityInfo(); + public SecurityInfo getSecurityInfo(); - public abstract boolean canMassMail(); + public boolean canMassMail(); - public abstract void massMail(String subject, String text) throws AccessError, DataException; + public void massMail(String subject, String text) throws AccessError, DataException; - public abstract List searchPosts(String search_terms, int offset, int count) - throws AccessError, DataException; + public List searchPosts(String search_terms, int offset, int count) throws AccessError, DataException; - public abstract int getSearchPostCount(String search_terms) throws AccessError, DataException; + public int getSearchPostCount(String search_terms) throws AccessError, DataException; } // end interface CommunityContext diff --git a/src/com/silverwrist/venice/core/CommunityHideMode.java b/src/com/silverwrist/venice/core/CommunityHideMode.java new file mode 100644 index 0000000..8b73361 --- /dev/null +++ b/src/com/silverwrist/venice/core/CommunityHideMode.java @@ -0,0 +1,165 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.core; + +import java.util.*; + +public final class CommunityHideMode implements Comparable +{ + /*-------------------------------------------------------------------------------- + * Static data members + *-------------------------------------------------------------------------------- + */ + + private static ArrayList s_order_list = null; + private static HashMap s_name_to_enum = null; + + /*-------------------------------------------------------------------------------- + * Attributes + *-------------------------------------------------------------------------------- + */ + + private String m_name; + private String m_to_string; + + /*-------------------------------------------------------------------------------- + * Constructor + *-------------------------------------------------------------------------------- + */ + + private CommunityHideMode(String name) + { + m_name = name; + if (s_order_list==null) + s_order_list = new ArrayList(); + s_order_list.add(this); + if (s_name_to_enum==null) + s_name_to_enum = new HashMap(); + s_name_to_enum.put(name,this); + m_to_string = "CommunityHideMode[" + name + "]"; + + } // end constructor + + /*-------------------------------------------------------------------------------- + * Overrides from class Object + *-------------------------------------------------------------------------------- + */ + + public String toString() + { + return m_to_string; + + } // end toString + + public int hashCode() + { + return m_name.hashCode(); + + } // end hashCode + + public boolean equals(Object other) + { + if (other==null) + return false; + if (other==this) + return true; + if (!(other instanceof CommunityHideMode)) + return false; + return m_name.equals(((CommunityHideMode)other).m_name); + + } // end equals + + /*-------------------------------------------------------------------------------- + * Implementations from class Comparable + *-------------------------------------------------------------------------------- + */ + + public int compareTo(Object other) + { + if (other==null) + throw new NullPointerException(); + if (!(other instanceof CommunityHideMode)) + throw new ClassCastException(); + return m_name.compareTo(((CommunityHideMode)other).m_name); + + } // end compareTo + + /*-------------------------------------------------------------------------------- + * External operations + *-------------------------------------------------------------------------------- + */ + + public Class getEnumClass() + { + return CommunityHideMode.class; + + } // end getEnumClass + + public String getName() + { + return m_name; + + } // end getName + + /*-------------------------------------------------------------------------------- + * External static operations + *-------------------------------------------------------------------------------- + */ + + public static CommunityHideMode getEnum(String name) + { + if (s_name_to_enum==null) + return null; + return (CommunityHideMode)(s_name_to_enum.get(name)); + + } // end getEnum + + public static Map getEnumMap() + { + if (s_name_to_enum==null) + return Collections.EMPTY_MAP; + else + return Collections.unmodifiableMap(s_name_to_enum); + + } // end getEnumMap + + public static List getEnumList() + { + if (s_order_list==null) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(s_order_list); + + } // end getEnumList + + public static Iterator iterator() + { + return getEnumList().iterator(); + + } // end iterator + + /*-------------------------------------------------------------------------------- + * Enumerated type values + *-------------------------------------------------------------------------------- + */ + + public static final CommunityHideMode NONE = new CommunityHideMode("NONE"); + public static final CommunityHideMode DIRECTORY = new CommunityHideMode("DIRECTORY"); + public static final CommunityHideMode BOTH = new CommunityHideMode("BOTH"); + +} // end class CommunityHideMode diff --git a/src/com/silverwrist/venice/core/UserContext.java b/src/com/silverwrist/venice/core/UserContext.java index 94228f6..8fb5e1a 100644 --- a/src/com/silverwrist/venice/core/UserContext.java +++ b/src/com/silverwrist/venice/core/UserContext.java @@ -85,7 +85,7 @@ public interface UserContext extends SearchMode public int getSearchCategoryCount(int mode, String term) throws DataException; public CommunityContext createCommunity(String name, String alias, String language, String synopsis, - String rules, String joinkey, int hide_mode) + String rules, String joinkey, CommunityHideMode hide_mode) throws DataException, AccessError; public boolean canCreateCommunity(); diff --git a/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java b/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java index 35737aa..0553675 100644 --- a/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/CommunityUserContextImpl.java @@ -655,29 +655,28 @@ class CommunityUserContextImpl implements CommunityContext, CommunityBackend } // end setRules - public int getHideMode() + public CommunityHideMode getHideMode() { CommunityData d = getDataNE(); + CommunityHideMode rc = null; if (d==null) - return HIDE_NONE; - if (d.getHideSearch()) - return HIDE_BOTH; + rc = CommunityHideMode.NONE; + else if (d.getHideSearch()) + rc = CommunityHideMode.BOTH; else if (d.getHideDirectory()) - return HIDE_DIRECTORY; + rc = CommunityHideMode.DIRECTORY; else - return HIDE_NONE; + rc = CommunityHideMode.NONE; + if (logger.isDebugEnabled()) + logger.debug("getHideMode() => " + rc); + return rc; } // end getHideMode - public void setHideMode(int mode) throws DataException, AccessError + public void setHideMode(CommunityHideMode mode) throws DataException, AccessError { - if ((mode!=HIDE_NONE) && (mode!=HIDE_DIRECTORY) && (mode!=HIDE_BOTH)) - { // the mode is not valid - logger.error("mode value " + mode + " is not valid"); - throw new IllegalArgumentException("invalid hide mode value"); - - } // end if - + if (logger.isDebugEnabled()) + logger.debug("setHideMode(" + mode + ") entry"); getData().testMembership(level,is_member); if (!(getData().canModifyCommunityProfile(level))) { // this user can't modify the community feature set @@ -693,8 +692,8 @@ class CommunityUserContextImpl implements CommunityContext, CommunityBackend } // end if - boolean hide_dir = (mode!=HIDE_NONE); - boolean hide_search = (mode==HIDE_BOTH); + boolean hide_dir = (CommunityHideMode.NONE.equals(mode) ? false : true); + boolean hide_search = CommunityHideMode.BOTH.equals(mode); getData().setHideFlags(env,hide_dir,hide_search); } // end setHideMode diff --git a/src/com/silverwrist/venice/core/impl/UserContextImpl.java b/src/com/silverwrist/venice/core/impl/UserContextImpl.java index c6c014c..1027d59 100644 --- a/src/com/silverwrist/venice/core/impl/UserContextImpl.java +++ b/src/com/silverwrist/venice/core/impl/UserContextImpl.java @@ -987,14 +987,14 @@ class UserContextImpl implements UserContext, ServiceProvider, PropertyProvider } // end getSearchCategoryCount public CommunityContext createCommunity(String name, String alias, String language, String synopsis, - String rules, String joinkey, int hide_mode) + String rules, String joinkey, CommunityHideMode hide_mode) throws DataException, AccessError { env.testPermission(EnvUser.PERM_CREATECOMMUNITY,"You are not authorized to create new communities."); // Convert the "hide mode" value into the two hide flags. - boolean hide_dir = (hide_mode!=CommunityContext.HIDE_NONE); - boolean hide_search = (hide_mode==CommunityContext.HIDE_BOTH); + boolean hide_dir = (CommunityHideMode.NONE.equals(hide_mode) ? false : true); + boolean hide_search = CommunityHideMode.BOTH.equals(hide_mode); // Create the new community's database entries and internal data. CommunityData new_comm = CommunityCoreData.createCommunity(globalsite,env,name,alias,uid,language, diff --git a/src/com/silverwrist/venice/ui/dlg/DialogElementLoader.java b/src/com/silverwrist/venice/ui/dlg/DialogElementLoader.java index 0866b5b..f66ed1b 100644 --- a/src/com/silverwrist/venice/ui/dlg/DialogElementLoader.java +++ b/src/com/silverwrist/venice/ui/dlg/DialogElementLoader.java @@ -37,10 +37,10 @@ public class DialogElementLoader private static final Class[] init_classes = { CategoryHeader.class, CheckBoxField.class, CommunityLogoField.class, CountryListField.class, - DateField.class, EMailAddressField.class, HiddenField.class, ImageButton.class, IntegerField.class, - IPAddressField.class, LanguageListField.class, LocaleListField.class, PasswordField.class, - RoleListField.class, StaticPickListField.class, TextField.class, TimeZoneListField.class, - UserPhotoField.class, VeniceIDField.class + DateField.class, EMailAddressField.class, EnumPickListField.class, HiddenField.class, + ImageButton.class, IntegerField.class, IPAddressField.class, LanguageListField.class, + LocaleListField.class, PasswordField.class, RoleListField.class, StaticPickListField.class, + TextField.class, TimeZoneListField.class, UserPhotoField.class, VeniceIDField.class }; /*-------------------------------------------------------------------------------- diff --git a/src/com/silverwrist/venice/ui/dlg/EnumPickListField.java b/src/com/silverwrist/venice/ui/dlg/EnumPickListField.java new file mode 100644 index 0000000..b1586e1 --- /dev/null +++ b/src/com/silverwrist/venice/ui/dlg/EnumPickListField.java @@ -0,0 +1,363 @@ +/* + * The contents of this file are subject to the Mozilla Public License Version 1.1 + * (the "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at . + * + * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT + * WARRANTY OF ANY KIND, either express or implied. See the License for the specific + * language governing rights and limitations under the License. + * + * The Original Code is the Venice Web Communities System. + * + * The Initial Developer of the Original Code is Eric J. Bowersox , + * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are + * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * + * Contributor(s): + */ +package com.silverwrist.venice.ui.dlg; + +import java.io.IOException; +import java.lang.reflect.*; +import java.util.*; +import org.w3c.dom.*; +import com.silverwrist.util.*; +import com.silverwrist.venice.except.*; +import com.silverwrist.venice.ui.*; +import com.silverwrist.venice.util.XMLLoader; + +public class EnumPickListField extends PickListField +{ + /*-------------------------------------------------------------------------------- + * Internal class for translating between strings and enums + *-------------------------------------------------------------------------------- + */ + + static class EnumExtractor + { + /*==================================================================== + * Attributes + *==================================================================== + */ + + private Class m_klass; + private Method m_getname; + private Map m_valmap; + private List m_list; + + /*==================================================================== + * Constructor + *==================================================================== + */ + + EnumExtractor(String klassname) + { + try + { // load the class + m_klass = Class.forName(klassname); + + // get the "getName" method + m_getname = m_klass.getMethod("getName",NO_PARAMS); + + // get the "getEnumMap" method and call it to get the value map + Method m = m_klass.getMethod("getEnumMap",NO_PARAMS); + m_valmap = (Map)(m.invoke(null,NO_ARGS)); + + // get the "getEnumList" method and call it to get the value list + m = m_klass.getMethod("getEnumList",NO_PARAMS); + m_list = (List)(m.invoke(null,NO_ARGS)); + + } // end try + catch (ClassNotFoundException e) + { // bounce me + throw new VeniceRuntimeException("class not found: " + klassname,e); + + } // end catch + catch (NoSuchMethodException e) + { // bounce me + throw new VeniceRuntimeException("method failure",e); + + } // end catch + catch (IllegalAccessException e) + { // bounce me + throw new VeniceRuntimeException("illegal access",e); + + } // end catch + catch (IllegalArgumentException e) + { // bounce me + throw new VeniceRuntimeException("illegal argument",e); + + } // end catch + catch (InvocationTargetException e) + { // an exception got thrown + throw new VeniceRuntimeException("target threw something",e); + + } // end catch + + } // end constructor + + /*==================================================================== + * External operations + *==================================================================== + */ + + final boolean isInstance(Object o) + { + return m_klass.isInstance(o); + + } // end isInstance + + final String getNameFromEnum(Object enum) + { + try + { // invoke the method to return the name + return (String)(m_getname.invoke(enum,NO_ARGS)); + + } // end try + catch (IllegalAccessException e) + { // bounce me + throw new VeniceRuntimeException("illegal access",e); + + } // end catch + catch (IllegalArgumentException e) + { // bounce me + throw new VeniceRuntimeException("illegal argument",e); + + } // end catch + catch (InvocationTargetException e) + { // an exception got thrown + throw new VeniceRuntimeException("target threw something",e); + + } // end catch + + } // end getNameFromEnum + + final Object getEnumFromName(String name) + { + return m_valmap.get(name); + + } // end getEnumFromName + + } // end class EnumExtractor + + /*-------------------------------------------------------------------------------- + * Internal class holding choices + *-------------------------------------------------------------------------------- + */ + + static class PickListChoice + { + /*==================================================================== + * Attributes + *==================================================================== + */ + + private String m_name; + private String m_value; + + /*==================================================================== + * Constructor + *==================================================================== + */ + + PickListChoice(String name, String value) + { + m_name = name; + m_value = value; + + } // end constructor + + /*==================================================================== + * External operations + *==================================================================== + */ + + final String getName() + { + return m_name; + + } // end getName + + final String getValue() + { + return m_value; + + } // end getValue + + } // end class PickListChoice + + /*-------------------------------------------------------------------------------- + * Static data members + *-------------------------------------------------------------------------------- + */ + + public static final String TAGNAME = "enumlist"; + + private static final Class[] NO_PARAMS = new Class[0]; + private static final Object[] NO_ARGS = new Object[0]; + + /*-------------------------------------------------------------------------------- + * Attributes + *-------------------------------------------------------------------------------- + */ + + private EnumExtractor m_extractor; + + /*-------------------------------------------------------------------------------- + * Constructors + *-------------------------------------------------------------------------------- + */ + + public EnumPickListField(String name, String caption, String caption2, boolean required, String classname, + String clist) + { + super(name,caption,caption2,required,Collections.EMPTY_LIST); + setChoicesList(createChoiceListFromString(classname,clist)); + + } // end constructor + + public EnumPickListField(Element elt) throws ConfigException + { + super(elt,Collections.EMPTY_LIST); + setChoicesList(createChoiceListFromXML(elt)); + + } // end constructor + + protected EnumPickListField(EnumPickListField other) + { + super(other); + m_extractor = other.m_extractor; + + } // end constructor + + /*-------------------------------------------------------------------------------- + * Internal operations + *-------------------------------------------------------------------------------- + */ + + private final List createChoiceListFromString(String classname, String s) + { + m_extractor = new EnumExtractor(classname); + char separator = s.charAt(0); + String work = s.substring(1); + ArrayList tmp = new ArrayList(); + while (work!=null) + { // split up the "choice list" string into name/value pairs + int p = work.indexOf(separator); + if (p<0) + break; + String n1 = work.substring(0,p); + work = work.substring(p+1); + p = work.indexOf(separator); + String v1; + if (p<0) + { // take the rest of the string + v1 = work; + work = null; + + } // end if + else + { // chop the string off the end + v1 = work.substring(0,p); + work = work.substring(p+1); + if (work.length()==0) + work = null; + + } // end else + + if (m_extractor.getEnumFromName(n1)!=null) + tmp.add(new PickListChoice(n1,v1)); + + } // end while + + tmp.trimToSize(); + if (tmp.isEmpty()) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(tmp); + + } // end createChoiceListFromString + + private final List createChoiceListFromXML(Element elt) throws ConfigException + { + XMLLoader loader = XMLLoader.get(); + m_extractor = new EnumExtractor(loader.configGetAttribute(elt,"enumClass")); + NodeList nl = elt.getChildNodes(); + ArrayList tmp = new ArrayList(); + for (int i=0; i element here + loader.configVerifyNodeName(n,"choice",elt); + String n1 = loader.configGetAttribute((Element)n,"id"); + String v1 = loader.configGetText((Element)n); + if (m_extractor.getEnumFromName(n1)!=null) + tmp.add(new PickListChoice(n1,v1)); + + } // end if + // else just ignore that + + } // end for + + tmp.trimToSize(); + if (tmp.isEmpty()) + return Collections.EMPTY_LIST; + else + return Collections.unmodifiableList(tmp); + + } // end createChoiceListFromXML + + /*-------------------------------------------------------------------------------- + * Overrides from class PickListField + *-------------------------------------------------------------------------------- + */ + + protected String getChoiceName(Object o) + { + return ((PickListChoice)o).getName(); + + } // end getChoiceName + + protected String getChoiceValue(Object o) + { + return ((PickListChoice)o).getValue(); + + } // end getChoiceValue + + /*-------------------------------------------------------------------------------- + * Implementations from interface DialogField + *-------------------------------------------------------------------------------- + */ + + public Object getValue() + { + Object o = super.getValue(); + if (o==null) + return null; + return m_extractor.getEnumFromName(o.toString()); + + } // end getValue + + public void setValue(Object o) + { + if (o==null) + { // null value + super.setValue(null); + return; + + } // end if + + if (!(m_extractor.isInstance(o))) + throw new IllegalArgumentException("not a valid enum instance"); + super.setValue(m_extractor.getNameFromEnum(o)); + + } // end setValue + + public DialogField duplicate() + { + return new EnumPickListField(this); + + } // end duplicate + +} // end class EnumPickListField diff --git a/src/com/silverwrist/venice/ui/dlg/PickListField.java b/src/com/silverwrist/venice/ui/dlg/PickListField.java index a4fd034..e23baef 100644 --- a/src/com/silverwrist/venice/ui/dlg/PickListField.java +++ b/src/com/silverwrist/venice/ui/dlg/PickListField.java @@ -19,6 +19,7 @@ package com.silverwrist.venice.ui.dlg; import java.io.IOException; import java.util.*; +import org.apache.log4j.*; import org.w3c.dom.*; import com.silverwrist.util.*; import com.silverwrist.venice.except.*; @@ -26,12 +27,19 @@ import com.silverwrist.venice.ui.*; public abstract class PickListField extends BaseDialogField { + /*-------------------------------------------------------------------------------- + * Static data members + *-------------------------------------------------------------------------------- + */ + + private static Logger logger = Logger.getLogger(PickListField.class); + /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ - private List choices; + private List m_choices; /*-------------------------------------------------------------------------------- * Constructors @@ -41,21 +49,21 @@ public abstract class PickListField extends BaseDialogField protected PickListField(String name, String caption, String caption2, boolean required, List choices) { super(false,name,caption,caption2,required); - this.choices = choices; + m_choices = choices; } // end constructor protected PickListField(Element elt, List choices) throws ConfigException { super(false,elt,true,true); - this.choices = choices; + m_choices = choices; } // end constructor protected PickListField(PickListField other) { super(other); - this.choices = other.choices; + m_choices = other.m_choices; } // end constructor @@ -79,18 +87,33 @@ public abstract class PickListField extends BaseDialogField if (!isEnabled()) out.write(" disabled=\"disabled\""); out.write(">\n"); - Iterator it = choices.iterator(); String val = getStringValue(); - while (it.hasNext()) + boolean not_selected = true; + for (Iterator it=m_choices.iterator(); it.hasNext(); ) { // loop through the list and render the choices Object ch = it.next(); String ch_name = getChoiceName(ch); out.write("\n"); - } // end while + } // end for + + if (not_selected && isEnabled()) + { // aw, shit! there's no selected item! + logger.warn("PickListField \"" + getName() + "\" did not find a match for selected value \"" + val + "\""); + ArrayList tmp = new ArrayList(m_choices.size()); + for (Iterator it=m_choices.iterator(); it.hasNext(); ) + tmp.add(getChoiceName(it.next())); + logger.warn("Possible choices were: " + tmp); + + } // end if out.write("\n"); // all done @@ -103,13 +126,13 @@ public abstract class PickListField extends BaseDialogField public List getChoicesList() { - return choices; + return m_choices; } // end getChoicesList public void setChoicesList(List list) { - this.choices = list; + m_choices = list; } // end setChoicesList diff --git a/src/com/silverwrist/venice/ui/dlg/StaticPickListField.java b/src/com/silverwrist/venice/ui/dlg/StaticPickListField.java index b7a4f5a..6d949cc 100644 --- a/src/com/silverwrist/venice/ui/dlg/StaticPickListField.java +++ b/src/com/silverwrist/venice/ui/dlg/StaticPickListField.java @@ -9,9 +9,9 @@ * * The Original Code is the Venice Web Communities System. * - * The Initial Developer of the Original Code is Eric J. Bowersox , + * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are - * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. + * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ @@ -34,25 +34,40 @@ public class StaticPickListField extends PickListField static class PickListChoice { - private String name; - private String value; + /*==================================================================== + * Attributes + *==================================================================== + */ + + private String m_name; + private String m_value; + + /*==================================================================== + * Constructor + *==================================================================== + */ PickListChoice(String name, String value) { - this.name = name; - this.value = value; + m_name = name; + m_value = value; } // end constructor + /*==================================================================== + * External operations + *==================================================================== + */ + final String getName() { - return name; + return m_name; } // end getName final String getValue() { - return value; + return m_value; } // end getValue