]> git.argeo.org Git - lgpl/argeo-commons.git/blob - argeo/cms/ui/CmsTheme.java
Prepare next development cycle
[lgpl/argeo-commons.git] / argeo / cms / ui / CmsTheme.java
1 package org.argeo.cms.ui;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Set;
6
7 import org.eclipse.swt.graphics.Image;
8 import org.eclipse.swt.widgets.Composite;
9 import org.eclipse.swt.widgets.Shell;
10
11 /** A CMS theme which can be applied to web apps as well as desktop apps. */
12 public interface CmsTheme {
13 /** Unique ID of this theme. */
14 String getThemeId();
15
16 /**
17 * Load a resource as an input stream, base don its relative path, or
18 * <code>null</code> if not found
19 */
20 InputStream getResourceAsStream(String resourceName) throws IOException;
21
22 /** Relative paths to standard web CSS. */
23 Set<String> getWebCssPaths();
24
25 /** Relative paths to RAP specific CSS. */
26 Set<String> getRapCssPaths();
27
28 /** Relative paths to SWT specific CSS. */
29 Set<String> getSwtCssPaths();
30
31 /** Relative paths to images such as icons. */
32 Set<String> getImagesPaths();
33
34 /** Tags to be added to the header section of the HTML page. */
35 String getHtmlHeaders();
36
37 /** The image registered at this path, or <code>null</code> if not found. */
38 Image getImage(String path);
39
40 /** The default icon size (typically the smallest). */
41 Integer getDefaultIconSize();
42
43 /** Loads one of the relative path provided by the other methods. */
44 InputStream loadPath(String path) throws IOException;
45
46 /**
47 * And icon with this file name (without the extension), with a best effort to
48 * find the appropriate size, or <code>null</code> if not found.
49 *
50 * @param name An icon file name without path and extension.
51 * @param preferredSize the preferred size, if <code>null</code>,
52 * {@link #getDefaultIconSize()} will be tried.
53 */
54 Image getIcon(String name, Integer preferredSize);
55
56 static CmsTheme getCmsTheme(Composite parent) {
57 CmsTheme theme = (CmsTheme) parent.getData(CmsTheme.class.getName());
58 if (theme == null) {
59 // find parent shell
60 Shell topShell = parent.getShell();
61 while (topShell.getParent() != null)
62 topShell = (Shell) topShell.getParent();
63 theme = (CmsTheme) topShell.getData(CmsTheme.class.getName());
64 parent.setData(CmsTheme.class.getName(), theme);
65 }
66 return theme;
67 }
68
69 static void registerCmsTheme(Shell shell, CmsTheme theme) {
70 // find parent shell
71 Shell topShell = shell;
72 while (topShell.getParent() != null)
73 topShell = (Shell) topShell.getParent();
74 // check if already set
75 if (topShell.getData(CmsTheme.class.getName()) != null) {
76 CmsTheme registeredTheme = (CmsTheme) topShell.getData(CmsTheme.class.getName());
77 throw new IllegalArgumentException(
78 "Theme " + registeredTheme.getThemeId() + " already registered in this shell");
79 }
80 topShell.setData(CmsTheme.class.getName(), theme);
81 }
82
83 }