1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.icon

File IconManagerScriptService.java

 

Coverage histogram

../../../img/srcFileCovDistChart2.png
81% of files have more coverage

Code metrics

2
44
12
1
243
122
23
0.52
3.67
12
1.92

Classes

Class Line # Actions
IconManagerScriptService 41 44 0% 23 46
0.2068965620.7%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.icon;
21   
22    import java.util.List;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.script.service.ScriptService;
31   
32    /**
33    * Script services to render an icon from the current icon set.
34    *
35    * @since 6.2M1
36    * @version $Id: 38385a31b85d72c712347413971343fc30e41d87 $
37    */
38    @Component
39    @Named("icon")
40    @Singleton
 
41    public class IconManagerScriptService implements ScriptService
42    {
43    /**
44    * The key under which the last encountered error is stored in the current execution context.
45    */
46    private static final String ERROR_KEY = "scriptservice.icon.error";
47   
48    @Inject
49    private IconManager iconManager;
50   
51    @Inject
52    private IconSetManager iconSetManager;
53   
54    @Inject
55    private Execution execution;
56   
57    /**
58    * Display an icon from the current {@link org.xwiki.icon.IconSet}.
59    * @param iconName name of the icon to display
60    * @return the wiki code that displays the icon
61    */
 
62  123 toggle public String render(String iconName)
63    {
64  123 try {
65  123 return iconManager.render(iconName);
66    } catch (IconException e) {
67  0 setLastError(e);
68  0 return null;
69    }
70    }
71   
72    /**
73    * Display an icon from the specified {@link org.xwiki.icon.IconSet}.
74    * @param iconName name of the icon to display
75    * @param iconSetName name of the icon set
76    * @return the wiki code that displays the icon
77    * @since 6.3RC1
78    */
 
79  18 toggle public String render(String iconName, String iconSetName)
80    {
81  18 try {
82  18 return iconManager.render(iconName, iconSetName);
83    } catch (IconException e) {
84  0 setLastError(e);
85  0 return null;
86    }
87    }
88   
89    /**
90    * Display an icon from the specified {@link org.xwiki.icon.IconSet}.
91    * @param iconName name of the icon to display
92    * @param iconSetName name of the icon set
93    * @param fallback enable the fallback to the default icon theme if the icon does not exist
94    * @return the wiki code that displays the icon
95    * @since 6.3RC1
96    */
 
97  0 toggle public String render(String iconName, String iconSetName, boolean fallback)
98    {
99  0 try {
100  0 return iconManager.render(iconName, iconSetName, fallback);
101    } catch (IconException e) {
102  0 setLastError(e);
103  0 return null;
104    }
105    }
106   
107    /**
108    * Display an icon from the current {@link org.xwiki.icon.IconSet}.
109    * @param iconName name of the icon to display
110    * @return the HTML code that displays the icon
111    */
 
112  2531 toggle public String renderHTML(String iconName)
113    {
114  2530 try {
115  2531 return iconManager.renderHTML(iconName);
116    } catch (IconException e) {
117  0 setLastError(e);
118  0 return null;
119    }
120    }
121   
122    /**
123    * Display an icon from the specified {@link org.xwiki.icon.IconSet}.
124    * @param iconName name of the icon to display
125    * @param iconSetName name of the icon set
126    * @return the HTML code that displays the icon
127    * @since 6.3RC1
128    */
 
129  0 toggle public String renderHTML(String iconName, String iconSetName)
130    {
131  0 try {
132  0 return iconManager.renderHTML(iconName, iconSetName);
133    } catch (IconException e) {
134  0 setLastError(e);
135  0 return null;
136    }
137    }
138   
139    /**
140    * Display an icon from the specified {@link org.xwiki.icon.IconSet}.
141    * @param iconName name of the icon to display
142    * @param iconSetName name of the icon set
143    * @param fallback enable the fallback to the default icon theme if the icon does not exist
144    * @return the HTML code that displays the icon
145    * @since 6.3RC1
146    */
 
147  0 toggle public String renderHTML(String iconName, String iconSetName, boolean fallback)
148    {
149  0 try {
150  0 return iconManager.renderHTML(iconName, iconSetName, fallback);
151    } catch (IconException e) {
152  0 setLastError(e);
153  0 return null;
154    }
155    }
156   
157    /**
158    * Get the name of all the icon sets present in the current wiki.
159    * @return the list of the name of the icon sets present in the current wiki.
160    * @since 6.4M1
161    */
 
162  9 toggle public List<String> getIconSetNames()
163    {
164  9 try {
165  9 return iconSetManager.getIconSetNames();
166    } catch (IconException e) {
167  0 setLastError(e);
168  0 return null;
169    }
170    }
171   
172    /**
173    * Get the list of the names of all available icons in the current icon set.
174    * @return the icon names
175    * @since 6.4M1
176    */
 
177  0 toggle public List<String> getIconNames()
178    {
179  0 try {
180  0 return iconManager.getIconNames();
181    } catch (IconException e) {
182  0 setLastError(e);
183  0 return null;
184    }
185    }
186   
187    /**
188    * Get the list of the names of all available icons in the specified icon set.
189    * @param iconSetName name of the icon set
190    * @return the icon names
191    * @since 6.4M1
192    */
 
193  0 toggle public List<String> getIconNames(String iconSetName)
194    {
195  0 try {
196  0 return iconManager.getIconNames(iconSetName);
197    } catch (IconException e) {
198  0 setLastError(e);
199  0 return null;
200    }
201    }
202   
203    /**
204    * Get the name of the current icon set.
205    * @return the name of the current icon set
206    * @since 6.4M2
207    */
 
208  0 toggle public String getCurrentIconSetName()
209    {
210  0 try {
211  0 IconSet currentIconSet = iconSetManager.getCurrentIconSet();
212  0 if (currentIconSet != null) {
213  0 return currentIconSet.getName();
214    }
215    } catch (IconException e) {
216  0 setLastError(e);
217    }
218   
219  0 return null;
220    }
221   
222    /**
223    * Get the error generated while performing the previously called action.
224    * @return an eventual exception or {@code null} if no exception was thrown
225    * @since 6.3RC1
226    */
 
227  0 toggle public IconException getLastError()
228    {
229  0 return (IconException) this.execution.getContext().getProperty(ERROR_KEY);
230    }
231   
232    /**
233    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
234    *
235    * @param e the exception to store, can be {@code null} to clear the previously stored exception
236    * @see #getLastError()
237    * @since 6.3RC1
238    */
 
239  0 toggle private void setLastError(IconException e)
240    {
241  0 this.execution.getContext().setProperty(ERROR_KEY, e);
242    }
243    }