001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.sun.manager.jbi.util;
043:
044: import java.awt.GridBagConstraints;
045: import java.awt.Insets;
046: import java.awt.event.ActionEvent;
047: import java.awt.event.ActionListener;
048: import javax.enterprise.deploy.shared.StateType;
049: import javax.enterprise.deploy.spi.status.DeploymentStatus;
050: import javax.enterprise.deploy.spi.status.ProgressEvent;
051: import javax.enterprise.deploy.spi.status.ProgressListener;
052: import javax.enterprise.deploy.spi.status.ProgressObject;
053: import javax.swing.JButton;
054: import javax.swing.JComponent;
055: import javax.swing.JDialog;
056: import javax.swing.JLabel;
057: import javax.swing.JPanel;
058: import org.netbeans.api.progress.ProgressHandle;
059: import org.netbeans.api.progress.ProgressHandleFactory;
060: import org.openide.util.NbBundle;
061:
062: /**
063: * Progress UI provides a feedback for long lasting taks like deploying to a server,
064: * starting or stopping a server, etc. The progress bar is indeterminate, displayed
065: * in the status bar if in non-modal mode, otherwise in a modal dialog.
066: *
067: * @author sherold
068: */
069: public class ProgressUI implements ProgressListener {
070:
071: private String title;
072: private boolean modal;
073:
074: private ProgressHandle handle;
075: private ProgressObject progObj;
076:
077: private JDialog dialog;
078: private JLabel messageLabel;
079: private String lastMessage;
080: private JComponent progressComponent;
081: private boolean finished;
082:
083: /** Creates a new instance of ProgressUI */
084: public ProgressUI(String title, boolean modal) {
085: this .modal = modal;
086: this .title = title;
087: handle = ProgressHandleFactory.createHandle(title);
088: }
089:
090: /** Start the progress indication for indeterminate task. */
091: public void start() {
092: if (modal) {
093: progressComponent = ProgressHandleFactory
094: .createProgressComponent(handle);
095: }
096: handle.start();
097: }
098:
099: /** Display the modal progress dialog. This method should be called from the
100: AWT Event Dispatch thread. */
101: // public void showProgressDialog() {
102: // if (finished) {
103: // return; // do not display the dialog if we are done
104: // }
105: // dialog = new JDialog(WindowManager.getDefault().getMainWindow(), title, true);
106: // dialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
107: // dialog.getContentPane().add(createProgressDialog(
108: // handle,
109: // lastMessage != null ? lastMessage : title));
110: // dialog.pack();
111: // dialog.setBounds(Utilities.findCenterBounds(dialog.getSize()));
112: // dialog.setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
113: // dialog.setVisible(true);
114: // }
115: /** Displays a specified progress message. */
116: public void progress(final String message) {
117: handle.progress(message);
118: if (modal) {
119: Utils.runInEventDispatchThread(new Runnable() {
120: public void run() {
121: if (messageLabel != null) {
122: messageLabel.setText(message);
123: } else {
124: lastMessage = message;
125: }
126: }
127: });
128: }
129: }
130:
131: /** Finish the task, unregister the progress object listener and dispose the ui. */
132: public void finish() {
133: handle.finish();
134: if (progObj != null) {
135: progObj.removeProgressListener(this );
136: progObj = null;
137: }
138: Utils.runInEventDispatchThread(new Runnable() {
139: public void run() {
140: finished = true;
141: if (dialog != null) {
142: dialog.setVisible(false);
143: dialog.dispose();
144: dialog = null;
145: }
146: }
147: });
148: }
149:
150: /** Display a failure dialog with the specified message and call finish. */
151: public void failed(String message) {
152: finish();
153: }
154:
155: /** Set a progress object this progress UI will monitor. */
156: public void setProgressObject(ProgressObject obj) {
157: // do not listen to the old progress object anymore
158: if (progObj != null) {
159: progObj.removeProgressListener(this );
160: }
161: progObj = obj;
162: if (progObj != null) {
163: progObj.addProgressListener(this );
164: }
165: }
166:
167: private JComponent createProgressDialog(ProgressHandle handle,
168: String message) {
169: JPanel panel = new JPanel();
170: messageLabel = new JLabel();
171:
172: panel.setLayout(new java.awt.GridBagLayout());
173:
174: messageLabel.setText(message);
175: GridBagConstraints gridBagConstraints = new GridBagConstraints();
176: gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
177: gridBagConstraints.insets = new Insets(12, 12, 0, 12);
178: panel.add(messageLabel, gridBagConstraints);
179:
180: gridBagConstraints = new GridBagConstraints();
181: gridBagConstraints.gridx = 0;
182: gridBagConstraints.gridy = 1;
183: gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
184: gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
185: gridBagConstraints.weightx = 1.0;
186: gridBagConstraints.insets = new Insets(5, 12, 0, 12);
187: panel.add(progressComponent, gridBagConstraints);
188:
189: gridBagConstraints = new GridBagConstraints();
190: gridBagConstraints.gridx = 0;
191: gridBagConstraints.gridy = 2;
192: gridBagConstraints.anchor = GridBagConstraints.CENTER;
193: gridBagConstraints.weightx = 1.0;
194: gridBagConstraints.insets = new Insets(11, 12, 12, 12);
195: JButton cancel = new JButton(NbBundle.getMessage(
196: ProgressUI.class, "LBL_Cancel")); // NOI18N
197: cancel.getAccessibleContext().setAccessibleDescription(
198: NbBundle.getMessage(ProgressUI.class, "AD_Cancel")); // NOI18N
199: cancel.addActionListener(new ActionListener() {
200: public void actionPerformed(ActionEvent e) {
201: finish();
202: }
203: });
204: panel.add(cancel, gridBagConstraints);
205:
206: return panel;
207: }
208:
209: // ProgressListener implementation ----------------------------------------
210:
211: public void handleProgressEvent(ProgressEvent progressEvent) {
212: DeploymentStatus status = progressEvent.getDeploymentStatus();
213: StateType state = status.getState();
214: if (state == StateType.COMPLETED) {
215: progress(status.getMessage());
216: } else if (state == StateType.RUNNING) {
217: progress(status.getMessage());
218: } else if (state == StateType.FAILED) {
219: failed(status.getMessage());
220: } else if (state == StateType.RELEASED) {
221: failed(status.getMessage());
222: }
223: }
224: }
|