[jboss-cvs] jboss-seam/src/main/org/jboss/seam/theme ...
Gavin King
gavin.king at jboss.com
Mon Dec 18 06:52:50 EST 2006
User: gavin
Date: 06/12/18 06:52:50
Modified: src/main/org/jboss/seam/theme ThemeSelector.java
Log:
JBSEAM-610, JBSEAM-536, refactor
Revision Changes Path
1.10 +174 -171 jboss-seam/src/main/org/jboss/seam/theme/ThemeSelector.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: ThemeSelector.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/src/main/org/jboss/seam/theme/ThemeSelector.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -b -r1.9 -r1.10
--- ThemeSelector.java 16 Dec 2006 02:58:13 -0000 1.9
+++ ThemeSelector.java 18 Dec 2006 11:52:50 -0000 1.10
@@ -3,7 +3,6 @@
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
-import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
@@ -12,11 +11,7 @@
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
-import javax.servlet.http.Cookie;
-import javax.servlet.http.HttpServletResponse;
-import org.jboss.seam.log.LogProvider;
-import org.jboss.seam.log.Logging;
import org.jboss.seam.Component;
import org.jboss.seam.ScopeType;
import org.jboss.seam.Seam;
@@ -26,8 +21,12 @@
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.contexts.Contexts;
-import org.jboss.seam.core.AbstractMutable;
+import org.jboss.seam.core.Events;
import org.jboss.seam.core.Locale;
+import org.jboss.seam.core.Messages;
+import org.jboss.seam.core.Selector;
+import org.jboss.seam.log.LogProvider;
+import org.jboss.seam.log.Logging;
/**
* Selects the current user's theme
@@ -38,25 +37,18 @@
@Name("org.jboss.seam.theme.themeSelector")
@Intercept(NEVER)
@Install(precedence=BUILT_IN)
-public class ThemeSelector extends AbstractMutable implements Serializable
+public class ThemeSelector extends Selector
{
private static final LogProvider log = Logging.getLogProvider(ThemeSelector.class);
private String theme;
private String[] availableThemes;
- private boolean cookieEnabled;
- private int cookieMaxAge = 31536000; //1 year
-
@Create
public void initDefaultTheme()
{
- if (cookieEnabled)
- {
- Cookie cookie = (Cookie) FacesContext.getCurrentInstance().getExternalContext()
- .getRequestCookieMap().get("org.jboss.seam.core.Theme");
- if (cookie!=null) theme = cookie.getValue();
- }
+ String themeName = getCookieValue();
+ if (themeName!=null) setTheme(themeName);
if (theme==null)
{
@@ -71,6 +63,17 @@
}
}
+ @Override
+ protected String getCookieName()
+ {
+ return "org.jboss.seam.core.Theme";
+ }
+
+ /**
+ * Recreate the JSF view, using the new theme, and raise the
+ * org.jboss.seam.themeSelected event
+ *
+ */
public void select()
{
Contexts.removeFromAllContexts( Seam.getComponentName(Theme.class) );
@@ -78,25 +81,31 @@
String viewId = facesContext.getViewRoot().getViewId();
UIViewRoot viewRoot = facesContext.getApplication().getViewHandler().createView(facesContext, viewId);
facesContext.setViewRoot(viewRoot);
- if (cookieEnabled)
+
+ setCookieValue( getTheme() );
+
+ if ( Events.exists() )
{
- HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
- Cookie cookie = new Cookie("org.jboss.seam.core.Theme", theme);
- cookie.setMaxAge(cookieMaxAge);
- response.addCookie( cookie );
+ Events.instance().raiseEvent( "org.jboss.seam.themeSelected", getTheme() );
}
}
+ /**
+ * Get a selectable list of available themes for display in the UI
+ */
public List<SelectItem> getThemes()
{
List<SelectItem> selectItems = new ArrayList<SelectItem>(availableThemes.length);
for ( String name: availableThemes )
{
- selectItems.add( new SelectItem(name, name) ); //TODO: allow meaningful name
+ selectItems.add( new SelectItem( name, getLocalizedThemeName(name) ) );
}
return selectItems;
}
+ /**
+ * Get the name of the current theme
+ */
public String getTheme()
{
return theme;
@@ -114,6 +123,9 @@
this.availableThemes = themeNames;
}
+ /**
+ * Get the resource bundle for the theme
+ */
public ResourceBundle getThemeResourceBundle()
{
try
@@ -133,6 +145,18 @@
}
}
+ /**
+ * Get the localized name of the named theme, by looking for
+ * org.jboss.seam.theme.<name> in the Seam resource
+ * bundle
+ */
+ public String getLocalizedThemeName(String name)
+ {
+ String key = "org.jboss.seam.theme." + name;
+ String localizedName = (String) Messages.instance().get(key);
+ return key.equals(localizedName) ? name : localizedName;
+ }
+
public static ThemeSelector instance()
{
if ( !Contexts.isSessionContextActive() )
@@ -147,25 +171,4 @@
return availableThemes;
}
- public boolean isCookieEnabled()
- {
- return cookieEnabled;
- }
-
- public void setCookieEnabled(boolean cookieEnabled)
- {
- setDirty(this.cookieEnabled, cookieEnabled);
- this.cookieEnabled = cookieEnabled;
- }
-
- protected int getCookieMaxAge()
- {
- return cookieMaxAge;
- }
-
- protected void setCookieMaxAge(int cookieMaxAge)
- {
- this.cookieMaxAge = cookieMaxAge;
- }
-
}
More information about the jboss-cvs-commits
mailing list