Instruction Table Editing Support now uses the correct font
[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;
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 FontAndColorHelper(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                 viewer.getTable().setFont(fRegistry.get(font));
62         }
63
64         public Color getBackground(Object element, int column)
65         {
66                 InstructionTableRow row = (InstructionTableRow) element;
67                 if (isDefault(row, column))
68                 {
69                         if (isHighlighted(row))
70                                 return cRegistry.get(colorHighlightedBackground);
71                         return viewer.getTable().getBackground();
72                 }
73                 return cRegistry.get(colorModifBackground);
74         }
75
76         public Color getForeground(Object element, int column)
77         {
78                 InstructionTableRow row = (InstructionTableRow) element;
79                 if (isDefault(row, column))
80                 {
81                         if (isHighlighted(row))
82                                 return cRegistry.get(colorHighlightedForeground);
83                         return viewer.getTable().getForeground();
84                 }
85                 return cRegistry.get(colorModifForeground);
86         }
87
88         public Font getFont(Object element, int column)
89         {
90                 InstructionTableRow row = (InstructionTableRow) element;
91                 boolean modified = !isDefault(row, column), highlighted = isHighlighted(row);
92                 if (modified && highlighted)
93                         return boldItalic;
94                 if (modified)
95                         return fRegistry.getItalic(font);
96                 if (highlighted)
97                         return fRegistry.getBold(font);
98                 return fRegistry.get(font);
99         }
100
101         private static boolean isDefault(InstructionTableRow row, int column)
102         {
103                 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
104         }
105
106         private boolean isHighlighted(InstructionTableRow row)
107         {
108                 return highlightedAddress == row.address;
109         }
110
111         /**
112          * @param index Index of the row to highlight; An negative index means no row is highlighted
113          */
114         public void highlight(long row)
115         {
116                 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
117         }
118
119         public void dispose()
120         {
121                 themeManager.removePropertyChangeListener(updateListener);
122         }
123 }