components/visual-panels/core/src/java/vpanels/client/com/oracle/solaris/vp/client/swing/SwingErrorPanelDescriptor.java
changeset 827 0944d8c0158b
child 1700 d04a7bb15a5b
equal deleted inserted replaced
826:c6aad84d2493 827:0944d8c0158b
       
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.client.swing;
       
    27 
       
    28 import java.awt.Component;
       
    29 import java.io.*;
       
    30 import java.util.*;
       
    31 import javax.swing.*;
       
    32 import com.oracle.solaris.vp.client.common.ErrorPanelDescriptor;
       
    33 import com.oracle.solaris.vp.panel.common.ClientContext;
       
    34 import com.oracle.solaris.vp.panel.common.api.panel.CustomPanel;
       
    35 import com.oracle.solaris.vp.panel.common.control.*;
       
    36 import com.oracle.solaris.vp.panel.common.model.*;
       
    37 import com.oracle.solaris.vp.panel.swing.control.*;
       
    38 import com.oracle.solaris.vp.panel.swing.model.SwingPanelDescriptor;
       
    39 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    40 
       
    41 /**
       
    42  * Implementation of {@code ErrorPanelDescriptor} for the Swing client.
       
    43  * Displays a tab containing the provided throwable's stack trace for
       
    44  * each custom panel that failed.
       
    45  */
       
    46 @SuppressWarnings({"serial"})
       
    47 public class SwingErrorPanelDescriptor
       
    48     extends AbstractPanelDescriptor<ManagedObject>
       
    49     implements SwingPanelDescriptor<ManagedObject>,
       
    50     ErrorPanelDescriptor<ManagedObject> {
       
    51 
       
    52     //
       
    53     // Inner classes
       
    54     //
       
    55 
       
    56     static class ErrorTab extends SwingControl<PanelDescriptor, JScrollPane> {
       
    57 	//
       
    58 	// Instance data
       
    59 	//
       
    60 
       
    61 	private CustomPanel panel;
       
    62 	private Throwable t;
       
    63 
       
    64 	//
       
    65 	// Constructors
       
    66 	//
       
    67 
       
    68 	public ErrorTab(CustomPanel panel, Throwable t, ClientContext context) {
       
    69 	    super(panel.getName(), Finder.getString("errorpanel.tab.name",
       
    70 		panel.getName()), context);
       
    71 
       
    72 	    this.panel = panel;
       
    73 	    this.t = t;
       
    74 	}
       
    75 
       
    76 	//
       
    77 	// SwingControl methods
       
    78 	//
       
    79 
       
    80 	@Override
       
    81 	public JScrollPane createComponent() {
       
    82 	    JTextArea area = new JTextArea();
       
    83 	    area.setEditable(false);
       
    84 	    area.setColumns(80);
       
    85 
       
    86 	    String header = Finder.getString("errorpanel.header",
       
    87 		panel.getName());
       
    88 
       
    89 	    StringWriter sw = new StringWriter();
       
    90 	    PrintWriter pw = new PrintWriter(sw);
       
    91 	    t.printStackTrace(pw);
       
    92 	    pw.flush();
       
    93 
       
    94 	    area.setText(header);
       
    95 	    area.append("\n\n");
       
    96 	    area.append(sw.toString());
       
    97 
       
    98 	    return new JScrollPane(area);
       
    99 	}
       
   100     }
       
   101 
       
   102     //
       
   103     // Instance data
       
   104     //
       
   105 
       
   106     private DefaultControl control;
       
   107     private List<Control> tabs = new LinkedList<Control>();
       
   108 
       
   109     //
       
   110     // Constructors
       
   111     //
       
   112 
       
   113     public SwingErrorPanelDescriptor(String name, ClientContext context) {
       
   114 	super(name, context);
       
   115     }
       
   116 
       
   117     //
       
   118     // ManagedObject methods
       
   119     //
       
   120 
       
   121     @Override
       
   122     public String getName() {
       
   123 	return Finder.getString("errorpanel.name", getId());
       
   124     }
       
   125 
       
   126     @Override
       
   127     public String getStatusText() {
       
   128 	int number = tabs.size();
       
   129 	return Finder.getString("errorpanel.status." + number, number);
       
   130     }
       
   131 
       
   132     @Override
       
   133     public ManagedObjectStatus getStatus() {
       
   134 	return ManagedObjectStatus.ERROR;
       
   135     }
       
   136 
       
   137     //
       
   138     // PanelDescriptor methods
       
   139     //
       
   140 
       
   141     /**
       
   142      * Returns a {@link TabbedControl}.
       
   143      */
       
   144     @Override
       
   145     public Control getControl() {
       
   146 	if (control == null) {
       
   147 	    String id = "tabs";
       
   148 	    String name = Finder.getString("errorpanel.tabs.name");
       
   149 	    TabbedControl tControl =
       
   150 		new TabbedControl<SwingErrorPanelDescriptor>(id, name, this);
       
   151 	    tControl.addChildren(tabs.toArray(new ErrorTab[tabs.size()]));
       
   152 
       
   153 	    control = new PanelFrameControl<SwingErrorPanelDescriptor>(this);
       
   154 	    control.addChildren(tControl);
       
   155 	}
       
   156 	return control;
       
   157     }
       
   158 
       
   159     //
       
   160     // ErrorPanelDescriptor methods
       
   161     //
       
   162 
       
   163     @Override
       
   164     public void addError(CustomPanel panel, Throwable t) {
       
   165 	tabs.add(new ErrorTab(panel, t, getClientContext()));
       
   166     }
       
   167 
       
   168     @Override
       
   169     public int getErrorCount() {
       
   170 	return tabs.size();
       
   171     }
       
   172 }