Search in sources :

Example 21 with IconSet

use of org.xwiki.icon.IconSet in project xwiki-platform by xwiki.

the class DefaultIconSetLoader method loadIconSet.

@Override
public IconSet loadIconSet(Reader input, String name) throws IconException {
    try {
        IconSet iconSet = new IconSet(name);
        Properties properties = new Properties();
        properties.load(input);
        // Load all the keys
        for (String key : properties.stringPropertyNames()) {
            String value = properties.getProperty(key);
            if (key.equals(CSS_PROPERTY_NAME)) {
                iconSet.setCss(value);
            } else if (key.equals(SSX_PROPERTY_NAME)) {
                iconSet.setSsx(value);
            } else if (key.equals(JSX_PROPERTY_NAME)) {
                iconSet.setJsx(value);
            } else if (key.equals(RENDER_WIKI_PROPERTY_NAME)) {
                iconSet.setRenderWiki(value);
            } else if (key.equals(RENDER_HTML_PROPERTY_NAME)) {
                iconSet.setRenderHTML(value);
            } else if (key.equals(ICON_TYPE_PROPERTY_NAME)) {
                iconSet.setType(IconType.valueOf(value.toUpperCase()));
            } else {
                Icon icon = new Icon();
                icon.setValue(properties.getProperty(key));
                iconSet.addIcon(key, icon);
            }
        }
        // return
        return iconSet;
    } catch (IOException e) {
        throw new IconException(String.format(ERROR_MSG, name), e);
    }
}
Also used : Icon(org.xwiki.icon.Icon) IOException(java.io.IOException) Properties(java.util.Properties) IconSet(org.xwiki.icon.IconSet) IconException(org.xwiki.icon.IconException)

Example 22 with IconSet

use of org.xwiki.icon.IconSet in project xwiki-platform by xwiki.

the class DefaultIconSetManager method getIconSet.

@Override
public IconSet getIconSet(String name) throws IconException {
    // Special case: the default icon theme
    if (DEFAULT_ICONSET_NAME.equals(name)) {
        return getDefaultIconSet();
    }
    // Get the icon set from the cache
    IconSet iconSet = iconSetCache.get(name, wikiDescriptorManager.getCurrentWikiId());
    // Load it if it is not loaded yet
    if (iconSet == null) {
        try {
            // Search by name
            String xwql = "FROM doc.object(IconThemesCode.IconThemeClass) obj WHERE obj.name = :name";
            Query query = queryManager.createQuery(xwql, Query.XWQL);
            query.bindValue("name", name);
            List<String> results = query.execute();
            if (results.isEmpty()) {
                return null;
            }
            // Get the first result
            String docName = results.get(0);
            DocumentReference docRef = documentReferenceResolver.resolve(docName);
            // Load the icon theme
            iconSet = iconSetLoader.loadIconSet(docRef);
            // Put it in the cache
            iconSetCache.put(docRef, iconSet);
            iconSetCache.put(name, wikiDescriptorManager.getCurrentWikiId(), iconSet);
        } catch (QueryException e) {
            throw new IconException(String.format("Failed to load the icon set [%s].", name), e);
        }
    }
    // Return the icon set
    return iconSet;
}
Also used : QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) DocumentReference(org.xwiki.model.reference.DocumentReference) IconSet(org.xwiki.icon.IconSet) IconException(org.xwiki.icon.IconException)

Example 23 with IconSet

use of org.xwiki.icon.IconSet in project xwiki-platform by xwiki.

the class DefaultIconSetManager method getCurrentIconSet.

@Override
public IconSet getCurrentIconSet() throws IconException {
    // Get the current icon theme
    String iconTheme = configurationSource.getProperty("iconTheme");
    // Get the icon set
    IconSet iconSet = null;
    DocumentReference iconThemeDocRef = documentReferenceResolver.resolve(iconTheme);
    if (!StringUtils.isBlank(iconTheme) && documentAccessBridge.exists(iconThemeDocRef)) {
        iconSet = iconSetCache.get(iconThemeDocRef);
        if (iconSet == null) {
            // lazy loading
            iconSet = iconSetLoader.loadIconSet(iconThemeDocRef);
            iconSetCache.put(iconThemeDocRef, iconSet);
            iconSetCache.put(iconSet.getName(), wikiDescriptorManager.getCurrentWikiId(), iconSet);
        }
    }
    return iconSet;
}
Also used : DocumentReference(org.xwiki.model.reference.DocumentReference) IconSet(org.xwiki.icon.IconSet)

Example 24 with IconSet

use of org.xwiki.icon.IconSet in project xwiki-platform by xwiki.

the class DefaultIconManagerTest method renderHTML.

@Test
public void renderHTML() throws Exception {
    IconSet iconSet = new IconSet("silk");
    iconSet.addIcon("test", new Icon("hello"));
    when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
    when(iconRenderer.renderHTML("test", iconSet)).thenReturn("rendered icon");
    // Test
    String result = mocker.getComponentUnderTest().renderHTML("test");
    assertEquals("rendered icon", result);
}
Also used : Icon(org.xwiki.icon.Icon) IconSet(org.xwiki.icon.IconSet) Test(org.junit.Test)

Example 25 with IconSet

use of org.xwiki.icon.IconSet in project xwiki-platform by xwiki.

the class DefaultIconManagerTest method renderWithFallBack.

@Test
public void renderWithFallBack() throws Exception {
    IconSet iconSet = new IconSet("silk");
    when(iconSetManager.getCurrentIconSet()).thenReturn(iconSet);
    IconSet defaultIconSet = new IconSet("default");
    when(iconSetManager.getDefaultIconSet()).thenReturn(defaultIconSet);
    when(iconRenderer.render("test", defaultIconSet)).thenReturn("rendered icon");
    // Test
    String result = mocker.getComponentUnderTest().render("test");
    assertEquals("rendered icon", result);
}
Also used : IconSet(org.xwiki.icon.IconSet) Test(org.junit.Test)

Aggregations

IconSet (org.xwiki.icon.IconSet)38 Test (org.junit.Test)33 Icon (org.xwiki.icon.Icon)14 DocumentReference (org.xwiki.model.reference.DocumentReference)9 InputStreamReader (java.io.InputStreamReader)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 IconException (org.xwiki.icon.IconException)4 InputStream (java.io.InputStream)2 Reader (java.io.Reader)2 HashMap (java.util.HashMap)2 Query (org.xwiki.query.Query)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiContext (com.xpn.xwiki.XWikiContext)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 Before (org.junit.Before)1 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)1