All Downloads are FREE. Search and download functionalities are using the official Maven repository.

editor.TabSelectionHistoryItem Maven / Gradle / Ivy

The newest version!
package editor;

import java.nio.file.Path;
import editor.util.SettleModalEventQueue;
import gw.util.GosuObjectUtil;

import javax.swing.undo.StateEditable;
import java.util.Hashtable;

/**
 */
public class TabSelectionHistoryItem implements StateEditable
{
  private NavigationHistory _tabHistory;
  private ITabHistoryContext _tabContext;
  private int _caretPos;
  private ITabHistoryContext _prevTabContext;
  private int _prevCaretPos;
  private boolean _bUndo;

  public TabSelectionHistoryItem( NavigationHistory tabHistory, EditorHost prevTab, int prevCaretPos, EditorHost selectedTab, int caretPos )
  {
    _tabHistory = tabHistory;
    _prevTabContext = _tabHistory.getTabHistoryHandler().makeTabContext( prevTab );
    _prevCaretPos = prevCaretPos;
    _tabContext = _tabHistory.getTabHistoryHandler().makeTabContext( selectedTab );
    _caretPos = caretPos;
    _bUndo = true;
  }

  public void storeState( Hashtable stateTable )
  {
    //noinspection unchecked
    stateTable.put( "_undo", _bUndo ? Boolean.TRUE : Boolean.FALSE );
    _bUndo = !_bUndo;
  }

  public void restoreState( Hashtable stateTable )
  {
    _tabHistory.lock();
    try
    {
      boolean bUndo = ((Boolean)stateTable.get( "_undo" )).booleanValue();
      if( !GosuObjectUtil.equals( _prevTabContext, _tabContext ) )
      {
        assert _caretPos < 0 && _prevCaretPos < 0;
        _tabHistory.getTabHistoryHandler().selectTab( bUndo ? _prevTabContext : _tabContext );
      }
      else
      {
        if( bUndo )
        {
          EditorHost editor = LabFrame.instance().getGosuPanel().findTab( (Path)_prevTabContext.getContentId() );
          _caretPos = editor.getEditor().getCaretPosition();
          editor.getEditor().setCaretPosition( _prevCaretPos );
        }
        else
        {
          EditorHost editor = LabFrame.instance().getGosuPanel().findTab( (Path)_tabContext.getContentId() );
          editor.getEditor().setCaretPosition( _caretPos );
        }
      }

      SettleModalEventQueue.instance().run();
    }
    finally
    {
      _tabHistory.unlock();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy