This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/v2 by this push:
     new f2b675f  ISIS-2047: ThemeChooser: support for custom ThemeProviders
f2b675f is described below

commit f2b675f1866d770cc9aae672bfe2635ba95835d6
Author: Andi Huber <ahu...@apache.org>
AuthorDate: Wed Dec 5 16:50:45 2018 +0100

    ISIS-2047: ThemeChooser: support for custom ThemeProviders
    
    Task-Url: https://issues.apache.org/jira/browse/ISIS-2047
---
 .../wicket/viewer/IsisWicketApplication.java       |  26 +---
 .../themepicker/IsisWicketThemeSupport.java        |  86 +++++++++++
 .../themepicker/IsisWicketThemeSupportDefault.java | 164 +++++++++++++++++++++
 .../widgets/themepicker/ThemeChooser.java          | 107 ++------------
 4 files changed, 272 insertions(+), 111 deletions(-)

diff --git 
a/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
 
b/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
index c3fd038..e1f4287 100644
--- 
a/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
+++ 
b/core/viewer-wicket-impl/src/main/java/org/apache/isis/viewer/wicket/viewer/IsisWicketApplication.java
@@ -33,7 +33,6 @@ import com.google.common.base.Charsets;
 import com.google.common.io.Resources;
 import com.google.inject.Guice;
 import com.google.inject.Injector;
-import com.google.inject.Module;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.ConverterLocator;
@@ -92,6 +91,7 @@ import 
org.apache.isis.viewer.wicket.ui.components.actionmenu.entityactions.Addi
 import 
org.apache.isis.viewer.wicket.ui.components.scalars.string.MultiLineStringPanel;
 import 
org.apache.isis.viewer.wicket.ui.components.widgets.select2.Select2BootstrapCssReference;
 import 
org.apache.isis.viewer.wicket.ui.components.widgets.select2.Select2JsReference;
+import 
org.apache.isis.viewer.wicket.ui.components.widgets.themepicker.IsisWicketThemeSupport;
 import org.apache.isis.viewer.wicket.ui.pages.PageClassRegistry;
 import org.apache.isis.viewer.wicket.ui.pages.PageClassRegistryAccessor;
 import org.apache.isis.viewer.wicket.ui.pages.accmngt.AccountConfirmationMap;
@@ -105,13 +105,12 @@ import 
org.apache.isis.viewer.wicket.viewer.integration.wicket.WebRequestCycleFo
 import org.apache.isis.viewer.wicket.viewer.settings.IsisResourceSettings;
 
 import static java.util.Objects.requireNonNull;
+import static org.apache.isis.commons.internal.base._With.acceptIfPresent;
 
 import de.agilecoders.wicket.core.Bootstrap;
 import 
de.agilecoders.wicket.core.markup.html.bootstrap.behavior.BootstrapBaseBehavior;
 import de.agilecoders.wicket.core.settings.BootstrapSettings;
 import de.agilecoders.wicket.core.settings.IBootstrapSettings;
-import de.agilecoders.wicket.themes.markup.html.bootswatch.BootswatchTheme;
-import 
de.agilecoders.wicket.themes.markup.html.bootswatch.BootswatchThemeProvider;
 import de.agilecoders.wicket.webjars.WicketWebjars;
 import de.agilecoders.wicket.webjars.settings.IWebjarsSettings;
 import de.agilecoders.wicket.webjars.settings.WebjarsSettings;
@@ -168,7 +167,6 @@ implements ComponentFactoryRegistryAccessor, 
PageClassRegistryAccessor, WicketVi
      */
     public static final String ENABLE_DEVELOPMENT_UTILITIES_KEY = 
"isis.viewer.wicket.developmentUtilities.enable";
     public static final boolean ENABLE_DEVELOPMENT_UTILITIES_DEFAULT = false;
