PageRenderTime 110ms CodeModel.GetById 26ms RepoModel.GetById 7ms app.codeStats 0ms

/emf-2.8.0/org.eclipse.emf.rap.edit.ui/src/org/eclipse/emf/edit/ui/provider/ExtendedColorRegistry.java

#
Java | 166 lines | 136 code | 16 blank | 14 comment | 15 complexity | 7c688739d59ab56b2817f14c1a6b41a7 MD5 | raw file
  1. /**
  2. * Copyright (c) 2008-2010 IBM Corporation and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors:
  9. * IBM - Initial API and implementation
  10. */
  11. package org.eclipse.emf.edit.ui.provider;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.HashMap;
  15. import org.eclipse.rwt.SessionSingletonBase;
  16. import org.eclipse.swt.graphics.Color;
  17. import org.eclipse.swt.graphics.RGB;
  18. import org.eclipse.swt.widgets.Display;
  19. import org.eclipse.emf.common.util.URI;
  20. import org.eclipse.emf.edit.provider.IItemColorProvider;
  21. import org.eclipse.emf.edit.ui.EMFEditUIPlugin;
  22. import org.eclipse.jface.resource.ColorDescriptor;
  23. import org.eclipse.jface.resource.DeviceResourceException;
  24. /**
  25. * A color registry for converting a color description into an actual color.
  26. * @see IItemColorProvider
  27. */
  28. public class ExtendedColorRegistry
  29. {
  30. public static final ExtendedColorRegistry INSTANCE = new ExtendedColorRegistry()
  31. {
  32. @Override
  33. public Color getColor(Color foregroundColor, Color backgroundColor, Object object)
  34. {
  35. return ((ExtendedColorRegistry)SessionSingletonBase.getInstance(ExtendedColorRegistry.class)).getColor(
  36. foregroundColor,
  37. backgroundColor,
  38. object);
  39. }
  40. };
  41. protected Display display;
  42. protected HashMap<Collection<?>, Color> table = new HashMap<Collection<?>, Color>(10);
  43. public ExtendedColorRegistry()
  44. {
  45. display = Display.getCurrent();
  46. hookDisplayDispose(display);
  47. }
  48. public ExtendedColorRegistry(Display display)
  49. {
  50. this.display = display;
  51. hookDisplayDispose(display);
  52. }
  53. public Color getColor(Color foregroundColor, Color backgroundColor, Object object)
  54. {
  55. if (object instanceof Color)
  56. {
  57. return (Color)object;
  58. }
  59. else
  60. {
  61. Collection<Object> key = new ArrayList<Object>(2);
  62. key.add(foregroundColor);
  63. key.add(backgroundColor);
  64. key.add(object);
  65. Color result = table.get(key);
  66. if (result == null)
  67. {
  68. if (object instanceof ColorDescriptor)
  69. {
  70. ColorDescriptor colorDescriptor = (ColorDescriptor)object;
  71. try
  72. {
  73. result = colorDescriptor.createColor(display);
  74. }
  75. catch (DeviceResourceException exception)
  76. {
  77. EMFEditUIPlugin.INSTANCE.log(exception);
  78. }
  79. }
  80. else if (object instanceof URI)
  81. {
  82. URI colorURI = (URI)object;
  83. if (!"color".equals(colorURI.scheme()))
  84. {
  85. throw new IllegalArgumentException("Only 'color' scheme is recognized" + colorURI);
  86. }
  87. RGB colorData;
  88. if ("rgb".equals(colorURI.authority()))
  89. {
  90. int red = Integer.parseInt(colorURI.segment(0));
  91. int green = Integer.parseInt(colorURI.segment(1));
  92. int blue = Integer.parseInt(colorURI.segment(2));
  93. colorData = new RGB(red, green, blue);
  94. }
  95. else if ("hsb".equals(colorURI.authority()))
  96. {
  97. float[] hsb = new float[3];
  98. for (int i = 0; i < 3; ++i)
  99. {
  100. String segment = colorURI.segment(i);
  101. hsb[i] =
  102. "".equals(segment) || "foreground".equals(segment) ?
  103. foregroundColor.getRGB().getHSB()[i]:
  104. "background".equals(segment) ?
  105. backgroundColor.getRGB().getHSB()[i]:
  106. Float.parseFloat(segment);
  107. }
  108. colorData = new RGB(hsb[0], hsb[1], hsb[2]);
  109. }
  110. else
  111. {
  112. throw new IllegalArgumentException("Only 'rgb' and 'hsb' authority are recognized" + colorURI);
  113. }
  114. try
  115. {
  116. result = ColorDescriptor.createFrom(colorData).createColor(display);
  117. }
  118. catch (DeviceResourceException exception)
  119. {
  120. EMFEditUIPlugin.INSTANCE.log(exception);
  121. }
  122. }
  123. if (result != null)
  124. {
  125. table.put(key, result);
  126. }
  127. }
  128. return result;
  129. }
  130. }
  131. protected void handleDisplayDispose()
  132. {
  133. for (Color color : table.values())
  134. {
  135. color.dispose();
  136. }
  137. table = null;
  138. }
  139. protected void hookDisplayDispose(Display display)
  140. {
  141. display.disposeExec
  142. (new Runnable()
  143. {
  144. public void run()
  145. {
  146. handleDisplayDispose();
  147. }
  148. });
  149. }
  150. }