View Javadoc

1   /*
2    * $HeadURL: $
3    * $Date: $
4    * $Revision: $
5    * $Author: $
6    * 
7    * Copyright (c) 2005 MindTree Consulting Ltd. 
8    * 
9    * This file is part of Insight.
10   * 
11   * Insight is free software: you can redistribute it and/or modify it under the 
12   * terms of the GNU General Public License as published by the Free Software 
13   * Foundation, either version 3 of the License, or (at your option) any later 
14   * version.
15   * 
16   * Insight is distributed in the hope that it will be useful, but 
17   * WITHOUT ANY WARRANTY; without even the implied warranty of 
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
19   * Public License for more details.
20   * 
21   * You should have received a copy of the GNU General Public License along with 
22   * Insight.  If not, see <http://www.gnu.org/licenses/>.
23   */
24  package com.mindtree.techworks.insight.gui.util;
25  
26  import java.awt.Color;
27  import java.awt.Component;
28  import java.awt.Dimension;
29  import java.awt.GridBagConstraints;
30  import java.awt.GridBagLayout;
31  import java.awt.Insets;
32  import java.awt.event.ActionEvent;
33  import java.awt.event.ActionListener;
34  import java.util.Iterator;
35  
36  import javax.swing.BorderFactory;
37  import javax.swing.JButton;
38  import javax.swing.JDialog;
39  import javax.swing.JOptionPane;
40  import javax.swing.JScrollPane;
41  import javax.swing.JTextPane;
42  
43  import com.mindtree.techworks.insight.InsightConstants;
44  import com.mindtree.techworks.insight.gui.Insight;
45  import com.mindtree.techworks.insight.model.ReceiverFormat;
46  import com.mindtree.techworks.insight.receiver.RuntimeNamespaceContainer;
47  import com.mindtree.techworks.insight.spi.LogNamespace;
48  
49  /**
50  *
51  * The <code>LoadedNamespacesFrame</code> class displays details about 
52  * files that have been loaded and their namespace details
53  *
54  * @author  Regunath B
55  * @version 1.0, 05/03/18
56  */
57  
58  public class LoadedNamespacesFrame extends JDialog {
59  	
60  	/**
61  	 * Used for object serialization
62  	 */
63  	private static final long serialVersionUID = -248346217650292459L;
64  	
65  	/*
66  	 * Useful constants for the preferred size of the text field 
67  	 */
68  	private static final int WIDTH = 600;
69  	private static final int HEIGHT = 300;
70  	
71  	/*
72  	 * Layout definition.
73  	 */
74  	private GridBagLayout gl;
75  	private GridBagConstraints gc;
76  	
77  	/*
78  	 * The JEditorPane instance used to render the details
79  	 */
80  	private JTextPane detailsPane;	
81  	
82  	/*
83  	 * Buttons in the display
84  	 */
85  	private JButton closeButton;
86  	
87  	/*
88  	 * The Insight instance that created this frame
89  	 */
90  	private Insight insight;
91  	
92  	/**
93  	 * Constructor for this class
94  	 * @param insight the Insight instance for this class
95  	 */
96  	public LoadedNamespacesFrame(Insight insight) {
97  		super(JOptionPane.getFrameForComponent(insight),InsightConstants.getLiteral("LOADED_NAMESPACES"),true);
98  		this.setResizable(false);
99  		this.insight = insight;
100 		gl = new GridBagLayout();
101 		gc = new GridBagConstraints();
102 		getContentPane().setLayout(gl);
103 
104         detailsPane = new JTextPane();
105         detailsPane.setEditable(false);
106         detailsPane.setContentType("text/html");        
107         addStylesAndContentToPane(detailsPane);
108         
109         JScrollPane scrollPane = new JScrollPane(detailsPane);
110         scrollPane.setBorder(BorderFactory.createTitledBorder(InsightConstants.getLiteral("NAMESPACE_DETAILS")));
111         
112         detailsPane.setPreferredSize(new Dimension(WIDTH, HEIGHT));
113 		addComponent(scrollPane,0,0,1,1,2,1);
114 		
115 		closeButton = new JButton(InsightConstants.getLiteral("OK"));
116 		addComponent(closeButton,0,1,0,0,1,1);
117 
118 		ButtionActionProcessor actionProcessor = new ButtionActionProcessor();
119 		closeButton.addActionListener(actionProcessor);
120 		
121 		// display this frame
122 		this.pack();
123 		// Center this window in Insight
124 		int insightX = insight.getX();
125 		int insightY = insight.getY();
126 		int thisX = insightX + ((this.insight.getWidth() - this.getWidth())/ 2);
127 		int thisY = insightY + ((this.insight.getHeight() - this.getHeight())/ 2);
128 		this.setLocation(thisX, thisY);
129 		this.show();	
130 	}
131 	
132 	/**
133 	 * Adds the specified component to this panel using the specified constraints.
134 	 **/
135 	private final void addComponent(Component c, int gridx, int gridy, int weightx,
136 		int weighty, int width, int height) {
137 		gc.fill = GridBagConstraints.BOTH;
138 		gc.anchor = GridBagConstraints.NORTHWEST;
139 		gc.insets = new Insets(5,5,5,5);		
140 		gc.gridx = gridx;
141 		gc.gridy = gridy;
142 		gc.weightx = weightx;
143 		gc.weighty = weighty;
144 		gc.gridwidth = width;
145 		gc.gridheight = height;		
146 		gl.setConstraints( c, gc);
147 		getContentPane().add(c);
148 	}
149 	
150 	/**
151 	 * Helper method to add namepsace contents and the respective styles to the
152 	 * specified JtextPane
153 	 * @param detailsPane the JTextPane instance to add the styles and contents
154 	 */
155 	private final void addStylesAndContentToPane(JTextPane detailsPane) {
156 		Iterator iterator = RuntimeNamespaceContainer.getLoadedNamespaces().iterator();
157 		StringBuffer buffer = new StringBuffer("<table width='100%' border='0'>");			
158 		while (iterator.hasNext()) {
159 			LogNamespace namespace = (LogNamespace)iterator.next();
160 			buffer.append("<tr><td bgColor='#");
161 			Color color = RuntimeNamespaceContainer.getNamespaceColor(namespace);
162 			buffer.append(Integer.toHexString(color.getRed()));
163 			buffer.append(Integer.toHexString(color.getGreen()));
164 			buffer.append(Integer.toHexString(color.getBlue()));
165 			buffer.append("' ><font face='" + InsightConstants.DEFAULT_FONT.getFamily()+ "' size=-1>");
166 			buffer.append(InsightConstants.getLiteral("NAMESPACE") + " : " + namespace.getSourceString());
167 			buffer.append("<br>");
168 			ReceiverFormat[] receiverFormat = namespace.getReceiverFormat();
169 			String loadedPatterns = new String("");
170 			for(int i=0; i<receiverFormat.length; i++){
171 				loadedPatterns += "<br>" + receiverFormat[i].getRawPattern();
172 			}
173 			buffer.append(InsightConstants.getLiteral("LOG4J_PATTERNS") + " : " + loadedPatterns);
174 			buffer.append("<br>");
175 			buffer.append(InsightConstants.getLiteral("NODE") + " : " + namespace.getNodeId());
176 			buffer.append("</font></td></tr>");
177 		}
178 		buffer.append("</table>");
179 		detailsPane.setText(buffer.toString());
180 		detailsPane.setCaretPosition(0);				
181 	}
182 
183 	/**
184 	 * Helper class to handle action events 
185 	 */
186 	private final class ButtionActionProcessor implements ActionListener {
187 		/** 
188 		 * Interface method implementation
189 		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
190 		 */
191 		public void actionPerformed(ActionEvent e) {
192 			Object source = e.getSource();
193 			if (source == closeButton) {
194 				hide();
195 				dispose();
196 			}
197 		}
198 
199 	}
200 
201 }