1 package net.mograsim.plugin.tables.mi;
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;
11 import net.mograsim.machine.mi.MicroInstructionMemory;
13 public class ColorProvider
15 private final TableViewer viewer;
16 private final IThemeManager themeManager;
17 private long highlightedAddress = -1;
18 private ColorRegistry cRegistry;
19 private FontRegistry fRegistry;
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;
28 public ColorProvider(TableViewer viewer, IThemeManager themeManager)
31 this.themeManager = themeManager;
32 this.cRegistry = themeManager.getCurrentTheme().getColorRegistry();
33 this.fRegistry = themeManager.getCurrentTheme().getFontRegistry();
36 switch (e.getProperty())
38 case IThemeManager.CHANGE_CURRENT_THEME:
39 cRegistry = themeManager.getCurrentTheme().getColorRegistry();
40 fRegistry = themeManager.getCurrentTheme().getFontRegistry();
43 case colorModifBackground:
44 case colorModifForeground:
51 themeManager.addPropertyChangeListener(updateListener);
54 public Color getBackground(Object element, int column)
56 InstructionTableRow row = (InstructionTableRow) element;
57 if (isDefault(row, column))
59 if (isHighlighted(row))
60 return cRegistry.get(colorHighlightedBackground);
61 return viewer.getTable().getBackground();
63 return cRegistry.get(colorModifBackground);
66 public Color getForeground(Object element, int column)
68 InstructionTableRow row = (InstructionTableRow) element;
69 if (isDefault(row, column))
71 if (isHighlighted(row))
72 return cRegistry.get(colorHighlightedForeground);
73 return viewer.getTable().getForeground();
75 return cRegistry.get(colorModifForeground);
78 public Font getFont(Object element, int column)
80 InstructionTableRow row = (InstructionTableRow) element;
81 return !isDefault(row, column) || isHighlighted(row) ? fRegistry.getBold(font) : fRegistry.get(font);
84 private static boolean isDefault(InstructionTableRow row, int column)
86 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
89 private boolean isHighlighted(InstructionTableRow row)
91 return highlightedAddress == row.address;
95 * @param index Index of the row to highlight; An negative index means no row is highlighted
97 public void highlight(long row)
99 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
102 public void dispose()
104 themeManager.removePropertyChangeListener(updateListener);