alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.modules.javacore;

import org.netbeans.api.java.classpath.ClassPath;
import org.netbeans.spi.java.classpath.ClassPathImplementation;
import org.netbeans.modules.javacore.classpath.MergedClassPathImplementation;
import org.netbeans.modules.javacore.internalapi.ProgressListener;
import org.netbeans.modules.javacore.internalapi.ProgressEvent;
import org.openide.util.RequestProcessor;
import org.openide.util.NbBundle;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.MessageFormat;

import javax.swing.border.EmptyBorder;
import org.openide.windows.WindowManager;

public class ProgressPanel extends JDialog implements ProgressListener, PropertyChangeListener{

    private static final float FIRST_RUN_PERCENTAGE = 100;
    private static final String MAX_TEXT = "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"; // NOI18N

    private JProgressBar progress;
    private ClassPathImplementation cp;
    private JMManager model;
    private boolean dirty = false;
    private float count;
    private float value;
    private JLabel labelScanning, labelNote, labelPackage, pleaseWait;
    private JPanel panel, pPanel;

    private ProgressPanel (JMManager model) {
        super(WindowManager.getDefault().getMainWindow());
        this.model = model;
        this.model.getProgressSupport().addProgressListener (this);
        this.cp = model.cpImpl;
        this.cp.addPropertyChangeListener (this);
        this.initComponents ();
        this.value = 0;
    }

    public void start(ProgressEvent event) {
        int ec = event.getCount();
        if (ec < 5) ec = 3;
        this.count = (FIRST_RUN_PERCENTAGE - value) / ((float)ec);
    }

    public void step(final ProgressEvent event) {
        SwingUtilities.invokeLater(
                new Runnable () {
                    public void run() {
                        value+=((float) event.getCount()) * count;
                        progress.setValue(Math.round(value));
                    }
                }
        );
    }

    public void stop(ProgressEvent event) {
        if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: stop1"); // NOI18N
        if (this.getDirty()) {
            if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: stop2 - dirty"); // NOI18N
            this.startResolution ();
        }
        else {
            if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: stop3"); // NOI18N
            SwingUtilities.invokeLater(
                new Runnable () {
                    public void run() {
                        if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: disposing dialog"); // NOI18N
                        ProgressPanel.this.progress.setValue(100);
                        ProgressPanel.this.setVisible(false);
                        ProgressPanel.this.dispose();
                    }
                }
            );
        }
    }

    public synchronized void propertyChange(PropertyChangeEvent evt) {
        if (MergedClassPathImplementation.PROP_UNRESOLVED_ROOTS.equals(evt.getPropertyName())) {
            if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: globalpath changed"); // NOI18N
            this.dirty = true;
        }
    }

    public void setVisible(boolean visible) {
        if (visible) {
            if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: setVisible1"); // NOI18N
            this.startResolution();
        }
        super.setVisible(visible);
        if (visible) {
            if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: setVisible2"); // NOI18N
            this.model.getProgressSupport().removeProgressListener (this);
            this.cp.removePropertyChangeListener (this);
        }
    }

    private synchronized boolean getDirty () {
        boolean res = this.dirty;
        this.dirty = false;
        return res;
    }

    private void startResolution() {
        if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: startResolution"); // NOI18N
        model.startResolution();
    }

    private void initComponents() {
        this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        this.getRootPane().setWindowDecorationStyle(JRootPane.NONE);
        this.setTitle(NbBundle.getMessage (ProgressPanel.class,"TXT_ApplyingPathsTitle"));
        this.setModal(true);
        JComponent pane = (JComponent) this.getContentPane();
        panel = new JPanel(new BorderLayout());
        pPanel = new JPanel(new BorderLayout());
        pane.setLayout(new BorderLayout());
        this.progress = new JProgressBar();
        this.progress.setMinimum(0);
        this.progress.setMaximum(100);
        this.progress.setValue(0);
        
        
        labelScanning = new JLabel(" "); // NOI18N
        labelScanning.setBorder(new EmptyBorder(0, 0, 6, 0));
        panel.add(this.labelScanning, BorderLayout.NORTH);
        labelPackage = new JLabel(MAX_TEXT); // NOI18N
        panel.add(this.labelPackage, BorderLayout.SOUTH);
        
        pPanel.add(this.progress, BorderLayout.NORTH);
        labelNote = new JLabel(" "); // NOI18N
        pPanel.add(labelNote, BorderLayout.SOUTH);

        pane.add(panel, BorderLayout.NORTH);
        pleaseWait = new JLabel(NbBundle.getMessage(ProgressPanel.class, "MSG_PleaseWait"));
        pleaseWait.setHorizontalAlignment(SwingConstants.CENTER);
        pane.add(pleaseWait, BorderLayout.CENTER);
        pane.add(pPanel, BorderLayout.SOUTH);
        pane.setBorder(new EmptyBorder(12,12,3,11));
        
        this.setUndecorated(false);
        this.pack();
        this.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());

        labelPackage.setText(" "); // NOI18N
        panel.setVisible(false);
        pPanel.setVisible(false);
    }

    private static ProgressPanel instance = null;
    
    public void startScanning(final boolean isInitial, final String cpRootName) {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    startScanning(isInitial, cpRootName);
                }
            });
            return;
        }
        pPanel.setVisible(true);
        panel.setVisible(true);
        pleaseWait.setVisible(false);
        if (isInitial) {
            labelNote.setText(NbBundle.getMessage(ProgressPanel.class, "MSG_InitialScanningNote"));
        }
        labelScanning.setText(formatMessage(NbBundle.getMessage(ProgressPanel.class, isInitial ? "MSG_InitialScanning" : "MSG_UpToDateCheck"), cpRootName));
    }
    
    public void startApplyingChanges() {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    startApplyingChanges();
                }
            });
            return;
        }
        labelScanning.setText(NbBundle.getMessage(ProgressPanel.class, "MSG_Applying"));
    }

    private String formatMessage(String message, String parameter) {
//        if (parameter.length() + message.length() > MAX_TEXT.length()) {
//            parameter = "..." + parameter.substring(parameter.length() + message.length() - MAX_TEXT.length());
//        }
        return MessageFormat.format(message, new Object[] {parameter});
    }
    
    public void updatePackage(final String packageName) {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    updatePackage(packageName);
                }
            });
            return;
        }
        labelPackage.setText(formatMessage(NbBundle.getMessage(ProgressPanel.class, "MSG_ScanningPackage"), packageName));
    }
    
    public void preBuildData() {
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    preBuildData();
                }
            });
            return;
        }
        labelPackage.setText(NbBundle.getMessage(ProgressPanel.class, "MSG_ReadingClass"));
    }
    
    public static ProgressPanel getVisibleProgressPanel() {
        if (instance != null && instance.isVisible()) {
            return instance;
        }
        return null;
    }
    
    public static boolean openProgressDialog(JMManager model) {
        if (JMManager.SCAN_DEBUG) System.err.println("ProgressPanel: openProgressDialog"); // NOI18N
        instance = new ProgressPanel(model);
        instance.setVisible(true);
        instance = null;
        return true;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.