@Override
  public void dispose() {
    // store current settings back into preferences
    IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
    String pName = getPartName();
    Integer setting = colourSettings.get(pName);
    if (setting != null)
      preferenceStore.setValue(
          pName + "." + PreferenceConstants.PLOT_VIEW_PLOT2D_COLOURMAP, setting);
    preferenceStore.setValue(
        pName + "." + PreferenceConstants.PLOT_VIEW_PLOT2D_SCALING, (useLog ? 1 : 0));
    deleteIObservers();
    if (histogramPlotter != null) histogramPlotter.cleanUp();
    data = null;
    if (histogramUI != null) {
      histogramUI.deactivate(false);
      histogramUI.dispose();
    }
    if (!cmbAlpha.isDisposed()) cmbAlpha.removeSelectionListener(this);
    if (!cmbRedColour.isDisposed()) cmbRedColour.removeSelectionListener(this);
    if (!cmbGreenColour.isDisposed()) cmbGreenColour.removeSelectionListener(this);
    if (!cmbBlueColour.isDisposed()) cmbBlueColour.removeSelectionListener(this);
    if (!btnGreenInverse.isDisposed()) btnGreenInverse.removeSelectionListener(this);
    if (!btnBlueInverse.isDisposed()) btnBlueInverse.removeSelectionListener(this);
    if (!btnAlphaInverse.isDisposed()) btnAlphaInverse.removeSelectionListener(this);
    if (!btnRedInverse.isDisposed()) btnRedInverse.removeSelectionListener(this);

    super.dispose();
  }
 /** @return thumbnail size */
 private int getPreferenceImageSize() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   int size =
       preferenceStore.isDefault(PreferenceConstants.IMAGEEXPLORER_IMAGESIZE)
           ? preferenceStore.getDefaultInt(PreferenceConstants.IMAGEEXPLORER_IMAGESIZE)
           : preferenceStore.getInt(PreferenceConstants.IMAGEEXPLORER_IMAGESIZE);
   return size;
 }
 private double getPreferenceAutoContrastHi() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   int v =
       preferenceStore.isDefault(PreferenceConstants.IMAGEEXPLORER_AUTOCONTRAST_HITHRESHOLD)
           ? preferenceStore.getDefaultInt(
               PreferenceConstants.IMAGEEXPLORER_AUTOCONTRAST_HITHRESHOLD)
           : preferenceStore.getInt(PreferenceConstants.IMAGEEXPLORER_AUTOCONTRAST_HITHRESHOLD);
   return v / 100.0;
 }
 private double getPreferenceAutoContrastLo() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   int v =
       preferenceStore.isDefault(PreferenceConstants.PLOT_VIEW_PLOT2D_AUTOCONTRAST_LOTHRESHOLD)
           ? preferenceStore.getDefaultInt(
               PreferenceConstants.PLOT_VIEW_PLOT2D_AUTOCONTRAST_LOTHRESHOLD)
           : preferenceStore.getInt(PreferenceConstants.PLOT_VIEW_PLOT2D_AUTOCONTRAST_LOTHRESHOLD);
   return v / 100.0;
 }
 private boolean getPreferenceColourMapExpertMode() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   // try to retrieve setting from last session
   if (preferenceStore.getBoolean(
       getPartName() + "." + PreferenceConstants.PLOT_VIEW_PLOT2D_CMAP_EXPERT)) return true;
   return preferenceStore.isDefault(PreferenceConstants.PLOT_VIEW_PLOT2D_CMAP_EXPERT)
       ? preferenceStore.getDefaultBoolean(PreferenceConstants.PLOT_VIEW_PLOT2D_CMAP_EXPERT)
       : preferenceStore.getBoolean(PreferenceConstants.PLOT_VIEW_PLOT2D_CMAP_EXPERT);
 }
 private int getPreferenceColourMapChoice() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   // try to retrieve setting from last session
   int i =
       preferenceStore.getInt(
           getPartName() + "." + PreferenceConstants.PLOT_VIEW_PLOT2D_COLOURMAP);
   if (i != 0) return i;
   return preferenceStore.isDefault(PreferenceConstants.PLOT_VIEW_PLOT2D_COLOURMAP)
       ? preferenceStore.getDefaultInt(PreferenceConstants.PLOT_VIEW_PLOT2D_COLOURMAP)
       : preferenceStore.getInt(PreferenceConstants.PLOT_VIEW_PLOT2D_COLOURMAP);
 }
  @Override
  public void createPartControl(Composite parent) {
    plotViewName = getViewSite().getRegisteredName();
    plotServer.addIObserver(this);
    setPartName(plotViewName);
    this.parent = parent;
    parent.setLayout(new GridLayout(1, true));

    locationRow = new Composite(parent, SWT.NONE);
    locationRow.setLayout(new GridLayout(4, false));
    {
      btnHistoryBack = new Button(locationRow, SWT.LEFT);
      btnHistoryBack.setEnabled(false);
      btnHistoryBack.setToolTipText("Go back in history");
      btnHistoryBack.setImage(
          AnalysisRCPActivator.getImageDescriptor("icons/arrow_left.png").createImage());
      btnHistoryBack.addSelectionListener(this);
      btnHistoryForw = new Button(locationRow, SWT.LEFT);
      btnHistoryForw.setEnabled(false);
      btnHistoryForw.setToolTipText("Go forward in history");
      btnHistoryForw.addSelectionListener(this);
      btnHistoryForw.setImage(
          AnalysisRCPActivator.getImageDescriptor("icons/arrow_right.png").createImage());
      lblLocation = new Label(locationRow, SWT.LEFT);
      lblLocation.setText("Location:");

      cmbDirectoryLocation = new Combo(locationRow, SWT.SINGLE | SWT.LEFT | SWT.BORDER);
      cmbDirectoryLocation.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
      cmbDirectoryLocation.addSelectionListener(this);
    }
    locationRow.setLayoutData(new GridData(SWT.FILL, SWT.NONE, false, false));

    canvas = new Canvas(parent, SWT.DOUBLE_BUFFERED | SWT.V_SCROLL | SWT.H_SCROLL);
    canvas.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    compHUD = new Group(parent, SWT.NONE);
    compHUD.setLayout(new GridLayout(6, false));
    compHUD.setText("Image Playback");
    {
      btnRewind = new Button(compHUD, SWT.PUSH);
      btnRewind.setImage(
          AnalysisRCPActivator.getImageDescriptor("icons/control_start.png").createImage());
      btnRewind.addSelectionListener(this);
      btnRewind.setToolTipText("Rewind");
      btnPlay = new Button(compHUD, SWT.TOGGLE);
      imgPlay = AnalysisRCPActivator.getImageDescriptor("icons/control_play.png").createImage();
      imgStill = AnalysisRCPActivator.getImageDescriptor("icons/control_pause.png").createImage();
      btnPlay.setImage(imgPlay);
      btnPlay.addSelectionListener(this);
      btnPlay.setToolTipText("Play/Pause");
      btnStop = new Button(compHUD, SWT.PUSH);
      btnStop.setImage(
          AnalysisRCPActivator.getImageDescriptor("icons/control_stop.png").createImage());
      btnStop.addSelectionListener(this);
      btnStop.setToolTipText("Stop playback");
      btnForward = new Button(compHUD, SWT.PUSH);
      btnForward.setImage(
          AnalysisRCPActivator.getImageDescriptor("icons/control_end.png").createImage());
      btnForward.addSelectionListener(this);
      btnForward.setToolTipText("Forward");
      btnPlayLoop = new Button(compHUD, SWT.TOGGLE);
      btnPlayLoop.setImage(
          AnalysisRCPActivator.getImageDescriptor("icons/control_repeat.png").createImage());
      btnPlayLoop.setToolTipText("Playback loop (On/off)");
      btnPlayLoop.addSelectionListener(this);
      sldProgress = new Scale(compHUD, SWT.HORIZONTAL);
      sldProgress.setPageIncrement(1);
      sldProgress.addSelectionListener(this);
      sldProgress.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    }
    compHUD.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false));

    imageGrid = new PlotServerSWTImageGrid(canvas, plotViewName);
    imageGrid.setThumbnailSize(getPreferenceImageSize());
    retainStateFromServer();

    if (dirPath != null && dirPath.length() > 0) {
      dirPath.trim();
      updateDirectory.setUser(true);
      updateDirectory.setPriority(Job.DECORATE);
      updateDirectory.schedule(1000);
      cmbDirectoryLocation.setText(dirPath);
      currentDir = dirPath;
    }
    playback =
        new ImagePlayBack(
            parent,
            getPreferencePlaybackView(),
            getViewSite().getPage(),
            sldProgress,
            getPreferenceTimeDelay(),
            getPreferencePlaybackRate());

    isDisposed = false;

    // listen to preference changes to update the Live plot play back view and colour map
    AnalysisRCPActivator.getDefault()
        .getPreferenceStore()
        .addPropertyChangeListener(
            new IPropertyChangeListener() {
              @Override
              public void propertyChange(PropertyChangeEvent event) {
                if (event.getProperty().equals(PreferenceConstants.IMAGEEXPLORER_PLAYBACKVIEW)) {
                  playback.setPlotView(getPreferencePlaybackView());
                }
                if (event.getProperty().equals(PreferenceConstants.IMAGEEXPLORER_COLOURMAP)
                    || event.getProperty().equals(PreferenceConstants.IMAGEEXPLORER_IMAGESIZE)) {
                  List<GridImageEntry> images = imageGrid.getListOfEntries();
                  imageGrid.setThumbnailSize(getPreferenceImageSize());
                  String colourScheme = getPreferenceColourMapChoice();
                  for (GridImageEntry entry : images) {
                    IDataset data = entry.getData();
                    SWTGridEntry gridEntry =
                        new SWTGridEntry(
                            entry.getFilename(),
                            data,
                            canvas,
                            colourScheme,
                            getPreferenceAutoContrastLo(),
                            getPreferenceAutoContrastHi());
                    imageGrid.addEntry(gridEntry, entry.getGridColumnPos(), entry.getGridRowPos());
                  }
                  // Check the colour menu accordingly
                  IAction currentColour = colorMenu.findAction(colourScheme);
                  if (currentColour != null) currentColour.setChecked(true);
                }
              }
            });
  }
 /** @return plot name that playback uses */
 public String getPreferencePlaybackView() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   return preferenceStore.isDefault(PreferenceConstants.IMAGEEXPLORER_PLAYBACKVIEW)
       ? preferenceStore.getDefaultString(PreferenceConstants.IMAGEEXPLORER_PLAYBACKVIEW)
       : preferenceStore.getString(PreferenceConstants.IMAGEEXPLORER_PLAYBACKVIEW);
 }
 private int getPreferencePlaybackRate() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   return preferenceStore.isDefault(PreferenceConstants.IMAGEEXPLORER_PLAYBACKRATE)
       ? preferenceStore.getDefaultInt(PreferenceConstants.IMAGEEXPLORER_PLAYBACKRATE)
       : preferenceStore.getInt(PreferenceConstants.IMAGEEXPLORER_PLAYBACKRATE);
 }
 private int getPreferenceTimeDelay() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   return preferenceStore.isDefault(PreferenceConstants.IMAGEEXPLORER_TIMEDELAYBETWEENIMAGES)
       ? preferenceStore.getDefaultInt(PreferenceConstants.IMAGEEXPLORER_TIMEDELAYBETWEENIMAGES)
       : preferenceStore.getInt(PreferenceConstants.IMAGEEXPLORER_TIMEDELAYBETWEENIMAGES);
 }
 private String getPreferenceColourMapChoice() {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   return preferenceStore.isDefault(PreferenceConstants.IMAGEEXPLORER_COLOURMAP)
       ? preferenceStore.getDefaultString(PreferenceConstants.IMAGEEXPLORER_COLOURMAP)
       : preferenceStore.getString(PreferenceConstants.IMAGEEXPLORER_COLOURMAP);
 }
 private void setPreferenceColourMapChoice(String colorMapChoice) {
   IPreferenceStore preferenceStore = AnalysisRCPActivator.getDefault().getPreferenceStore();
   preferenceStore.setValue(PreferenceConstants.IMAGEEXPLORER_COLOURMAP, colorMapChoice);
 }
 @Override
 public void init(IWorkbench workbench) {
   setPreferenceStore(AnalysisRCPActivator.getDefault().getPreferenceStore());
 }