View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.chainsaw.receivers;
19  
20  import java.awt.BorderLayout;
21  import java.awt.Dimension;
22  import java.awt.event.ActionEvent;
23  import java.awt.event.ActionListener;
24  import java.beans.PropertyChangeEvent;
25  import java.beans.PropertyChangeListener;
26  import java.io.IOException;
27  import java.net.URL;
28  
29  import javax.swing.JDialog;
30  import javax.swing.JEditorPane;
31  import javax.swing.JFrame;
32  import javax.swing.JPanel;
33  import javax.swing.JScrollPane;
34  import javax.swing.JSplitPane;
35  import javax.swing.UIManager;
36  
37  import org.apache.log4j.LogManager;
38  import org.apache.log4j.chainsaw.help.HelpManager;
39  import org.apache.log4j.chainsaw.helper.OkCancelPanel;
40  import org.apache.log4j.chainsaw.messages.MessageCenter;
41  import org.apache.log4j.net.SocketHubReceiver;
42  import org.apache.log4j.plugins.Plugin;
43  import org.apache.log4j.plugins.Receiver;
44  
45  
46  /***
47   * A panel that allows a user to configure a new Plugin, and
48   * view that plugins javadoc at the same time
49   *
50   * @author Paul Smith <psmith@apache.org>
51   *
52   */
53  public class NewReceiverDialogPanel extends JPanel {
54  
55      private PluginPropertyEditorPanel pluginEditorPanel =
56          new PluginPropertyEditorPanel();
57      private final OkCancelPanel okPanel = new OkCancelPanel();
58      private final JEditorPane javaDocPane = new JEditorPane();
59      private final JScrollPane javaDocScroller = new JScrollPane(javaDocPane);
60      private final JSplitPane splitter = new JSplitPane();
61  
62      private NewReceiverDialogPanel() {
63          setupComponents();
64          setupListeners();
65      }
66  
67      /***
68       *
69       */
70      private void setupListeners() {
71  
72          /***
73           * We listen for the plugin change, and modify the editor panes
74           * url to be the Help resource for that class
75           */
76          pluginEditorPanel.addPropertyChangeListener("plugin",
77              new PropertyChangeListener() {
78  
79                  public void propertyChange(PropertyChangeEvent evt) {
80  
81                      Plugin plugin = (Plugin) evt.getNewValue();
82                      URL url = HelpManager.getInstance().getHelpForClass(
83                              plugin.getClass());
84  
85                      try {
86                          javaDocPane.setPage(url);
87                      } catch (IOException e) {
88                          MessageCenter.getInstance().getLogger().error(
89                              "Failed to load the Help resource for " +
90                              plugin.getClass(), e);
91                      }
92                  }
93              });
94      }
95  
96      /***
97       *
98       */
99      private void setupComponents() {
100         setLayout(new BorderLayout());
101 
102         setupJavadoc();
103 
104         setupPluginPropertyPanel();
105 
106         setupSplitter();
107 
108         add(splitter, BorderLayout.CENTER);
109         add(okPanel, BorderLayout.SOUTH);
110         setMinimumSize(new Dimension(600, 600));
111         setPreferredSize(getMinimumSize());
112     }
113 
114     private void setupPluginPropertyPanel() {
115         pluginEditorPanel.setMinimumSize(new Dimension(320, 160));
116         pluginEditorPanel.setPreferredSize(pluginEditorPanel.getMinimumSize());
117     }
118 
119     private void setupSplitter() {
120         splitter.setTopComponent(javaDocScroller);
121         splitter.setBottomComponent(pluginEditorPanel);
122         splitter.setResizeWeight(0.8);
123         splitter.setOrientation(JSplitPane.VERTICAL_SPLIT);
124     }
125 
126     private void setupJavadoc() {
127         javaDocPane.setEditable(false);
128     }
129 
130     /***
131      * Creates a new panel, with the contents configured to allow the editing
132      * of a NEW instance of the specified class (which must implement the Receiver
133      * interface)
134      * @param receiverClass
135      * @return NewReceiverDialogPanel
136      * @throws IllegalArgumentException if the specified class is not a Receiver
137      */
138     public static NewReceiverDialogPanel create(Class receiverClass) {
139 
140         if (!Receiver.class.isAssignableFrom(receiverClass)) {
141             throw new IllegalArgumentException(receiverClass.getName() +
142                 " is not a Receiver");
143         }
144 
145         Receiver receiverInstance = null;
146 
147         try {
148             receiverInstance = (Receiver) receiverClass.newInstance();
149 
150         } catch (Exception e) {
151         	LogManager.getLogger(NewReceiverDialogPanel.class).error(
152                 "Failed to create a new Receiver instance, this exception is unexpected",
153                 e);
154         }
155 
156         NewReceiverDialogPanel panel = new NewReceiverDialogPanel();
157 
158         panel.pluginEditorPanel.setPlugin(receiverInstance);
159 
160         return panel;
161     }
162 
163     public static void main(String[] args) throws Exception {
164 
165         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
166 
167         NewReceiverDialogPanel panel = NewReceiverDialogPanel.create(
168                 SocketHubReceiver.class);
169 
170         JDialog dialog = new JDialog((JFrame) null, true);
171         dialog.getContentPane().add(panel);
172 
173         ActionListener al = new ActionListener() {
174                 public void actionPerformed(ActionEvent e) {
175                     System.exit(1);
176                 }
177             };
178 
179         panel.okPanel.getOkButton().addActionListener(al);
180         panel.okPanel.getCancelButton().addActionListener(al);
181 
182         dialog.pack();
183 
184         dialog.setVisible(true);
185     }
186 
187     /***
188      * @return Returns the okPanel.
189      */
190     public final OkCancelPanel getOkPanel() {
191 
192         return okPanel;
193     }
194 
195     /***
196      *
197      */
198     public Plugin getPlugin() {
199 
200         return this.pluginEditorPanel.getPlugin();
201     }
202 
203 }