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.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;
14 import net.mograsim.machine.mi.MicroInstructionMemory;
16 public class ColorProvider
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;
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;
32 public ColorProvider(TableViewer viewer, IThemeManager themeManager)
35 this.themeManager = themeManager;
36 themeChanged(themeManager.getCurrentTheme());
39 switch (e.getProperty())
41 case IThemeManager.CHANGE_CURRENT_THEME:
42 themeChanged(themeManager.getCurrentTheme());
45 case colorModifBackground:
46 case colorModifForeground:
53 themeManager.addPropertyChangeListener(updateListener);
56 private void themeChanged(ITheme theme)
58 cRegistry = theme.getColorRegistry();
59 fRegistry = theme.getFontRegistry();
60 boldItalic = fRegistry.getDescriptor(font).setStyle(SWT.BOLD | SWT.ITALIC).createFont(Display.getDefault());
63 public Color getBackground(Object element, int column)
65 InstructionTableRow row = (InstructionTableRow) element;
66 if (isDefault(row, column))
68 if (isHighlighted(row))
69 return cRegistry.get(colorHighlightedBackground);
70 return viewer.getTable().getBackground();
72 return cRegistry.get(colorModifBackground);
75 public Color getForeground(Object element, int column)
77 InstructionTableRow row = (InstructionTableRow) element;
78 if (isDefault(row, column))
80 if (isHighlighted(row))
81 return cRegistry.get(colorHighlightedForeground);
82 return viewer.getTable().getForeground();
84 return cRegistry.get(colorModifForeground);
87 public Font getFont(Object element, int column)
89 InstructionTableRow row = (InstructionTableRow) element;
90 boolean modified = !isDefault(row, column), highlighted = isHighlighted(row);
91 if (modified && highlighted)
94 return fRegistry.getItalic(font);
96 return fRegistry.getBold(font);
97 return fRegistry.get(font);
100 private static boolean isDefault(InstructionTableRow row, int column)
102 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
105 private boolean isHighlighted(InstructionTableRow row)
107 return highlightedAddress == row.address;
111 * @param index Index of the row to highlight; An negative index means no row is highlighted
113 public void highlight(long row)
115 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
118 public void dispose()
120 themeManager.removePropertyChangeListener(updateListener);