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

/*
 * DeployProgressMonitor.java
 *
 * Created on December 17, 2002, 10:38 AM
 */

package org.netbeans.modules.j2ee.deployment.impl.ui;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JCheckBox;
import javax.swing.SwingUtilities;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.MessageFormat;

import org.openide.util.NbBundle;
import org.openide.NotifyDescriptor;
import org.openide.DialogDisplayer;
import org.openide.ErrorManager;

import javax.enterprise.deploy.spi.status.ProgressObject;
import javax.enterprise.deploy.spi.status.ProgressListener;
import javax.enterprise.deploy.spi.status.DeploymentStatus;
import javax.enterprise.deploy.spi.status.ProgressEvent;
import javax.enterprise.deploy.shared.StateType;
import javax.enterprise.deploy.shared.CommandType;
import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException;
import org.netbeans.modules.j2ee.deployment.plugins.api.ServerProgress;
import org.netbeans.modules.j2ee.deployment.impl.ServerInstance;

import java.util.Vector;
import java.util.Iterator;
import java.util.Map;
import java.util.HashMap;

/**
 *
 * @author  Jeri Lockhart
 */
public class ServerStatusBar implements DeployProgressUI, ProgressListener {
    
    private ServerInstance instance;
    
    private ProgressUI progUI = null;
    private ProgressObject progObj = null;
    private DeploymentStatus status = null;
    private int max = 0;
    private CommandType currCommand = null;
    private int workCompleted = 0;
    private int progressRate = 1;
    private static boolean debugMode = false;
    private boolean stateChanging = false;
    
    private JPanel buttonPanel;
    private JButton startCommandBtn;
    private JButton startDebugCommandBtn;
    private JButton stopCommandBtn;
    private JButton closeCommandBtn;
    private JCheckBox debugModeCheck;
    private GridBagConstraints gridBagConstraints;
    private int progressEventCount = 0;
    private Vector cancelHandlers = new Vector();
    
    public ServerStatusBar(String title) {
        progUI = new ProgressUI(false);
        progUI.setTitle(title);
    }

    public ServerStatusBar(ServerInstance instance) {
        this(NbBundle.getMessage(ServerStatusBar.class, "LBL_ServerStatus", instance.getDisplayName()));
        this.instance = instance;
        addButtonPanel();
    }
    
    public void startProgressUI(ProgressObject obj, int max) throws NullPointerException {
        setProgressObject(obj);
        startProgressUI(max);
    }
    
    public void startProgressUI(int max) {
        this.max = max;
        String msg = NbBundle.getMessage(ServerStatusBar.class, "MSG_CheckingStatus");
        progUI.startTask("", max); //NOI18N
        progUI.addMessage(msg);
        setCommandControlButtons();
    }
    
    public boolean setProgressObject(ProgressObject obj) {
        if (obj == null) {
            if (progObj != null) {
                progObj.removeProgressListener(this);
                progObj = null;
            }
            return false;
        }

        //unregister with previous PO
        if (progObj != null) 
            progObj.removeProgressListener(this);

        progObj = obj;
        progObj.addProgressListener(this);
        progUI.addError("");    //NOI18N
        return true;
    }
    
    public void handleProgressEvent(ProgressEvent progressEvent) {
        status = progressEvent.getDeploymentStatus();
        StateType state = status.getState();
        progUI.addError("");
        
        if (state == StateType.COMPLETED) {
            progUI.addMessage(status.getMessage());
            progUI.recordWork(max);
            stateChanging = false;
            setCommandControlButtons();
        }
        else if (state == StateType.FAILED) {
            progUI.addError(status.getMessage());
            stateChanging = false;
            setCommandControlButtons();
        }
        else if (state == StateType.RUNNING) {
            progUI.addMessage(status.getMessage());
            // If too high, keep it at "almost done"        //NOI18N
            workCompleted += progressRate;
            if (workCompleted > max-1) {
                workCompleted = max-1;
            }
            progUI.recordWork(workCompleted);
        }
        else if (state == StateType.RELEASED) {
            progUI.addMessage(status.getMessage());
        }
    }
    
    private void addButtonPanel() {
        //Runnable r = new Runnable() {
        //    public void run() {
                buttonPanel = new JPanel();
                buttonPanel.getAccessibleContext().setAccessibleName(NbBundle.getMessage(ServerStatusBar.class, "LBL_Stop_and_Cancel_Button_Panel"));
                buttonPanel.setLayout(new GridBagLayout());
                
                //PENDING: is debugModeCheck desirable???  If so need change semantics of StartServer.startDebugging
                if (false && instance.getStartServer().isAlsoTargetServer(null)) {
                    debugModeCheck = new JCheckBox();
                    debugModeCheck.setText(NbBundle.getMessage(ServerStatusBar.class, "LBL_DebugMode"));
                    debugModeCheck.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            debugModeCheckActionPerformed(evt);
                        }
                    });
                    gridBagConstraints = new java.awt.GridBagConstraints();
                    gridBagConstraints.gridx = 0;
                    gridBagConstraints.gridy = 4;
                    gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
                    gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
                    gridBagConstraints.insets = new java.awt.Insets(0, 24, 12, 24);
                    buttonPanel.add(debugModeCheck, gridBagConstraints);
                }
                
                startCommandBtn = new JButton();
                startCommandBtn.setText(NbBundle.getMessage(ServerStatusBar.class, "LBL_StartServer"));
                startCommandBtn.setMnemonic(NbBundle.getMessage(ServerStatusBar.class, "LBL_Start_Mnemonic").charAt(0));
                startCommandBtn.setToolTipText(NbBundle.getMessage(ServerStatusBar.class, "MSG_Start_Command_Tip"));
                startCommandBtn.setEnabled(false);
                startCommandBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        startCommandBtnActionPerformed(evt);
                    }
                });
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
                buttonPanel.add(startCommandBtn, gridBagConstraints);

                startDebugCommandBtn = new JButton();
                startDebugCommandBtn.setText(NbBundle.getMessage(ServerStatusBar.class, "LBL_StartDebugServer"));
                startDebugCommandBtn.setMnemonic(NbBundle.getMessage(ServerStatusBar.class, "LBL_StartDebug_Mnemonic").charAt(0));
                startDebugCommandBtn.setToolTipText(NbBundle.getMessage(ServerStatusBar.class, "MSG_StartDebug_Command_Tip"));
                startDebugCommandBtn.setEnabled(false);
                startDebugCommandBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        startDebugCommandBtnActionPerformed(evt);
                    }
                });
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
                buttonPanel.add(startDebugCommandBtn, gridBagConstraints);
                
                stopCommandBtn = new JButton();
                stopCommandBtn.setText(NbBundle.getMessage(ServerStatusBar.class, "LBL_StopServer"));
                stopCommandBtn.setMnemonic(NbBundle.getMessage(ServerStatusBar.class, "LBL_Stop_Mnemonic").charAt(0));
                stopCommandBtn.setToolTipText(NbBundle.getMessage(ServerStatusBar.class, "MSG_Stop_Command_Tip_Generic"));
                stopCommandBtn.setEnabled(false);
                stopCommandBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        stopCommandBtnActionPerformed(evt);
                    }
                });
                gridBagConstraints = new java.awt.GridBagConstraints();
                gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 5);
                buttonPanel.add(stopCommandBtn, gridBagConstraints);
                
                closeCommandBtn = new JButton();
                closeCommandBtn.setText(NbBundle.getMessage(ServerStatusBar.class, "LBL_Close"));
                closeCommandBtn.setMnemonic(NbBundle.getMessage(ServerStatusBar.class, "LBL_Close_Mnemonic").charAt(0));
                closeCommandBtn.setToolTipText(NbBundle.getMessage(ServerStatusBar.class, "MSG_Close_Command_Tip"));
                closeCommandBtn.setEnabled(false);
                closeCommandBtn.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent evt) {
                        closeCommandBtnActionPerformed(evt);
                    }
                });
                buttonPanel.add(closeCommandBtn, gridBagConstraints);
                
                gridBagConstraints = new GridBagConstraints();
                gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
                gridBagConstraints.gridx = 0;
                gridBagConstraints.gridy = 5;
                gridBagConstraints.insets = new java.awt.Insets(0, 24, 12, 24);
                progUI.add(buttonPanel, gridBagConstraints);
        //    }
        //};
        
        //SwingUtilities.invokeLater(r);
    }
    
    private static String TXT_STATE_RUNNING;
    private static String TXT_STATE_STOPPED;
    private String getStateText(boolean running) {
        if (TXT_STATE_RUNNING == null) {
            TXT_STATE_RUNNING = NbBundle.getMessage(ServerStatusBar.class, "MSG_ServerRunning");
            TXT_STATE_STOPPED = NbBundle.getMessage(ServerStatusBar.class, "MSG_ServerStopped");
        }
        return (running ? TXT_STATE_RUNNING : TXT_STATE_STOPPED);
    }

    private void setCommandControlButtons() {
        if (buttonPanel != null) {
            //SwingUtilities.invokeLater(new Runnable() {
            //    public void run() {
                    if (stateChanging) {
                        startCommandBtn.setEnabled(false);
                        startDebugCommandBtn.setEnabled(false);
                        stopCommandBtn.setEnabled(false);
                        closeCommandBtn.setEnabled(false);
                        return;
                    }
            
                    boolean isRunning = instance.isRunning();
                    //boolean isDebuggable = instance.isDebuggable();
                    
                    progUI.addMessage(getStateText(isRunning));
                    /*if (debugModeCheck != null) {
                        debugModeCheck.setEnabled(!isRunning);
                        debugModeCheck.setSelected(instance.isDebuggable());
                    }*/
                    closeCommandBtn.setEnabled(!stateChanging);
                    if (instance.canStartServer()) {
                        startCommandBtn.setEnabled(!isRunning);
                        startDebugCommandBtn.setEnabled(!isRunning);
                        stopCommandBtn.setEnabled(isRunning);
                    } else {
                        startCommandBtn.setEnabled(false);
                        startDebugCommandBtn.setEnabled(false);
                        stopCommandBtn.setEnabled(false);
                        String msg = NbBundle.getMessage(ServerInstance.class, "MSG_StartingThisServerNotSupported", instance.getDisplayName());
                        this.addError(msg, false);
                    }
            //    }
            //});
        }
    }
    
    private void closeCommandBtnActionPerformed(java.awt.event.ActionEvent evt) {
        progUI.finished(false);
    }
    
    private void debugModeCheckActionPerformed(java.awt.event.ActionEvent evt) {
        debugMode = debugModeCheck.isSelected();
    }
    
    private void stopCommandBtnActionPerformed(java.awt.event.ActionEvent evt) {
        String msg = NbBundle.getMessage(ServerStatusBar.class, "LBL_StopServer", instance.getDisplayName());
        workCompleted = 0;
        recordWork(0);
        addError("", false); // NOI18N
        progUI.addMessage(msg);
        
        org.openide.util.RequestProcessor.getDefault().post(new Runnable() {
            public void run() {
                instance.stop(ServerStatusBar.this);
            }
        });
        stateChanging = true;
        setCommandControlButtons();
    }
    
    private void startCommandBtnActionPerformed(java.awt.event.ActionEvent evt) {
        String msg = NbBundle.getMessage(ServerStatusBar.class, "LBL_StartServer", instance.getDisplayName());
        workCompleted = 0;
        recordWork(0);
        addError("", false); // NOI18N
        progUI.addMessage(msg);
        
        org.openide.util.RequestProcessor.getDefault().post(new Runnable() {
            public void run() {
                instance.start(ServerStatusBar.this);
            }
        });
        stateChanging = true;
        setCommandControlButtons();
    }
    
    private void startDebugCommandBtnActionPerformed(java.awt.event.ActionEvent evt) {
        String msg = NbBundle.getMessage(ServerStatusBar.class, "LBL_StartDebugServer", instance.getDisplayName());
        progUI.addMessage(msg);
        
        org.openide.util.RequestProcessor.getDefault().post(new Runnable() {
            public void run() {
                instance.startDebugTarget(null, ServerStatusBar.this);
            }
        });
        stateChanging = true;
        setCommandControlButtons();
    }

    public void addMessage(String message) {
        progUI.addMessage(message);
    }
    
    public void addError(String errorMessage) {
        addError(errorMessage, true);
    }
    public void addError(String errorMessage, boolean refreshButtons) {
        progUI.addError(errorMessage);
        stateChanging = false;
        if (refreshButtons)
            setCommandControlButtons();
    }
    
    public void recordWork(int work) {
        progUI.recordWork(work);
        if (work >= max)
            progUI.finished();
    }
    
    public boolean checkCancelled() {
        return progUI.checkCancelled();
    }
    
    public void addCancelHandler(CancelHandler handler) {
        cancelHandlers.add(handler);
    }
    
    public void removeCancelHandler(CancelHandler handler) {
        cancelHandlers.remove(handler);
    }
    
    private static Map commandTexts = null;
    private static String _getCommandText(CommandType command){
        if (commandTexts == null) {
            commandTexts = new HashMap();
            commandTexts.put(CommandType.START, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_StartServer"));
            commandTexts.put(CommandType.STOP, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_StopServer"));
            commandTexts.put(ServerProgress.START_SERVER, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_StartServer"));
            commandTexts.put(ServerProgress.STOP_SERVER, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_StopServer"));
        }
        String ret = (String) commandTexts.get(command);
        if (ret == null) ret = ""; //NOI18N
        return ret;
    }
    private String getCommandText() {
        return getCommandText(currCommand);
    }
    private String getCommandText(CommandType command) {
        if (command == null) {
            return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Unknown_Command");
        } else
            return _getCommandText(command);
    }
}
... 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.