components/visual-panels/core/src/java/vpanels/panel/com/oracle/solaris/vp/panel/swing/smf/SmfLogPanel.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     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, 2013, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.panel.swing.smf;
       
    27 
       
    28 import java.awt.*;
       
    29 import java.beans.*;
       
    30 import java.io.IOException;
       
    31 import javax.swing.*;
       
    32 import com.oracle.solaris.scf.common.ScfException;
       
    33 import com.oracle.solaris.vp.panel.common.api.smf_old.*;
       
    34 import com.oracle.solaris.vp.panel.common.smf.*;
       
    35 import com.oracle.solaris.vp.util.misc.finder.Finder;
       
    36 import com.oracle.solaris.vp.util.swing.SettingsPanel;
       
    37 
       
    38 @SuppressWarnings({"serial"})
       
    39 public class SmfLogPanel extends SettingsPanel {
       
    40     //
       
    41     // Instance data
       
    42     //
       
    43 
       
    44     private JTextArea field;
       
    45     private ServiceTracker tracker;
       
    46 
       
    47     private PropertyChangeListener serviceListener =
       
    48 	new PropertyChangeListener() {
       
    49 	    @Override
       
    50 	    public void propertyChange(PropertyChangeEvent event) {
       
    51 		updateContentOnEventThread();
       
    52 	    }
       
    53 	};
       
    54 
       
    55     //
       
    56     // Constructors
       
    57     //
       
    58 
       
    59     public SmfLogPanel() {
       
    60 	field = new JTextArea() {
       
    61 	    @Override
       
    62 	    public Dimension getPreferredScrollableViewportSize() {
       
    63 		FontMetrics metrics = getFontMetrics(
       
    64 		    getFont().deriveFont(Font.BOLD));
       
    65 		Dimension d = super.getPreferredScrollableViewportSize();
       
    66 
       
    67 		// Set scroll pane size (20 lines heigh, 20 characters wide)
       
    68 		d.height = Math.min(20 * metrics.getHeight(), d.height);
       
    69 		d.width = Math.min(20 * metrics.charWidth('e'), d.width);
       
    70 
       
    71 		return d;
       
    72 	    }
       
    73 	};
       
    74 
       
    75 	field.setEditable(false);
       
    76 	field.setLineWrap(false);
       
    77 	field.setBackground(Color.white);
       
    78 
       
    79 	setContent(new JScrollPane(field), false, false);
       
    80     }
       
    81 
       
    82     //
       
    83     // SmfLogPanel methods
       
    84     //
       
    85 
       
    86     public void init(ServiceTracker tracker)
       
    87 	throws IOException {
       
    88 
       
    89 	if (this.tracker != tracker) {
       
    90 	    if (this.tracker != null) {
       
    91 		this.tracker.removePropertyChangeListener(
       
    92 		    ServiceTracker.PROPERTY_SERVICE, serviceListener);
       
    93 	    }
       
    94 
       
    95 	    this.tracker = tracker;
       
    96 
       
    97 	    if (tracker != null) {
       
    98 		tracker.addPropertyChangeListener(
       
    99 		    ServiceTracker.PROPERTY_SERVICE, serviceListener);
       
   100 	    }
       
   101 
       
   102 	    updateContentOnEventThread();
       
   103 	}
       
   104     }
       
   105 
       
   106     //
       
   107     // Private methods
       
   108     //
       
   109 
       
   110     private void updateContent() {
       
   111 	String content = null;
       
   112 	ServiceBean service = tracker == null ? null : tracker.getService();
       
   113 
       
   114 	if (tracker != null) {
       
   115 	    try {
       
   116 		// Limit to 1mb to not overwhelm network/memory
       
   117 		LogInfo info = service.getLogInfo(1 << 20);
       
   118 		byte[] bytes = info.getContents();
       
   119 		content = new String(bytes);
       
   120 		String resource = "service.log.contents";
       
   121 		if (bytes.length < info.getSize()) {
       
   122 		    resource += ".truncated";
       
   123 		}
       
   124 		content = Finder.getString(resource, info.getName(), content);
       
   125 	    } catch (ScfException e) {
       
   126 		content = Finder.getString("service.log.error");
       
   127 	    }
       
   128 	}
       
   129 
       
   130 	field.setText(content);
       
   131         field.scrollRectToVisible(new Rectangle(0,
       
   132 	    field.getPreferredSize().height));
       
   133     }
       
   134 
       
   135     private void updateContentOnEventThread() {
       
   136 	EventQueue.invokeLater(
       
   137 	    new Runnable() {
       
   138 		@Override
       
   139 		public void run() {
       
   140 		    updateContent();
       
   141 		}
       
   142 	    });
       
   143     }
       
   144 }