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;
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 FontAndColorHelper(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());
61 viewer.getTable().setFont(fRegistry.get(font));
64 public Color getBackground(Object element, int column)
66 InstructionTableRow row = (InstructionTableRow) element;
67 if (isDefault(row, column))
69 if (isHighlighted(row))
70 return cRegistry.get(colorHighlightedBackground);
71 return viewer.getTable().getBackground();
73 return cRegistry.get(colorModifBackground);
76 public Color getForeground(Object element, int column)
78 InstructionTableRow row = (InstructionTableRow) element;
79 if (isDefault(row, column))
81 if (isHighlighted(row))
82 return cRegistry.get(colorHighlightedForeground);
83 return viewer.getTable().getForeground();
85 return cRegistry.get(colorModifForeground);
88 public Font getFont(Object element, int column)
90 InstructionTableRow row = (InstructionTableRow) element;
91 boolean modified = !isDefault(row, column), highlighted = isHighlighted(row);
92 if (modified && highlighted)
95 return fRegistry.getItalic(font);
97 return fRegistry.getBold(font);
98 return fRegistry.get(font);
101 private static boolean isDefault(InstructionTableRow row, int column)
103 return column == -1 ? true : row.data.getCell(row.address).getParameter(column).isDefault();
106 private boolean isHighlighted(InstructionTableRow row)
108 return highlightedAddress == row.address;
112 * @param index Index of the row to highlight; An negative index means no row is highlighted
114 public void highlight(long row)
116 highlightedAddress = row + ((MicroInstructionMemory) viewer.getInput()).getDefinition().getMinimalAddress();
119 public void dispose()
121 themeManager.removePropertyChangeListener(updateListener);