-    public static final BootswatchTheme BOOTSWATCH_THEME_DEFAULT = 
BootswatchTheme.Flatly;
 
     /**
      * Convenience locator, down-casts inherited functionality.
@@ -284,7 +282,7 @@ implements ComponentFactoryRegistryAccessor, 
PageClassRegistryAccessor, WicketVi
             //
             final Injector injector = Guice.createInjector(
                     newIsisModule(), 
-                    newIsisWicketModule(configuration));
+                    newIsisWicketModule());
             
             initWicketComponentInjection(injector);
             
@@ -360,18 +358,10 @@ implements ComponentFactoryRegistryAccessor, 
PageClassRegistryAccessor, WicketVi
             ThreadPoolSupport.getInstance().join(futures);
         }
 
-        final String themeName = configuration.getString(
-                "isis.viewer.wicket.themes.initial", 
BOOTSWATCH_THEME_DEFAULT.name());
-        BootswatchTheme bootswatchTheme;
-        try {
-            bootswatchTheme = BootswatchTheme.valueOf(themeName);
-        } catch(Exception ex) {
-            bootswatchTheme = BOOTSWATCH_THEME_DEFAULT;
-            LOG.warn("Did not recognise configured bootswatch theme '{}', 
defaulting to '{}'", bootswatchTheme);
-
-        }
-        IBootstrapSettings settings = Bootstrap.getSettings();
-        settings.setThemeProvider(new 
BootswatchThemeProvider(bootswatchTheme));
+        acceptIfPresent(IsisWicketThemeSupport.getInstance(), themeSupport->{
+            IBootstrapSettings settings = Bootstrap.getSettings();
+            settings.setThemeProvider(themeSupport.getThemeProvider());
+        });
 
     }
     
@@ -507,7 +497,7 @@ implements ComponentFactoryRegistryAccessor, 
PageClassRegistryAccessor, WicketVi
     /**
      * Override if required
      */
