|
What this is
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.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 java.util.Vector; import java.util.Iterator; import java.util.Map; import java.util.HashMap; import org.netbeans.modules.j2ee.deployment.devmodules.api.Deployment; /** * * @author Jeri Lockhart */ public class DeployProgressMonitor implements DeployProgressUI, ProgressListener { 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 JPanel buttonPanel; private JButton stopCommandBtn; private JButton cancelCommandBtn; private GridBagConstraints gridBagConstraints; private int progressEventCount = 0; private Vector cancelHandlers = new Vector(); private boolean closeEnabled = false; private Deployment.Logger logger = null; /** Creates a new instance of DeployProgressMonitor */ /** * The default is to show ProgressUI non-modally * with a panel of buttons to stop * or cancel the current DeploymentManager command. * * If the app server doesn't support cancel or stop * the button(s) will be disabled (dimmed). * */ public DeployProgressMonitor() { // Non-modal, show stop and cancel buttons this(false, true); } public DeployProgressMonitor(boolean modal) { // show stop and cancel buttons this(modal, true); } public DeployProgressMonitor(boolean modal, boolean stopOrCancelPermitted) { this(modal, stopOrCancelPermitted, null); } public DeployProgressMonitor(boolean modal, boolean stopOrCancelPermitted, Deployment.Logger logger) { this(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Deploy_Progress_Monitor"), modal, stopOrCancelPermitted); this.logger = logger; } public DeployProgressMonitor(String title, boolean modal, boolean stopOrCancelPermitted) { progUI = new ProgressUI(modal); if (stopOrCancelPermitted) { addButtonPanel(); } progUI.setTitle(title); } /** * startProgressUI This must be called once after instantiation. * * @param obj ProgressObject returned from the first, or only, DeploymentManager command * @param max The maximum value for the progress bar, a total for all the commands issued. * Use the total count of ProgressEvents with StateTypes "Running" and "Completed" * that will be raised for all ProgressObjects that will be attached to this * Progress UI. * To get an estimate, use log messages "ProgressEvent RUNNING..." and "ProgressEvent COMPLETED..." * after executing the DeploymentManager command. * */ public void startProgressUI(ProgressObject obj, int max) throws NullPointerException { try { setProgressObject(obj); } catch(NullPointerException e) { throw e; } startProgressUI(max); } /** * startProgressUI must be called once after instantiation. * Use this override if the ProgressObject is not available yet. * When you have the ProgressObject, use setProgressObject() * * @param max The maximum value for the progress bar, a total for all the commands issued. * @param rate An estimated total number of times that the progress bar * will be updated by ProgressEvents for all the ProgressObjects involved. * */ public void startProgressUI(int max) { String message; this.max = max; if (currCommand != null) { message = MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Command_Begins"), new Object[] {getCommandText(currCommand)}); } else { message = ""; //NOI18N } progUI.startTask(message, max); } /** * @param obj ProgressObject that is returned from DeploymentManager commands. * */ 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); currCommand = progObj.getDeploymentStatus().getCommand(); setCommandControlButtons(); progUI.addMessage(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Command_Begins"), new Object[] {getCommandText(currCommand)})); // e.g., "Distribution begins..." progUI.addError(""); //NOI18N return true; } public void handleProgressEvent(ProgressEvent progressEvent) { status = progressEvent.getDeploymentStatus(); StateType state = status.getState(); //progUI.setTitle(MessageFormat.format( //NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Progress_Title"), new Object[] {getStateText(state)})); // e.g., "Running..." progUI.addError(""); //PENDING: maybe we need to allow users to see history of deployment events, especially in case of errors? if (state == StateType.COMPLETED) { progUI.addMessage(status.getMessage()); // Note: shared b/w multiple progresses so avoid closing. // Caller need to close when no more progresses to display progUI.recordWork(max > 0 ? max-1 : max); enableClose(); } else if (state == StateType.FAILED) { progUI.addError(status.getMessage()); enableClose(); } 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) { ErrorManager.getDefault().log(ErrorManager.WARNING, status.getMessage()); // Disconnected from app server progUI.addMessage(status.getMessage()); } } private String getCommandText() { return getCommandText(currCommand); } private static Map commandTexts = null; private static String _getCommandText(CommandType command){ if (commandTexts == null) { commandTexts = new HashMap(); commandTexts.put(CommandType.DISTRIBUTE, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Distribute")); commandTexts.put(CommandType.START, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Start_Application")); commandTexts.put(CommandType.STOP, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Stop_Application")); commandTexts.put(CommandType.UNDEPLOY, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Undeploy")); commandTexts.put(CommandType.REDEPLOY, NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Redeploy")); 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(CommandType command) { if (command == null) { return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Unknown_Command"); } else return _getCommandText(command); } private String getStateText(StateType state) { if (state == null) { return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Unknown_State"); } if (state == StateType.RUNNING) { return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Running"); } else if (state == StateType.COMPLETED) { return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Completed"); } else if (state == StateType.FAILED) { return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Failed"); } else if (state == StateType.RELEASED) { return NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Released"); } else { // This shouldn't happen return ""; //NOI18N } } private void addButtonPanel() { Runnable r = new Runnable() { public void run() { buttonPanel = new JPanel(); buttonPanel.getAccessibleContext().setAccessibleName(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Stop_and_Cancel_Button_Panel")); buttonPanel.setLayout(new GridBagLayout()); stopCommandBtn = new JButton(); stopCommandBtn.setText(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Stop")); stopCommandBtn.setMnemonic(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Stop_Mnemonic").charAt(0)); stopCommandBtn.setToolTipText(NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Stop_Command_Tip_Generic")); stopCommandBtn.setEnabled(false); stopCommandBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { stopCommandBtnActionPerformed(evt); } }); buttonPanel.add(stopCommandBtn, new GridBagConstraints()); cancelCommandBtn = new JButton(); cancelCommandBtn.setText(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Cancel")); cancelCommandBtn.getAccessibleContext().setAccessibleName(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Cancel")); cancelCommandBtn.setToolTipText(NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Cancel_Command_Tip_Generic")); cancelCommandBtn.setEnabled(false); cancelCommandBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent evt) { cancelCommandBtnActionPerformed(evt); } }); gridBagConstraints = new java.awt.GridBagConstraints(); gridBagConstraints.insets = new java.awt.Insets(0, 12, 0, 0); buttonPanel.add(cancelCommandBtn, 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 void enableClose() { // setProgressObject(null); // SwingUtilities.invokeLater(new Runnable() { // public void run() { if (cancelCommandBtn != null) { closeEnabled = true; cancelCommandBtn.setText(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Close")); cancelCommandBtn.setEnabled(true); cancelCommandBtn.getAccessibleContext().setAccessibleName (NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Close")); cancelCommandBtn.setToolTipText(NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Close_Command_Tip")); } // } // }); } private void setCommandControlButtons() { SwingUtilities.invokeLater(new Runnable() { public void run() { if (buttonPanel != null && progObj != null) { String op = getCommandText(); String tipOp = op; if (currCommand == ServerProgress.STOP_SERVER || currCommand == ServerProgress.START_SERVER){ op = ""; //NOI18N } stopCommandBtn.setText(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Stop_Command"), new Object[]{ op })); stopCommandBtn.setMnemonic(NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Stop_Command_Mnemonic").charAt(0)); stopCommandBtn.setToolTipText(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Stop_Command_Tip"), new Object[]{ tipOp })); cancelCommandBtn.setText(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Cancel_Command"), new Object[]{ op })); cancelCommandBtn.getAccessibleContext().setAccessibleName(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "LBL_Cancel_Command"), new Object[]{ op })); cancelCommandBtn.setToolTipText(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Cancel_Command_Tip"), new Object[]{ tipOp })); if (buttonPanel != null && progObj != null) { cancelCommandBtn.setEnabled(progObj.isCancelSupported()); stopCommandBtn.setEnabled(progObj.isStopSupported()); } } } }); } private void cancelCommandBtnActionPerformed(java.awt.event.ActionEvent evt) { if (progObj == null || closeEnabled) { progUI.finished(false); } else { try { progObj.cancel(); } catch (OperationUnsupportedException e) { progUI.addMessage(e.getMessage()); } for (Iterator i=cancelHandlers.iterator(); i.hasNext();) { CancelHandler h = (CancelHandler) i.next(); h.handle(); } } progUI.addMessage(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Command_Canceled"), new Object[] {getCommandText(currCommand)})); } private void stopCommandBtnActionPerformed(java.awt.event.ActionEvent evt) { // Add your handling code here: //System.out.println("DeployProgressMonitor: Stop button pressed."); //NOI18N if (progObj != null) { try { progObj.stop(); } catch (OperationUnsupportedException e) { progUI.addMessage(e.getMessage()); } for (Iterator i=cancelHandlers.iterator(); i.hasNext();) { CancelHandler h = (CancelHandler) i.next(); h.handle(); } } progUI.addMessage(MessageFormat.format( NbBundle.getMessage(DeployProgressMonitor.class, "MSG_Command_Stopped"), new Object[] {getCommandText(currCommand)})); } /** * DeployProgressMonitor will write messages to * the window when it hears a ProgressEvent for all StateTypes ("Running", * "Completed", and "Released"), except "Failed". See addError() for StateType "Failed". * You can call addMessage() if you want your own message to be written. * */ public void addMessage(String message) { progUI.addMessage(message); log(message); } /** * DeployProgressMonitor will write error messages to * the window when a ProgressEvent with a StateType of "Failed" is posted. * You can call addError() if you want your own error message * to be written. * */ public void addError(String errorMessage) { progUI.addError(errorMessage); log(errorMessage); enableClose(); } /* * DeployProgressMonitor will use the ProgressObject to increment the * progress bar. * You can call recordWork() if you want to increment the * progress bar explicitly. * */ 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 void log(String msg) { if (logger != null) { logger.log(msg); } } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
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.