+ * This will either immediately update the UI or trigger an asynchronous + * update to the UI on the display's UI thread. + *
+ */ + protected void refresh() { + if (stackComposite != null && !stackComposite.isDisposed()) { + // If we are not on the UI thread, update the UI asynchronously on + // the UI thread. + if (Display.getCurrent() == null) { + stackComposite.getDisplay().asyncExec(new Runnable() { + @Override + public void run() { + refreshUI(); + } + }); + } + // If we are on the UI thread, update the UI synchronously. + else { + refreshUI(); + } + } + return; + } + + /** + * This method updates the UI contributions for this plot. This method + * should only called from the UI thread via + * {@link #refresh()}. + */ + private void refreshUI() { + // Get the StackLayout from the plot Composite. + final StackLayout stackLayout = (StackLayout) stackComposite + .getLayout(); + + try { + // Update the plotComposite. Create it if necessary. + if (plotComposite == null) { + plotComposite = createPlotComposite(stackComposite, SWT.NONE); + } + updatePlotComposite(plotComposite); + + // If the plotComposite was successfully updated, we can dispose the + // infoComposite. + if (infoComposite != null && !infoComposite.isDisposed()) { + disposeInfoComposite(infoComposite); + infoComposite = null; + } + + // Update the stack layout, putting the plotComposite in front. + if (stackLayout.topControl != plotComposite) { + stackLayout.topControl = plotComposite; + stackComposite.layout(); + } + } catch (Exception e) { + // Update the infoComposite. Create it if necessary. + if (infoComposite == null) { + infoComposite = createInfoComposite(stackComposite, SWT.NONE); + } + updateInfoComposite(infoComposite, e.getMessage()); + + // Dispose the plotComposite. + if (plotComposite != null && !plotComposite.isDisposed()) { + disposePlotComposite(plotComposite); + plotComposite = null; + } + + // Update the stack layout, putting the infoComposite in front. + if (stackLayout.topControl != infoComposite) { + stackLayout.topControl = infoComposite; + stackComposite.layout(); + } + } + + return; + } + + protected Composite createInfoComposite(Composite parent, int style) { + + Composite infoComposite = new Composite(parent, style); + infoComposite.setLayout(new GridLayout(2, false)); + + // Create an info label with an image. + iconLabel = new Label(infoComposite, SWT.NONE); + iconLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, + false, false)); + + // Create a Composite to contain the info message. + Composite msgComposite = new Composite(infoComposite, SWT.NONE); + msgComposite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, + false, false)); + msgComposite.setLayout(new GridLayout(1, false)); + + // Create an info label with informative text. + msgLabel = new Label(msgComposite, SWT.NONE); + msgLabel.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, + false)); + + return infoComposite; + } + + protected void updateInfoComposite(Composite infoComposite, + final String message) { + // Set the message and icon based on the state of the connection. + final Image image; + final Display display = infoComposite.getDisplay(); + + // Set a default image. + image = display.getSystemImage(SWT.ICON_WARNING); + + // Update the contents of the infoComposite's widgets. + iconLabel.setImage(image); + msgLabel.setText(message); + + return; + } + + protected void disposeInfoComposite(Composite infoComposite) { + infoComposite.dispose(); + iconLabel = null; + msgLabel = null; + } + + protected Composite createPlotComposite(Composite parent, int style) + throws Exception { + return new Composite(parent, style); + } + + protected void updatePlotComposite(Composite plotComposite) + throws Exception { + int seed = (category + type).hashCode(); + Random r = new Random(seed); + plotComposite.setBackground(new Color(plotComposite.getDisplay(), r + .nextInt(255), r.nextInt(255), r.nextInt(255))); + } + + protected void disposePlotComposite(Composite plotComposite) { + // Nothing to do. + } + +}