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