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: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.modules.profiler.ui.panels;
042:
043: import org.netbeans.modules.profiler.ui.ProfilerDialogs;
044: import org.netbeans.modules.profiler.ui.stp.Utils;
045: import org.openide.DialogDescriptor;
046: import org.openide.util.Cancellable;
047: import org.openide.util.NbBundle;
048: import java.awt.Dialog;
049: import java.awt.Dimension;
050: import java.awt.GridBagConstraints;
051: import java.awt.GridBagLayout;
052: import java.awt.Insets;
053: import java.awt.event.ActionEvent;
054: import java.awt.event.ActionListener;
055: import javax.swing.JButton;
056: import javax.swing.JDialog;
057: import javax.swing.JLabel;
058: import javax.swing.JPanel;
059: import javax.swing.JProgressBar;
060: import javax.swing.SwingConstants;
061: import javax.swing.SwingUtilities;
062: import javax.swing.WindowConstants;
063:
064: /**
065: *
066: * @author Jiri Sedlacek
067: */
068: public class ProgressDisplayer extends JPanel {
069: //~ Inner Interfaces ---------------------------------------------------------------------------------------------------------
070:
071: // --- ProgressController interface ------------------------------------------
072: public static interface ProgressController extends Cancellable {
073: }
074:
075: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
076:
077: // -----
078: // I18N String constants
079: private static final String PROGRESS_STRING = NbBundle
080: .getMessage(ProgressDisplayer.class,
081: "ProgressDisplayer_ProgressString"); // NOI18N
082: private static final String CANCEL_BUTTON_TEXT = NbBundle
083: .getMessage(ProgressDisplayer.class,
084: "ProgressDisplayer_CancelButtonText"); // NOI18N
085: // -----
086: private static final Object displayerLock = new Object();
087:
088: // --- Private implementation ------------------------------------------------
089: private static ProgressDisplayer defaultInstance;
090:
091: //~ Instance fields ----------------------------------------------------------------------------------------------------------
092:
093: private Dialog owner;
094: private JButton cancelButton;
095:
096: // --- UI components declaration ---------------------------------------------
097: private JLabel progressLabel;
098: private JProgressBar progressBar;
099:
100: // --- Instance variables declaration ----------------------------------------
101: private ProgressController controller;
102: private boolean isOpened;
103:
104: //~ Constructors -------------------------------------------------------------------------------------------------------------
105:
106: private ProgressDisplayer() {
107: initComponents();
108: }
109:
110: //~ Methods ------------------------------------------------------------------------------------------------------------------
111:
112: // --- Public interface ------------------------------------------------------
113: public static ProgressDisplayer showProgress(String message) {
114: return showProgress(message, null);
115: }
116:
117: public static ProgressDisplayer showProgress(String message,
118: ProgressController controller) {
119: return showProgress(PROGRESS_STRING, message, controller);
120: }
121:
122: public static ProgressDisplayer showProgress(String caption,
123: String message, ProgressController controller) {
124: synchronized (displayerLock) {
125: final ProgressDisplayer pd = ProgressDisplayer.getDefault();
126:
127: final DialogDescriptor dd = pd.createDialogDescriptor(
128: caption, message, controller);
129: final Dialog d = ProfilerDialogs.createDialog(dd);
130: d.pack();
131:
132: pd.setOwner(d);
133:
134: SwingUtilities.invokeLater(new Runnable() {
135: public void run() {
136: pd.setOpened(true);
137: d.setVisible(true);
138: }
139: });
140:
141: return pd;
142: }
143: }
144:
145: public boolean isOpened() {
146: synchronized (displayerLock) {
147: return isOpened;
148: }
149: }
150:
151: public void close() {
152: synchronized (displayerLock) {
153: if (owner != null) {
154: owner.setVisible(false);
155: owner.dispose();
156: }
157:
158: cleanup();
159: }
160: }
161:
162: private static ProgressDisplayer getDefault() {
163: if (defaultInstance == null) {
164: defaultInstance = new ProgressDisplayer();
165: }
166:
167: return defaultInstance;
168: }
169:
170: private void setOpened(boolean isOpened) {
171: this .isOpened = isOpened;
172: }
173:
174: private void setOwner(Dialog owner) {
175: this .owner = owner;
176:
177: if (owner instanceof JDialog) {
178: ((JDialog) owner)
179: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
180: }
181: }
182:
183: private void cleanup() {
184: if (progressBar != null) {
185: progressBar.setIndeterminate(false);
186: }
187:
188: isOpened = false;
189: controller = null;
190: owner = null;
191: }
192:
193: private DialogDescriptor createDialogDescriptor(String caption,
194: String message, ProgressController controller) {
195: this .controller = controller;
196:
197: progressLabel.setText(message);
198: progressBar.setIndeterminate(true);
199:
200: DialogDescriptor dd = null;
201:
202: if (controller == null) {
203: dd = new DialogDescriptor(this , caption, true,
204: new Object[0], null, 0, null, null);
205: } else {
206: dd = new DialogDescriptor(this , caption, true,
207: new Object[] { cancelButton }, null, 0, null, null);
208: }
209:
210: dd.setClosingOptions(new Object[0]);
211:
212: return dd;
213: }
214:
215: // --- UI definition ---------------------------------------------------------
216: private void initComponents() {
217: setLayout(new GridBagLayout());
218:
219: GridBagConstraints constraints;
220:
221: // progressLabel
222: progressLabel = new JLabel();
223: constraints = new GridBagConstraints();
224: constraints.gridx = 0;
225: constraints.gridy = 0;
226: constraints.gridwidth = GridBagConstraints.REMAINDER;
227: constraints.fill = GridBagConstraints.NONE;
228: constraints.anchor = GridBagConstraints.WEST;
229: constraints.insets = new Insets(15, 8, 0, 8);
230: add(progressLabel, constraints);
231:
232: // progressBar
233: progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
234: progressBar.setPreferredSize(new Dimension(300, progressBar
235: .getPreferredSize().height));
236: constraints = new GridBagConstraints();
237: constraints.gridx = 0;
238: constraints.gridy = 1;
239: constraints.gridwidth = GridBagConstraints.REMAINDER;
240: constraints.fill = GridBagConstraints.HORIZONTAL;
241: constraints.anchor = GridBagConstraints.WEST;
242: constraints.insets = new Insets(10, 8, 15, 8);
243: add(progressBar, constraints);
244:
245: // fillerPanel
246: constraints = new GridBagConstraints();
247: constraints.gridx = 0;
248: constraints.gridy = 2;
249: constraints.weightx = 1;
250: constraints.weighty = 1;
251: constraints.gridwidth = GridBagConstraints.REMAINDER;
252: constraints.fill = GridBagConstraints.BOTH;
253: constraints.anchor = GridBagConstraints.NORTHWEST;
254: constraints.insets = new Insets(0, 0, 0, 0);
255: add(Utils.createFillerPanel(), constraints);
256:
257: // cancelButton
258: cancelButton = new JButton(CANCEL_BUTTON_TEXT);
259: cancelButton.addActionListener(new ActionListener() {
260: public void actionPerformed(ActionEvent e) {
261: if (controller != null) {
262: controller.cancel();
263: }
264: }
265: });
266: }
267: }
|