📄 bulkdownloadpanel.java
字号:
startButtonActionPerformed(event);
}
});
startPanel.add(startButton);
this.add(startPanel);
// Download monitor panel
monitorPanel = new JPanel();
monitorPanel.setLayout(new BoxLayout(monitorPanel, BoxLayout.Y_AXIS));
monitorPanel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
//this.add(monitorPanel);
// Put the monitor panel in a scroll pane.
JPanel dummyPanel = new JPanel(new BorderLayout());
dummyPanel.add(monitorPanel, BorderLayout.NORTH);
JScrollPane scrollPane = new JScrollPane(dummyPanel);
scrollPane.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
scrollPane.setPreferredSize(new Dimension(350, 200));
this.add(scrollPane);
}
public static String makeSectorDescription(Sector sector)
{
return String.format("S %7.4f\u00B0 W %7.4f\u00B0 N %7.4f\u00B0 E %7.4f\u00B0",
sector.getMinLatitude().degrees,
sector.getMinLongitude().degrees,
sector.getMaxLatitude().degrees,
sector.getMaxLongitude().degrees);
}
public static String makeSizeDescription(long size)
{
return String.format("%.1f MB", (double)size / 1024 / 1024);
}
public class BulkRetrievablePanel extends JPanel
{
private BulkRetrievable retrievable;
private JCheckBox selectCheckBox;
private JLabel descriptionLabel;
private Thread updateThread;
private Sector sector;
BulkRetrievablePanel(BulkRetrievable retrievable)
{
this.retrievable = retrievable;
this.initComponents();
}
private void initComponents()
{
this.setLayout(new BorderLayout());
this.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
// Check + name
this.selectCheckBox = new JCheckBox(this.retrievable.getName());
this.selectCheckBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (((JCheckBox)e.getSource()).isSelected() && sector != null)
updateDescription(sector);
}
});
this.add(this.selectCheckBox, BorderLayout.WEST);
// Description (size...)
this.descriptionLabel = new JLabel();
this.add(this.descriptionLabel, BorderLayout.EAST);
}
public void updateDescription(final Sector sector)
{
if (this.updateThread != null && this.updateThread.isAlive())
this.updateThread.interrupt();
this.sector = sector;
if (!this.selectCheckBox.isSelected())
{
doUpdateDescription(null);
return;
}
this.updateThread = new Thread(new Runnable()
{
public void run()
{
descriptionLabel.setText("...");
doUpdateDescription(sector);
}
});
this.updateThread.setDaemon(true);
this.updateThread.start();
}
private void doUpdateDescription(Sector sector)
{
if (sector != null)
{
try
{
this.descriptionLabel.setText(
BulkDownloadPanel.makeSizeDescription(this.retrievable.getEstimatedMissingDataSize(sector, 0)));
}
catch (Exception e)
{
this.descriptionLabel.setText("-");
}
}
else
this.descriptionLabel.setText("-");
}
public String toString()
{
return this.retrievable.getName();
}
}
public class DownloadMonitorPanel extends JPanel
{
private BulkRetrievalThread thread;
private Progress progress;
private Timer updateTimer;
private JLabel descriptionLabel;
private JProgressBar progressBar;
private JButton cancelButton;
public DownloadMonitorPanel(BulkRetrievalThread thread)
{
this.thread = thread;
this.progress = thread.getProgress();
this.initComponents();
this.updateTimer = new Timer(1000, new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
updateStatus();
}
});
this.updateTimer.start();
}
private void updateStatus()
{
// Update description
String text = thread.getRetrievable().getName();
text = text.length() > 30 ? text.substring(0, 27) + "..." : text;
text += " (" + BulkDownloadPanel.makeSizeDescription(this.progress.getCurrentSize())
+ " / " + BulkDownloadPanel.makeSizeDescription(this.progress.getTotalSize())
+ ")";
this.descriptionLabel.setText(text);
// Update progress bar
int percent = 0;
if (this.progress.getTotalCount() > 0)
percent = (int)((float)this.progress.getCurrentCount() / this.progress.getTotalCount() * 100f);
this.progressBar.setValue(Math.min(percent, 100));
// Update tooltip
String tooltip = BulkDownloadPanel.makeSectorDescription(this.thread.getSector());
this.descriptionLabel.setToolTipText(tooltip);
this.progressBar.setToolTipText(makeProgressDescription());
// Check for end of thread
if (!this.thread.isAlive())
{
// Thread is done
this.cancelButton.setText("Remove");
this.cancelButton.setBackground(Color.GREEN);
this.updateTimer.stop();
}
}
private void cancelButtonActionPerformed(ActionEvent event)
{
if (this.thread.isAlive())
{
// Cancel thread
this.thread.interrupt();
this.cancelButton.setBackground(Color.ORANGE);
this.cancelButton.setText("Remove");
this.updateTimer.stop();
}
else
{
// Remove from monitor panel
Container top = this.getTopLevelAncestor();
this.getParent().remove(this);
top.validate();
}
}
private void initComponents()
{
int border = 2;
this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
this.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
// Description label
JPanel descriptionPanel = new JPanel(new GridLayout(0, 1, 0, 0));
descriptionPanel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
String text = thread.getRetrievable().getName();
text = text.length() > 40 ? text.substring(0, 37) + "..." : text;
descriptionLabel = new JLabel(text);
descriptionPanel.add(descriptionLabel);
this.add(descriptionPanel);
// Progrees and cancel button
JPanel progressPanel = new JPanel();
progressPanel.setLayout(new BoxLayout(progressPanel, BoxLayout.X_AXIS));
progressPanel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
progressBar = new JProgressBar(0, 100);
progressBar.setPreferredSize(new Dimension(100, 16));
progressPanel.add(progressBar);
progressPanel.add(Box.createHorizontalStrut(8));
cancelButton = new JButton("Cancel");
cancelButton.setBackground(Color.RED);
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
cancelButtonActionPerformed(event);
}
});
progressPanel.add(cancelButton);
this.add(progressPanel);
}
private String makeProgressDescription()
{
String text = "";
if (this.progress.getTotalCount() > 0)
{
int percent = (int)((float)this.progress.getCurrentCount() / this.progress.getTotalCount() * 100f);
text = percent + "% of ";
text += makeSizeDescription(this.progress.getTotalSize());
}
return text;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -