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 org.apache.log4j.LogManager;
    21  import org.apache.log4j.chainsaw.help.HelpManager;
    22  import org.apache.log4j.chainsaw.helper.OkCancelPanel;
    23  import org.apache.log4j.chainsaw.messages.MessageCenter;
    24  import org.apache.log4j.plugins.Plugin;
    25  import org.apache.log4j.plugins.Receiver;
    26
    27  import javax.swing.*;
    28  import java.awt.*;
    29  import java.awt.event.ActionListener;
    30  import java.io.IOException;
    31  import java.net.URL;
    32
    33
    34  /**
    35   * A panel that allows a user to configure a new Plugin, and
    36   * view that plugins javadoc at the same time
    37   *
    38   * @author Paul Smith &lt;psmith@apache.org&gt;
    39   */
    40  public class NewReceiverDialogPanel extends JPanel {
    41
    42      private PluginPropertyEditorPanel pluginEditorPanel =
    43          new PluginPropertyEditorPanel();
    44      private final OkCancelPanel okPanel = new OkCancelPanel();
    45      private final JEditorPane javaDocPane = new JEditorPane();
    46      private final JScrollPane javaDocScroller = new JScrollPane(javaDocPane);
    47      private final JSplitPane splitter = new JSplitPane();
    48
    49      private NewReceiverDialogPanel() {
    50          setupComponents();
    51          setupListeners();
    52      }
    53
    54      /**
    55       *
    56       */
    57      private void setupListeners() {
    58
    59          /**
    60           * We listen for the plugin change, and modify the editor panes
    61           * url to be the Help resource for that class
    62           */
    63          pluginEditorPanel.addPropertyChangeListener("plugin",
    64              evt -> {
    65
    66                  Plugin plugin = (Plugin) evt.getNewValue();
    67                  URL url = HelpManager.getInstance().getHelpForClass(
    68                      plugin.getClass());
    69
    70                  try {
    71                      javaDocPane.setPage(url);
    72                  } catch (IOException e) {
    73                      MessageCenter.getInstance().getLogger().error(
    74                          "Failed to load the Help resource for " +
    75                              plugin.getClass(), e);
    76                  }
    77              });
    78      }
    79
    80      /**
    81       *
    82       */
    83      private void setupComponents() {
    84          setLayout(new BorderLayout());
    85
    86          setupJavadoc();
    87
    88          setupPluginPropertyPanel();
    89
    90          setupSplitter();
    91
    92          add(splitter, BorderLayout.CENTER);
    93          add(okPanel, BorderLayout.SOUTH);
    94          setMinimumSize(new Dimension(600, 600));
    95          setPreferredSize(getMinimumSize());
    96      }
    97
    98      private void setupPluginPropertyPanel() {
    99          pluginEditorPanel.setMinimumSize(new Dimension(320, 160));
    100         pluginEditorPanel.setPreferredSize(pluginEditorPanel.getMinimumSize());
    101     }
    102
    103     private void setupSplitter() {
    104         splitter.setTopComponent(javaDocScroller);
    105         splitter.setBottomComponent(pluginEditorPanel);
    106         splitter.setResizeWeight(0.8);
    107         splitter.setOrientation(JSplitPane.VERTICAL_SPLIT);
    108     }
    109
    110     private void setupJavadoc() {
    111         javaDocPane.setEditable(false);
    112     }
    113
    114     /**
    115      * Creates a new panel, with the contents configured to allow the editing
    116      * of a NEW instance of the specified class (which must implement the Receiver
    117      * interface)
    118      *
    119      * @param receiverClass
    120      * @return NewReceiverDialogPanel
    121      * @throws IllegalArgumentException if the specified class is not a Receiver
    122      */
    123     public static NewReceiverDialogPanel create(Class receiverClass) {
    124
    125         if (!Receiver.class.isAssignableFrom(receiverClass)) {
    126             throw new IllegalArgumentException(receiverClass.getName() +
    127                 " is not a Receiver");
    128         }
    129
    130         Receiver receiverInstance = null;
    131
    132         try {
    133             receiverInstance = (Receiver) receiverClass.newInstance();
    134
    135         } catch (Exception e) {
    136             LogManager.getLogger(NewReceiverDialogPanel.class).error(
    137                 "Failed to create a new Receiver instance, this exception is unexpected",
    138                 e);
    139         }
    140
    141         NewReceiverDialogPanel panel = new NewReceiverDialogPanel();
    142
    143         panel.pluginEditorPanel.setPlugin(receiverInstance);
    144
    145         return panel;
    146     }
    147
    148     /**
    149      * @return Returns the okPanel.
    150      */
    151     public final OkCancelPanel getOkPanel() {
    152
    153         return okPanel;
    154     }
    155
    156     /**
    157      *
    158      */
    159     public Plugin getPlugin() {
    160
    161         return this.pluginEditorPanel.getPlugin();
    162     }
    163
    164 }