-    protected Module newIsisWicketModule(IsisConfiguration isisConfiguration) {
+    protected IsisWicketModule newIsisWicketModule() {
         return new IsisWicketModule(getServletContext());
     }
 
diff --git 
a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java
 
b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java
new file mode 100644
index 0000000..1952e23
--- /dev/null
+++ 
b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupport.java
@@ -0,0 +1,86 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.viewer.wicket.ui.components.widgets.themepicker;
+
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.isis.commons.internal.context._Context;
+import org.apache.isis.config.IsisConfiguration;
+import org.apache.isis.core.commons.factory.InstanceUtil;
+import org.apache.isis.core.runtime.system.context.IsisContext;
+
+import de.agilecoders.wicket.core.settings.ThemeProvider;
+
+/**
+ * @since 2.0.0-M2
+ */
+public interface IsisWicketThemeSupport {
+
+    // -- INTERFACE
+    
+    ThemeProvider getThemeProvider();
+    List<String> getEnabledThemeNames();
+    
+    // -- CONSTANTS
+    
+    /**
+     * A configuration setting which value could be a comma separated list of 
enabled theme names
+     */
+    static final String ENABLED_THEMES_KEY  = 
"isis.viewer.wicket.themes.enabled";
+    static final String DEFAULT_THEME_KEY  = 
"isis.viewer.wicket.themes.initial";
+    static final Class<? extends IsisWicketThemeSupport> 
THEME_SUPPORT_DEFAULT_CLASS = 
+            IsisWicketThemeSupportDefault.class;
+    
+    // -- LOOKUP
+    
+    static IsisWicketThemeSupport getInstance() {
+        return _Context.computeIfAbsent(IsisWicketThemeSupport.class, 
__->createInstance());
+    }
+    
+    // -- FACTORY
+    
+    /*private*/ static IsisWicketThemeSupport createInstance() {
+        
+        
+        final IsisConfiguration configuration = IsisContext.getConfiguration();
+        
+        final String themeSupportClassName = configuration.getString(
+                "isis.viewer.wicket.themes.provider", 
THEME_SUPPORT_DEFAULT_CLASS.getName());
+        
+        try {
+            
+            IsisWicketThemeSupport themeSupport = 
+                    (IsisWicketThemeSupport) 
InstanceUtil.createInstance(themeSupportClassName);
+            return themeSupport;
+            
+        } catch (Exception e) {
+            final Logger LOG = 
LoggerFactory.getLogger(IsisWicketThemeSupport.class);
+            LOG.warn("Could not instantiate configured theme support class 
'{}', defaulting to '{}'",
+                    themeSupportClassName,
+                    THEME_SUPPORT_DEFAULT_CLASS.getName());
+        }
+        
+        return (IsisWicketThemeSupport) 
InstanceUtil.createInstance(THEME_SUPPORT_DEFAULT_CLASS);
+    }
+    
+
+}
diff --git 
a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupportDefault.java
 
b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupportDefault.java
new file mode 100644
index 0000000..93a2a44
--- /dev/null
+++ 
b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/IsisWicketThemeSupportDefault.java
@@ -0,0 +1,164 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.viewer.wicket.ui.components.widgets.themepicker;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.isis.commons.internal.base._Lazy;
+import org.apache.isis.commons.internal.base._NullSafe;
+import org.apache.isis.commons.internal.collections._Lists;
+import org.apache.isis.config.IsisConfiguration;
+import org.apache.isis.core.runtime.system.context.IsisContext;
+
+import de.agilecoders.wicket.core.settings.ITheme;
+import de.agilecoders.wicket.core.settings.ThemeProvider;
+import de.agilecoders.wicket.themes.markup.html.bootstrap.BootstrapThemeTheme;
+import de.agilecoders.wicket.themes.markup.html.bootswatch.BootswatchTheme;
+import 
de.agilecoders.wicket.themes.markup.html.bootswatch.BootswatchThemeProvider;
+
+/**
+ * @since 2.0.0-M2
+ */
+class IsisWicketThemeSupportDefault implements IsisWicketThemeSupport {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(IsisWicketThemeSupportDefault.class);
+    
+    private static final BootswatchTheme BOOTSWATCH_THEME_DEFAULT = 
BootswatchTheme.Flatly;
+    
+    private final _Lazy<ThemeProvider> themeProvider = 
_Lazy.of(this::createThemeProvider); 
+    
+    
+    @Override
+    public ThemeProvider getThemeProvider() {
+        return themeProvider.get();
+    }
+    
+    @Override
+    public List<String> getEnabledThemeNames() {
+        final BootstrapThemeTheme bootstrapTheme = new BootstrapThemeTheme();
+        List<BootswatchTheme> bootswatchThemes = 
Arrays.asList(BootswatchTheme.values());
+        //        List<VegibitTheme> vegibitThemes = 
Arrays.asList(VegibitTheme.values());
+
+        List<String> allThemes = new ArrayList<>();
+        allThemes.add(bootstrapTheme.name());
+
+        for (ITheme theme : bootswatchThemes) {
+            allThemes.add(theme.name());
+        }
+
+        //        for (ITheme theme : vegibitThemes) {
+        //            allThemes.add(theme.name());
+        //        }
+
+        allThemes = filterThemes(allThemes);
+
+        return allThemes;
+    }
+    
+    
+    // -- HELPER
+    
+    private ThemeProvider createThemeProvider() {
+        
+        final String themeName = getConfiguration().getString(
+                DEFAULT_THEME_KEY, BOOTSWATCH_THEME_DEFAULT.name());
+        BootswatchTheme bootswatchTheme;
+        try {
+            bootswatchTheme = BootswatchTheme.valueOf(themeName);
+        } catch(Exception ex) {
+            bootswatchTheme = BOOTSWATCH_THEME_DEFAULT;
+            LOG.warn("Did not recognise configured bootswatch theme '{}', 
defaulting to '{}'", 
+                    themeName, 
+                    bootswatchTheme);
+
+        }
+        
+        return new BootswatchThemeProvider(bootswatchTheme);/* {
+            @Override
+            public ITheme byName(String name) {
+                // legacy behavior
+                return getThemeByName(name);
+            }
+        };*/
+    }
+
+// legacy code ...    
+//    private ITheme getThemeByName(String themeName) {
+//        ITheme theme;
+//        try {
+//            if ("bootstrap-theme".equals(themeName)) {
+//                theme = new BootstrapThemeTheme();
+//            } else if (themeName.startsWith("veg")) {
+//                theme = VegibitTheme.valueOf(themeName);
+//            } else {
+//                theme = BootswatchTheme.valueOf(themeName);
+//            }
+//        } catch (Exception x) {
+//            LOG.warn("Cannot find a theme with name '{}' in all available 
theme providers: {}", themeName, x.getMessage());
+//            // fallback to Bootstrap default theme if the parsing by name 
failed somehow
+//            theme = new BootstrapThemeTheme();
+//        }
+//        return theme;
+//    }
+    
+    /**
+     * Filters which themes to show in the drop up by using the provided values
+     * in {@value #ENABLED_THEMES_KEY}
+     *
+     * @param allThemes All available themes
+     * @return A list of all enabled themes
+     */
+    private List<String> filterThemes(List<String> allThemes) {
+        List<String> enabledThemes;
+
+        final String[] enabledThemesArray = 
getConfiguration().getList(ENABLED_THEMES_KEY);
+        if (enabledThemesArray.length > 0) {
+            final Set<String> enabledThemesSet = 
_NullSafe.stream(enabledThemesArray)
+                    .collect(Collectors.toSet());
+
+            Iterable<String> enabled = Iterables.filter(allThemes, new 
Predicate<String>() {
+                @Override
+                public boolean apply(String themeName) {
+                    return enabledThemesSet.contains(themeName);
+                }
+            });
+
+            enabledThemes = _Lists.newArrayList(enabled);
+        } else {
+            enabledThemes = allThemes;
+        }
+
+        return enabledThemes;
+    }
+    
+    private IsisConfiguration getConfiguration() {
+        return IsisContext.getConfiguration();
+    }
+
+}
diff --git 
a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/ThemeChooser.java
 
b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/ThemeChooser.java
index b24c47d..5a1ec4b 100644
--- 
a/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/ThemeChooser.java
+++ 
b/core/viewer-wicket-ui/src/main/java/org/apache/isis/viewer/wicket/ui/components/widgets/themepicker/ThemeChooser.java
@@ -18,17 +18,6 @@
  */
 package org.apache.isis.viewer.wicket.ui.components.widgets.themepicker;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-import java.util.stream.Collectors;
-
-import com.google.common.base.Predicate;
-import com.google.common.collect.Iterables;
-import org.apache.isis.commons.internal.collections._Lists;
-import org.apache.isis.config.IsisConfiguration;
-
 import org.apache.wicket.AttributeModifier;
 import org.apache.wicket.ajax.AjaxRequestTarget;
 import org.apache.wicket.ajax.markup.html.AjaxLink;
@@ -39,10 +28,8 @@ import org.apache.wicket.markup.html.panel.Panel;
 import org.apache.wicket.model.Model;
 import org.apache.wicket.util.cookies.CookieUtils;
 import org.apache.wicket.util.string.Strings;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
-import org.apache.isis.commons.internal.base._NullSafe;
+import org.apache.isis.config.IsisConfiguration;
 import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.core.runtime.system.session.IsisSessionFactory;
 
@@ -64,7 +51,8 @@ import 
de.agilecoders.wicket.themes.markup.html.vegibit.VegibitThemeProvider;
  */
 public class ThemeChooser extends Panel {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(ThemeChooser.class);
+    //private static final Logger LOG = 
LoggerFactory.getLogger(ThemeChooser.class);
+    private static final long serialVersionUID = 1L;
 
     /**
      * A configuration setting which value determines whether the theme 
chooser should be available in the footer
@@ -73,15 +61,10 @@ public class ThemeChooser extends Panel {
     private static final boolean SHOW_THEME_PICKER_DEFAULT = false;
 
     /**
-     * A configuration setting which value could be a comma separated list of 
enabled theme names
-     */
-    private static final String ENABLED_THEMES_KEY  = 
"isis.viewer.wicket.themes.enabled";
-
-    /**
      * The name of the cookie that stores the last user selection
      */
     private static final String ISIS_THEME_COOKIE_NAME = 
"isis.viewer.wicket.themes.selected";
-
+    
     /**
      * Constructor
      *
@@ -97,9 +80,10 @@ public class ThemeChooser extends Panel {
             // if anything other than the default, then we do NOT initialize
             // (on the assumption that it is a persistent store and we don't 
want to overwrite).
         }
-
-        ListView<String> themesView = new ListView<String>("themes", 
getThemeNames()) {
-
+        
+        ListView<String> themesView = new ListView<String>("themes", 
getThemeSupport().getEnabledThemeNames()) {
+            private static final long serialVersionUID = 1L;
+            
             @Override
             protected void populateItem(ListItem<String> item) {
                 final String themeName = item.getModelObject();
@@ -108,6 +92,7 @@ public class ThemeChooser extends Panel {
                     item.add(AttributeModifier.append("class", "active"));
                 }
                 item.add(new AjaxLink<Void>("themeLink") {
+                    private static final long serialVersionUID = 1L;
                     // use Ajax link because Link's url looks like /ENTITY:3 
and this confuses the browser
                     @Override
                     public void onClick(AjaxRequestTarget target) {
@@ -136,7 +121,7 @@ public class ThemeChooser extends Panel {
 
     private void setActiveTheme(String activeTheme) {
         IBootstrapSettings bootstrapSettings = Bootstrap.getSettings();
-        ITheme theme = getThemeByName(activeTheme);
+        ITheme theme = 
getThemeSupport().getThemeProvider().byName(activeTheme);
         getActiveThemeProvider().setActiveTheme(theme);
         if (theme instanceof BootstrapThemeTheme) {
             bootstrapSettings.setThemeProvider(new SingleThemeProvider(theme));
@@ -147,24 +132,6 @@ public class ThemeChooser extends Panel {
         }
     }
 
-    private ITheme getThemeByName(String themeName) {
-        ITheme theme;
-        try {
-            if ("bootstrap-theme".equals(themeName)) {
-                theme = new BootstrapThemeTheme();
-            } else if (themeName.startsWith("veg")) {
-                theme = VegibitTheme.valueOf(themeName);
-            } else {
-                theme = BootswatchTheme.valueOf(themeName);
-            }
-        } catch (Exception x) {
-            LOG.warn("Cannot find a theme with name '{}' in all available 
theme providers: {}", themeName, x.getMessage());
-            // fallback to Bootstrap default theme if the parsing by name 
failed somehow
-            theme = new BootstrapThemeTheme();
-        }
-        return theme;
-    }
-
     private ActiveThemeProvider getActiveThemeProvider() {
         return Bootstrap.getSettings().getActiveThemeProvider();
     }
@@ -177,56 +144,6 @@ public class ThemeChooser extends Panel {
         Attributes.addClass(tag, "dropdown");
     }
 
-    private List<String> getThemeNames() {
-        final BootstrapThemeTheme bootstrapTheme = new BootstrapThemeTheme();
-        List<BootswatchTheme> bootswatchThemes = 
Arrays.asList(BootswatchTheme.values());
-        //        List<VegibitTheme> vegibitThemes = 
Arrays.asList(VegibitTheme.values());
-
-        List<String> allThemes = new ArrayList<>();
-        allThemes.add(bootstrapTheme.name());
-
-        for (ITheme theme : bootswatchThemes) {
-            allThemes.add(theme.name());
-        }
-
-        //        for (ITheme theme : vegibitThemes) {
-        //            allThemes.add(theme.name());
-        //        }
-
-        allThemes = filterThemes(allThemes);
-
-        return allThemes;
-    }
-
-    /**
-     * Filters which themes to show in the drop up by using the provided values
-     * in {@value #ENABLED_THEMES_KEY}
-     *
-     * @param allThemes All available themes
-     * @return A list of all enabled themes
-     */
-    private List<String> filterThemes(List<String> allThemes) {
-        List<String> enabledThemes;
-
-        final String[] enabledThemesArray = 
getConfiguration().getList(ENABLED_THEMES_KEY);
-        if (enabledThemesArray.length > 0) {
-            final Set<String> enabledThemesSet = 
_NullSafe.stream(enabledThemesArray)
-                    .collect(Collectors.toSet());
-
-            Iterable<String> enabled = Iterables.filter(allThemes, new 
Predicate<String>() {
-                @Override
-                public boolean apply(String themeName) {
-                    return enabledThemesSet.contains(themeName);
-                }
-            });
-
-            enabledThemes = _Lists.newArrayList(enabled);
-        } else {
-            enabledThemes = allThemes;
-        }
-
-        return enabledThemes;
-    }
 
     @Override
     protected void onConfigure() {
@@ -236,6 +153,10 @@ public class ThemeChooser extends Panel {
         setVisible(shouldShow);
     }
 
+    private IsisWicketThemeSupport getThemeSupport() {
+        return IsisWicketThemeSupport.getInstance();
+    }
+    
     private IsisConfiguration getConfiguration() {
         return getIsisSessionFactory().getConfiguration();
     }

Reply via email to