More colors managed by Preferences
[Mograsim.git] / net.mograsim.plugin.core / src / net / mograsim / plugin / views / LogicUIPart.java
1 package net.mograsim.plugin.views;
2
3 import javax.inject.Inject;
4
5 import org.eclipse.core.runtime.IStatus;
6 import org.eclipse.core.runtime.Status;
7 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
8 import org.eclipse.jface.resource.ColorRegistry;
9 import org.eclipse.swt.SWT;
10 import org.eclipse.swt.graphics.Color;
11 import org.eclipse.swt.graphics.RGB;
12 import org.eclipse.swt.widgets.Composite;
13 import org.eclipse.ui.PlatformUI;
14 import org.eclipse.ui.part.ViewPart;
15 import org.eclipse.ui.statushandlers.StatusManager;
16
17 import net.haspamelodica.swt.helper.zoomablecanvas.helper.ZoomableCanvasUserInput;
18 import net.mograsim.logic.core.timeline.Timeline;
19 import net.mograsim.logic.ui.LogicExecuter;
20 import net.mograsim.logic.ui.LogicUICanvas;
21 import net.mograsim.logic.ui.model.ViewModelModifiable;
22 import net.mograsim.logic.ui.model.components.GUIBitDisplay;
23 import net.mograsim.logic.ui.model.components.GUIManualSwitch;
24 import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
25 import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
26 import net.mograsim.logic.ui.model.wires.GUIWire;
27 import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
28 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
29 import net.mograsim.preferences.ColorDefinition;
30 import net.mograsim.preferences.Preferences;
31
32 public class LogicUIPart extends ViewPart
33 {
34         @Inject
35         private MPart part;
36
37         private LogicExecuter exec;
38         private LogicUICanvas ui;
39
40         @Override
41         public void dispose()
42         {
43                 if (exec != null)
44                         exec.stopLiveExecution();
45         }
46
47         @Override
48         public void createPartControl(Composite parent)
49         {
50                 // set preferences
51                 Preferences.setPreferences(new Preferences()
52                 {
53                         @Override
54                         public ColorDefinition getColorDefinition(String name)
55                         {
56                                 RGB rgb = getColorRegistry().getRGB(name);
57                                 if (rgb == null)
58                                 {
59                                         StatusManager.getManager().handle(new Status(IStatus.ERROR, "net.mograsim.plugin.core", "No color for name " + name));
60                                         return null;
61                                 }
62                                 return new ColorDefinition(rgb.red, rgb.green, rgb.blue);
63                         }
64
65                         @Override
66                         public Color getColor(String name)
67                         {
68                                 return getColorRegistry().get(name);
69                         }
70
71                         private ColorRegistry getColorRegistry()
72                         {
73                                 return PlatformUI.getWorkbench().getThemeManager().getCurrentTheme().getColorRegistry();
74                         }
75                 });
76
77                 // setup view model
78                 ViewModelModifiable viewModel = new ViewModelModifiable();
79                 createTestbench(viewModel);
80
81                 // convert to logic model
82                 LogicModelParameters params = new LogicModelParameters();
83                 params.gateProcessTime = 50;
84                 params.wireTravelTime = 10;
85                 Timeline timeline = ViewLogicModelAdapter.convert(viewModel, params);
86
87                 // initialize UI
88                 ui = new LogicUICanvas(parent, SWT.NONE, viewModel);
89                 ui.addTransformListener((x, y, z) -> part.setDirty(z < 1));
90                 ZoomableCanvasUserInput userInput = new ZoomableCanvasUserInput(ui);
91                 userInput.buttonDrag = 3;
92                 userInput.buttonZoom = 2;
93                 userInput.enableUserInput();
94
95                 // initialize executer
96                 exec = new LogicExecuter(timeline);
97
98                 // run it
99                 exec.startLiveExecution();
100         }
101
102         @Override
103         public void setFocus()
104         {
105                 ui.setFocus();
106         }
107
108         @SuppressWarnings("unused") // for GUIWires being created
109         public static void createTestbench(ViewModelModifiable model)
110         {
111                 SimpleRectangularSubmodelComponent comp = new GUIAm2901(model);
112
113                 comp.moveTo(100, 0);
114                 for (int i = 0; i < comp.getInputPinNames().size(); i++)
115                 {
116                         GUIManualSwitch sw = new GUIManualSwitch(model);
117                         sw.moveTo(0, 20 * i);
118                         new GUIWire(model, comp.getPin(comp.getInputPinNames().get(i)), sw.getOutputPin());
119                 }
120                 for (int i = 0; i < comp.getOutputPinNames().size(); i++)
121                 {
122                         GUIBitDisplay bd = new GUIBitDisplay(model);
123                         bd.moveTo(200, 20 * i);
124                         new GUIWire(model, comp.getPin(comp.getOutputPinNames().get(i)), bd.getInputPin());
125                 }
126         }
127 }