FileDocCategorySizeDatePackage
ScreenViewer.javaAPI DocAndroid 1.5 API27120Wed May 06 22:41:10 BST 2009com.android.hierarchyviewer.ui

ScreenViewer

public class ScreenViewer extends JPanel implements ActionListener

Fields Summary
private final Workspace
workspace
private final com.android.ddmlib.Device
device
private GetScreenshotTask
task
private BufferedImage
image
private int[]
scanline
private volatile boolean
isLoading
private BufferedImage
overlay
private AlphaComposite
overlayAlpha
private LoupeStatus
status
private LoupeViewer
loupe
private Crosshair
crosshair
private int
zoom
private int
y
private Timer
timer
private com.android.hierarchyviewer.scene.ViewNode
node
private JSlider
zoomSlider
Constructors Summary
ScreenViewer(Workspace workspace, com.android.ddmlib.Device device, int spacing)


          
        setLayout(new BorderLayout());
        setOpaque(false);

        this.workspace = workspace;
        this.device = device;

        timer = new Timer(5000, this);
        timer.setInitialDelay(0);
        timer.setRepeats(true);

        JPanel panel = buildViewerAndControls();
        add(panel, BorderLayout.WEST);

        JPanel loupePanel = buildLoupePanel(spacing);
        add(loupePanel, BorderLayout.CENTER);

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                timer.start();                
            }
        });
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent event)

        if (task != null && !task.isDone()) {
            return;
        }
        task = new GetScreenshotTask();
        task.execute();
    
private javax.swing.JPanelbuildLoupePanel(int spacing)

        loupe = new LoupeViewer();
        loupe.addMouseWheelListener(new WheelZoomListener());
        CrosshairPanel crosshairPanel = new CrosshairPanel(loupe);

        JPanel loupePanel = new JPanel(new BorderLayout());
        loupePanel.add(crosshairPanel);
        status = new LoupeStatus();
        loupePanel.add(status, BorderLayout.SOUTH);

        loupePanel.setBorder(BorderFactory.createEmptyBorder(0, spacing, 0, 0));
        return loupePanel;
    
private voidbuildOverlayExtraControls(javax.swing.JPanel panel)

        JPanel extras = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));

        JButton loadOverlay = new JButton("Load...");
        loadOverlay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                SwingWorker<?, ?> worker = openOverlay();
                if (worker != null) {
                    worker.execute();
                }
            }
        });
        extras.add(loadOverlay);

        JCheckBox showInLoupe = new JCheckBox("Show in Loupe");
        showInLoupe.setSelected(false);
        showInLoupe.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                loupe.showOverlay = ((JCheckBox) event.getSource()).isSelected();
                loupe.repaint();
            }
        });
        extras.add(showInLoupe);

        panel.add(extras, new GridBagConstraints(1, y++, 1, 1, 1.0f, 0.0f,
                    GridBagConstraints.LINE_START, GridBagConstraints.NONE,
                        new Insets(0, 0, 0, 0), 0, 0));
    
private javax.swing.JSliderbuildSlider(javax.swing.JPanel panel, java.lang.String title, java.lang.String minName, java.lang.String maxName, int min, int max, int value, int tick)

        panel.add(new JLabel(title), new GridBagConstraints(0, y, 1, 1, 1.0f, 0.0f,
                    GridBagConstraints.LINE_END, GridBagConstraints.NONE,
                    new Insets(0, 0, 0, 6), 0, 0));
        JPanel sliderPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        sliderPanel.add(new JLabel(minName));
        JSlider slider = new JSlider(min, max, value);
        slider.setMinorTickSpacing(tick);
        slider.setMajorTickSpacing(tick);
        slider.setSnapToTicks(true);
        sliderPanel.add(slider);
        sliderPanel.add(new JLabel(maxName));
        panel.add(sliderPanel, new GridBagConstraints(1, y++, 1, 1, 1.0f, 0.0f,
                    GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE,
                        new Insets(0, 0, 0, 0), 0, 0));
        return slider;
    
private javax.swing.JPanelbuildViewerAndControls()

        JPanel panel = new JPanel(new GridBagLayout());
        crosshair = new Crosshair(new ScreenshotViewer());
        crosshair.addMouseWheelListener(new WheelZoomListener());
        panel.add(crosshair,
                new GridBagConstraints(0, y++, 2, 1, 1.0f, 0.0f,
                    GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE,
                    new Insets(0, 0, 0, 0), 0, 0));
        buildSlider(panel, "Overlay:", "0%", "100%", 0, 100, 30, 1).addChangeListener(
                new ChangeListener() {
                    public void stateChanged(ChangeEvent event) {
                        float opacity = ((JSlider) event.getSource()).getValue() / 100.0f;
                        overlayAlpha = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity);
                        repaint();
                    }
        });
        buildOverlayExtraControls(panel);
        buildSlider(panel, "Refresh Rate:", "1s", "40s", 1, 40, 5, 1).addChangeListener(
                new ChangeListener() {
                    public void stateChanged(ChangeEvent event) {
                        int rate = ((JSlider) event.getSource()).getValue() * 1000;
                        timer.setDelay(rate);
                        timer.setInitialDelay(0);
                        timer.restart();
                    }
        });
        zoomSlider = buildSlider(panel, "Zoom:", "2x", "24x", 2, 24, 8, 2);
        zoomSlider.addChangeListener(
                new ChangeListener() {
                    public void stateChanged(ChangeEvent event) {
                        zoom = ((JSlider) event.getSource()).getValue();
                        loupe.clearGrid = true;
                        loupe.moveToPoint(crosshair.crosshair.x, crosshair.crosshair.y);
                        repaint();
                    }
        });
        panel.add(Box.createVerticalGlue(),
                new GridBagConstraints(0, y++, 2, 1, 1.0f, 1.0f,
                    GridBagConstraints.FIRST_LINE_START, GridBagConstraints.NONE,
                    new Insets(0, 0, 0, 0), 0, 0));
        return panel;
    
public org.jdesktop.swingworker.SwingWorkeropenOverlay()

        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(new PngFileFilter());
        int choice = chooser.showOpenDialog(this);
        if (choice == JFileChooser.APPROVE_OPTION) {
            return new OpenOverlayTask(chooser.getSelectedFile());
        } else {
            return null;
        }
    
voidselect(com.android.hierarchyviewer.scene.ViewNode node)

        this.node = node;
        repaint();
    
voidstart()

        timer.start();
    
voidstop()

        timer.stop();