logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From psm...@apache.org
Subject cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw ReceiversPanel.java
Date Thu, 19 Jun 2003 06:18:00 GMT
psmith      2003/06/18 23:18:00

  Modified:    src/java/org/apache/log4j/chainsaw ReceiversPanel.java
  Log:
  added beginnings of right click popup menu support for each node in the
  Receiver tree.  Also added Restart icon to Reciever panel toolbar
  which stops and starts all the Receiver plugins in Log4j (causing a disconnect
  to any Socket, but you are warned via Confirm dialog).
  
  
  Revision  Changes    Path
  1.4       +221 -99   jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/ReceiversPanel.java
  
  Index: ReceiversPanel.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/ReceiversPanel.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ReceiversPanel.java	19 Jun 2003 00:58:50 -0000	1.3
  +++ ReceiversPanel.java	19 Jun 2003 06:17:59 -0000	1.4
  @@ -54,6 +54,7 @@
   import org.apache.log4j.net.SocketNodeEventListener;
   import org.apache.log4j.net.SocketReceiver;
   import org.apache.log4j.plugins.PluginRegistry;
  +import org.apache.log4j.plugins.Receiver;
   
   import java.awt.BasicStroke;
   import java.awt.BorderLayout;
  @@ -69,7 +70,9 @@
   import java.awt.event.MouseAdapter;
   import java.awt.event.MouseEvent;
   import java.awt.event.MouseListener;
  +import java.awt.event.MouseMotionListener;
   
  +import java.util.Collection;
   import java.util.Iterator;
   import java.util.List;
   
  @@ -79,7 +82,11 @@
   import javax.swing.Icon;
   import javax.swing.ImageIcon;
   import javax.swing.JComponent;
  +import javax.swing.JMenu;
  +import javax.swing.JMenuItem;
  +import javax.swing.JOptionPane;
   import javax.swing.JPanel;
  +import javax.swing.JPopupMenu;
   import javax.swing.JScrollPane;
   import javax.swing.JTree;
   import javax.swing.SwingUtilities;
  @@ -98,6 +105,9 @@
   class ReceiversPanel extends JPanel {
     private final JTree receiversTree = new JTree();
     private final LogUI logui;
  +  private final Runnable updateReceiverTree;
  +  private final JPopupMenu popupMenu = new ReceiverPopupMenu();
  +  private final Action startAllAction;
   
     ReceiversPanel(final LogUI logui) {
       super(new BorderLayout());
  @@ -106,9 +116,36 @@
       receiversTree.setModel(ReceiversTreeModel.create());
   
       receiversTree.setToolTipText("Allows you to manage Log4j Receivers");
  -    
   
  -    
  +    startAllAction =
  +      new AbstractAction(
  +        "(Re)start All Receivers", new ImageIcon(ChainsawIcons.ICON_RESTART)) {
  +          public void actionPerformed(ActionEvent e) {
  +            if (
  +              JOptionPane.showConfirmDialog(
  +                  logui,
  +                  "This will cause any active Receiver to stop, and disconnect.  Is this
ok?",
  +                  "Confirm", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
  +              Collection allReceivers =
  +                PluginRegistry.getPlugins(
  +                  LogManager.getLoggerRepository(), Receiver.class);
  +
  +              for (Iterator iter = allReceivers.iterator(); iter.hasNext();) {
  +                Receiver item = (Receiver) iter.next();
  +                PluginRegistry.stopPlugin(item);
  +                PluginRegistry.startPlugin(item);
  +              }
  +
  +              updateReceiverTree.run();
  +              logui.getStatusBar().setMessage(
  +                "All Receivers have been (re)started");
  +            }
  +          }
  +        };
  +    startAllAction.putValue(
  +      Action.SHORT_DESCRIPTION,
  +      "Ensures that any Receiver that isn't active, is started.");
  +
       /**
        * We need to setup a runnable that updates the tree
        * any time a Socket event happens (opening/closing of a socket).
  @@ -116,12 +153,12 @@
        * We do this by installing a SocketNodeEventListener in ALL the
        * registered SocketReceivers
        */
  -    final Runnable updateReceiverTree =
  +    updateReceiverTree =
         new Runnable() {
  -        public void run() {
  -          receiversTree.setModel(ReceiversTreeModel.create());
  -        }
  -      };
  +          public void run() {
  +            receiversTree.setModel(ReceiversTreeModel.create());
  +          }
  +        };
   
       receiversTree.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
   
  @@ -159,22 +196,24 @@
       SmallButton pauseReceiverButton =
         new SmallButton(pauseReceiverButtonAction);
   
  +    SmallButton restartAllButton = new SmallButton(startAllAction);
  +    restartAllButton.setText(null);
  +
       c.weightx = 0.0;
       c.weighty = 0.0;
       c.ipadx = 1;
       c.ipady = 1;
  -    c.gridx = 0;
  -    c.gridy = 0;
       c.fill = GridBagConstraints.NONE;
       c.anchor = GridBagConstraints.WEST;
  +
  +    buttonPanel.add(restartAllButton, c);
  +
       buttonPanel.add(playReceiverButton, c);
   
  -    c.gridx = 1;
       c.weightx = 1.0;
       c.weighty = 1.0;
       buttonPanel.add(pauseReceiverButton, c);
   
  -    c.gridx = 2;
       c.weightx = 0.00;
       c.weighty = 0.0;
       c.ipadx = 1;
  @@ -194,59 +233,59 @@
   
       buttonPanel.add(new SmallButton(closeAction));
   
  -//    receiversTree.addTreeSelectionListener(
  -//      new TreeSelectionListener() {
  -//        public void valueChanged(TreeSelectionEvent e) {
  -//        }
  -//      });
  -    receiversTree.addMouseListener(new MouseAdapter(){
  -      
  -      public void mouseRelease(MouseEvent e) {
  -        handle(e);
  -      }
  -
  -      public void mousePressed(MouseEvent e){
  -        handle(e);      
  -      }
  -      
  -      public void handle(MouseEvent e) {
  -        if((e.getModifiers() & InputEvent.CTRL_MASK)>0) {
  -          DefaultMutableTreeNode node =
  -            (DefaultMutableTreeNode) receiversTree
  -            .getLastSelectedPathComponent();
  -
  -          if (node == null) {
  -            return;
  -          }
  -//          e.consume();
  -
  -          Object nodeInfo = node.getUserObject();
  -
  -          if (nodeInfo instanceof SocketReceiver) {
  -            SocketReceiver receiver = (SocketReceiver) nodeInfo;
  -            
  -            
  -            if (receiver.isActive()) {
  -              receiver.setActive(false);
  -              pauseReceiverButtonAction.setEnabled(false);
  -              playReceiverButtonAction.setEnabled(true);
  -              logui.getStatusBar().setMessage("Deactivating " + receiver.getName());
  -              
  -            } else {
  -              receiver.setActive(true);
  -              pauseReceiverButtonAction.setEnabled(true);
  -              playReceiverButtonAction.setEnabled(false);
  -              logui.getStatusBar().setMessage("Activating " + receiver.getName());
  -            }
  -
  -            updateReceiverTree.run();
  -          } else {
  -            pauseReceiverButtonAction.setEnabled(false);
  -            playReceiverButtonAction.setEnabled(false);
  -          }
  -        }
  -      }
  -    });
  +    PopupListener popupListener = new PopupListener(popupMenu);
  +    receiversTree.addMouseListener(popupListener);
  +    this.addMouseListener(popupListener);
  +
  +    //    receiversTree.addMouseMotionListener(new MouseMotionListener(){
  +    //      
  +    //      public void handle(MouseEvent e) {
  +    ////        (e.getModifiers() & InputEvent.CTRL_MASK)>0
  +    //        if( e.isPopupTrigger()) {
  +    //          DefaultMutableTreeNode node =
  +    //            (DefaultMutableTreeNode) receiversTree
  +    //            .getLastSelectedPathComponent();
  +    //
  +    //          if (node == null) {
  +    //            return;
  +    //          }
  +    //          
  +    ////          e.consume();
  +    ////
  +    ////          Object nodeInfo = node.getUserObject();
  +    ////
  +    ////          if (nodeInfo instanceof SocketReceiver) {
  +    ////            SocketReceiver receiver = (SocketReceiver) nodeInfo;
  +    ////            
  +    ////            
  +    ////            if (receiver.isActive()) {
  +    ////              receiver.setActive(false);
  +    ////              pauseReceiverButtonAction.setEnabled(false);
  +    ////              playReceiverButtonAction.setEnabled(true);
  +    ////              logui.getStatusBar().setMessage("Deactivating " + receiver.getName());
  +    ////              
  +    ////            } else {
  +    ////              receiver.setActive(true);
  +    ////              pauseReceiverButtonAction.setEnabled(true);
  +    ////              playReceiverButtonAction.setEnabled(false);
  +    ////              logui.getStatusBar().setMessage("Activating " + receiver.getName());
  +    ////            }
  +    ////
  +    ////            updateReceiverTree.run();
  +    ////          } else {
  +    ////            pauseReceiverButtonAction.setEnabled(false);
  +    ////            playReceiverButtonAction.setEnabled(false);
  +    ////          }
  +    //        }
  +    //      }
  +    //      public void mouseDragged(MouseEvent e) {
  +    //        
  +    //      }
  +    //
  +    //      public void mouseMoved(MouseEvent e) {
  +    //        
  +    //      }
  +    //    });
       JComponent component = receiversTree;
   
       add(new JScrollPane(component), BorderLayout.CENTER);
  @@ -273,48 +312,131 @@
       }
     }
   
  +  class ReceiverPopupMenu extends JPopupMenu {
  +    final JMenu startMenu = new JMenu("Start");
  +    final JMenu pauseMenu = new JMenu("Pause");
  +    final JMenu shutdownMenu = new JMenu("Shutdown");
  +
  +    
  +    ReceiverPopupMenu() {
  +      add(startMenu);
  +      add(pauseMenu);
  +      add(shutdownMenu);
  +    }
  +    
  +    private JMenuItem createNotDoneYet() {
  +      final JMenuItem notDoneYet = new JMenuItem("Not Implemented Yet, sorry");
  +      notDoneYet.setEnabled(false);
  +      return notDoneYet;
  +      
  +    }
  +
  +    /* (non-Javadoc)
  +     * @see javax.swing.JPopupMenu#show(java.awt.Component, int, int)
  +     */
  +    public void show(Component invoker, int x, int y) {
  +      DefaultMutableTreeNode node =
  +        (DefaultMutableTreeNode) receiversTree.getLastSelectedPathComponent();
  +
  +      if (node == null) {
  +        return;
  +      }
  +
  +      Object userObject = node.getUserObject();
  +      startMenu.removeAll();
  +      pauseMenu.removeAll();
  +      shutdownMenu.removeAll();
  +
  +      System.out.println(userObject);
  +
  +      if (userObject == ReceiversTreeModel.RootNode.getUserObject()) {
  +        buildForReceiversRoot();
  +      } else {
  +        buildForReceiverNode();
  +      }
  +
  +      this.invalidate();
  +      this.validate();
  +
  +      super.show(invoker, x, y);
  +    }
  +
  +    /**
  +     *
  +     */
  +    private void buildForReceiverNode() {
  +      final Action pauseReceiver =
  +        new AbstractAction(
  +          "Pause this Receiver", new ImageIcon(ChainsawIcons.PAUSE)) {
  +          public void actionPerformed(ActionEvent e) {
  +            DefaultMutableTreeNode node =
  +              (DefaultMutableTreeNode) receiversTree
  +              .getLastSelectedPathComponent();
  +
  +            if (node == null) {
  +              return;
  +            }
  +
  +            Object userObject = node.getUserObject();
  +
  +            if (userObject instanceof SocketReceiver) {
  +              SocketReceiver socketReceiver = (SocketReceiver) userObject;
  +              socketReceiver.setActive(false);
  +              logui.getStatusBar().setMessage(
  +                socketReceiver.getName() + " has been paused");
  +              updateReceiverTree.run();
  +            }
  +          }
  +        };
  +        
  +      startMenu.add(createNotDoneYet());
  +      pauseMenu.add(createNotDoneYet());
  +      shutdownMenu.add(createNotDoneYet());
  +
  +    }
  +
  +    private void buildForReceiversRoot() {
  +      JMenuItem startAll = new JMenuItem(startAllAction);
  +      startMenu.add(startAll);
  +      pauseMenu.add(createNotDoneYet());
  +      shutdownMenu.add(createNotDoneYet());
  +    }
  +  }
  +
     /**
      *
      * @author Scott Deboy <sdeboy@apache.org>
      *
      */
  -  class CloseIcon
  -      implements Icon
  -  {
  -      int size;
  -      int xOffSet;
  -      int yOffSet;
  -
  -      public CloseIcon(int size, int xOffSet, int yOffSet) {
  -          this.size=size;
  -          this.xOffSet=xOffSet;
  -          this.yOffSet=yOffSet;        
  -      }
  +  class CloseIcon implements Icon {
  +    int size;
  +    int xOffSet;
  +    int yOffSet;
  +
  +    public CloseIcon(int size, int xOffSet, int yOffSet) {
  +      this.size = size;
  +      this.xOffSet = xOffSet;
  +      this.yOffSet = yOffSet;
  +    }
   
  -      public int getIconHeight()
  -      {
  -          return size;
  -      }
  +    public int getIconHeight() {
  +      return size;
  +    }
   
  -      public int getIconWidth()
  -      {
  -          return size;
  -      }
  +    public int getIconWidth() {
  +      return size;
  +    }
   
  -      public void paintIcon(
  -          Component c,
  -          Graphics g,
  -          int x,
  -          int y)
  -      {
  -          Graphics2D g2D = (Graphics2D)g;
  -          g2D.setStroke(new BasicStroke(1.5f));
  -          g2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
  -          RenderingHints.VALUE_STROKE_PURE);
  -          g2D.setColor(Color.black);
  -          g2D.drawLine(x + xOffSet, y + yOffSet, x+size +xOffSet, y+size+yOffSet);
  -          g2D.drawLine(x + xOffSet, y+size+yOffSet, x+size+xOffSet, y+yOffSet);
  -      }
  +    public void paintIcon(Component c, Graphics g, int x, int y) {
  +      Graphics2D g2D = (Graphics2D) g;
  +      g2D.setStroke(new BasicStroke(1.5f));
  +      g2D.setRenderingHint(
  +        RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
  +      g2D.setColor(Color.black);
  +      g2D.drawLine(
  +        x + xOffSet, y + yOffSet, x + size + xOffSet, y + size + yOffSet);
  +      g2D.drawLine(
  +        x + xOffSet, y + size + yOffSet, x + size + xOffSet, y + yOffSet);
  +    }
     }
  -
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org


Mime
View raw message