<commit_before>/*
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 */
package org.mwc.debrief.track_shift.views;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.data.time.DateRange;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;
import org.mwc.cmap.core.CorePlugin;
import org.mwc.debrief.track_shift.controls.ZoneChart.ColorProvider;
import org.mwc.debrief.track_shift.controls.ZoneChart.ZoneSlicer;
import org.mwc.debrief.track_shift.freq.DopplerCurveFinMath;
import org.mwc.debrief.track_shift.freq.IDopplerCurve;
import org.mwc.debrief.track_shift.views.StackedDotHelper.SetBackgroundShade;

import Debrief.Wrappers.SensorContactWrapper;
import Debrief.Wrappers.SensorWrapper;
import Debrief.Wrappers.TrackWrapper;
import MWC.GUI.BaseLayer;
import MWC.GUI.Editable;
import MWC.GUI.Layers.OperateFunction;
import MWC.GUI.JFreeChart.ColourStandardXYItemRenderer;
import MWC.GUI.JFreeChart.ColouredDataItem;
import MWC.Utilities.TextFormatting.GeneralFormat;

public class FrequencyResidualsView extends BaseStackedDotsView
{
  private static final int NUM_DOPPLER_STEPS = 30;
  private Action calcBaseFreq;
  protected SensorWrapper _activeSource;

  public FrequencyResidualsView()
  {
    super(false, true);
  }
  
  @Override
  protected void addToolbarExtras(final IToolBarManager toolBarManager)
  {
    super.addToolbarExtras(toolBarManager);

    toolBarManager.add(calcBaseFreq);
  }

  private static interface SourceProvider
  {
    public List<SensorWrapper> getSources();
  }
  
  private List<SensorWrapper> getPotentialSources()
  {
    final List<SensorWrapper> res = new ArrayList<SensorWrapper>();
    if (_ourLayersSubject != null)
    {
      OperateFunction handleMe = new OperateFunction()
      {

        @Override
        public void operateOn(Editable item)
        {
          TrackWrapper track = (TrackWrapper) item;
          BaseLayer sensors = track.getSensors();
          if (sensors != null)
          {
            Enumeration<Editable> sIter = sensors.elements();
            while (sIter.hasMoreElements())
            {
              SensorWrapper sensor = (SensorWrapper) sIter.nextElement();
              if (sensor.getBaseFrequency() > 0)
              {
                res.add(sensor);
              }
            }
          }
        }
      };
      _ourLayersSubject.walkVisibleItems(TrackWrapper.class, handleMe);
    }
    return res;
  }
  
  public static interface SelectSource
  {
    public void select(SensorWrapper source);
  }
  
  
  private static class SourceMenu implements IMenuListener
  {
    private final SourceProvider provider;
    private final MenuManager parent;
    private SelectSource select;

    public SourceMenu(MenuManager menu, SourceProvider sourceProvider, SelectSource selectSource)
    {
      provider = sourceProvider;
      parent = menu;
      select = selectSource;
    }

    public void menuAboutToShow(IMenuManager manager)
    {
      List<SensorWrapper> sources = provider.getSources();
      if (sources != null && sources.size() > 0)
      {
        final DecimalFormat df = new DecimalFormat("0.00");
        for (final SensorWrapper sensor : sources)
        {
          final String host = sensor.getHost().getName();
          final String name = host + "/" + sensor.getName() + " (" + df
              .format(sensor.getBaseFrequency()) + " Hz)";
          Action deleteAction = new Action(name)
          {
            public void run()
            {
              select.select(sensor);
            }
          };
          parent.add(deleteAction);
        }
      }
      else
      {
        Action deleteAction2 = new Action("No sources found")
        {
          public void run()
          {
            System.out.println("no sources pressed");
          }
        };
        parent.add(deleteAction2);
      }
    }
  }

  @Override
  protected void addPullDownExtras(IMenuManager manager)
  {
    super.addPullDownExtras(manager);

    manager.add(new Separator());
    
    // ok, provide the list of acoustic sources.
    final MenuManager newMenu = new MenuManager("Acoustic Source");
    SelectSource selectSource = new SelectSource() {

      @Override
      public void select(SensorWrapper source)
      {
        _activeSource = source;
        System.out.println("Setting active source to:" + source);
      }};
    SourceProvider sourceProvider = new SourceProvider() {
      @Override
      public List<SensorWrapper> getSources()
      {
        return getPotentialSources();
      }      
    };
    
    // make this new menu dynamic
    newMenu.setRemoveAllWhenShown(true);
    newMenu.addMenuListener(new SourceMenu(newMenu, sourceProvider, selectSource));
    
    manager.add(newMenu);
  }

  @Override
  protected boolean allowDisplayOfTargetOverview()
  {
    return false;
  }

