Merge branch 'machines-are-launch-configs' into development
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / ColorProvider.java
1 package net.mograsim.plugin.tables.mi;
2
3 import org.eclipse.jface.resource.ColorRegistry;
4 import org.eclipse.jface.resource.FontRegistry;
5 import org.eclipse.jface.util.IPropertyChangeListener;
6 import org.eclipse.jface.viewers.TableViewer;
7 import org.eclipse.swt.SWT;
8 import org.eclipse.swt.graphics.Color;
9 import org.eclipse.swt.graphics.Font;
10 import org.eclipse.swt.widgets.Display;
11 import org.eclipse.ui.themes.ITheme;
12 import org.eclipse.ui.themes.IThemeManager;
13
14 import net.mograsim.machine.mi.MicroInstructionMemory;
15
16 public class ColorProvider
17 {
18         private final TableViewer viewer;
19         private final IThemeManager themeManager;
20         private long highlightedAddress = -1;
21         private ColorRegistry cRegistry;
22         private FontRegistry fRegistry;
23         private Font boldItalic;
24
25         private final static String font = "net.mograsim.plugin.table_font",
26                         colorModifBackground = "net.mograsim.plugin.modified_cell_bg_color",
27                         colorModifForeground = "net.mograsim.plugin.modified_cell_fg_color",
28                         colorHighlightedForeground = "net.mograsim.plugin.highlighted_cell_fg_color",
29                         colorHighlightedBackground = "net.mograsim.plugin.highlighted_cell_bg_color";
30         private final IPropertyChangeListener updateListener;
31
32         public ColorProvider(TableViewer viewer, IThemeManager themeManager)
33         {
34                 this.viewer = viewer;
35                 this.themeManager = themeManager;
36                 themeChanged(themeManager.getCurrentTheme());
37                 updateListener = e ->
38                 {
39                         switch (e.getProperty())
40                         {
41                         case IThemeManager.CHANGE_CURRENT_THEME:
42                                 themeChanged(themeManager.getCurrentTheme());
43                                 //$FALL-THROUGH$
44                         case font:
45                         case colorModifBackground:
46                         case colorModifForeground:
47                                 viewer.refresh();
48                                 break;
49                         default:
50                                 break;
51                         }
52                 };
53                 themeManager.addPropertyChangeListener(updateListener);
54         }
55
56         private void themeChanged(ITheme theme)
57         {
58                 cRegistry = theme.getColorRegistry();
59                 fRegistry = theme.getFontRegistry();
60                 boldItalic = fRegistry.getDescriptor(font).setStyle(SWT.BOLD | SWT.ITALIC).createFont(Display.getDefault());
61         }
62
63         public Color getBackground(Object element, int column)
64         {
65                 InstructionTableRow row = (InstructionTableRow) element;
66                 if (isDefault(row, column))
67                 {
68                         if (isHighlighted(row))
69                                 return cRegistry.get(colorHighlightedBackground);
70                         return viewer.getTable().getBackground();
71                 }
72                 return cRegistry.get(colorModifBackground);
73         }
74
75         public Color getForeground(Object element, int column)
76         {
77                 InstructionTableRow row = (InstructionTableRow) element;
78                 if (isDefault(row, column))
79                 {
80                         if (isHighlighted(row))
81                                 return cRegistry.get(colorHighlightedForeground);
82                         return viewer.getTable().getForeground();
83                 }
84                 return cRegistry.get(colorModifForeground);
85         }
86
87         public Font getFont(Object element, int column)
88         {
89                 InstructionTableRow row = (InstructionTableRow) element;
90                 boolean modified = !isDefault(row, column), highlighted = isHighlighted(row);
91                 if (modified && highlighted)
92                         return boldItalic;
93                 if (modified)
94                         return fRegistry.getItalic(font);
95                 if (highlighted)
96                         return fRegistry.getBold(font);
97                 return fRegistry.get(font);
98         }
99
100         private static boolean isDefault(InstructionTableRow row, int column)
101         {
102                 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
103         }
104
105         private boolean isHighlighted(InstructionTableRow row)
106         {
107                 return highlightedAddress == row.address;
108         }
109
110         /**
111          * @param index Index of the row to highlight; An negative index means no row is highlighted
112          */
113         public void highlight(long row)
114         {
115                 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
116         }
117
118         public void dispose()
119         {
120                 themeManager.removePropertyChangeListener(updateListener);
121         }
122 }