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 FontAndColorHelper
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;
26 private final static String font = "net.mograsim.plugin.mi.table_font",
27 colorModifBackground = "net.mograsim.plugin.mi.modified_cell_bg_color",
28 colorModifForeground = "net.mograsim.plugin.mi.modified_cell_fg_color",
29 colorHighlightForeground = "net.mograsim.plugin.mi.highlighted_cell_fg_color",
30 colorHighlightBackground = "net.mograsim.plugin.mi.highlighted_cell_bg_color";
31 private final IPropertyChangeListener updateListener;
33 public FontAndColorHelper(TableViewer viewer, IThemeManager themeManager)
36 this.themeManager = themeManager;
37 themeChanged(themeManager.getCurrentTheme());
40 switch (e.getProperty())
42 case IThemeManager.CHANGE_CURRENT_THEME:
43 themeChanged(themeManager.getCurrentTheme());
48 case colorModifBackground:
49 colorModifBackgroundChanged();
51 case colorModifForeground:
52 colorModifForegroundChanged();
54 case colorHighlightBackground:
55 colorHighlightBackgroundChanged();
57 case colorHighlightForeground:
58 colorHighlightForegroundChanged();
65 themeManager.addPropertyChangeListener(updateListener);
68 private void themeChanged(ITheme theme)
70 cRegistry = theme.getColorRegistry();
71 fRegistry = theme.getFontRegistry();
73 colorHighlightBackgroundChanged();
74 colorHighlightForegroundChanged();
75 colorModifBackgroundChanged();
76 colorModifForegroundChanged();
79 private void fontChanged()
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);
88 private void colorModifBackgroundChanged()
90 modifBackground = cRegistry.get(colorModifBackground);
93 private void colorModifForegroundChanged()
95 modifForeground = cRegistry.get(colorModifForeground);
98 private void colorHighlightBackgroundChanged()
100 highlightBackground = cRegistry.get(colorHighlightBackground);
103 private void colorHighlightForegroundChanged()
105 highlightForeground = cRegistry.get(colorHighlightForeground);
108 public Color getBackground(Object element, int column)
110 InstructionTableRow row = (InstructionTableRow) element;
111 if (isDefault(row, column))
113 if (isHighlighted(row))
114 return highlightBackground;
115 return viewer.getTable().getBackground();
117 return modifBackground;
120 public Color getForeground(Object element, int column)
122 InstructionTableRow row = (InstructionTableRow) element;
123 if (isDefault(row, column))
125 if (isHighlighted(row))
126 return highlightForeground;
127 return viewer.getTable().getForeground();
129 return modifForeground;
132 public Font getFont(Object element, int column)
134 InstructionTableRow row = (InstructionTableRow) element;
135 boolean modified = !isDefault(row, column), highlighted = isHighlighted(row);
136 if (modified && highlighted)
145 private static boolean isDefault(InstructionTableRow row, int column)
147 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
150 private boolean isHighlighted(InstructionTableRow row)
152 return highlightedAddress == row.address;
156 * @param index Index of the row to highlight; An negative index means no row is highlighted
158 public void highlight(long row)
160 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
163 public void dispose()
165 themeManager.removePropertyChangeListener(updateListener);