📄 functiondocument.java
字号:
} //------------------------------------------------------------------ public FileEx getFileEx( ) { return new FileEx( file, fileType ); } //------------------------------------------------------------------ public long getTimestamp( ) { return timestamp; } //------------------------------------------------------------------ public PlotInterval getXInterval( ) { return xInterval; } //------------------------------------------------------------------ public PlotInterval getYInterval( ) { return yInterval; } //------------------------------------------------------------------ public boolean hasComments( ) { return commented; } //------------------------------------------------------------------ public boolean isChanged( ) { return editList.isChanged( ); } //------------------------------------------------------------------ public int getNumFunctions( ) { return functions.size( ); } //------------------------------------------------------------------ public Function getFunction( int index ) { return functions.get( index ); } //------------------------------------------------------------------ public boolean hasFunctions( ) { return !functions.isEmpty( ); } //------------------------------------------------------------------ public boolean isFull( ) { return ( functions.size( ) >= MAX_NUM_FUNCTIONS ); } //------------------------------------------------------------------ public String getTitleString( boolean fullPathname ) { String str = (file == null) ? UNTITLED_STR + untitledIndex : fullPathname ? App.getPathname( file ) : file.getName( ); if ( isChanged( ) ) str += AppConstants.FILE_CHANGED_SUFFIX; return str; } //------------------------------------------------------------------ public void setTimestamp( long timestamp ) { this.timestamp = timestamp; } //------------------------------------------------------------------ public void setIntervals( PlotInterval xInterval, PlotInterval yInterval ) { setXInterval( xInterval ); setYInterval( yInterval ); } //------------------------------------------------------------------ public void read( File file, List<String> errorStrs ) throws AppException { // Clear error strings errorStrs.clear( ); // Test whether file exists if ( !file.exists( ) ) throw new FileException( ErrorId.FILE_DOES_NOT_EXIST, file ); if ( !file.isFile( ) ) throw new FileException( ErrorId.NOT_A_FILE, file ); // Set file this.file = file; // Update information field in progress view OperationDialog progressView = (OperationDialog)Operation.getProgressView( ); progressView.setInfo( READING_STR, file ); progressView.setProgress( 0.0 ); // Run garbage collector to maximise available memory System.gc( ); // Read file TextFile textFile = new TextFile( file, XmlConstants.UTF8_STR ); textFile.addProgressListener( progressView ); StringBuilder text = textFile.read( ); // Change line separators to LFs TextFile.changeLineSeparators( text, false ); // Parse file if ( isXml( text ) ) { fileType = FileType.XML; parseXml( text ); } else { fileType = FileType.TEXT; parseText( text, errorStrs ); } // Set timestamp if ( errorStrs.isEmpty( ) ) timestamp = file.lastModified( ); } //------------------------------------------------------------------ public void write( FileEx fileEx, boolean includeColours ) throws AppException { if ( fileEx != null ) { file = fileEx.file; if ( fileEx.fileType != null ) fileType = fileEx.fileType; } write( includeColours ); } //------------------------------------------------------------------ public void writeImage( File file ) throws AppException { // Reset progress view ProgressView progressView = Operation.getProgressView( ); progressView.setInfo( WRITING_STR, file ); progressView.setProgress( -1.0 ); // Write file File tempFile = null; FileChannel fileChannel = null; BufferedOutputStream outStream = null; boolean oldFileDeleted = false; try { // Create temporary file try { tempFile = File.createTempFile( AppConstants.TEMP_FILE_PREFIX, null, file.getAbsoluteFile( ).getParentFile( ) ); } catch ( Exception e ) { throw new AppException( ErrorId.FAILED_TO_CREATE_TEMPORARY_FILE ); } // Open output stream on temporary file try { FileOutputStream fileOutStream = new FileOutputStream( tempFile ); fileChannel = fileOutStream.getChannel( ); outStream = new BufferedOutputStream( fileOutStream ); } catch ( FileNotFoundException e ) { throw new FileException( ErrorId.FAILED_TO_OPEN_FILE, tempFile ); } catch ( SecurityException e ) { throw new FileException( ErrorId.FILE_ACCESS_NOT_PERMITTED, tempFile ); } // Lock file try { if ( fileChannel.tryLock( ) == null ) throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, tempFile ); } catch ( Exception e ) { throw new FileException( ErrorId.FAILED_TO_LOCK_FILE, tempFile, e ); } // Write file try { // Generate image BufferedImage image = getImage( ); // Test whether operation has been cancelled Operation.throwOnTerminated( ); // Write image to output stream if ( !ImageIO.write( image, PNG_STR, outStream ) ) throw new AppException( ErrorId.PNG_NOT_SUPPORTED ); // Update progress of operation progressView.setProgress( 1.0 ); } catch ( IOException e ) { throw new FileException( ErrorId.ERROR_WRITING_FILE, tempFile ); } // Close output stream try { outStream.close( ); outStream = null; } catch ( IOException e ) { throw new FileException( ErrorId.FAILED_TO_CLOSE_FILE, tempFile ); } // Delete any existing file try { if ( file.exists( ) && !file.delete( ) ) throw new FileException( ErrorId.FAILED_TO_DELETE_FILE, file ); oldFileDeleted = true; } catch ( SecurityException e ) { throw new FileException( ErrorId.FAILED_TO_DELETE_FILE, file ); } // Rename temporary file try { if ( !tempFile.renameTo( file ) ) throw new TempFileException( ErrorId.FAILED_TO_RENAME_FILE, file, tempFile ); } catch ( SecurityException e ) { throw new TempFileException( ErrorId.FAILED_TO_RENAME_FILE, file, tempFile ); } } catch ( AppException e ) { try { if ( outStream != null ) outStream.close( ); } catch ( Exception e1 ) { // ignore } try { if ( !oldFileDeleted && (tempFile != null) && tempFile.exists( ) ) tempFile.delete( ); } catch ( Exception e1 ) { // ignore } throw e; } } //------------------------------------------------------------------ public void parseStartupParam( String name, String value, int functionIndex ) { try { if ( name.equals( X_INTERVAL_KEY ) ) setXInterval( parseInterval( value ) ); else if ( name.equals( Y_INTERVAL_KEY ) ) setYInterval( parseInterval( value ) ); else if ( name.equals( FUNCTION_KEY ) ) { if ( !isFull( ) ) { Color defaultColour = AppConfig.getInstance( ).getFunctionColour( functionIndex++ ); functions.add( parseFunction( value, defaultColour ) ); } } } catch ( AppException e ) { App.getInstance( ).showErrorMessage( App.SHORT_NAME, e ); } } //------------------------------------------------------------------ public void updateCommands( ) { int index = getSelectedIndex( ); boolean isFunctionSelected = hasFunctions( ) && (index >= 0); boolean hidden = isFunctionSelected && !getFunction( index ).isVisible( ); Command.setAllEnabled( true ); Command.UNDO.setEnabled( editList.canUndo( ) ); Command.REDO.setEnabled( editList.canRedo( ) ); Command.CLEAR_EDIT_LIST.setEnabled( !editList.isEmpty( ) ); Command.ADD_FUNCTION.setEnabled( !isFull( ) ); Command.EDIT_FUNCTION.setEnabled( isFunctionSelected ); Command.COPY_FUNCTION.setEnabled( AppConfig.getInstance( ).hasPermissionAccessClipboard( ) && isFunctionSelected ); Command command = Command.HIDE_SHOW_FUNCTION; command.setEnabled( isFunctionSelected ); command.putValue( Action.NAME, hidden ? Command.SHOW_STR : Command.HIDE_STR ); command.putValue( Action.SHORT_DESCRIPTION, hidden ? Command.SHOW_FUNCTION_STR : Command.HIDE_FUNCTION_STR ) ; Command.DELETE_FUNCTION.setEnabled( isFunctionSelected ); Command.CONFIRM_DELETE_FUNCTION.setEnabled( isFunctionSelected ); Command.CLEAR_FUNCTIONS.setEnabled( hasFunctions( ) ); Command.REVERSE_FUNCTIONS.setEnabled( getNumFunctions( ) > 1 ); } //------------------------------------------------------------------ public void executeCommand( Command command ) { // Perform command Edit edit = null; try { try { edit = (Edit)Util.getDeclaredMethod( getClass( ), command.getMethodName( ), false ). invoke( this ); } catch ( InvocationTargetException e ) { if ( e.getCause( ) instanceof OutOfMemoryError ) throw new AppException( ErrorId.NOT_ENOUGH_MEMORY_TO_PERFORM_COMMAND ); if ( e.getCause( ) instanceof AppException ) throw (AppException)e.getCause( ); System.err.println( e ); e.getCause( ).printStackTrace( ); } catch ( Exception e ) { e.printStackTrace( ); } } catch ( AppException e ) { App.getInstance( ).showErrorMessage( App.SHORT_NAME, e ); } // Add edit to undo list if ( edit != null ) editList.addEdit( edit ); // Update function buttons
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -