befbac8cd560405b550dd02c15da10ac0f66b401
[Mograsim.git] / plugins / net.mograsim.plugin.core / src / net / mograsim / plugin / tables / mi / FontAndColorHelper.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 FontAndColorHelper
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, bold, italic, normal;
24         private Color modifBackground, modifForeground, highlightBackground, highlightForeground;
25
26         private final static String font = "net.mograsim.plugin.table_font",
27                         colorModifBackground = "net.mograsim.plugin.modified_cell_bg_color",
28                         colorModifForeground = "net.mograsim.plugin.modified_cell_fg_color",
29                         colorHighlightForeground = "net.mograsim.plugin.highlighted_cell_fg_color",
30                         colorHighlightBackground = "net.mograsim.plugin.highlighted_cell_bg_color";
31         private final IPropertyChangeListener updateListener;
32
33         public FontAndColorHelper(TableViewer viewer, IThemeManager themeManager)
34         {
35                 this.viewer = viewer;
36                 this.themeManager = themeManager;
37                 themeChanged(themeManager.getCurrentTheme());
38                 updateListener = e ->
39                 {
40                         switch (e.getProperty())
41                         {
42                         case IThemeManager.CHANGE_CURRENT_THEME:
43                                 themeChanged(themeManager.getCurrentTheme());
44                                 break;
45                         case font:
46                                 fontChanged();
47                                 break;
48                         case colorModifBackground:
49                                 colorModifBackgroundChanged();
50                                 break;
51                         case colorModifForeground:
52                                 colorModifForegroundChanged();
53                                 break;
54                         case colorHighlightBackground:
55                                 colorHighlightBackgroundChanged();
56                                 break;
57                         case colorHighlightForeground:
58                                 colorHighlightForegroundChanged();
59                                 break;
60                         default:
61                                 return;
62                         }
63                         viewer.refresh();
64                 };
65                 themeManager.addPropertyChangeListener(updateListener);
66         }
67
68         private void themeChanged(ITheme theme)
69         {
70                 cRegistry = theme.getColorRegistry();
71                 fRegistry = theme.getFontRegistry();
72                 fontChanged();
73                 colorHighlightBackgroundChanged();
74                 colorHighlightForegroundChanged();
75                 colorModifBackgroundChanged();
76                 colorModifForegroundChanged();
77         }
78
79         private void fontChanged()
80         {
81                 boldItalic = fRegistry.getDescriptor(font).setStyle(SWT.BOLD | SWT.ITALIC).createFont(Display.getDefault());
82                 bold = fRegistry.getBold(font);
83                 italic = fRegistry.getItalic(font);
84                 normal = fRegistry.get(font);
85                 viewer.getTable().setFont(normal);
86         }
87
88         private void colorModifBackgroundChanged()
89         {
90                 modifBackground = cRegistry.get(colorModifBackground);
91         }
92
93         private void colorModifForegroundChanged()
94         {
95                 modifForeground = cRegistry.get(colorModifForeground);
96         }
97
98         private void colorHighlightBackgroundChanged()
99         {
100                 highlightBackground = cRegistry.get(colorHighlightBackground);
101         }
102
103         private void colorHighlightForegroundChanged()
104         {
105                 highlightForeground = cRegistry.get(colorHighlightForeground);
106         }
107
108         public Color getBackground(Object element, int column)
109         {
110                 InstructionTableRow row = (InstructionTableRow) element;
111                 if (isDefault(row, column))
112                 {
113                         if (isHighlighted(row))
114                                 return highlightBackground;
115                         return viewer.getTable().getBackground();
116                 }
117                 return modifBackground;
118         }
119
120         public Color getForeground(Object element, int column)
121         {
122                 InstructionTableRow row = (InstructionTableRow) element;
123                 if (isDefault(row, column))
124                 {
125                         if (isHighlighted(row))
126                                 return highlightForeground;
127                         return viewer.getTable().getForeground();
128                 }
129                 return modifForeground;
130         }
131
132         public Font getFont(Object element, int column)
133         {
134                 InstructionTableRow row = (InstructionTableRow) element;
135                 boolean modified = !isDefault(row, column), highlighted = isHighlighted(row);
136                 if (modified && highlighted)
137                         return boldItalic;
138                 if (modified)
139                         return italic;
140                 if (highlighted)
141                         return bold;
142                 return normal;
143         }
144
145         private static boolean isDefault(InstructionTableRow row, int column)
146         {
147                 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
148         }
149
150         private boolean isHighlighted(InstructionTableRow row)
151         {
152                 return highlightedAddress == row.address;
153         }
154
155         /**
156          * @param index Index of the row to highlight; An negative index means no row is highlighted
157          */
158         public void highlight(long row)
159         {
160                 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
161         }
162
163         public void dispose()
164         {
165                 themeManager.removePropertyChangeListener(updateListener);
166         }
167 }