⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bulkdownloadpanel.java

📁 world wind java sdk 源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
Copyright (C) 2001, 2009 United States Government
as represented by the Administrator of the
National Aeronautics and Space Administration.
All Rights Reserved.
*/
package gov.nasa.worldwind.examples;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.globes.ElevationModel;
import gov.nasa.worldwind.terrain.CompoundElevationModel;
import gov.nasa.worldwind.retrieve.*;
import gov.nasa.worldwind.layers.Layer;
import gov.nasa.worldwind.examples.util.SectorSelector;
import gov.nasa.worldwind.geom.Sector;

import javax.swing.*;
import javax.swing.border.*;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.beans.*;

/**
 * Bulk download panel.
 *
 * @author Patrick Murris
 * @version $Id: BulkDownloadPanel.java 10895 2009-05-06 00:24:04Z patrickmurris $
 * @see BulkRetrievalThread, BulkRetrievable
 */
public class BulkDownloadPanel extends JPanel
{
    private WorldWindow wwd;
    private Sector currentSector;
    private ArrayList<BulkRetrievablePanel> retrievables;

    private JButton selectButton;
    private JLabel sectorLabel;
    private JButton startButton;
    private JPanel monitorPanel;

    private SectorSelector selector;

    public BulkDownloadPanel(WorldWindow wwd)
    {
        this.wwd = wwd;

        // Init retievable list
        this.retrievables = new ArrayList<BulkRetrievablePanel>();
        // Layers
        for (Layer layer : this.wwd.getModel().getLayers())
            if (layer instanceof BulkRetrievable)
                this.retrievables.add(new BulkRetrievablePanel((BulkRetrievable)layer));
        // Elevation models
        CompoundElevationModel cem = (CompoundElevationModel)wwd.getModel().getGlobe().getElevationModel();
        for (ElevationModel elevationModel : cem.getElevationModels())
            if (elevationModel instanceof BulkRetrievable)
                this.retrievables.add(new BulkRetrievablePanel((BulkRetrievable)elevationModel));

        // Init sector selector
        this.selector = new SectorSelector(wwd);
        this.selector.setInteriorColor(new Color(1f, 1f, 1f, 0.1f));
        this.selector.setBorderColor(new Color(1f, 0f, 0f, 0.5f));
        this.selector.setBorderWidth(3);
        this.selector.addPropertyChangeListener(SectorSelector.SECTOR_PROPERTY, new PropertyChangeListener()
        {
            public void propertyChange(PropertyChangeEvent evt)
            {
                updateSector();
            }
        });

        JPopupMenu.setDefaultLightWeightPopupEnabled(false);
        ToolTipManager.sharedInstance().setLightWeightPopupEnabled(false);
        this.initComponents();
    }

    private void updateSector()
    {
        this.currentSector = this.selector.getSector();
        if (this.currentSector != null)
        {
            // Update sector description
            this.sectorLabel.setText(makeSectorDescription(this.currentSector));
            this.selectButton.setText("Clear sector");
            this.startButton.setEnabled(true);
        }
        else
        {
            // null sector
            this.sectorLabel.setText("-");
            this.selectButton.setText("Select sector");
            this.startButton.setEnabled(false);
        }
        updateRetrievablePanels(this.currentSector);
    }

    private void updateRetrievablePanels(Sector sector)
    {
        for (BulkRetrievablePanel panel : this.retrievables)
            panel.updateDescription(sector);
    }

    private void selectButtonActionPerformed(ActionEvent event)
    {
        if (this.selector.getSector() != null)
        {
            this.selector.disable();
        }
        else
        {
            this.selector.enable();
        }
        updateSector();
    }

    /**
     * Clear the current selection sector and remove it from the globe.
     */
    public void clearSector()
    {
        if (this.selector.getSector() != null)
        {
            this.selector.disable();
        }
        updateSector();
    }

    private void startButtonActionPerformed(ActionEvent event)
    {
        for( BulkRetrievablePanel panel : this.retrievables)
        {
            if (panel.selectCheckBox.isSelected())
            {
                BulkRetrievable retrievable = panel.retrievable;
                BulkRetrievalThread thread = retrievable.makeLocal(this.currentSector, 0);
                this.monitorPanel.add(new DownloadMonitorPanel(thread));
            }
        }
        this.getTopLevelAncestor().validate();
    }

    /**
     * Determines whether there are any active downloads running.
     *
     * @return <code>true</code> if at leat one download thread is active.
     */
    public boolean hasActiveDownloads()
    {
        for (Component c : this.monitorPanel.getComponents())
        {
            if (c instanceof DownloadMonitorPanel)
                if (((DownloadMonitorPanel)c).thread.isAlive())
                    return true;
        }
        return false;
    }

    /**
     * Cancel all active downloads.
     */
    public void cancelActiveDownloads()
    {
        for (Component c : this.monitorPanel.getComponents())
        {
            if (c instanceof DownloadMonitorPanel)
            {
                if (((DownloadMonitorPanel)c).thread.isAlive())
                {
                    DownloadMonitorPanel panel = (DownloadMonitorPanel)c;
                    panel.cancelButtonActionPerformed(null);
                    try
                    {
                        // Wait for thread to die before moving on
                        long t0 = System.currentTimeMillis();
                        while (panel.thread.isAlive() && System.currentTimeMillis() - t0 < 500)
                            Thread.sleep(10);
                    }
                    catch (Exception ignore) {}
                }
            }
        }
    }

    /**
     * Remove inactive downloads from the monitor panel.
     */
    public void clearInactiveDownloads()
    {
        for (int i = this.monitorPanel.getComponentCount() - 1; i >= 0; i--)
        {
            Component c = this.monitorPanel.getComponents()[i];
            if (c instanceof DownloadMonitorPanel)
            {
                DownloadMonitorPanel panel = (DownloadMonitorPanel)c;
                if (!panel.thread.isAlive() || panel.thread.isInterrupted())
                {
                    this.monitorPanel.remove(i);
                }
            }
        }
        this.monitorPanel.validate();
    }

    private void initComponents()
    {
        int border = 6;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setBorder(
            new CompoundBorder(BorderFactory.createEmptyBorder(9, 9, 9, 9), new TitledBorder("Download")));
        this.setToolTipText("Layer imagery bulk download.");

        // Select sector button
        JPanel sectorPanel = new JPanel(new GridLayout(0, 1, 0, 0));
        sectorPanel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
        selectButton = new JButton("Select sector");
        selectButton.setToolTipText("Press Select then press and drag button 1 on globe");
        selectButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
                selectButtonActionPerformed(event);
            }
        });
        sectorPanel.add(selectButton);
        sectorLabel = new JLabel("-");
        sectorLabel.setPreferredSize(new Dimension(350, 16));
        sectorLabel.setHorizontalAlignment(JLabel.CENTER);
        sectorPanel.add(sectorLabel);
        this.add(sectorPanel);

        // Retrievable list combo and start button
        JPanel retrievablesPanel = new JPanel();
        retrievablesPanel.setLayout(new BoxLayout(retrievablesPanel, BoxLayout.Y_AXIS));
        retrievablesPanel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));

        // RetrievablePanel list
        for (JPanel panel : this.retrievables)
            retrievablesPanel.add(panel);
        this.add(retrievablesPanel);

        // Start button
        JPanel startPanel = new JPanel(new GridLayout(0, 1, 0, 0));
        startPanel.setBorder(BorderFactory.createEmptyBorder(border, border, border, border));
        startButton = new JButton("Start download");
        startButton.setEnabled(false);
        startButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -