📄 functiondocument.java
字号:
private static class YInterval extends Edit { //////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////// private YInterval( PlotInterval oldInterval, PlotInterval newInterval ) { this.oldInterval = new PlotInterval( oldInterval ); this.newInterval = new PlotInterval( newInterval ); timestamp = System.currentTimeMillis( ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////// public void undo( FunctionDocument document ) { document.setYInterval( oldInterval ); } //---------------------------------------------------------- public void redo( FunctionDocument document ) { document.setYInterval( newInterval ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////// private PlotInterval oldInterval; private PlotInterval newInterval; private long timestamp; } //============================================================== // COMMENT EDIT CLASS private static class Comment extends Edit { //////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////// private Comment( String oldText, String newText ) { this.oldText = oldText; this.newText = newText; } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////// public void undo( FunctionDocument document ) { document.comment.setText( oldText ); } //---------------------------------------------------------- public void redo( FunctionDocument document ) { document.comment.setText( newText ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////// private String oldText; private String newText; } //============================================================== // COMPOUND EDIT CLASS private static class Compound extends Edit { //////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////// private Compound( ) { edits = new ArrayList<Edit>( ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////// public void undo( FunctionDocument document ) { for ( int i = edits.size( ) - 1; i >= 0; --i ) edits.get( i ).undo( document ); } //---------------------------------------------------------- public void redo( FunctionDocument document ) { for ( Edit edit : edits ) edit.redo( document ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////// public void addEdit( Edit edit ) { edits.add( edit ); } //---------------------------------------------------------- //////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////// private List<Edit> edits; } //============================================================== //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private Edit( ) { } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : abstract methods //////////////////////////////////////////////////////////////////// public abstract void undo( FunctionDocument document ); //-------------------------------------------------------------- public abstract void redo( FunctionDocument document ); //-------------------------------------------------------------- } //================================================================== // EDIT LIST CLASS private static class EditList extends LinkedList<Edit> { //////////////////////////////////////////////////////////////////// // Constructors //////////////////////////////////////////////////////////////////// private EditList( ) { } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods : overriding methods //////////////////////////////////////////////////////////////////// public void clear( ) { super.clear( ); unchangedIndex = currentIndex = 0; } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance methods //////////////////////////////////////////////////////////////////// public Edit getUndo( ) { return ( canUndo( ) ? get( --currentIndex ) : null ); } //-------------------------------------------------------------- public Edit getRedo( ) { return ( canRedo( ) ? get( currentIndex++ ) : null ); } //-------------------------------------------------------------- public boolean canUndo( ) { return ( currentIndex > 0 ); } //-------------------------------------------------------------- public boolean canRedo( ) { return ( currentIndex < size( ) ); } //-------------------------------------------------------------- public boolean isChanged( ) { return ( currentIndex != unchangedIndex ); } //-------------------------------------------------------------- public void addEdit( Edit edit ) { // Remove redos while ( size( ) > currentIndex ) removeLast( ); // Preserve changed status if unchanged state cannot be recovered if ( unchangedIndex > currentIndex ) unchangedIndex = -1; // Merge intervals mergeXIntervals( edit ); mergeYIntervals( edit ); // Add new edit add( edit ); ++currentIndex; } //-------------------------------------------------------------- public void reset( ) { while ( size( ) > currentIndex ) removeLast( ); unchangedIndex = currentIndex; } //-------------------------------------------------------------- private void mergeXIntervals( Edit edit ) { if ( edit instanceof Edit.XInterval ) { Edit.XInterval currEdit = (Edit.XInterval)edit; while ( canUndo( ) ) { if ( !(getLast( ) instanceof Edit.XInterval) ) break; Edit.XInterval prevEdit = (Edit.XInterval)getLast( ); if ( currEdit.timestamp - prevEdit.timestamp > MERGE_INTERVAL_EDITS_INTERVAL ) break; currEdit.oldInterval = prevEdit.oldInterval; removeLast( ); --currentIndex; } } } //-------------------------------------------------------------- private void mergeYIntervals( Edit edit ) { if ( edit instanceof Edit.YInterval ) { Edit.YInterval currEdit = (Edit.YInterval)edit; while ( canUndo( ) ) { if ( !(getLast( ) instanceof Edit.YInterval) ) break; Edit.YInterval prevEdit = (Edit.YInterval)getLast( ); if ( currEdit.timestamp - prevEdit.timestamp > MERGE_INTERVAL_EDITS_INTERVAL ) break; currEdit.oldInterval = prevEdit.oldInterval; removeLast( ); --currentIndex; } } } //-------------------------------------------------------------- //////////////////////////////////////////////////////////////////// // Instance variables //////////////////////////////////////////////////////////////////// private int currentIndex; private int unchangedIndex; } //==================================================================////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// public FunctionDocument( ) { fileType = AppConfig.getInstance( ).getDefaultFileType( ); functions = new ArrayList<Function>( ); xInterval = new PlotInterval( ); yInterval = new PlotInterval( ); comment = new Comment( ); editList = new EditList( ); } //------------------------------------------------------------------ public FunctionDocument( int untitledIndex ) { this( ); this.untitledIndex = untitledIndex; } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static boolean canWriteImages( ) { return ( Util.getIndex( ImageIO.getWriterFormatNames( ), PNG_STR ) >= 0 ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : SingleSelectionList.Model interface//////////////////////////////////////////////////////////////////////// public int getNumElements( ) { return getNumFunctions( ); } //------------------------------------------------------------------ public Function getElement( int index ) { return getFunction( index ); } //------------------------------------------------------------------ public void setElement( int index, Function function ) { functions.set( index, function ); } //------------------------------------------------------------------ public void addElement( int index, Function function ) { functions.add( index, function ); } //------------------------------------------------------------------ public Function removeElement( int index ) { return functions.remove( index ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// public File getFile( ) { return file;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -