0b65d2bec8ecf83891cd766bfd4cef246f0772d2
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / ColorHelper.java
1 package net.mograsim.logic.ui;
2
3 import java.util.function.Consumer;
4 import java.util.function.Supplier;
5
6 import org.eclipse.swt.graphics.Color;
7
8 import net.haspamelodica.swt.helper.gcs.GeneralGC;
9 import net.mograsim.preferences.ColorDefinition;
10 import net.mograsim.preferences.ColorManager;
11
12 public class ColorHelper
13 {
14         public static void executeWithDifferentForeground(GeneralGC gc, ColorDefinition col, Runnable exec)
15         {
16                 executeWithDifferentColor(col, gc::getForeground, gc::setForeground, exec);
17         }
18
19         public static void executeWithDifferentBackground(GeneralGC gc, ColorDefinition col, Runnable exec)
20         {
21                 executeWithDifferentColor(col, gc::getBackground, gc::setBackground, exec);
22         }
23
24         private static void executeWithDifferentColor(ColorDefinition col, Supplier<Color> getColor, Consumer<Color> setColor, Runnable exec)
25         {
26                 if (col == null)
27                         exec.run();
28                 else
29                 {
30                         ColorManager cm = ColorManager.current();
31                         Color oldColor = getColor.get();
32                         Color newColor = cm.toColor(col);
33                         setColor.accept(newColor);
34                         exec.run();
35                         setColor.accept(oldColor);
36                 }
37         }
38
39         private ColorHelper()
40         {
41                 throw new UnsupportedOperationException("No instances of ColorHelper");
42         }
43 }