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 ProfilerProgressDisplayer extends JPanel {
069: //~ Static fields/initializers -----------------------------------------------------------------------------------------------
070:
071: // -----
072: // I18N String constants
073: private static final String PROGRESS_STRING = NbBundle.getMessage(
074: ProfilerProgressDisplayer.class,
075: "ProgressDisplayer_ProgressString"); // NOI18N
076: private static final String CANCEL_BUTTON_TEXT = NbBundle
077: .getMessage(ProfilerProgressDisplayer.class,
078: "ProgressDisplayer_CancelButtonText"); // NOI18N
079: // -----
080: private static final Object displayerLock = new Object();
081:
082: // --- Private implementation ------------------------------------------------
083: private static ProfilerProgressDisplayer defaultInstance;
084:
085: //~ Instance fields ----------------------------------------------------------------------------------------------------------
086:
087: // --- Instance variables declaration ----------------------------------------
088: private Cancellable controller;
089: private Dialog owner;
090: private JButton cancelButton;
091:
092: // --- UI components declaration ---------------------------------------------
093: private JLabel progressLabel;
094: private JProgressBar progressBar;
095: private boolean isOpened;
096:
097: //~ Constructors -------------------------------------------------------------------------------------------------------------
098:
099: private ProfilerProgressDisplayer() {
100: initComponents();
101: }
102:
103: //~ Methods ------------------------------------------------------------------------------------------------------------------
104:
105: // --- Public interface ------------------------------------------------------
106: public static ProfilerProgressDisplayer showProgress(String message) {
107: return showProgress(message, null);
108: }
109:
110: public static ProfilerProgressDisplayer showProgress(
111: String message, Cancellable controller) {
112: return showProgress(PROGRESS_STRING, message, controller);
113: }
114:
115: public static ProfilerProgressDisplayer showProgress(
116: String caption, String message, Cancellable controller) {
117: synchronized (displayerLock) {
118: final ProfilerProgressDisplayer pd = ProfilerProgressDisplayer
119: .getDefault();
120:
121: final DialogDescriptor dd = pd.createDialogDescriptor(
122: caption, message, controller);
123: final Dialog d = ProfilerDialogs.createDialog(dd);
124: d.pack();
125:
126: pd.setOwner(d);
127:
128: SwingUtilities.invokeLater(new Runnable() {
129: public void run() {
130: pd.setOpened(true);
131: d.setVisible(true);
132: }
133: });
134:
135: return pd;
136: }
137: }
138:
139: public boolean isOpened() {
140: synchronized (displayerLock) {
141: return isOpened;
142: }
143: }
144:
145: public void close() {
146: synchronized (displayerLock) {
147: if (owner != null) {
148: owner.setVisible(false);
149: owner.dispose();
150: }
151:
152: cleanup();
153: }
154: }
155:
156: private static ProfilerProgressDisplayer getDefault() {
157: if (defaultInstance == null) {
158: defaultInstance = new ProfilerProgressDisplayer();
159: }
160:
161: return defaultInstance;
162: }
163:
164: private void setOpened(boolean isOpened) {
165: this .isOpened = isOpened;
166: }
167:
168: private void setOwner(Dialog owner) {
169: this .owner = owner;
170:
171: if (owner instanceof JDialog) {
172: ((JDialog) owner)
173: .setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
174: }
175: }
176:
177: private void cleanup() {
178: if (progressBar != null) {
179: progressBar.setIndeterminate(false);
180: }
181:
182: isOpened = false;
183: controller = null;
184: owner = null;
185: }
186:
187: private DialogDescriptor createDialogDescriptor(String caption,
188: String message, Cancellable controller) {
189: this .controller = controller;
190:
191: progressLabel.setText(message);
192: progressBar.setIndeterminate(true);
193:
194: DialogDescriptor dd = null;
195:
196: if (controller == null) {
197: dd = new DialogDescriptor(this , caption, true,
198: new Object[0], null, 0, null, null);
199: } else {
200: dd = new DialogDescriptor(this , caption, true,
201: new Object[] { cancelButton }, null, 0, null, null);
202: }
203:
204: dd.setClosingOptions(new Object[0]);
205:
206: return dd;
207: }
208:
209: // --- UI definition ---------------------------------------------------------
210: private void initComponents() {
211: setLayout(new GridBagLayout());
212:
213: GridBagConstraints constraints;
214:
215: // progressLabel
216: progressLabel = new JLabel();
217: constraints = new GridBagConstraints();
218: constraints.gridx = 0;
219: constraints.gridy = 0;
220: constraints.gridwidth = GridBagConstraints.REMAINDER;
221: constraints.fill = GridBagConstraints.NONE;
222: constraints.anchor = GridBagConstraints.WEST;
223: constraints.insets = new Insets(15, 8, 0, 8);
224: add(progressLabel, constraints);
225:
226: // progressBar
227: progressBar = new JProgressBar(SwingConstants.HORIZONTAL);
228: progressBar.setPreferredSize(new Dimension(300, progressBar
229: .getPreferredSize().height));
230: constraints = new GridBagConstraints();
231: constraints.gridx = 0;
232: constraints.gridy = 1;
233: constraints.gridwidth = GridBagConstraints.REMAINDER;
234: constraints.fill = GridBagConstraints.HORIZONTAL;
235: constraints.anchor = GridBagConstraints.WEST;
236: constraints.insets = new Insets(10, 8, 15, 8);
237: add(progressBar, constraints);
238:
239: // fillerPanel
240: constraints = new GridBagConstraints();
241: constraints.gridx = 0;
242: constraints.gridy = 2;
243: constraints.weightx = 1;
244: constraints.weighty = 1;
245: constraints.gridwidth = GridBagConstraints.REMAINDER;
246: constraints.fill = GridBagConstraints.BOTH;
247: constraints.anchor = GridBagConstraints.NORTHWEST;
248: constraints.insets = new Insets(0, 0, 0, 0);
249: add(Utils.createFillerPanel(), constraints);
250:
251: // cancelButton
252: cancelButton = new JButton(CANCEL_BUTTON_TEXT);
253: cancelButton.addActionListener(new ActionListener() {
254: public void actionPerformed(ActionEvent e) {
255: if (controller != null) {
256: controller.cancel();
257: }
258: }
259: });
260: }
261: }
|