KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > NotifyDialog


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.exceptions;
21
22 import java.awt.Container JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.ComponentAdapter JavaDoc;
28 import java.awt.event.ComponentEvent JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.StringWriter JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.LinkedList JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.LogRecord JavaDoc;
35 import java.util.logging.Logger JavaDoc;
36 import java.util.logging.SimpleFormatter JavaDoc;
37 import javax.swing.ImageIcon JavaDoc;
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JFrame JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JScrollPane JavaDoc;
42 import javax.swing.JTextPane JavaDoc;
43 import javax.swing.UIManager JavaDoc;
44 import org.jdesktop.layout.GroupLayout;
45 import org.jdesktop.layout.GroupLayout.ParallelGroup;
46 import org.jdesktop.layout.GroupLayout.SequentialGroup;
47 import org.jdesktop.layout.LayoutStyle;
48 import org.netbeans.modules.exceptions.data.Record;
49 import org.openide.DialogDisplayer;
50 import org.openide.NotifyDescriptor;
51 import org.openide.util.NbBundle;
52 import org.openide.windows.WindowManager;
53 /**
54  *
55  * @author jindra
56  */

57 public class NotifyDialog extends JFrame JavaDoc{
58     private static final String JavaDoc imgSource ="org/netbeans/modules/exceptions/img.png";// NOI18N
59
private static ReportDialog rDialog;
60     private Dimension JavaDoc NO_DETAILS_DIMENSION= new Dimension JavaDoc(450, 250);
61     private Dimension JavaDoc DETAILS_DIMENSION= new Dimension JavaDoc(700, 500);
62     //bounds, location
63

64     java.util.ResourceBundle JavaDoc bundle = NbBundle.getBundle(NotifyDialog.class);
65     
66     private final JButton JavaDoc okButton = new JButton JavaDoc(bundle.getString("CTL_OK"));;
67     private final JButton JavaDoc nextButton = new JButton JavaDoc(bundle.getString("CTL_NextException"));
68     private final JButton JavaDoc previousButton = new JButton JavaDoc(bundle.getString("CTL_PreviousException"));
69     private final JButton JavaDoc hideDetailsButton = new JButton JavaDoc(bundle.getString("CTL_Exception_Hide_Details"));
70     private final JButton JavaDoc showDetailsButton = new JButton JavaDoc(bundle.getString("CTL_Exception_Show_Details"));
71     private final JButton JavaDoc reportButton = new JButton JavaDoc(bundle.getString("CTL_Report_Exception"));
72     private final JTextPane JavaDoc text = new JTextPane JavaDoc() {
73         public boolean getScrollableTracksViewportWidth() {
74             return false;
75         }
76     };
77     private final JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc(text);
78     private final JLabel JavaDoc icon = new JLabel JavaDoc();
79     private final ActionListener JavaDoc notifyListener = new NotifyListener();
80     private boolean details; // false
81
private LinkedList JavaDoc<LinkedList JavaDoc<LogRecord JavaDoc>> throwables; // null
82
private int activ = 0;//activ runs from 1..n; 0 means there are no exceptions
83

84     public NotifyDialog() {
85         super();
86         initializeComponents();
87     }
88     
89     private void initializeComponents(){
90         getAccessibleContext().setAccessibleName(bundle.getString("Dialog_Accessible_name"));
91         getAccessibleContext().setAccessibleDescription(bundle.getString("Dialog_Accessible_description"));
92         setTitle(bundle.getString("CTL_Title_Exception"));
93         icon.setIcon(new ImageIcon JavaDoc(org.openide.util.Utilities.loadImage(imgSource)));
94         okButton.addActionListener(notifyListener);
95         okButton.setMnemonic(bundle.getString("CTL_OK").charAt(0));
96         nextButton.addActionListener(notifyListener);
97         nextButton.setMnemonic(bundle.getString("CTL_NextException").charAt(0));
98         previousButton.addActionListener(notifyListener);
99         previousButton.setMnemonic(bundle.getString("CTL_PreviousException").charAt(2));
100         hideDetailsButton.addActionListener(notifyListener);
101         hideDetailsButton.setMnemonic(bundle.getString("CTL_Exception_Hide_Details").charAt(0));
102         showDetailsButton.addActionListener(notifyListener);
103         showDetailsButton.setMnemonic(bundle.getString("CTL_Exception_Show_Details").charAt(0));
104         reportButton.addActionListener(notifyListener);
105         reportButton.setMnemonic(bundle.getString("CTL_Report_Exception").charAt(0));
106         text.setEditable(false);
107         // dialog size setting
108
addComponentListener(new ComponentAdapter JavaDoc() {
109             public void componentResized(ComponentEvent JavaDoc event) {
110                 if (details) DETAILS_DIMENSION = getSize();
111                 else NO_DETAILS_DIMENSION = getSize();
112             }
113         });
114         Font JavaDoc font = new Font JavaDoc("Monospaced", Font.PLAIN, text.getFont().getSize()); // NOI18N
115
text.setFont(font);
116         text.setForeground(UIManager.getColor("Label.foreground")); // NOI18N
117
text.setBackground(UIManager.getColor("Label.background")); // NOI18N
118
Runnable JavaDoc runable = new Runnable JavaDoc(){
119             public void run(){
120                 setLocationRelativeTo(WindowManager.getDefault().getMainWindow());
121             }
122         };
123         WindowManager.getDefault().invokeWhenUIReady(runable);
124         update();
125     }
126     
127     private void fillInText(){
128         if (activ!= 0){
129             LogRecord JavaDoc shown = throwables.get(activ - 1).getFirst();
130             StringWriter JavaDoc swr = new StringWriter JavaDoc();
131             if (details) {
132                 SimpleFormatter JavaDoc formatter = new SimpleFormatter JavaDoc();
133                 swr.write(formatter.format(shown));
134                 swr.write('\n');
135                 // LogRecord[] anns = null;
136
// if (anns != null) {
137
// swr.write("Annotation: \n");
138
// for (int i = 0; i < anns.length; i++) {
139
// swr.write(formatter.format(anns[i]));
140
// }
141
// }
142
} else {
143                 swr.write(new Date JavaDoc(shown.getMillis()).toString() + '\n');
144                 swr.write(bundle.getString("Severity") + shown.getLevel().toString() + '\n');
145                 swr.write(shown.getThrown().getClass().toString() + '\n');
146             }
147             text.setText(swr.toString());
148             try {
149                 swr.close();
150             } catch (IOException JavaDoc ex) {
151                 // do nothing, not so important to close StringWriter
152
}
153         }
154     }
155     
156     private void update(){
157         fillInText();
158         if ((throwables!= null)&&(throwables.size()>0)) reportButton.setEnabled(true);else reportButton.setEnabled(false);
159         Container JavaDoc contentPane = getContentPane();
160         //removing old components
161
contentPane.removeAll();
162         GroupLayout layout = new GroupLayout(contentPane);
163         ParallelGroup horGroup = layout.createParallelGroup(GroupLayout.LEADING);
164         ParallelGroup verGroup =layout.createParallelGroup(GroupLayout.LEADING);
165         
166         //----- hor group -----//
167
//---baseline group---//
168
SequentialGroup baseLine = layout.createSequentialGroup();
169         if (!details) baseLine.add(showDetailsButton); else baseLine.add(hideDetailsButton);
170         baseLine.addPreferredGap(LayoutStyle.RELATED, 106, Short.MAX_VALUE);
171         baseLine.add(reportButton);
172         baseLine.addPreferredGap(LayoutStyle.RELATED);
173         if (activ > 1){
174             baseLine.add(previousButton)
175                     .addPreferredGap(LayoutStyle.RELATED);
176         }
177         if ((throwables != null)&&(throwables.size() > activ)){
178             baseLine.add(nextButton)
179                     .addPreferredGap(LayoutStyle.RELATED);
180         }
181         baseLine.add(okButton);
182         
183         //-- text group ---- //
184
SequentialGroup textGroup = layout.createSequentialGroup();
185         if (!details) textGroup.add(icon, 34, 34, 34);
186         textGroup.addPreferredGap(LayoutStyle.RELATED)
187                 .add(scrollPane, GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE);
188         
189         
190         horGroup.add(GroupLayout.TRAILING, layout.createSequentialGroup()
191                 .addContainerGap()
192                 .add(layout.createParallelGroup(GroupLayout.TRAILING)
193                 .add(GroupLayout.LEADING, textGroup)
194                 .add(baseLine))
195                 .addContainerGap());
196         
197         
198         //------ ver group ------//
199
//-- text group ---- //
200
ParallelGroup textVer = layout.createParallelGroup(GroupLayout.LEADING)
201                 .add(scrollPane, GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE);
202         if (!details) textVer.add(icon, 42, 42, 42);
203         
204         //---baseline group---//
205
ParallelGroup buttons = layout.createParallelGroup(GroupLayout.BASELINE);
206         if (!details) buttons.add(showDetailsButton); else buttons.add(hideDetailsButton);
207         buttons.add(reportButton);
208         if (activ > 1) buttons.add(previousButton);
209         if ((throwables != null)&&(throwables.size() > activ)) buttons.add(nextButton);
210         buttons.add(okButton);
211         
212         verGroup.add(GroupLayout.TRAILING, layout.createSequentialGroup()
213                 .addContainerGap()
214                 .add(textVer)
215                 .addPreferredGap(LayoutStyle.RELATED)
216                 .add(buttons)
217                 .addContainerGap());
218         
219         if (!details)setPreferredSize(NO_DETAILS_DIMENSION);
220         else setPreferredSize(DETAILS_DIMENSION);
221         
222         layout.setHorizontalGroup(horGroup);
223         layout.setVerticalGroup(verGroup);
224         contentPane.setLayout(layout);
225         pack();
226     }
227     
228     public void notify(LinkedList JavaDoc<LinkedList JavaDoc<LogRecord JavaDoc>> logRecords){
229         throwables = logRecords;
230         setActiv(throwables.size());
231         update();
232     }
233     
234     //-------------ACTION LISTENNER------------//
235
private class NotifyListener implements ActionListener JavaDoc{
236         NotifyListener(){
237         }
238         
239         public void actionPerformed(ActionEvent JavaDoc event) {
240             if (getReportDialog().isVisible()) getReportDialog().setVisible(true);
241             else if (event.getSource().equals(okButton)){
242                 dispose();
243             }else if (event.getSource().equals(showDetailsButton)){
244                 details = true;
245                 update();
246             }else if (event.getSource().equals(hideDetailsButton)){
247                 details = false;
248                 update();
249             }else if (event.getSource().equals(nextButton)){
250                 if (activ < throwables.size()) setActiv(activ+1);
251                 update();
252             }else if(event.getSource().equals(previousButton)){
253                 if (activ > 1) setActiv(activ-1);
254                 update();
255             }else if(event.getSource().equals(reportButton)){
256                 CheckIssue check = new CheckIssue();
257                 int exists = -1;
258                 try{
259                     exists = check.exists(throwables.get(activ-1).getFirst().getThrown());
260                     Logger.getLogger(NotifyDialog.class.getName()).log(Level.FINEST, "ISSUE_EXISTS: "+Integer.toString(exists)); // NOI18A
261
}catch(IOException JavaDoc e){
262                     Logger.getLogger(NotifyDialog.class.getName()).log(Level.WARNING, "CONNECTION REFUSED");// NOI18A
263
exists = -1;
264                 }
265                 // if ((ExceptionsRepos.getInstance().contains(throwables.get(activ-1).getFirst()))) -local repository of reported issues
266
if (exists != -1){
267                     String JavaDoc message = NbBundle.getMessage(NotifyDialog.class, "ALREADY_REPORTED");
268                     message = message.concat(Integer.toString(exists));
269                     DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, NotifyDescriptor.INFORMATION_MESSAGE));
270                 }else{
271                     java.awt.EventQueue.invokeLater(new Runnable JavaDoc() {
272                         public void run() {
273                             getReportDialog().setVisible(true);
274                         }
275                     });
276                 }
277             }
278             
279         }
280     }
281     
282     private synchronized ReportDialog getReportDialog(){
283         if (rDialog == null){
284             rDialog = new ReportDialog();
285             rDialog.setLocationRelativeTo(this);
286         }
287         return rDialog;
288     }
289     
290     private void setActiv(int _activ){
291         if ((_activ <= throwables.size())&&(_activ > 0)) activ = _activ;
292         Sender.setTransport(throwables.get(activ-1));
293     }
294         
295     //------------MAIN ---------------//
296

297 /* public static void main(String[] args){
298     Transport transport[] = new Transport[3];
299     NotifyDialog nb = new NotifyDialog();
300     transport[0] = new Transport(ErrorManager.EXCEPTION, new NullPointerException("NPE"));
301     transport[1] = new Transport(ErrorManager.ERROR, new IOException("IOE"));
302     transport[2] = new Transport(ErrorManager.USER, new AssertionError("AE"));
303     nb.notify(transport);
304     nb.setVisible(true);
305  }
306  */

307 }
308
Popular Tags