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.uml.integration.ide.dialogs;
043:
044: import javax.swing.*;
045: import java.awt.event.*;
046:
047: import javax.swing.text.html.*;
048: import java.awt.*;
049: import java.io.*;
050: import org.netbeans.modules.uml.integration.ide.UMLSupport;
051:
052: /**
053: * ExceptionDialog makes it easy to display an exception error to user. To
054: * display an error message to the user use the helper showExceptionError methods.
055: * @see #showExceptionError(String msg, Exception e)
056: * @see #showExceptionError(Exception e)
057: * @author Trey Spiva
058: * @version 1.0
059: */
060: public class ExceptionDialog extends JDialog {
061: protected final static String REPORT_MESSAGE = UMLSupport
062: .getString("Errors.StdMessage");
063: protected final static Dimension NO_DETAILS_SIZE = new Dimension(
064: 500, 160);
065: protected final static Dimension WITH_DETAILS_SIZE = new Dimension(
066: 650, 250);
067: protected final static String HIDE_DETAILS = UMLSupport
068: .getString("Errors.HideDetails");
069: protected final static String SHOW_DETAILS = UMLSupport
070: .getString("Errors.ShowDetails");
071: protected final static String DETAILS_CARD = UMLSupport
072: .getString("Errors.WithDetails");
073: protected final static String NO_DETAILS_CARD = UMLSupport
074: .getString("Errors.NoDetails");
075:
076: /**
077: * Constructs a ExceptionDialog object.
078: * @param parnet The parent of the dialog.
079: * @param modal Is the dialog modal or modeless.
080: */
081: public ExceptionDialog(java.awt.Frame parent, boolean modal) {
082: super (parent, modal);
083: initComponents();
084:
085: setSize(NO_DETAILS_SIZE);
086: mErrorMessage.setEditorKit(new HTMLEditorKit());
087:
088: //mDetailTxt.setPreferredSize(new Dimension(WITH_DETAILS_SIZE.width, WITH_DETAILS_SIZE.height - NO_DETAILS_SIZE.height));
089:
090: // Center the window.
091: setLocationRelativeTo(null);
092:
093: setupForNoDetails();
094: mButtonPane.getRootPane().setDefaultButton(mOkBtn);
095: }
096:
097: /**
098: * Sets the message of the dialog.
099: */
100: public void setMessage(String msg) {
101: mErrorMessage.setText(msg + "<br>" + REPORT_MESSAGE);
102: }
103:
104: /**
105: * Retrieves the message of the dialog.
106: */
107: public void setDetailMessage(String msg) {
108: mDetailTxt.setText(msg);
109: }
110:
111: /**
112: * Sets the detailed message of the dialog. The detail message will include
113: * the stack trace of the exception.
114: * @param e The exception used to build the detailed message.
115: */
116: public void setDetailMessage(Exception e) {
117: mDetailTxt.setText(getStackTrace(e));
118: mDetailTxt.setCaretPosition(0);
119: }
120:
121: /**
122: * Retrieves the stack trace information from an exception.
123: * @param e The exception used to retrieve the stack information.
124: */
125: protected String getStackTrace(Exception e) {
126: StringWriter sWriter = new StringWriter();
127: e.printStackTrace(new PrintWriter(sWriter));
128: return sWriter.toString();
129: }
130:
131: /** This method is called from within the constructor to
132: * initialize the form.
133: * WARNING: Do NOT modify this code. The content of this method is
134: * always regenerated by the FormEditor.
135: */
136: private void initComponents()//GEN-BEGIN:initComponents
137: {
138: mCards = new javax.swing.JPanel();
139: mErrorMessage = new javax.swing.JEditorPane();
140: jScrollPane1 = new javax.swing.JScrollPane();
141: mDetailTxt = new javax.swing.JTextArea();
142: mButtonPane = new javax.swing.JPanel();
143: mShowDetailsBtn = new javax.swing.JButton();
144: mOkBtn = new javax.swing.JButton();
145: setTitle(UMLSupport.getString("Errors.title"));
146: addWindowListener(new java.awt.event.WindowAdapter() {
147: public void windowClosing(java.awt.event.WindowEvent evt) {
148: closeDialog(evt);
149: }
150: });
151:
152: mCards.setLayout(new java.awt.CardLayout());
153: mCards.setBorder(new javax.swing.border.EmptyBorder(
154: new java.awt.Insets(5, 5, 5, 5)));
155:
156: mErrorMessage.setPreferredSize(new java.awt.Dimension(0, 0));
157: mErrorMessage.setEditable(false);
158: mErrorMessage.setOpaque(false);
159: mCards.add(mErrorMessage, NO_DETAILS_CARD);
160:
161: mDetailTxt.setEditable(false);
162: mDetailTxt.setTabSize(4);
163: mDetailTxt.setOpaque(false);
164: jScrollPane1.setViewportView(mDetailTxt);
165:
166: mCards.add(jScrollPane1, DETAILS_CARD);
167:
168: getContentPane().add(mCards, java.awt.BorderLayout.CENTER);
169:
170: mButtonPane
171: .setLayout(new javax.swing.BoxLayout(mButtonPane, 0));
172: mButtonPane.setBorder(new javax.swing.border.EmptyBorder(
173: new java.awt.Insets(0, 5, 5, 5)));
174:
175: mShowDetailsBtn.setActionCommand(SHOW_DETAILS);
176: mShowDetailsBtn.setText(SHOW_DETAILS);
177: mShowDetailsBtn
178: .addActionListener(new java.awt.event.ActionListener() {
179: public void actionPerformed(
180: java.awt.event.ActionEvent evt) {
181: mShowDetailsBtnActionPerformed(evt);
182: }
183: });
184: mButtonPane.add(mShowDetailsBtn);
185:
186: mButtonPane.add(Box.createHorizontalGlue());
187: mOkBtn.setText(UMLSupport.getString("labels.ok"));
188: mOkBtn.addActionListener(new java.awt.event.ActionListener() {
189: public void actionPerformed(java.awt.event.ActionEvent evt) {
190: mOkBtnActionPerformed(evt);
191: }
192: });
193: mButtonPane.add(mOkBtn);
194:
195: getContentPane().add(mButtonPane, java.awt.BorderLayout.SOUTH);
196:
197: }//GEN-END:initComponents
198:
199: private void mShowDetailsBtnActionPerformed(
200: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mShowDetailsBtnActionPerformed
201:
202: String command = evt.getActionCommand();
203: if (command.equals(SHOW_DETAILS) == true) {
204: setupForWithDetails();
205: } else {
206: setupForNoDetails();
207: }
208: this .validate();
209: }//GEN-LAST:event_mShowDetailsBtnActionPerformed
210:
211: /**
212: * Display the error message and resize the diaglog to the standard size
213: * of the dialog.
214: */
215: protected void setupForNoDetails() {
216: setSize(NO_DETAILS_SIZE);
217: mShowDetailsBtn.setText(SHOW_DETAILS);
218: mShowDetailsBtn.setActionCommand(SHOW_DETAILS);
219:
220: CardLayout cl = (CardLayout) mCards.getLayout();
221: cl.show(mCards, NO_DETAILS_CARD);
222: }
223:
224: /**
225: * When details are shown display the detailed message information.
226: */
227: protected void setupForWithDetails() {
228: setSize(WITH_DETAILS_SIZE);
229: mShowDetailsBtn.setText(HIDE_DETAILS);
230: mShowDetailsBtn.setActionCommand(HIDE_DETAILS);
231:
232: CardLayout cl = (CardLayout) mCards.getLayout();
233: cl.show(mCards, DETAILS_CARD);
234:
235: }
236:
237: private void mOkBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mOkBtnActionPerformed
238: //System.exit(0);
239: setVisible(false);
240: dispose();
241: }//GEN-LAST:event_mOkBtnActionPerformed
242:
243: /** Closes the dialog */
244: private void closeDialog(WindowEvent evt)//GEN-FIRST:event_closeDialog
245: {
246: setVisible(false);
247: dispose();
248: }//GEN-LAST:event_closeDialog
249:
250: /**
251: * Intialize and display the error dialog. The dialog will be initailized with
252: * a error message and a detailed message. The detailed message will be filled
253: * in from the specifed exception.
254: * @param msg The error message to be displayed.
255: * @param e The exception used to retrieve the detailed information.
256: */
257: public static void showExceptionError(String msg, Exception e) {
258: ExceptionDialog dialog = new ExceptionDialog(new JFrame(), true);
259: dialog.setMessage(msg);
260:
261: dialog.setDetailMessage(e);
262: dialog.setVisible(true);
263: }
264:
265: /**
266: * Intialize and display the error dialog. The dialog will be initailized with
267: * a error message and a detailed message from the exception.
268: * @param e The exception used to retrieve the detailed information.
269: */
270: public static void showExceptionError(Exception e) {
271: ExceptionDialog dialog = new ExceptionDialog(new JFrame(), true);
272: dialog.setMessage(e.getLocalizedMessage());
273:
274: dialog.setDetailMessage(e);
275: dialog.setVisible(true);
276: }
277:
278: /**
279: * @param args the command line arguments
280: */
281: public static void main(String args[]) {
282: ExceptionDialog dialog = new ExceptionDialog(
283: new javax.swing.JFrame(), true);
284: dialog.setMessage("This is a <B>TEST</B> Error Message!");
285:
286: String detailMessage = "This is a very long detail message..........................VERY VERY VERY VERY VERY................................. Very long indeed\n";
287: detailMessage += "I really hope that his scrollbars are working.............\n";
288: detailMessage += "I really hope that his scrollbars are working.............\n";
289: detailMessage += "I really hope that his scrollbars are working.............\n";
290: detailMessage += "I really hope that his scrollbars are working.............\n";
291: detailMessage += "I really hope that his scrollbars are working.............\n";
292: detailMessage += "I really hope that his scrollbars are working.............\n";
293: detailMessage += "I really hope that his scrollbars are working.............\n";
294: detailMessage += "I really hope that his scrollbars are working.............\n";
295: detailMessage += "I really hope that his scrollbars are working.............\n";
296: detailMessage += "I really hope that his scrollbars are working.............\n";
297: detailMessage += "I really hope that his scrollbars are working.............\n";
298: detailMessage += "I really hope that his scrollbars are working.............\n";
299: detailMessage += "I really hope that his scrollbars are working.............\n";
300: detailMessage += "I really hope that his scrollbars are working.............\n";
301: detailMessage += "I really hope that his scrollbars are working.............\n";
302: detailMessage += "I really hope that his scrollbars are working.............\n";
303: detailMessage += "I really hope that his scrollbars are working.............\n";
304: detailMessage += "I really hope that his scrollbars are working.............\n";
305: detailMessage += "I really hope that his scrollbars are working.............\n";
306: detailMessage += "I really hope that his scrollbars are working.............\n";
307: detailMessage += "I really hope that his scrollbars are working.............\n";
308: detailMessage += "I really hope that his scrollbars are working.............\n";
309: detailMessage += "I really hope that his scrollbars are working.............\n";
310: detailMessage += "I really hope that his scrollbars are working.............\n";
311: dialog.setDetailMessage(detailMessage);
312: dialog.setVisible(true);
313: }
314:
315: // Variables declaration - do not modify//GEN-BEGIN:variables
316: private javax.swing.JPanel mCards;
317: private javax.swing.JEditorPane mErrorMessage;
318: private javax.swing.JScrollPane jScrollPane1;
319: private javax.swing.JTextArea mDetailTxt;
320: private javax.swing.JPanel mButtonPane;
321: private javax.swing.JButton mShowDetailsBtn;
322: private javax.swing.JButton mOkBtn;
323: // End of variables declaration//GEN-END:variables
324:
325: }
|