<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.cmap.NarrativeViewer;

import java.util.Iterator;
import java.util.LinkedList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.nebula.jface.gridviewer.GridColumnLayout;
import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.mwc.cmap.NarrativeViewer.actions.NarrativeViewerActions;
import org.mwc.cmap.NarrativeViewer.filter.ui.FilterDialog;
import org.mwc.cmap.NarrativeViewer.model.TimeFormatter;

import MWC.GenericData.HiResDate;
import MWC.TacticalData.IRollingNarrativeProvider;
import MWC.TacticalData.NarrativeEntry;

public class NarrativeViewer
{

  final NarrativeViewerModel myModel;
  private NarrativeViewerActions myActions;

  final GridTableViewer viewer;
  private Composite composite;

  public NarrativeViewer(final Composite parent,
      final IPreferenceStore preferenceStore)
  {
    GridColumnLayout layout = new GridColumnLayout(); 
     composite = new Composite(parent, SWT.NONE);
    composite.setLayout(layout);
//    FilteredTree tree = new FilteredTree(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI, new PatternFilter(){
//      
//      @Override
//      protected boolean isLeafMatch(Viewer viewer, Object element)
//      {
//        
//        AbstractColumn[] allColumns = myModel.getAllColumns();
//        for (AbstractColumn abstractColumn : allColumns)
//        {
//          if(abstractColumn.isVisible())
//          {
//            String text = abstractColumn.getProperty((NarrativeEntry) element).toString();
//           if(text!=null && wordMatches(text))
//           {
//             return true;
//           }
//          }
//        }
//        
//        return false;
//      }
//      
//    }, true) ;
    viewer = new GridTableViewer(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI);
    viewer.getGrid().setHeaderVisible(true);
    viewer.getGrid().setLinesVisible(true);
    viewer.getGrid().addControlListener(new ControlAdapter() {
      @Override
      public void controlResized(ControlEvent e) {
        calculateGridRawHeight();
      }
    });

    myModel = new NarrativeViewerModel(preferenceStore);

   
    myModel.createTable(this,layout);
 
    
    //
    // addCellDoubleClickListener(new KTableCellDoubleClickAdapter()
    // {
    // public void fixedCellDoubleClicked(final int col, final int row, final int statemask)
    // {
    // final Column column = myModel.getVisibleColumn(col);
    // showFilterDialog(column);
    // final ColumnFilter filter = column.getFilter();
    // if (filter != null)
    // {
    //
    // }
    // }
    // });
  }

 

  public GridTableViewer getViewer()
  {
    return viewer;
  }
  public Composite getControl()
  {
    return composite;
  }

  public NarrativeViewerActions getViewerActions()
  {
    if (myActions == null)
    {
      myActions = new NarrativeViewerActions(this);
    }
    return myActions;
  }

  public void showFilterDialog(final Column column)
  {
    if (!myModel.hasInput())
    {
      return;
    }

    final ColumnFilter filter = column.getFilter();
    if (filter == null)
    {
      return;
    }

    final FilterDialog dialog =
        new FilterDialog(viewer.getGrid().getShell(), myModel.getInput(),
            column);

    if (Dialog.OK == dialog.open())
    {
      dialog.commitFilterChanges();
      refresh();
    }
  }

  public void setInput(final IRollingNarrativeProvider entryWrapper)
  {
    myModel.setInput(entryWrapper);
    refresh();
  }

  public void setTimeFormatter(final TimeFormatter timeFormatter)
  {
    myModel.setTimeFormatter(timeFormatter);
    viewer.refresh();
  }

  public void refresh()
  {
    viewer.setInput(new Object());
    calculateGridRawHeight();
  }

