You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IconLoader.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.dialogs.prefs;
  18. import com.dmdirc.addons.ui_swing.components.IconManager;
  19. import com.dmdirc.addons.ui_swing.components.LoggingSwingWorker;
  20. import java.util.concurrent.ExecutionException;
  21. import javax.swing.Icon;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import static com.dmdirc.util.LogUtils.USER_ERROR;
  25. /**
  26. * Loads an icon in the background and uses it for a category label once it has been loaded.
  27. */
  28. public class IconLoader extends LoggingSwingWorker<Icon, Void> {
  29. private static final Logger LOG = LoggerFactory.getLogger(IconLoader.class);
  30. /** Category this icon will be used for. */
  31. private final CategoryLabel label;
  32. /** Icon to load. */
  33. private final String icon;
  34. /** Icon manager. */
  35. private final IconManager iconManager;
  36. /**
  37. * Creates a new icon loader adding the specified icon to the specified icon after it has been
  38. * loaded in the background.
  39. *
  40. * @param iconManager Icon manager
  41. * @param label Label to load category for
  42. * @param icon Icon to load
  43. */
  44. public IconLoader(final IconManager iconManager, final CategoryLabel label, final String icon) {
  45. this.iconManager = iconManager;
  46. this.label = label;
  47. this.icon = icon;
  48. }
  49. @Override
  50. protected Icon doInBackground() {
  51. return iconManager.getIcon(icon);
  52. }
  53. @Override
  54. protected void done() {
  55. try {
  56. label.setIcon(get());
  57. } catch (InterruptedException ex) {
  58. //Ignore
  59. } catch (ExecutionException ex) {
  60. LOG.info(USER_ERROR, ex.getMessage(), ex);
  61. }
  62. }
  63. }