  @Override<commit_msg>Include (broken) drop-down toolbar item<commit_after>/*
 *    Debrief - the Open Source Maritime Analysis Application
 *    http://debrief.info
 *
 *    (C) 2000-2014, PlanetMayo Ltd
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the Eclipse Public License v1.0
 *    (http://www.eclipse.org/legal/epl-v10.html)
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 */
package org.mwc.debrief.track_shift.views;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ControlContribution;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IContributionItem;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.jfree.chart.plot.Marker;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.data.time.DateRange;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
import org.jfree.ui.RectangleAnchor;
import org.jfree.ui.TextAnchor;
import org.mwc.cmap.core.CorePlugin;
import org.mwc.debrief.track_shift.controls.ZoneChart.ColorProvider;
import org.mwc.debrief.track_shift.controls.ZoneChart.ZoneSlicer;
import org.mwc.debrief.track_shift.freq.DopplerCurveFinMath;
import org.mwc.debrief.track_shift.freq.IDopplerCurve;
import org.mwc.debrief.track_shift.views.StackedDotHelper.SetBackgroundShade;

import Debrief.Wrappers.SensorContactWrapper;
import Debrief.Wrappers.SensorWrapper;
import Debrief.Wrappers.TrackWrapper;
import MWC.GUI.BaseLayer;
import MWC.GUI.Editable;
import MWC.GUI.Layers.OperateFunction;
import MWC.GUI.JFreeChart.ColourStandardXYItemRenderer;
import MWC.GUI.JFreeChart.ColouredDataItem;
import MWC.Utilities.TextFormatting.GeneralFormat;

public class FrequencyResidualsView extends BaseStackedDotsView
{
  private static final int NUM_DOPPLER_STEPS = 30;
  private Action calcBaseFreq;
  protected SensorWrapper _activeSource;

  public FrequencyResidualsView()
  {
    super(false, true);
  }

  @Override
  protected void addToolbarExtras(final IToolBarManager toolBarManager)
  {
    super.addToolbarExtras(toolBarManager);

    toolBarManager.add(calcBaseFreq);

    IContributionItem comboCI = new ControlContribution("Acoustic Source")
    {
      protected Control createControl(Composite parent)
      {
        final Combo c = new Combo(parent, SWT.READ_ONLY);
        
        int numItems =  2 + (int) (Math.random() * 6d);
        for (int i = 0; i <= numItems; i++)
        {
          c.add("item " + i);
        }
        c.addSelectionListener(new SelectionAdapter()
        {
          public void widgetSelected(SelectionEvent e)
          {
            System.out.println("pressed:" + c.getText());
          }
        });
        return c;
      }};
      toolBarManager.add(comboCI);

  }

  private static interface SourceProvider
  {
    public List<SensorWrapper> getSources();
  }

  private List<SensorWrapper> getPotentialSources()
  {
    final List<SensorWrapper> res = new ArrayList<SensorWrapper>();
    if (_ourLayersSubject != null)
    {
      OperateFunction handleMe = new OperateFunction()
      {

        @Override
        public void operateOn(Editable item)
        {
          TrackWrapper track = (TrackWrapper) item;
          BaseLayer sensors = track.getSensors();
          if (sensors != null)
          {
            Enumeration<Editable> sIter = sensors.elements();
            while (sIter.hasMoreElements())
            {
              SensorWrapper sensor = (SensorWrapper) sIter.nextElement();
              if (sensor.getBaseFrequency() > 0)
              {
                res.add(sensor);
              }
            }
          }
        }
      };
      _ourLayersSubject.walkVisibleItems(TrackWrapper.class, handleMe);
    }
    return res;
  }

  public static interface SelectSource
  {
    public void select(SensorWrapper source);
  }

  private static class SourceMenu implements IMenuListener
  {
    private final SourceProvider provider;
    private final MenuManager parent;
    private SelectSource select;

    public SourceMenu(MenuManager menu, SourceProvider sourceProvider,
        SelectSource selectSource)
    {
      provider = sourceProvider;
      parent = menu;
      select = selectSource;
    }

    public void menuAboutToShow(IMenuManager manager)
    {
      List<SensorWrapper> sources = provider.getSources();
      if (sources != null && sources.size() > 0)
      {
        final DecimalFormat df = new DecimalFormat("0.00");
        for (final SensorWrapper sensor : sources)
        {
          final String host = sensor.getHost().getName();
          final String name = host + "/" + sensor.getName() + " (" + df.format(
              sensor.getBaseFrequency()) + " Hz)";
          Action deleteAction = new Action(name)
          {
            public void run()
            {
              select.select(sensor);
            }
          };
          parent.add(deleteAction);
        }
      }
      else
      {
        Action deleteAction2 = new Action("No sources found")
        {
          public void run()
          {
            System.out.println("no sources pressed");
          }
        };
        parent.add(deleteAction2);
      }
    }
  }

  @Override
  protected void addPullDownExtras(IMenuManager manager)
  {
    super.addPullDownExtras(manager);

    manager.add(new Separator());

    // ok, provide the list of acoustic sources.
    final MenuManager newMenu = new MenuManager("Acoustic Source");
    SelectSource selectSource = new SelectSource()
    {

      @Override
      public void select(SensorWrapper source)
      {
        _activeSource = source;
        System.out.println("Setting active source to:" + source);
      }
    };
    SourceProvider sourceProvider = new SourceProvider()
    {
      @Override
      public List<SensorWrapper> getSources()
      {
        return getPotentialSources();
      }
    };

    // make this new menu dynamic
    newMenu.setRemoveAllWhenShown(true);
    newMenu.addMenuListener(new SourceMenu(newMenu, sourceProvider,
        selectSource));

    manager.add(newMenu);
  }

  @Override
  protected boolean allowDisplayOfTargetOverview()
  {
    return false;
  }

  @Override
{'fraction_non_alphanumeric': 0.09433962264150944, 'fraction_numerical': 0.0033692722371967657, 'mean_word_length': 2.9158604514805044, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1699726', 'n_tokens_mistral': 3922, 'n_tokens_neox': 3855, 'n_words': 842}