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.vcscore.commands;
import java.awt.event.ActionListener;
import org.netbeans.api.vcs.commands.CommandTask;
import org.openide.awt.Actions;
import org.openide.util.Utilities;
import org.openide.windows.WindowManager;
/**
* Dialog wrapper of a command visualizer.
* This dialog is to be used when it is necessary to wait for a command task
* in AWT thread. This dialog is able to wrap any GUI that can be presented
* by the command so that it will not collide with the blocked AWT event queue.
*
* This dialog is modal, therefore its presentation is blocking, but another
* event pump is started so that the system stays responsive.
*
* @author Martin Entlicher
*/
public class DialogVisualizerWrapper extends javax.swing.JDialog implements VcsCommandVisualizer.Wrapper, CommandProcessListener {
private CommandTask task;
private String msgRunning;
private String msgFinished;
private ActionListener killActionListener;
/** Creates new form DialogVisualizerWrapper */
public DialogVisualizerWrapper() {
super(WindowManager.getDefault().getMainWindow(), true);
this.msgRunning = org.openide.util.NbBundle.getMessage(DialogVisualizerWrapper.class, "DialogVisualizerWrapper.msgCmdRunning");
this.msgFinished = org.openide.util.NbBundle.getMessage(DialogVisualizerWrapper.class, "DialogVisualizerWrapper.msgCmdFinished");
initComponents();
commandProgressBar.setIndeterminate(true);
statusLabel.setText(msgRunning);
setBounds(Utilities.findCenterBounds(getSize()));
}
public void setTask(CommandTask task) {
this.task = task;
String cmdName = task.getDisplayName();
if (cmdName == null) {
cmdName = task.getName();
} else {
cmdName = Actions.cutAmpersand(cmdName);
}
this.msgRunning = org.openide.util.NbBundle.getMessage(DialogVisualizerWrapper.class, "DialogVisualizerWrapper.msgCmdRunning", cmdName);
this.msgFinished = org.openide.util.NbBundle.getMessage(DialogVisualizerWrapper.class, "DialogVisualizerWrapper.msgCmdFinished", cmdName);
killActionListener = new CommandOutputVisualizer.CommandKillListener(task, this);
cancelButton.addActionListener(killActionListener);
statusLabel.setText(msgRunning);
CommandProcessor.getInstance().addCommandProcessListener(this);
if (task.isFinished()) {
commandHasFinished();
}
javax.swing.SwingUtilities.invokeLater(new Runnable () {
public void run() {
pack();
}
});
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {//GEN-BEGIN:initComponents
java.awt.GridBagConstraints gridBagConstraints;
statusLabel = new javax.swing.JLabel();
visualizerPanel = new javax.swing.JPanel();
commandProgressBar = new javax.swing.JProgressBar();
buttonPanel = new javax.swing.JPanel();
cancelButton = new javax.swing.JButton();
getContentPane().setLayout(new java.awt.GridBagLayout());
setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog(evt);
}
});
statusLabel.setText("jLabel1");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 6);
getContentPane().add(statusLabel, gridBagConstraints);
visualizerPanel.add(commandProgressBar);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 6);
getContentPane().add(visualizerPanel, gridBagConstraints);
buttonPanel.setLayout(new java.awt.GridBagLayout());
cancelButton.setLabel(org.openide.util.NbBundle.getMessage(DialogVisualizerWrapper.class, "DialogVisualizerWrapper.btnCancel"));
cancelButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
cancelButtonActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 1.0;
buttonPanel.add(cancelButton, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridy = 2;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 6, 6);
getContentPane().add(buttonPanel, gridBagConstraints);
pack();
}//GEN-END:initComponents
private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
// Add your handling code here:
if (task.isFinished()) {
setVisible(false);
dispose();
}
}//GEN-LAST:event_cancelButtonActionPerformed
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
if (task.isFinished()) {
setVisible(false);
dispose();
}
}//GEN-LAST:event_closeDialog
public ActionListener wrap(javax.swing.JComponent visualizerComponent,
boolean showStatus, boolean showClose) {
if (!showStatus && !showClose) {
getContentPane().removeAll();
java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
getContentPane().add(visualizerPanel, gridBagConstraints);
} else {
if (!showStatus) {
remove(statusLabel);
}
if (!showClose) {
remove(buttonPanel);
}
}
visualizerPanel.removeAll();
visualizerPanel.setLayout(new java.awt.GridBagLayout());
java.awt.GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
visualizerPanel.add(visualizerComponent, gridBagConstraints);
String title = (String) visualizerComponent.getClientProperty("wrapper-title");
if (title != null) {
setTitle(title);
} else {
if (task != null) {
String cmdName = task.getDisplayName();
if (cmdName == null) {
cmdName = task.getName();
} else {
cmdName = Actions.cutAmpersand(cmdName);
}
setTitle(cmdName);
}
}
pack();
setBounds(Utilities.findCenterBounds(getSize()));
return new ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
setVisible(false);
dispose();
}
};
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel buttonPanel;
private javax.swing.JButton cancelButton;
private javax.swing.JProgressBar commandProgressBar;
private javax.swing.JLabel statusLabel;
private javax.swing.JPanel visualizerPanel;
// End of variables declaration//GEN-END:variables
private void commandHasFinished() {
CommandProcessor.getInstance().removeCommandProcessListener(this);
javax.swing.SwingUtilities.invokeLater(new Runnable () {
public void run() {
cancelButton.removeActionListener(killActionListener);
statusLabel.setText(msgFinished);
cancelButton.setText(org.openide.util.NbBundle.getMessage(DialogVisualizerWrapper.class, "DialogVisualizerWrapper.btnClose"));
javax.swing.JRootPane root = getRootPane();
if (root != null) {
root.setDefaultButton(cancelButton);
}
commandProgressBar.setValue(commandProgressBar.getMaximum());
commandProgressBar.setIndeterminate(false);
}
});
}
public void commandDone(CommandTaskInfo info) {
if (task.equals(info.getTask())) {
commandHasFinished();
}
}
public void commandPreprocessed(org.netbeans.api.vcs.commands.Command cmd, boolean status) {
}
public void commandPreprocessing(org.netbeans.api.vcs.commands.Command cmd) {
}
public void commandStarting(CommandTaskInfo info) {
}
public org.netbeans.spi.vcs.VcsCommandsProvider getProvider() {
return null;
}
/**
* @param args the command line arguments
*
public static void main(String args[]) {
new DialogVisualizerWrapper(new javax.swing.JFrame(), true).show();
}
*/
}
|