📄 app.java
字号:
if ( lookAndFeelInfo.getName( ).equals( lookAndFeelName ) ) { try { UIManager.setLookAndFeel( lookAndFeelInfo.getClassName( ) ); } catch ( Exception e ) { // ignore } lookAndFeelName = null; break; } } if ( lookAndFeelName != null ) showErrorMessage( SHORT_NAME + CONFIG_ERROR_STR, LAF_ERROR_STR1 + lookAndFeelName + LAF_ERROR_STR2 ); // Create document for layout of view documentsViews.add( new DocumentView( new FunctionDocument( ) ) ); // Application: create main window if ( applet == null ) { // Create main window mainWindow = new MainWindow( ); // Start file-check timer new Timer( FILE_CHECK_TIMER_INTERVAL, AppCommand.CHECK_MODIFIED_FILE ).start( ); // No command-line arguments: open new file if configured to do so if ( args == null ) { if ( config.isNewDocumentOnStartup( ) ) AppCommand.NEW_FILE.execute( ); } // Command-line arguments: open files else { // Create list of files from command-line arguments File[] files = new File[args.length]; for ( int i = 0; i < args.length; ++i ) files[i] = new File( args[i] ); // Open files openFiles( files ); // Update title and menus mainWindow.updateTitleAndMenus( ); } } // Applet: create view else { // Set view as applet's content pane FunctionView view = getView( ); applet.setContentPane( view ); // Parse startup parameters String paramName = STARTUP_PARAM_KEY + "." + FunctionDocument.X_INTERVAL_KEY; String paramValue = applet.getParameter( paramName ); if ( paramValue != null ) getDocument( ).parseStartupParam( FunctionDocument.X_INTERVAL_KEY, paramValue, 0 ); paramName = STARTUP_PARAM_KEY + "." + FunctionDocument.Y_INTERVAL_KEY; paramValue = applet.getParameter( paramName ); if ( paramValue != null ) getDocument( ).parseStartupParam( FunctionDocument.Y_INTERVAL_KEY, paramValue, 0 ); for ( int i = 0; i < FunctionDocument.MAX_NUM_FUNCTIONS; ++i ) { paramName = STARTUP_PARAM_KEY + "." + FunctionDocument.FUNCTION_KEY + i; paramValue = applet.getParameter( paramName ); if ( paramValue != null ) getDocument( ).parseStartupParam( FunctionDocument.FUNCTION_KEY, paramValue, i ); } if ( getDocument( ).hasFunctions( ) ) { view.updateFunctionList( ); view.updatePlot( ); } // Set focus view.setDefaultFocus( ); // Test size of applet Dimension prefSize = applet.getPreferredSize( ); String minDimensionsStr = new String( MINIMUM_DIMENSIONS_STR + prefSize.width + " " + WIDTH_STR + " \u00D7 " + prefSize.height + " " + HEIGHT_STR + "." ); System.out.println( APPLET_STR + SHORT_NAME + " " + getVersionString( ) ); if ( (applet.getWidth( ) < prefSize.width) || (applet.getHeight( ) < prefSize.height) ) { System.out.println( DIMENSIONS_ERROR_STR ); showErrorMessage( SHORT_NAME, DIMENSIONS_ERROR_STR + "\n" + minDimensionsStr ); } System.out.println( minDimensionsStr ); } } //------------------------------------------------------------------ public String getVersionString( ) { return getVersionString( false ); } //------------------------------------------------------------------ public String getVersionString( boolean full ) { String str = new String( VERSION_MAJOR + "." + VERSION_MINOR ); if ( full ) { str += "." + VERSION_BUILD; if ( debug ) str += " Debug"; } return str; } //------------------------------------------------------------------ public String getTitleString( ) { return ( LONG_NAME + " " + getVersionString( ) ); } //------------------------------------------------------------------ public void updateTabText( FunctionDocument document ) { for ( int i = 0; i < getNumDocuments( ); ++i ) { if ( getDocument( i ) == document ) { mainWindow.setTabText( i, document.getTitleString( false ), document.getTitleString( true ) ); break; } } } //------------------------------------------------------------------ public void executeCommand( AppCommand command ) { try { Util.getDeclaredMethod( getClass( ), command.getMethodName( ), false ).invoke( this ); } catch ( InvocationTargetException e ) { if ( e.getCause( ) instanceof AppException ) showErrorMessage( SHORT_NAME, e.getCause( ) ); else { System.err.println( e ); e.getCause( ).printStackTrace( ); } } catch ( Exception e ) { e.printStackTrace( ); } if ( command != AppCommand.CHECK_MODIFIED_FILE ) { updateTabText( getDocument( ) ); mainWindow.updateTitleAndMenus( ); } } //------------------------------------------------------------------ public void closeDocument( int index ) { if ( confirmCloseDocument( index ) ) removeDocument( index ); } //------------------------------------------------------------------ private void addDocument( FunctionDocument document ) { DocumentView documentView = new DocumentView( document ); documentsViews.add( documentView ); mainWindow.addView( document.getTitleString( false ), document.getTitleString( true ), documentView.view ); } //------------------------------------------------------------------ private void removeDocument( int index ) { documentsViews.remove( index ); mainWindow.removeView( index ); } //------------------------------------------------------------------ private void openFiles( File[] files ) { for ( int i = 0; i < files.length; ++i ) { if ( isDocumentsFull( ) ) break; try { openDocument( new FunctionDocument( ), files[i] ); if ( Operation.isTerminated( ) ) break; } catch ( AppException e ) { if ( i == files.length - 1 ) showErrorMessage( OPEN_FILE_STR, e ); else { if ( JOptionPane.showOptionDialog( mainWindow, e, OPEN_FILE_STR, JOptionPane.OK_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE, null, CC_OPTION_STRS, CC_OPTION_STRS[1] ) != JOptionPane.OK_OPTION ) break; } } } } //------------------------------------------------------------------ private void openDocument( FunctionDocument document, File file ) throws AppException { // Test whether document is already open for ( int i = 0; i < documentsViews.size( ); ++i ) { if ( Util.isSameFile( file, getDocument( i ).getFile( ) ) ) { mainWindow.selectView( i ); return; } } // Read document and add to list if ( readDocument( document, file ) ) { addDocument( document ); warnComments( document ); } } //------------------------------------------------------------------ private void reopenDocument( File file ) throws AppException { FunctionDocument document = new FunctionDocument( ); if ( readDocument( document, file ) ) { int index = mainWindow.getTabIndex( ); documentsViews.set( index, new DocumentView( document ) ); mainWindow.setTabText( index, document.getTitleString( false ), document.getTitleString( true ) ); mainWindow.setView( index, getView( ) ); warnComments( document ); } } //------------------------------------------------------------------ private boolean confirmCloseDocument( int index ) { // Test whether document has changed FunctionDocument document = getDocument( index ); if ( !document.isChanged( ) ) return true; // Display document mainWindow.selectView( index ); // Display prompt to save changed document FunctionDocument.FileEx fileEx = document.getFileEx( ); String messageStr = ((fileEx.file == null) ? UNNAMED_FILE_STR : getPathname( fileEx.file ) + FILE_STR) + CHANGED_MESSAGE_STR; int result = JOptionPane.showOptionDialog( mainWindow, messageStr, SAVE_CLOSE_FILE_STR, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, SDC_OPTION_STRS, SDC_OPTION_STRS[0] ); // Discard changed document if ( result == JOptionPane.NO_OPTION ) return true; // Save changed document if ( result == JOptionPane.YES_OPTION ) { // Choose filename if ( fileEx.file == null ) { try { fileEx = chooseSave( null, fileEx.fileType ); } catch ( AppException e ) { showErrorMessage( SAVE_CLOSE_FILE_STR, e ); } if ( fileEx == null ) return false; if ( fileEx.file.exists( ) ) { messageStr = getPathname( fileEx.file ) + AppConstants.ALREADY_EXISTS_STR; result = JOptionPane.showConfirmDialog( mainWindow, messageStr, SAVE_CLOSE_FILE_STR, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE ); if ( result == JOptionPane.NO_OPTION ) return true; if ( result != JOptionPane.YES_OPTION ) return false; } } // Write file try { IncludeColours includeColours = confirmIncludeColours( document, SAVE_CLOSE_FILE_STR ); if ( includeColours == null ) return false; writeDocument( document, fileEx, includeColours == IncludeColours.YES ); return true; } catch ( AppException e ) { showErrorMessage( SAVE_CLOSE_FILE_STR, e ); } } return false; } //------------------------------------------------------------------ private boolean readDocument( FunctionDocument document, File file ) throws AppException { boolean documentRead = false; try { List<String> errorStrs = new Vector<String>( ); OperationDialog.showDialog( mainWindow, READ_DOCUMENT_STR, new Operation.ReadDocument( document, file, errorStrs ) ); if ( errorStrs.isEmpty( ) ) documentRead = true; else ErrorListDialog.showDialog( mainWindow, ERRORS_STR, getPathname( file ), errorStrs ); } catch ( TerminatedException e ) { // ignore } return documentRead; } //------------------------------------------------------------------ private void writeDocument( FunctionDocument document, FunctionDocument.FileEx fileEx, boolean includeColours ) throws AppException { OperationDialog.showDialog( mainWindow, WRITE_DOCUMENT_STR, new Operation.WriteDocument( document, fileEx, includeColours ) ); } //------------------------------------------------------------------ private File chooseOpen( ) throws AppException { if ( openFileChooser == null ) { try { openFileChooser = new JFileChooser( AppConfig.getInstance( ).getFunctionDirectory( ) ); openFileChooser.setDialogTitle( OPEN_FUNCTION_FILE_STR );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -