📄 operationdialog.java
字号:
public void windowClosing( WindowEvent event ) { location = getLocation( ); if ( closed ) dispose( ); else { cancelled = true; Operation.setTerminated( true ); } } //-------------------------------------------------------------- public void windowOpened( WindowEvent event ) { Operation.setProgressView( (OperationDialog)event.getWindow( ) ); Operation.setException( null, true ); Operation.setTerminated( false ); operation.start( ); } //-------------------------------------------------------------- } //==================================================================////////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////////// private OperationDialog( Window owner, String titleStr, Operation operation ) throws AppException { super( owner, titleStr, Dialog.ModalityType.APPLICATION_MODAL ); init( owner, operation ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class methods//////////////////////////////////////////////////////////////////////// public static void showDialog( Window owner, String titleStr, Operation operation ) throws AppException { new OperationDialog( owner, titleStr, operation ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : ActionListener interface//////////////////////////////////////////////////////////////////////// public void actionPerformed( ActionEvent event ) { if ( event.getActionCommand( ).equals( Command.CLOSE ) ) { cancelButton.setEnabled( false ); dispatchEvent( new WindowEvent( this, Event.WINDOW_DESTROY ) ); } } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : ProgressView interface//////////////////////////////////////////////////////////////////////// public void setInfo( String str ) { setInfo( str, null ); } //------------------------------------------------------------------ public void setInfo( String str, File file ) { try { SwingUtilities.invokeAndWait( new DoSetInfo( str, file ) ); } catch ( Exception e ) { // ignore } } //------------------------------------------------------------------ public void setProgress( double value ) { try { SwingUtilities.invokeAndWait( new DoSetProgress( value ) ); } catch ( Exception e ) { // ignore } } //------------------------------------------------------------------ public void close( ) { closed = true; dispatchEvent( new WindowEvent( this, Event.WINDOW_DESTROY ) ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods : TextFile.ProgressListener interface//////////////////////////////////////////////////////////////////////// public void progress( double fractionDone ) { setProgress( fractionDone ); } //------------------------------------------------------------------ public boolean isOperationTerminated( ) { return Operation.isTerminated( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Instance methods//////////////////////////////////////////////////////////////////////// private void init( Window owner, Operation operation ) throws AppException { // Set icons setIconImages( owner.getIconImages( ) ); // Initialise instance variables this.operation = operation; //---- Info field infoField = new InfoField( ); //---- Progress bar progressBar = new JProgressBar( 0, PROGRESS_BAR_MAX_VALUE ); progressBar.setPreferredSize( new Dimension( PROGRESS_BAR_WIDTH, PROGRESS_BAR_HEIGHT ) ); //---- Button panel JPanel buttonPanel = new JPanel( new GridLayout( 1, 0, 0, 0 ) ); buttonPanel.setBorder( BorderFactory.createEmptyBorder( 2, 8, 3, 8 ) ); // Button: cancel cancelButton = new FButton( AppConstants.CANCEL_STR ); cancelButton.setActionCommand( Command.CLOSE ); cancelButton.addActionListener( this ); buttonPanel.add( cancelButton ); //---- Main panel GridBagLayout gridBag = new GridBagLayout( ); GridBagConstraints gbc = new GridBagConstraints( ); JPanel mainPanel = new JPanel( gridBag ); mainPanel.setBorder( BorderFactory.createEmptyBorder( 2, 2, 2, 2 ) ); int gridY = 0; gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 8, 6, 8, 6 ); gridBag.setConstraints( infoField, gbc ); mainPanel.add( infoField ); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets( 4, 4, 0, 4 ); gridBag.setConstraints( progressBar, gbc ); mainPanel.add( progressBar ); gbc.gridx = 0; gbc.gridy = gridY++; gbc.gridwidth = 1; gbc.gridheight = 1; gbc.weightx = 0.0; gbc.weighty = 0.0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.NONE; gbc.insets = new Insets( 4, 0, 0, 0 ); gridBag.setConstraints( buttonPanel, gbc ); mainPanel.add( buttonPanel ); //---- Window // Set content pane setContentPane( mainPanel ); // Set orientation of components according to locale App.applyOrientationByLocale( this ); // Add key commands to action map KeyAction.create( mainPanel, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ), Command.CLOSE, this ); // Dispose of window explicitly setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); // Handle window closing addWindowListener( new WindowEventHandler( ) ); // Prevent dialog from being resized setResizable( false ); // Resize dialog to its preferred size pack( ); // Set location of dialog box if ( location == null ) location = GuiUtilities.getComponentLocation( this, owner ); setLocation( location ); // Set default button getRootPane( ).setDefaultButton( cancelButton ); // Show dialog setVisible( true ); // Throw any exception from operation thread Operation.throwOnException( ); } //------------------------------------------------------------------////////////////////////////////////////////////////////////////////////// Class variables//////////////////////////////////////////////////////////////////////// private static Point location;////////////////////////////////////////////////////////////////////////// Instance variables//////////////////////////////////////////////////////////////////////// private Operation operation; private boolean closed; private boolean cancelled; private InfoField infoField; private JProgressBar progressBar; private JButton cancelButton;}//----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -