]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/ui/CmsTheme.java
Merge remote-tracking branch 'origin/master' into v2.x
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / 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 /** Relative paths to fonts. */
35 Set<String> getFontsPaths();
36
37 /** Tags to be added to the header section of the HTML page. */
38 String getHtmlHeaders();
39
40 /** The image registered at this path, or <code>null</code> if not found. */
41 Image getImage(String path);
42
43 /** The default icon size (typically the smallest). */
44 Integer getDefaultIconSize();
45
46 /** Loads one of the relative path provided by the other methods. */
47 InputStream loadPath(String path) throws IOException;
48
49 /**
50 * And icon with this file name (without the extension), with a best effort to
51 * find the appropriate size, or <code>null</code> if not found.
52 *
53 * @param name An icon file name without path and extension.
54 * @param preferredSize the preferred size, if <code>null</code>,
55 * {@link #getDefaultIconSize()} will be tried.
56 */
57 Image getIcon(String name, Integer preferredSize);
58
59 static CmsTheme getCmsTheme(Composite parent) {
60 CmsTheme theme = (CmsTheme) parent.getData(CmsTheme.class.getName());
61 if (theme == null) {
62 // find parent shell
63 Shell topShell = parent.getShell();
64 while (topShell.getParent() != null)
65 topShell = (Shell) topShell.getParent();
66 theme = (CmsTheme) topShell.getData(CmsTheme.class.getName());
67 parent.setData(CmsTheme.class.getName(), theme);
68 }
69 return theme;
70 }
71
72 static void registerCmsTheme(Shell shell, CmsTheme theme) {
73 // find parent shell
74 Shell topShell = shell;
75 while (topShell.getParent() != null)
76 topShell = (Shell) topShell.getParent();
77 // check if already set
78 if (topShell.getData(CmsTheme.class.getName()) != null) {
79 CmsTheme registeredTheme = (CmsTheme) topShell.getData(CmsTheme.class.getName());
80 throw new IllegalArgumentException(
81 "Theme " + registeredTheme.getThemeId() + " already registered in this shell");
82 }
83 topShell.setData(CmsTheme.class.getName(), theme);
84 }
85
86 }