📄 app.java
字号:
openFileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); openFileChooser.addChoosableFileFilter( FileType.XML.getFilter( ) ); openFileChooser.addChoosableFileFilter( FileType.TEXT.getFilter( ) ); openFileChooser. setFileFilter( AppConfig.getInstance( ).getDefaultFileType( ).getFilter( ) ); applyOrientationByLocale( openFileChooser ); } catch ( SecurityException e ) { throw new AppException( ErrorId.NO_FILE_CHOOSER ); } } openFileChooser.rescanCurrentDirectory( ); return ( (openFileChooser.showOpenDialog( mainWindow ) == JFileChooser.APPROVE_OPTION) ? openFileChooser.getSelectedFile( ) : null ); } //------------------------------------------------------------------ private FunctionDocument.FileEx chooseSave( File file, FileType fileType ) throws AppException { if ( saveFileChooser == null ) { try { saveFileChooser = new JFileChooser( AppConfig.getInstance( ).getFunctionDirectory( ) ); saveFileChooser.setDialogTitle( SAVE_FUNCTION_FILE_STR ); saveFileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); saveFileChooser.addChoosableFileFilter( FileType.XML.getFilter( ) ); saveFileChooser.addChoosableFileFilter( FileType.TEXT.getFilter( ) ); applyOrientationByLocale( saveFileChooser ); } catch ( SecurityException e ) { throw new AppException( ErrorId.NO_FILE_CHOOSER ); } } FunctionDocument.FileEx fileEx = null; if ( file != null ) saveFileChooser.setSelectedFile( file.getAbsoluteFile( ) ); saveFileChooser.rescanCurrentDirectory( ); saveFileChooser.setFileFilter( fileType.getFilter( ) ); if ( saveFileChooser.showSaveDialog( mainWindow ) == JFileChooser.APPROVE_OPTION ) { fileEx = new FunctionDocument.FileEx( saveFileChooser.getSelectedFile( ), null ); if ( saveFileChooser.getFileFilter( ) instanceof FilenameSuffixFilter ) { fileEx.fileType = FileType.get( (FilenameSuffixFilter)saveFileChooser.getFileFilter( ) ); if ( fileEx.fileType != null ) fileEx.file = Util.appendSuffix( fileEx.file, fileEx.fileType.getFilter( ).getSuffix( 0 ) ); } } return fileEx; } //------------------------------------------------------------------ private File chooseExport( ) throws AppException { if ( exportFileChooser == null ) { try { exportFileChooser = new JFileChooser( AppConfig.getInstance( ).getFunctionDirectory( ) ); exportFileChooser.setDialogTitle( EXPORT_IMAGE_FILE_STR ); exportFileChooser.setFileSelectionMode( JFileChooser.FILES_ONLY ); exportFileChooser. addChoosableFileFilter( new FilenameSuffixFilter( AppConstants.PNG_FILE_SUFFIX, AppConstants.PNG_FILES_STR ) ); applyOrientationByLocale( exportFileChooser ); } catch ( SecurityException e ) { throw new AppException( ErrorId.NO_FILE_CHOOSER ); } } if ( exportFile != null ) exportFileChooser.setSelectedFile( exportFile.getAbsoluteFile( ) ); exportFileChooser.rescanCurrentDirectory( ); if ( exportFileChooser.showSaveDialog( mainWindow ) == JFileChooser.APPROVE_OPTION ) { exportFile = Util.appendSuffix( exportFileChooser.getSelectedFile( ), AppConstants.PNG_FILE_SUFFIX ); return exportFile; } return null; } //------------------------------------------------------------------ private void warnComments( FunctionDocument document ) { if ( document.hasComments( ) ) JOptionPane.showMessageDialog( mainWindow, HAS_COMMENTS_STR, OPEN_FILE_STR, JOptionPane.WARNING_MESSAGE ); } //------------------------------------------------------------------ private boolean confirmWriteFile( File file, String titleStr ) { return ( !file.exists( ) || (JOptionPane.showOptionDialog( mainWindow, getPathname( file ) + AppConstants.ALREADY_EXISTS_STR, titleStr, JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, AppConstants.RC_OPTION_STRS, AppConstants.RC_OPTION_STRS[1] ) == JOptionPane.OK_OPTION) ); } //------------------------------------------------------------------ private IncludeColours confirmIncludeColours( FunctionDocument document, String titleStr ) { if ( !document.hasFunctions( ) ) return IncludeColours.NO; switch ( AppConfig.getInstance( ).getSaveFunctionColours( ) ) { case NO: return IncludeColours.NO; case YES: return IncludeColours.YES; case ASK: switch ( JOptionPane.showConfirmDialog( mainWindow, SAVE_COLOURS_STR, titleStr, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE ) ) { case JOptionPane.YES_OPTION: return IncludeColours.YES; case JOptionPane.NO_OPTION: return IncludeColours.NO; } } return null; } //------------------------------------------------------------------ private void doTransferFiles( ) { openFiles( (File[])AppCommand.TRANSFER_FILES.getValue( AppCommand.Property.FILES ) ); } //------------------------------------------------------------------ private void doCheckModifiedFile( ) throws AppException { FunctionDocument document = getDocument( ); if ( document != null ) { File file = document.getFile( ); long timestamp = document.getTimestamp( ); if ( (file != null) && (timestamp != 0) ) { long currentTimestamp = file.lastModified( ); if ( (currentTimestamp != 0) && (currentTimestamp != timestamp) ) { String messageStr = getPathname( file ) + MODIFIED_MESSAGE_STR; if ( JOptionPane.showConfirmDialog( mainWindow, messageStr, MODIFIED_FILE_STR, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE ) == JOptionPane.YES_OPTION ) { reopenDocument( file ); updateTabText( getDocument( ) ); mainWindow.updateTitleAndMenus( ); } else document.setTimestamp( currentTimestamp ); } } } } //------------------------------------------------------------------ private void doNewFile( ) { if ( !isDocumentsFull( ) ) addDocument( new FunctionDocument( ++newFileIndex ) ); } //------------------------------------------------------------------ private void doOpenFile( ) throws AppException { if ( !isDocumentsFull( ) ) { File file = chooseOpen( ); if ( file != null ) openDocument( new FunctionDocument( ), file ); } } //------------------------------------------------------------------ private void doReopenFile( ) throws AppException { FunctionDocument document = getDocument( ); if ( (document != null) && document.isChanged( ) ) { File file = document.getFile( ); if ( file != null ) { String messageStr = getPathname( file ) + REOPEN_MESSAGE_STR; if ( JOptionPane.showOptionDialog( mainWindow, messageStr, REOPEN_FILE_STR, JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, OC_OPTION_STRS, OC_OPTION_STRS[1] ) == JOptionPane.OK_OPTION ) reopenDocument( file ); } } } //------------------------------------------------------------------ private void doCloseFile( ) { if ( hasDocuments( ) ) closeDocument( mainWindow.getTabIndex( ) ); } //------------------------------------------------------------------ private void doCloseAllFiles( ) { while ( hasDocuments( ) ) { int index = getNumDocuments( ) - 1; if ( !confirmCloseDocument( index ) ) break; removeDocument( index ); } } //------------------------------------------------------------------ private void doSaveFile( ) throws AppException { FunctionDocument document = getDocument( ); if ( (document != null) && document.isChanged( ) ) { if ( document.getFile( ) == null ) doSaveFileAs( ); else { IncludeColours includeColours = confirmIncludeColours( document, SAVE_FILE_STR ); if ( includeColours != null ) writeDocument( document, null, includeColours == IncludeColours.YES ); } } } //------------------------------------------------------------------ private void doSaveFileAs( ) throws AppException { FunctionDocument document = getDocument( ); if ( document != null ) { FunctionDocument.FileEx fileEx = document.getFileEx( ); fileEx = chooseSave( fileEx.file, fileEx.fileType ); if ( (fileEx != null) && confirmWriteFile( fileEx.file, SAVE_FILE_AS_STR ) ) { IncludeColours includeColours = confirmIncludeColours( document, SAVE_FILE_AS_STR ); if ( includeColours != null ) writeDocument( document, fileEx, includeColours == IncludeColours.YES ); } } } //------------------------------------------------------------------ private void doExportImage( ) throws AppException { FunctionDocument document = getDocument( ); if ( document != null ) { File file = chooseExport( ); if ( (file != null) && confirmWriteFile( file, EXPORT_IMAGE_STR ) ) OperationDialog.showDialog( mainWindow, WRITE_IMAGE_STR, new Operation.WriteImage( document, file ) ); } } //------------------------------------------------------------------ private void doExit( ) { // Prevent re-entry to this method if ( exiting ) return; exiting = true; // Close all open documents while ( hasDocuments( ) ) { int index = getNumDocuments( ) - 1; if ( !confirmCloseDocument( index ) ) { exiting = false; return; } removeDocument( index ); } // Get location of main window AppConfig config = AppConfig.getInstance( ); config.setMainWindowLocation( mainWindow.getLocationOnScreen( ) ); // Write configuration file config.write( ); // Destroy main window mainWindow.setVisible( false ); mainWindow.dispose( ); // Exit application System.exit( 0 ); } //------------------------------------------------------------------ private void doCopyIntervals( ) { int numDocuments = getNumDocuments( ); if ( numDocuments >= 2 ) { int currentIndex = mainWindow.getTabIndex( ); String[] strs = new String[numDocuments - 1]; int index = 0; for ( int i = 0; i < numDocuments; ++i ) { if ( i != currentIndex ) strs[index++] = getDocument( i ). getTitleString( AppConfig.getInstance( ).isShowFullPathnames( ) ); } ListSelectionDialog dialog = ListSelectionDialog.showDialog( mainWindow, COPY_INTERVALS_STR, DOCUMENTS_STR, strs ); if ( dialog.isAccepted( ) ) { FunctionDocument document = getDocument( ); for ( int i : dialog.getSelections( ) ) { if ( i >= currentIndex ) ++i; getDocument( i ).setIntervals( document.getXInterval( ), document.getYInterval( ) ); getView( i ).updateIntervals( ); } } } } //------------------------------------------------------------------ private void doEditPreferences( ) { if ( PreferencesDialog.showDialog( mainWindow ).isAccepted( ) ) { ExceptionUtilities.setUnix( AppConfig.getInstance( ).isShowUnixPathnames( ) ); if ( getView( ) != null ) getView( ).repaint( ); } } //------------------------------------------------------------------ private void doShowFullPathnames( ) { AppConfig.getInstance( ).setShowFullPathnames( !AppConfig.getInstance( ).isShowFullPathnames( ) ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static App instance; private static boolean debug;////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private JApplet applet; private MainWindow mainWindow; private List<DocumentView> documentsViews; private JFileChooser openFileChooser; private JFileChooser saveFileChooser; private JFileChooser exportFileChooser; private File exportFile; private int newFileIndex; private boolean exiting;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -