Tango.java
001 /*
002  * Copyright 2014-2017 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.plugins.tangoicons;
017 
018 import javax.annotation.Nonnull;
019 
020 import static griffon.util.GriffonClassUtils.requireState;
021 import static griffon.util.GriffonNameUtils.isBlank;
022 import static griffon.util.GriffonNameUtils.requireNonBlank;
023 
024 /**
025  @author Andres Almiray
026  */
027 public enum Tango {
028     ACTIONS_ADDRESS_BOOK_NEW("actions""address-book-new"),
029     ACTIONS_APPOINTMENT_NEW("actions""appointment-new"),
030     ACTIONS_BOOKMARK_NEW("actions""bookmark-new"),
031     ACTIONS_CONTACT_NEW("actions""contact-new"),
032     ACTIONS_DOCUMENT_NEW("actions""document-new"),
033     ACTIONS_DOCUMENT_OPEN("actions""document-open"),
034     ACTIONS_DOCUMENT_PRINT_PREVIEW("actions""document-print-preview"),
035     ACTIONS_DOCUMENT_PRINT("actions""document-print"),
036     ACTIONS_DOCUMENT_PROPERTIES("actions""document-properties"),
037     ACTIONS_DOCUMENT_SAVE_AS("actions""document-save-as"),
038     ACTIONS_DOCUMENT_SAVE("actions""document-save"),
039     ACTIONS_EDIT_CLEAR("actions""edit-clear"),
040     ACTIONS_EDIT_COPY("actions""edit-copy"),
041     ACTIONS_EDIT_CUT("actions""edit-cut"),
042     ACTIONS_EDIT_DELETE("actions""edit-delete"),
043     ACTIONS_EDIT_FIND_REPLACE("actions""edit-find-replace"),
044     ACTIONS_EDIT_FIND("actions""edit-find"),
045     ACTIONS_EDIT_PASTE("actions""edit-paste"),
046     ACTIONS_EDIT_REDO("actions""edit-redo"),
047     ACTIONS_EDIT_SELECT_ALL("actions""edit-select-all"),
048     ACTIONS_EDIT_UNDO("actions""edit-undo"),
049     ACTIONS_FOLDER_NEW("actions""folder-new"),
050     ACTIONS_FORMAT_INDENT_LESS("actions""format-indent-less"),
051     ACTIONS_FORMAT_INDENT_MORE("actions""format-indent-more"),
052     ACTIONS_FORMAT_JUSTIFY_CENTER("actions""format-justify-center"),
053     ACTIONS_FORMAT_JUSTIFY_FILL("actions""format-justify-fill"),
054     ACTIONS_FORMAT_JUSTIFY_LEFT("actions""format-justify-left"),
055     ACTIONS_FORMAT_JUSTIFY_RIGHT("actions""format-justify-right"),
056     ACTIONS_FORMAT_TEXT_BOLD("actions""format-text-bold"),
057     ACTIONS_FORMAT_TEXT_ITALIC("actions""format-text-italic"),
058     ACTIONS_FORMAT_TEXT_STRIKETHROUGH("actions""format-text-strikethrough"),
059     ACTIONS_FORMAT_TEXT_UNDERLINE("actions""format-text-underline"),
060     ACTIONS_GO_BOTTOM("actions""go-bottom"),
061     ACTIONS_GO_DOWN("actions""go-down"),
062     ACTIONS_GO_FIRST("actions""go-first"),
063     ACTIONS_GO_HOME("actions""go-home"),
064     ACTIONS_GO_JUMP("actions""go-jump"),
065     ACTIONS_GO_LAST("actions""go-last"),
066     ACTIONS_GO_NEXT("actions""go-next"),
067     ACTIONS_GO_PREVIOUS("actions""go-previous"),
068     ACTIONS_GO_TOP("actions""go-top"),
069     ACTIONS_GO_UP("actions""go-up"),
070     ACTIONS_LIST_ADD("actions""list-add"),
071     ACTIONS_LIST_REMOVE("actions""list-remove"),
072     ACTIONS_MAIL_FORWARD("actions""mail-forward"),
073     ACTIONS_MAIL_MARK_JUNK("actions""mail-mark-junk"),
074     ACTIONS_MAIL_MARK_NOT_JUNK("actions""mail-mark-not-junk"),
075     ACTIONS_MAIL_MESSAGE_NEW("actions""mail-message-new"),
076     ACTIONS_MAIL_REPLY_ALL("actions""mail-reply-all"),
077     ACTIONS_MAIL_REPLY_SENDER("actions""mail-reply-sender"),
078     ACTIONS_MAIL_SEND_RECEIVE("actions""mail-send-receive"),
079     ACTIONS_MEDIA_EJECT("actions""media-eject"),
080     ACTIONS_MEDIA_PLAYBACK_PAUSE("actions""media-playback-pause"),
081     ACTIONS_MEDIA_PLAYBACK_START("actions""media-playback-start"),
082     ACTIONS_MEDIA_PLAYBACK_STOP("actions""media-playback-stop"),
083     ACTIONS_MEDIA_RECORD("actions""media-record"),
084     ACTIONS_MEDIA_SEEK_BACKWARD("actions""media-seek-backward"),
085     ACTIONS_MEDIA_SEEK_FORWARD("actions""media-seek-forward"),
086     ACTIONS_MEDIA_SKIP_BACKWARD("actions""media-skip-backward"),
087     ACTIONS_MEDIA_SKIP_FORWARD("actions""media-skip-forward"),
088     ACTIONS_PROCESS_STOP("actions""process-stop"),
089     ACTIONS_SYSTEM_LOCK_SCREEN("actions""system-lock-screen"),
090     ACTIONS_SYSTEM_LOG_OUT("actions""system-log-out"),
091     ACTIONS_SYSTEM_SEARCH("actions""system-search"),
092     ACTIONS_SYSTEM_SHUTDOWN("actions""system-shutdown"),
093     ACTIONS_TAB_NEW("actions""tab-new"),
094     ACTIONS_VIEW_FULLSCREEN("actions""view-fullscreen"),
095     ACTIONS_VIEW_REFRESH("actions""view-refresh"),
096     ACTIONS_WINDOW_NEW("actions""window-new"),
097     APPS_ACCESSORIES_CALCULATOR("apps""accessories-calculator"),
098     APPS_ACCESSORIES_CHARACTER_MAP("apps""accessories-character-map"),
099     APPS_ACCESSORIES_TEXT_EDITOR("apps""accessories-text-editor"),
100     APPS_HELP_BROWSER("apps""help-browser"),
101     APPS_INTERNET_GROUP_CHAT("apps""internet-group-chat"),
102     APPS_INTERNET_MAIL("apps""internet-mail"),
103     APPS_INTERNET_NEWS_READER("apps""internet-news-reader"),
104     APPS_INTERNET_WEB_BROWSER("apps""internet-web-browser"),
105     APPS_OFFICE_CALENDAR("apps""office-calendar"),
106     APPS_PREFERENCES_DESKTOP_ACCESSIBILITY("apps""preferences-desktop-accessibility"),
107     APPS_PREFERENCES_DESKTOP_ASSISTIVE_TECHNOLOGY("apps""preferences-desktop-assistive-technology"),
108     APPS_PREFERENCES_DESKTOP_FONT("apps""preferences-desktop-font"),
109     APPS_PREFERENCES_DESKTOP_KEYBOARD_SHORTCUTS("apps""preferences-desktop-keyboard-shortcuts"),
110     APPS_PREFERENCES_DESKTOP_LOCALE("apps""preferences-desktop-locale"),
111     APPS_PREFERENCES_DESKTOP_MULTIMEDIA("apps""preferences-desktop-multimedia"),
112     APPS_PREFERENCES_DESKTOP_REMOTE_DESKTOP("apps""preferences-desktop-remote-desktop"),
113     APPS_PREFERENCES_DESKTOP_SCREENSAVER("apps""preferences-desktop-screensaver"),
114     APPS_PREFERENCES_DESKTOP_THEME("apps""preferences-desktop-theme"),
115     APPS_PREFERENCES_DESKTOP_WALLPAPER("apps""preferences-desktop-wallpaper"),
116     APPS_PREFERENCES_SYSTEM_NETWORK_PROXY("apps""preferences-system-network-proxy"),
117     APPS_PREFERENCES_SYSTEM_SESSION("apps""preferences-system-session"),
118     APPS_PREFERENCES_SYSTEM_WINDOWS("apps""preferences-system-windows"),
119     APPS_SYSTEM_FILE_MANAGER("apps""system-file-manager"),
120     APPS_SYSTEM_INSTALLER("apps""system-installer"),
121     APPS_SYSTEM_SOFTWARE_UPDATE("apps""system-software-update"),
122     APPS_SYSTEM_USERS("apps""system-users"),
123     APPS_UTILITIES_SYSTEM_MONITOR("apps""utilities-system-monitor"),
124     APPS_UTILITIES_TERMINAL("apps""utilities-terminal"),
125     CATEGORIES_APPLICATIONS_ACCESSORIES("categories""applications-accessories"),
126     CATEGORIES_APPLICATIONS_DEVELOPMENT("categories""applications-development"),
127     CATEGORIES_APPLICATIONS_GAMES("categories""applications-games"),
128     CATEGORIES_APPLICATIONS_GRAPHICS("categories""applications-graphics"),
129     CATEGORIES_APPLICATIONS_INTERNET("categories""applications-internet"),
130     CATEGORIES_APPLICATIONS_MULTIMEDIA("categories""applications-multimedia"),
131     CATEGORIES_APPLICATIONS_OFFICE("categories""applications-office"),
132     CATEGORIES_APPLICATIONS_OTHER("categories""applications-other"),
133     CATEGORIES_APPLICATIONS_SYSTEM("categories""applications-system"),
134     CATEGORIES_PREFERENCES_DESKTOP_PERIPHERALS("categories""preferences-desktop-peripherals"),
135     CATEGORIES_PREFERENCES_DESKTOP("categories""preferences-desktop"),
136     CATEGORIES_PREFERENCES_SYSTEM("categories""preferences-system"),
137     DEVICES_AUDIO_CARD("devices""audio-card"),
138     DEVICES_AUDIO_INPUT_MICROPHONE("devices""audio-input-microphone"),
139     DEVICES_BATTERY("devices""battery"),
140     DEVICES_CAMERA_PHOTO("devices""camera-photo"),
141     DEVICES_CAMERA_VIDEO("devices""camera-video"),
142     DEVICES_COMPUTER("devices""computer"),
143     DEVICES_DRIVE_HARDDISK("devices""drive-harddisk"),
144     DEVICES_DRIVE_OPTICAL("devices""drive-optical"),
145     DEVICES_DRIVE_REMOVABLE_MEDIA("devices""drive-removable-media"),
146     DEVICES_INPUT_GAMING("devices""input-gaming"),
147     DEVICES_INPUT_KEYBOARD("devices""input-keyboard"),
148     DEVICES_INPUT_MOUSE("devices""input-mouse"),
149     DEVICES_MEDIA_FLASH("devices""media-flash"),
150     DEVICES_MEDIA_FLOPPY("devices""media-floppy"),
151     DEVICES_MEDIA_OPTICAL("devices""media-optical"),
152     DEVICES_MULTIMEDIA_PLAYER("devices""multimedia-player"),
153     DEVICES_NETWORK_WIRED("devices""network-wired"),
154     DEVICES_NETWORK_WIRELESS("devices""network-wireless"),
155     DEVICES_PRINTER("devices""printer"),
156     DEVICES_VIDEO_DISPLAY("devices""video-display"),
157     EMBLEMS_EMBLEM_FAVORITE("emblems""emblem-favorite"),
158     EMBLEMS_EMBLEM_IMPORTANT("emblems""emblem-important"),
159     EMBLEMS_EMBLEM_PHOTOS("emblems""emblem-photos"),
160     EMBLEMS_EMBLEM_READONLY("emblems""emblem-readonly"),
161     EMBLEMS_EMBLEM_SYMBOLIC_LINK("emblems""emblem-symbolic-link"),
162     EMBLEMS_EMBLEM_SYSTEM("emblems""emblem-system"),
163     EMBLEMS_EMBLEM_UNREADABLE("emblems""emblem-unreadable"),
164     EMOTES_FACE_ANGEL("emotes""face-angel"),
165     EMOTES_FACE_CRYING("emotes""face-crying"),
166     EMOTES_FACE_DEVILISH("emotes""face-devilish"),
167     EMOTES_FACE_GLASSES("emotes""face-glasses"),
168     EMOTES_FACE_GRIN("emotes""face-grin"),
169     EMOTES_FACE_KISS("emotes""face-kiss"),
170     EMOTES_FACE_MONKEY("emotes""face-monkey"),
171     EMOTES_FACE_PLAIN("emotes""face-plain"),
172     EMOTES_FACE_SAD("emotes""face-sad"),
173     EMOTES_FACE_SMILE_BIG("emotes""face-smile-big"),
174     EMOTES_FACE_SMILE("emotes""face-smile"),
175     EMOTES_FACE_SURPRISE("emotes""face-surprise"),
176     EMOTES_FACE_WINK("emotes""face-wink"),
177     MIMETYPES_APPLICATION_CERTIFICATE("mimetypes""application-certificate"),
178     MIMETYPES_APPLICATION_X_EXECUTABLE("mimetypes""application-x-executable"),
179     MIMETYPES_AUDIO_X_GENERIC("mimetypes""audio-x-generic"),
180     MIMETYPES_FONT_X_GENERIC("mimetypes""font-x-generic"),
181     MIMETYPES_IMAGE_X_GENERIC("mimetypes""image-x-generic"),
182     MIMETYPES_PACKAGE_X_GENERIC("mimetypes""package-x-generic"),
183     MIMETYPES_TEXT_HTML("mimetypes""text-html"),
184     MIMETYPES_TEXT_X_GENERIC_TEMPLATE("mimetypes""text-x-generic-template"),
185     MIMETYPES_TEXT_X_GENERIC("mimetypes""text-x-generic"),
186     MIMETYPES_TEXT_X_SCRIPT("mimetypes""text-x-script"),
187     MIMETYPES_VIDEO_X_GENERIC("mimetypes""video-x-generic"),
188     MIMETYPES_X_OFFICE_ADDRESS_BOOK("mimetypes""x-office-address-book"),
189     MIMETYPES_X_OFFICE_CALENDAR("mimetypes""x-office-calendar"),
190     MIMETYPES_X_OFFICE_DOCUMENT_TEMPLATE("mimetypes""x-office-document-template"),
191     MIMETYPES_X_OFFICE_DOCUMENT("mimetypes""x-office-document"),
192     MIMETYPES_X_OFFICE_DRAWING_TEMPLATE("mimetypes""x-office-drawing-template"),
193     MIMETYPES_X_OFFICE_DRAWING("mimetypes""x-office-drawing"),
194     MIMETYPES_X_OFFICE_PRESENTATION_TEMPLATE("mimetypes""x-office-presentation-template"),
195     MIMETYPES_X_OFFICE_PRESENTATION("mimetypes""x-office-presentation"),
196     MIMETYPES_X_OFFICE_SPREADSHEET_TEMPLATE("mimetypes""x-office-spreadsheet-template"),
197     MIMETYPES_X_OFFICE_SPREADSHEET("mimetypes""x-office-spreadsheet"),
198     PLACES_FOLDER_REMOTE("places""folder-remote"),
199     PLACES_FOLDER_SAVED_SEARCH("places""folder-saved-search"),
200     PLACES_FOLDER("places""folder"),
201     PLACES_NETWORK_SERVER("places""network-server"),
202     PLACES_NETWORK_WORKGROUP("places""network-workgroup"),
203     PLACES_START_HERE("places""start-here"),
204     PLACES_USER_DESKTOP("places""user-desktop"),
205     PLACES_USER_HOME("places""user-home"),
206     PLACES_USER_TRASH("places""user-trash"),
207     STATUS_AUDIO_VOLUME_HIGH("status""audio-volume-high"),
208     STATUS_AUDIO_VOLUME_LOW("status""audio-volume-low"),
209     STATUS_AUDIO_VOLUME_MEDIUM("status""audio-volume-medium"),
210     STATUS_AUDIO_VOLUME_MUTED("status""audio-volume-muted"),
211     STATUS_BATTERY_CAUTION("status""battery-caution"),
212     STATUS_DIALOG_ERROR("status""dialog-error"),
213     STATUS_DIALOG_INFORMATION("status""dialog-information"),
214     STATUS_DIALOG_WARNING("status""dialog-warning"),
215     STATUS_FOLDER_DRAG_ACCEPT("status""folder-drag-accept"),
216     STATUS_FOLDER_OPEN("status""folder-open"),
217     STATUS_FOLDER_VISITING("status""folder-visiting"),
218     STATUS_IMAGE_LOADING("status""image-loading"),
219     STATUS_IMAGE_MISSING("status""image-missing"),
220     STATUS_MAIL_ATTACHMENT("status""mail-attachment"),
221     STATUS_NETWORK_ERROR("status""network-error"),
222     STATUS_NETWORK_IDLE("status""network-idle"),
223     STATUS_NETWORK_OFFLINE("status""network-offline"),
224     STATUS_NETWORK_RECEIVE("status""network-receive"),
225     STATUS_NETWORK_TRANSMIT_RECEIVE("status""network-transmit-receive"),
226     STATUS_NETWORK_TRANSMIT("status""network-transmit"),
227     STATUS_NETWORK_WIRELESS_ENCRYPTED("status""network-wireless-encrypted"),
228     STATUS_PRINTER_ERROR("status""printer-error"),
229     STATUS_SOFTWARE_UPDATE_AVAILABLE("status""software-update-available"),
230     STATUS_SOFTWARE_UPDATE_URGENT("status""software-update-urgent"),
231     STATUS_USER_TRASH_FULL("status""user-trash-full"),
232     STATUS_WEATHER_CLEAR_NIGHT("status""weather-clear-night"),
233     STATUS_WEATHER_CLEAR("status""weather-clear"),
234     STATUS_WEATHER_FEW_CLOUDS_NIGHT("status""weather-few-clouds-night"),
235     STATUS_WEATHER_FEW_CLOUDS("status""weather-few-clouds"),
236     STATUS_WEATHER_OVERCAST("status""weather-overcast"),
237     STATUS_WEATHER_SEVERE_ALERT("status""weather-severe-alert"),
238     STATUS_WEATHER_SHOWERS_SCATTERED("status""weather-showers-scattered"),
239     STATUS_WEATHER_SHOWERS("status""weather-showers"),
240     STATUS_WEATHER_SNOW("status""weather-snow"),
241     STATUS_WEATHER_STORM("status""weather-storm");
242 
243     private static final String ERROR_DESCRIPTION_BLANK = "Argument 'description' must not be blank";
244 
245     private final String category;
246     private final String description;
247 
248     Tango(@Nonnull String category, @Nonnull String description) {
249         this.category = category;
250         this.description = description;
251     }
252 
253     @Nonnull
254     public String getCategory() {
255         return category;
256     }
257 
258     @Nonnull
259     public String getDescription() {
260         return description;
261     }
262 
263     @Nonnull
264     public String formatted() {
265         return category + ":" + description;
266     }
267 
268     @Nonnull
269     public String asResource(int size) {
270         requireValidSize(size);
271         return "org/freedesktop/tango/" + size + "x" + size + "/" + category + "/" + description + ".png";
272     }
273 
274     @Nonnull
275     public static String asResource(@Nonnull String description) {
276         int size = 16;
277         checkDescription(description);
278 
279         String[] parts = description.split(":");
280         if (parts.length == 3) {
281             try {
282                 size = Integer.parseInt(parts[2]);
283             catch (NumberFormatException e) {
284                 throw invalidDescription(description, e);
285             }
286         }
287 
288         Tango tango = findByDescription(description, size);
289         return tango.asResource(size);
290     }
291 
292     @Nonnull
293     public static Tango findByDescription(@Nonnull String description) {
294         checkDescription(description);
295 
296         Tango tango = null;
297         String[] parts = description.split(":");
298         for (Tango t : values()) {
299             if (t.category.equalsIgnoreCase(parts[0]) && t.description.equalsIgnoreCase(parts[1])) {
300                 tango = t;
301                 break;
302             }
303         }
304 
305         if (tango == null) {
306             throw invalidDescription(description);
307         }
308 
309         if (parts.length == 3) {
310             int size = 16;
311             try {
312                 size = Integer.parseInt(parts[2]);
313             catch (NumberFormatException e) {
314                 throw invalidDescription(description, e);
315             }
316             ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
317             if (classLoader.getResource(tango.asResource(size)) != null) {
318                 return tango;
319             }
320         }
321 
322         return tango;
323     }
324 
325     @Nonnull
326     public static Tango findByDescription(@Nonnull String description, int size) {
327         checkDescription(description);
328 
329         Tango tango = null;
330         String[] parts = description.split(":");
331         for (Tango t : values()) {
332             if (t.category.equalsIgnoreCase(parts[0]) && t.description.equalsIgnoreCase(parts[1])) {
333                 tango = t;
334                 break;
335             }
336         }
337 
338         if (tango == null) {
339             throw invalidDescription(description);
340         }
341 
342         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
343         if (classLoader.getResource(tango.asResource(size)) != null) {
344             return tango;
345         }
346 
347         throw invalidDescription(description);
348     }
349 
350     public static int requireValidSize(int size) {
351         requireState(size == 16 || size == 22 || size == 32"Argument 'size' must be one of [16, 22, 32].");
352         return size;
353     }
354 
355     private static void checkDescription(String description) {
356         if (isBlank(description)) {
357             throw invalidDescription(description);
358         }
359     }
360 
361     @Nonnull
362     public static IllegalArgumentException invalidDescription(@Nonnull String description) {
363         requireNonBlank(description, ERROR_DESCRIPTION_BLANK);
364         throw new IllegalArgumentException("Description " + description + " is not a valid Tango icon description");
365     }
366 
367     @Nonnull
368     public static IllegalArgumentException invalidDescription(@Nonnull String description, Exception e) {
369         requireNonBlank(description, ERROR_DESCRIPTION_BLANK);
370         throw new IllegalArgumentException("Description " + description + " is not a valid Tango icon description", e);
371     }
372 }