📄 fileprogressdialog.java
字号:
/* * Copyright (C) 2003 Adam Olsen * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 1, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 675 Mass * Ave, Cambridge, MA 02139, USA. */package com.valhalla.jbother;import javax.swing.*;import java.awt.*;import com.valhalla.gui.*;import java.io.FileNotFoundException;import com.valhalla.jbother.jabber.smack.*;public class FileProgressDialog extends ProgressDialog implements FileTransferProgressListener{ private StreamInitiation.FileDetails fileDetails; private JLabel fileProgressLabel = new JLabel( "", SwingConstants.CENTER ); public FileProgressDialog( Component parent, String message, StreamInitiation.FileDetails aFileDetails ) { super( parent, message, 0, 100 ); fileDetails = aFileDetails; fileProgressLabel.setAlignmentX( Component.CENTER_ALIGNMENT ); progressPanel.add( fileProgressLabel ); progressPanel.add( Box.createVerticalGlue()); buttonPanel.removeAll(); buttonPanel.setLayout( new BoxLayout( buttonPanel, BoxLayout.X_AXIS ) ); buttonPanel.add( Box.createHorizontalGlue() ); buttonPanel.add( cancelButton ); buttonPanel.add( Box.createHorizontalGlue() ); fileProgressLabel.setText("0 /" + formatFileSize(fileDetails.getFileSize())); validate(); container.setSize( new Dimension (300,130) ); container.setDefaultCloseOperation( JFrame.DO_NOTHING_ON_CLOSE ); if( !isVisible() ) setVisible( true ); } public void progressUpdate( final int progress, final long currentPosition ) { SwingUtilities.invokeLater( new Runnable() { public void run() { setValue( progress ); fileProgressLabel.setText( formatFileSize(currentPosition) + " / " + formatFileSize(fileDetails.getFileSize()) ); if( progress >= 100 ) { container.dispose(); } } } ); } /** * formats file sizes so that "10.3 kB" is shown instead of 10547 and 1 MB instead of 1048576 * handles file sizes up to petabytes (1024^5), should be enough for us :-) * @param number of bytes * @return String of formatted size (eg. 4.5 MB) */ private String formatFileSize( long number ) { String[] modifier = new String[] { "k", "M", "G", "T", "P" }; int divider = 0; double result = number; if(number < 1024) { return "" + number + " B"; } while( (result = result / 1024) > 1024 ) { divider ++; } String stripped = "" + result; return "" + stripped.substring(0,stripped.indexOf('.') + 2) + " " + modifier[divider] + "B"; } // testing public static void main(String[] args) { try { StreamInitiation.FileDetails fileDetails = new StreamInitiation.FileDetails("/home/luke/snapshot1.png"); FileProgressDialog dialog = new FileProgressDialog(null,"Receiving file",fileDetails); } catch (FileNotFoundException e) { System.err.println("File not found"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -