001: /*
002: * Copyright 2006-2007 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt.
007: *
008: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
009: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
010: * the license for the specific language governing your rights and limitations.
011: *
012: * Additional Contributor(s): Martin Schmid gridvision engineering GmbH
013: */
014: package org.pentaho.reportdesigner.crm.report.components;
015:
016: import org.jetbrains.annotations.NotNull;
017: import org.jetbrains.annotations.Nullable;
018: import org.pentaho.reportdesigner.lib.client.components.CenterPanelDialog;
019: import org.pentaho.reportdesigner.lib.client.components.ComponentFactory;
020: import org.pentaho.reportdesigner.lib.client.components.TextComponentHelper;
021: import org.pentaho.reportdesigner.lib.client.components.docking.IconCreator;
022: import org.pentaho.reportdesigner.lib.client.util.IOUtil;
023: import org.pentaho.reportdesigner.lib.client.util.UndoHelper;
024: import org.pentaho.reportdesigner.lib.client.util.WindowUtils;
025:
026: import javax.swing.*;
027: import javax.swing.text.View;
028: import java.awt.*;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.awt.event.WindowAdapter;
032: import java.awt.event.WindowEvent;
033: import java.io.CharArrayWriter;
034: import java.io.PrintWriter;
035:
036: /**
037: * User: Martin
038: * Date: 09.02.2006
039: * Time: 13:17:10
040: */
041: @SuppressWarnings({"HardCodedStringLiteral","HardcodedLineSeparator"})
042: public class MessageDialog extends CenterPanelDialog {
043: public static void main(@NotNull
044: String[] args) {
045:
046: EventQueue.invokeLater(new Runnable() {
047: public void run() {
048: JFrame frame = new JFrame("testFrame");
049: frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
050: frame.setSize(300, 300);
051: frame.setLocation(400, 400);
052:
053: frame.setVisible(true);
054:
055: try {
056: //longMethoNameXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX();
057: recursiveMethod(100);
058: } catch (Throwable t) {
059: MessageDialog.showExceptionDialog(frame, "title",
060: "Message" + "Message\n" + "Message\n"
061: + "Message\n" + "Message\n"
062: + "Message\n" + "Message\n"
063: + "Message\n" + "Message\n"
064: + "Message", t,
065: ModalityType.DOCUMENT_MODAL,
066: ModalExclusionType.TOOLKIT_EXCLUDE);
067: }
068: }
069: });
070: }
071:
072: @SuppressWarnings({"UnusedDeclaration"})
073: private static void longMethoNameXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX() {
074: throw new RuntimeException("Bla");
075: }
076:
077: private static void recursiveMethod(int i) {
078: i--;
079: if (i > 0) {
080: recursiveMethod(i);
081: } else {
082: throw new RuntimeException();
083: }
084: }
085:
086: public static void showExceptionDialog(@Nullable
087: final Component parent, @NotNull
088: final String title, @NotNull
089: final String message, @NotNull
090: final Throwable t, @NotNull
091: CenterPanelDialog.ModalityType modalityType, @NotNull
092: final ModalExclusionType modalExclusionType) {
093: if (EventQueue.isDispatchThread()) {
094: showExceptionDialogInEDT(parent, title, message, t,
095: modalityType, modalExclusionType);
096: } else {
097: EventQueue.invokeLater(new Runnable() {
098: public void run() {
099: showExceptionDialogInEDT(parent, title, message, t,
100: ModalityType.MODELESS, modalExclusionType);
101: }
102: });
103: }
104: }
105:
106: private static void showExceptionDialogInEDT(@Nullable
107: final Component parent, @NotNull
108: final String title, @NotNull
109: final String message, @NotNull
110: final Throwable t, @NotNull
111: CenterPanelDialog.ModalityType modalityType, @NotNull
112: ModalExclusionType modalExclusionType) {
113: MessageDialog messageDialog;
114: if (parent instanceof Frame) {
115: Frame frame = (Frame) parent;
116: messageDialog = new MessageDialog(frame, title,
117: modalityType);
118: } else if (parent instanceof Dialog) {
119: Dialog dialog = (Dialog) parent;
120: messageDialog = new MessageDialog(dialog, title,
121: modalityType);
122: } else if (parent == null) {
123: messageDialog = new MessageDialog((Frame) null, title,
124: modalityType);
125: messageDialog.setModal(false);
126: } else {
127: Window window = SwingUtilities.getWindowAncestor(parent);
128: if (window instanceof Frame) {
129: Frame frame = (Frame) window;
130: messageDialog = new MessageDialog(frame, title,
131: modalityType);
132: } else if (window instanceof Dialog) {
133: messageDialog = new MessageDialog((Dialog) window,
134: title, modalityType);
135: } else {
136: messageDialog = new MessageDialog((Frame) null, title,
137: modalityType);
138: messageDialog.setModal(false);
139: }
140: }
141:
142: CenterPanelDialog.setModalExclusionType(messageDialog,
143: modalExclusionType);
144:
145: messageDialog.setMessage(message);
146: messageDialog.setDetailThrowable(t);
147: messageDialog.pack();
148: WindowUtils.setLocationRelativeTo(messageDialog, null);
149: messageDialog.setVisible(true);
150: }
151:
152: @NotNull
153: private JToggleButton detailsButton;
154: @NotNull
155: private JTextArea detailTextArea;
156: @NotNull
157: private JTextArea messageTextArea;
158: @NotNull
159: private JPanel contentPane;
160: @Nullable
161: private Runnable execAfter;
162:
163: private MessageDialog(@Nullable
164: Frame owner, @NotNull
165: String title, @NotNull
166: ModalityType modalityType) {
167: super (owner, title, modalityType);
168: init();
169: }
170:
171: private MessageDialog(@Nullable
172: Dialog owner, @NotNull
173: String title, @NotNull
174: ModalityType modalityType) {
175: super (owner, title, modalityType);
176: init();
177: }
178:
179: private void init() {
180: //setResizable(false);
181:
182: JPanel upperPanel = new JPanel(new BorderLayout());
183: contentPane = new JPanel(new BorderLayout());
184: contentPane.setBorder(BorderFactory.createEmptyBorder(10, 10,
185: 0, 10));
186:
187: JPanel helperPanel = new JPanel(new BorderLayout(10, 0));
188: Icon icon = UIManager.getIcon("OptionPane.errorIcon");//NON-NLS
189:
190: helperPanel.add(new JLabel(IconCreator
191: .converIconToImageIcon(icon)), BorderLayout.WEST);
192: messageTextArea = new JTextArea();
193:
194: JLabel helperLabel = new JLabel();
195: messageTextArea.setBorder(helperLabel.getBorder());
196: messageTextArea.setFont(helperLabel.getFont());
197: messageTextArea.setBackground(helperLabel.getBackground());
198: messageTextArea.setForeground(helperLabel.getForeground());
199:
200: TextComponentHelper.installDefaultPopupMenu(messageTextArea);
201:
202: messageTextArea.setEditable(false);
203: messageTextArea.setColumns(50);
204: messageTextArea.setLineWrap(true);
205: messageTextArea.setWrapStyleWord(true);
206:
207: JScrollPane scrollpane = new JScrollPane(messageTextArea);
208: scrollpane.setBorder(BorderFactory.createEmptyBorder());
209: helperPanel.add(scrollpane, BorderLayout.CENTER);
210:
211: upperPanel.add(helperPanel, BorderLayout.NORTH);
212:
213: JPanel buttonPanel = new JPanel(new GridLayout(1, 0, 10, 10));
214: buttonPanel.setBorder(BorderFactory.createEmptyBorder(10, 10,
215: 10, 10));
216: JButton closeButton = ComponentFactory.createButton("R",
217: "MessageDialog.Close");
218: closeButton.setDefaultCapable(true);
219: detailsButton = ComponentFactory.createToggleButton("R",
220: "MessageDialog.Details");
221: detailsButton.setEnabled(false);
222: buttonPanel.add(detailsButton);
223: buttonPanel.add(closeButton);
224:
225: JPanel helperPanel2 = new JPanel(new GridBagLayout());
226: helperPanel2.add(buttonPanel);
227: upperPanel.add(helperPanel2, BorderLayout.SOUTH);
228:
229: contentPane.add(upperPanel, BorderLayout.NORTH);
230:
231: getRootPane().setDefaultButton(closeButton);
232:
233: detailTextArea = new JTextArea();
234: UndoHelper.installUndoSupport(detailTextArea);
235: TextComponentHelper.installDefaultPopupMenu(detailTextArea);
236:
237: detailTextArea.setEditable(false);
238: detailTextArea.setRows(20);
239:
240: final JScrollPane detailScrollPane = new JScrollPane(
241: detailTextArea);
242: final JPanel scrollPanePanel = new JPanel(new BorderLayout());
243: scrollPanePanel.add(detailScrollPane, BorderLayout.CENTER);
244: scrollPanePanel.setBorder(BorderFactory.createEmptyBorder(0, 0,
245: 10, 0));
246: scrollPanePanel.setVisible(false);
247: contentPane.add(scrollPanePanel, BorderLayout.CENTER);
248:
249: getContentPane().add(contentPane, BorderLayout.CENTER);
250:
251: closeButton.addActionListener(new ActionListener() {
252: public void actionPerformed(@NotNull
253: ActionEvent e) {
254: dispose();
255: if (execAfter != null) {
256: execAfter.run();
257: }
258: }
259: });
260:
261: detailsButton.addActionListener(new ActionListener() {
262: public void actionPerformed(@NotNull
263: ActionEvent e) {
264: Point p = getLocation();
265: Dimension s = getSize();
266:
267: if (detailsButton.isSelected()) {
268: scrollPanePanel.setVisible(true);
269: //contentPane.add(scrollPanePanel, BorderLayout.CENTER);
270: //setResizable(true);
271: } else {
272: scrollPanePanel.setVisible(false);
273: //contentPane.remove(scrollPanePanel);
274: //setResizable(false);
275: }
276:
277: Dimension nS = getPreferredSize();
278:
279: int x = p.x + s.width / 2 - nS.width / 2;
280: int y = p.y;
281:
282: if (x < 0) {
283: x = 0;
284: }
285:
286: if (y < 0) {
287: y = 0;
288: }
289:
290: setLocation(x, y);
291:
292: pack();
293:
294: contentPane.revalidate();
295: contentPane.repaint();
296: }
297: });
298:
299: addWindowListener(new WindowAdapter() {
300: public void windowClosing(@NotNull
301: WindowEvent e) {
302: dispose();
303: if (execAfter != null) {
304: execAfter.run();
305: }
306: }
307: });
308: }
309:
310: public void setMessage(@NotNull
311: String message) {
312: messageTextArea.setText(message);
313: messageTextArea.setCaretPosition(0);
314: View rootView = messageTextArea.getUI().getRootView(
315: messageTextArea);
316: if (rootView.getViewCount() > 0) {
317: View view = rootView.getView(0);
318: int viewCount = view.getViewCount();
319: if (viewCount > 20) {
320: messageTextArea.setRows(20);
321: }
322: }
323: }
324:
325: public void setDetailMessage(@NotNull
326: String detail) {
327: detailTextArea.setText(detail);
328: detailTextArea.setCaretPosition(0);
329: detailsButton.setEnabled(true);
330: }
331:
332: public void setDetailThrowable(@Nullable
333: Throwable ex) {
334: if (ex != null) {
335: CharArrayWriter charArrayWriter = new CharArrayWriter();
336: PrintWriter pw = null;
337: try {
338: //noinspection IOResourceOpenedButNotSafelyClosed
339: pw = new PrintWriter(charArrayWriter);
340: ex.printStackTrace(pw);
341:
342: setDetailMessage(charArrayWriter.toString());
343: } finally {
344: IOUtil.closeStream(pw);
345: }
346: }
347: }
348:
349: }
|