1 | package de.uka.ipd.sdq.codegen.simucontroller.gui; |
2 | |
3 | import java.text.DecimalFormat; |
4 | import java.util.Observable; |
5 | import java.util.Observer; |
6 | |
7 | import org.apache.log4j.Logger; |
8 | import org.eclipse.swt.SWT; |
9 | import org.eclipse.swt.events.PaintEvent; |
10 | import org.eclipse.swt.events.PaintListener; |
11 | import org.eclipse.swt.events.SelectionAdapter; |
12 | import org.eclipse.swt.events.SelectionEvent; |
13 | import org.eclipse.swt.graphics.Image; |
14 | import org.eclipse.swt.layout.FillLayout; |
15 | import org.eclipse.swt.layout.GridData; |
16 | import org.eclipse.swt.layout.GridLayout; |
17 | import org.eclipse.swt.widgets.Button; |
18 | import org.eclipse.swt.widgets.Canvas; |
19 | import org.eclipse.swt.widgets.Composite; |
20 | import org.eclipse.swt.widgets.Group; |
21 | import org.eclipse.swt.widgets.Label; |
22 | import org.eclipse.swt.widgets.ProgressBar; |
23 | import org.eclipse.ui.IViewPart; |
24 | import org.eclipse.ui.PartInitException; |
25 | import org.eclipse.ui.PlatformUI; |
26 | |
27 | import swing2swt.layout.BorderLayout; |
28 | import de.uka.ipd.sdq.codegen.simucontroller.SimuControllerImages; |
29 | import de.uka.ipd.sdq.codegen.simucontroller.dockmodel.DockModel; |
30 | |
31 | public class DockStatusViewer extends Composite implements Observer { |
32 | /** Logger for this class. */ |
33 | private static final Logger logger = Logger.getLogger(DockStatusViewer.class); |
34 | |
35 | private Image idle_image; |
36 | private DockModel model; |
37 | private Label simTimeLabel; |
38 | private Label measurementsLabel; |
39 | private ProgressBar progressBar; |
40 | private Label remoteLocationLabel; |
41 | private Label dockIdLabel; |
42 | private Image running_image; |
43 | private Image pause_image; |
44 | private Canvas iconCanvas; |
45 | private int lastUIUpdate = -1; |
46 | |
47 | /** |
48 | * Create the composite |
49 | * @param parent |
50 | * @param style |
51 | */ |
52 | public DockStatusViewer(final DockModel model, Composite parent, int style) { |
53 | super(parent, style); |
54 | this.model = model; |
55 | model.addObserver(this); |
56 | |
57 | setLayout(new BorderLayout(0, 0)); |
58 | |
59 | final Composite composite = new Composite(this, SWT.NONE); |
60 | composite.setLayout(new FillLayout()); |
61 | |
62 | iconCanvas = new Canvas(composite, SWT.NONE); |
63 | |
64 | final Composite composite_1 = new Composite(this, SWT.NONE); |
65 | composite_1.setLayoutData(BorderLayout.SOUTH); |
66 | composite_1.setLayout(new FillLayout()); |
67 | |
68 | final Group statusGroup = new Group(composite_1, SWT.NONE); |
69 | final GridLayout gridLayout = new GridLayout(); |
70 | gridLayout.marginRight = 100; |
71 | gridLayout.numColumns = 4; |
72 | statusGroup.setLayout(gridLayout); |
73 | statusGroup.setText("Status"); |
74 | |
75 | final Label idLabel = new Label(statusGroup, SWT.NONE); |
76 | idLabel.setText("ID:"); |
77 | |
78 | dockIdLabel = new Label(statusGroup, SWT.NONE); |
79 | final GridData gd_dockIdLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1); |
80 | dockIdLabel.setLayoutData(gd_dockIdLabel); |
81 | dockIdLabel.setText("Label"); |
82 | |
83 | final Label locationLabel = new Label(statusGroup, SWT.NONE); |
84 | locationLabel.setLayoutData(new GridData()); |
85 | locationLabel.setText("Location:"); |
86 | |
87 | remoteLocationLabel = new Label(statusGroup, SWT.NONE); |
88 | remoteLocationLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1)); |
89 | remoteLocationLabel.setText("Label"); |
90 | |
91 | final Label simulationTimeLabel = new Label(statusGroup, SWT.NONE); |
92 | simulationTimeLabel.setText("Simulation Time:"); |
93 | |
94 | simTimeLabel = new Label(statusGroup, SWT.NONE); |
95 | simTimeLabel.setLayoutData(new GridData(83, SWT.DEFAULT)); |
96 | simTimeLabel.setText("Label"); |
97 | |
98 | final Label measurementsLabelLabel = new Label(statusGroup, SWT.NONE); |
99 | measurementsLabelLabel.setLayoutData(new GridData()); |
100 | measurementsLabelLabel.setText("Measurements:"); |
101 | |
102 | measurementsLabel = new Label(statusGroup, SWT.NONE); |
103 | final GridData gd_measurementsLabel = new GridData(SWT.LEFT, SWT.CENTER, true, false); |
104 | gd_measurementsLabel.widthHint = 93; |
105 | measurementsLabel.setLayoutData(gd_measurementsLabel); |
106 | measurementsLabel.setText("Label"); |
107 | |
108 | progressBar = new ProgressBar(statusGroup, SWT.NONE); |
109 | final GridData gd_progressBar = new GridData(SWT.FILL, SWT.CENTER, true, false, 4, 1); |
110 | progressBar.setLayoutData(gd_progressBar); |
111 | // |
112 | |
113 | idle_image = SimuControllerImages.imageRegistry.get(SimuControllerImages.MASCHINE); |
114 | running_image = SimuControllerImages.imageRegistry.get(SimuControllerImages.MASCHINE_BUSY); |
115 | pause_image = SimuControllerImages.imageRegistry.get(SimuControllerImages.MASCHINE_PAUSE); |
116 | iconCanvas.addPaintListener(new PaintListener(){ |
117 | |
118 | public void paintControl(PaintEvent e) { |
119 | Image image = null; |
120 | |
121 | if (DockStatusViewer.this.model.isIdle()) |
122 | image = idle_image; |
123 | else if (DockStatusViewer.this.model.isSuspended() && !DockStatusViewer.this.model.isStepping()) |
124 | image = pause_image; |
125 | else |
126 | image = running_image; |
127 | if (!image.isDisposed()) |
128 | e.gc.drawImage(image, 0, 0); |
129 | } |
130 | |
131 | }); |
132 | |
133 | final Button stopButton = new Button(composite, SWT.NONE); |
134 | stopButton.addSelectionListener(new SelectionAdapter() { |
135 | public void widgetSelected(final SelectionEvent e) { |
136 | model.getService().stopSimulation(); |
137 | } |
138 | }); |
139 | stopButton.setText("Stop!"); |
140 | |
141 | update(model,null); |
142 | } |
143 | |
144 | @Override |
145 | protected void checkSubclass() { |
146 | // Disable the check that prevents subclassing of SWT components |
147 | } |
148 | |
149 | public void update(Observable o, Object arg) { |
150 | if (model.getPercentDone() != lastUIUpdate || model.isStepping() || model.isSuspended() || model.isIdle()) { |
151 | PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable(){ |
152 | |
153 | public void run() { |
154 | showSimuDockView(); |
155 | if (!dockIdLabel.isDisposed()) |
156 | dockIdLabel.setText(model.getID()); |
157 | if (!remoteLocationLabel.isDisposed()) |
158 | remoteLocationLabel.setText(model.getRemoteMaschineURI() == null ? "<local>" : model.getRemoteMaschineURI()); |
159 | if (!measurementsLabel.isDisposed()) |
160 | measurementsLabel.setText(model.getMeasurementCount()+""); |
161 | if (!simTimeLabel.isDisposed()) |
162 | simTimeLabel.setText(new DecimalFormat("0.#").format(model.getSimTime())); |
163 | if (!progressBar.isDisposed()) |
164 | progressBar.setSelection(model.getPercentDone()); |
165 | if (!iconCanvas.isDisposed()) |
166 | iconCanvas.redraw(); |
167 | |
168 | } |
169 | |
170 | }); |
171 | } |
172 | lastUIUpdate = model.getPercentDone(); |
173 | } |
174 | |
175 | public static void showSimuDockView() { |
176 | PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable(){ |
177 | |
178 | public void run() { |
179 | IViewPart viewer; |
180 | try { |
181 | viewer = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(DockStatusViewPart.ID); |
182 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().bringToTop(viewer); |
183 | viewer.setFocus(); |
184 | } catch (PartInitException e) { |
185 | logger.warn("Could not show SimuDockView and set focus to it.", e); |
186 | } |
187 | } |
188 | |
189 | }); |
190 | } |
191 | |
192 | } |