Made highlighted/modified cells bold; New default modified cell color
[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.graphics.Color;
8 import org.eclipse.swt.graphics.Font;
9 import org.eclipse.ui.themes.IThemeManager;
10
11 import net.mograsim.machine.mi.MicroInstructionMemory;
12
13 public class ColorProvider
14 {
15         private final TableViewer viewer;
16         private final IThemeManager themeManager;
17         private long highlightedAddress = -1;
18         private ColorRegistry cRegistry;
19         private FontRegistry fRegistry;
20
21         private final static String font = "net.mograsim.plugin.table_font",
22                         colorModifBackground = "net.mograsim.plugin.modified_cell_bg_color",
23                         colorModifForeground = "net.mograsim.plugin.modified_cell_fg_color",
24                         colorHighlightedForeground = "net.mograsim.plugin.highlighted_cell_fg_color",
25                         colorHighlightedBackground = "net.mograsim.plugin.highlighted_cell_bg_color";
26         private final IPropertyChangeListener updateListener;
27
28         public ColorProvider(TableViewer viewer, IThemeManager themeManager)
29         {
30                 this.viewer = viewer;
31                 this.themeManager = themeManager;
32                 this.cRegistry = themeManager.getCurrentTheme().getColorRegistry();
33                 this.fRegistry = themeManager.getCurrentTheme().getFontRegistry();
34                 updateListener = e ->
35                 {
36                         switch (e.getProperty())
37                         {
38                         case IThemeManager.CHANGE_CURRENT_THEME:
39                                 cRegistry = themeManager.getCurrentTheme().getColorRegistry();
40                                 fRegistry = themeManager.getCurrentTheme().getFontRegistry();
41                                 //$FALL-THROUGH$
42                         case font:
43                         case colorModifBackground:
44                         case colorModifForeground:
45                                 viewer.refresh();
46                                 break;
47                         default:
48                                 break;
49                         }
50                 };
51                 themeManager.addPropertyChangeListener(updateListener);
52         }
53
54         public Color getBackground(Object element, int column)
55         {
56                 InstructionTableRow row = (InstructionTableRow) element;
57                 if (isDefault(row, column))
58                 {
59                         if (isHighlighted(row))
60                                 return cRegistry.get(colorHighlightedBackground);
61                         return viewer.getTable().getBackground();
62                 }
63                 return cRegistry.get(colorModifBackground);
64         }
65
66         public Color getForeground(Object element, int column)
67         {
68                 InstructionTableRow row = (InstructionTableRow) element;
69                 if (isDefault(row, column))
70                 {
71                         if (isHighlighted(row))
72                                 return cRegistry.get(colorHighlightedForeground);
73                         return viewer.getTable().getForeground();
74                 }
75                 return cRegistry.get(colorModifForeground);
76         }
77
78         public Font getFont(Object element, int column)
79         {
80                 InstructionTableRow row = (InstructionTableRow) element;
81                 return !isDefault(row, column) || isHighlighted(row) ? fRegistry.getBold(font) : fRegistry.get(font);
82         }
83
84         private static boolean isDefault(InstructionTableRow row, int column)
85         {
86                 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
87         }
88
89         private boolean isHighlighted(InstructionTableRow row)
90         {
91                 return highlightedAddress == row.address;
92         }
93
94         /**
95          * @param index Index of the row to highlight; An negative index means no row is highlighted
96          */
97         public void highlight(long row)
98         {
99                 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
100         }
101
102         public void dispose()
103         {
104                 themeManager.removePropertyChangeListener(updateListener);
105         }
106 }