  private void calculateGridRawHeight()
  {
    Display.getDefault().asyncExec(new Runnable()
    {
      @Override
      public void run()
      {
        final Grid grid = getViewer().getGrid();
        if(grid.isDisposed())<commit_msg>avoid using disposed control<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.cmap.NarrativeViewer;

import java.util.Iterator;
import java.util.LinkedList;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.nebula.jface.gridviewer.GridColumnLayout;
import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
import org.eclipse.nebula.widgets.grid.Grid;
import org.eclipse.nebula.widgets.grid.GridColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.mwc.cmap.NarrativeViewer.actions.NarrativeViewerActions;
import org.mwc.cmap.NarrativeViewer.filter.ui.FilterDialog;
import org.mwc.cmap.NarrativeViewer.model.TimeFormatter;

import MWC.GenericData.HiResDate;
import MWC.TacticalData.IRollingNarrativeProvider;
import MWC.TacticalData.NarrativeEntry;

public class NarrativeViewer
{

  final NarrativeViewerModel myModel;
  private NarrativeViewerActions myActions;

  final GridTableViewer viewer;
  private Composite composite;

  public NarrativeViewer(final Composite parent,
      final IPreferenceStore preferenceStore)
  {
    GridColumnLayout layout = new GridColumnLayout(); 
     composite = new Composite(parent, SWT.NONE);
    composite.setLayout(layout);
//    FilteredTree tree = new FilteredTree(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI, new PatternFilter(){
//      
//      @Override
//      protected boolean isLeafMatch(Viewer viewer, Object element)
//      {
//        
//        AbstractColumn[] allColumns = myModel.getAllColumns();
//        for (AbstractColumn abstractColumn : allColumns)
//        {
//          if(abstractColumn.isVisible())
//          {
//            String text = abstractColumn.getProperty((NarrativeEntry) element).toString();
//           if(text!=null && wordMatches(text))
//           {
//             return true;
//           }
//          }
//        }
//        
//        return false;
//      }
//      
//    }, true) ;
    viewer = new GridTableViewer(composite, SWT.V_SCROLL | SWT.BORDER | SWT.MULTI);
    viewer.getGrid().setHeaderVisible(true);
    viewer.getGrid().setLinesVisible(true);
    viewer.getGrid().addControlListener(new ControlAdapter() {
      @Override
      public void controlResized(ControlEvent e) {
        calculateGridRawHeight();
      }
    });
    
    myModel = new NarrativeViewerModel(preferenceStore);

   
    myModel.createTable(this,layout);
 
    
    //
    // addCellDoubleClickListener(new KTableCellDoubleClickAdapter()
    // {
    // public void fixedCellDoubleClicked(final int col, final int row, final int statemask)
    // {
    // final Column column = myModel.getVisibleColumn(col);
    // showFilterDialog(column);
    // final ColumnFilter filter = column.getFilter();
    // if (filter != null)
    // {
    //
    // }
    // }
    // });
  }

 

  public GridTableViewer getViewer()
  {
    return viewer;
  }
  public Composite getControl()
  {
    return composite;
  }

  public NarrativeViewerActions getViewerActions()
  {
    if (myActions == null)
    {
      myActions = new NarrativeViewerActions(this);
    }
    return myActions;
  }

  public void showFilterDialog(final Column column)
  {
    if (!myModel.hasInput())
    {
      return;
    }

    final ColumnFilter filter = column.getFilter();
    if (filter == null)
    {
      return;
    }

    final FilterDialog dialog =
        new FilterDialog(viewer.getGrid().getShell(), myModel.getInput(),
            column);

    if (Dialog.OK == dialog.open())
    {
      dialog.commitFilterChanges();
      refresh();
    }
  }

  public void setInput(final IRollingNarrativeProvider entryWrapper)
  {
    myModel.setInput(entryWrapper);
    refresh();
  }

  public void setTimeFormatter(final TimeFormatter timeFormatter)
  {
    myModel.setTimeFormatter(timeFormatter);
    viewer.refresh();
  }

  public void refresh()
  {
    // check we're not closing
    if(!viewer.getControl().isDisposed())
    {
      viewer.setInput(new Object());
      calculateGridRawHeight();
    }
  }

  private void calculateGridRawHeight()
  {
    Display.getDefault().asyncExec(new Runnable()
    {
      @Override
      public void run()
      {
        final Grid grid = getViewer().getGrid();
        if(grid.isDisposed())
{'fraction_non_alphanumeric': 0.11000810372771475, 'fraction_numerical': 0.0024311183144246355, 'mean_word_length': 2.8962115232833465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4630861', 'n_tokens_mistral': 3048, 'n_tokens_neox': 2975, 'n_words': 607}