Question : GUI Tree Problem

The best way to explain my problem is as follows...
Please compile the two files MonitorPanel and RunMonitorPanel and run the RunMonitorPanel class.

You will see a small GUI pops up with a tree with two nodes "panels" and "modes".

Step 1. Right click on any of these branches it will popup "Create alarm panel" and "Create Alarm".

Step 2. Now expand any of the nodes ( you r will see app1, app2 etc). Do step 1.

Why is that the pop up is not coming out now?

import java.awt.BorderLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
 
 
public class MonitorPanel extends Panel implements TreeSelectionListener {

 
  private JPanel configPanel = new JPanel();
  private JLabel configPathLabel = new JLabel("Path");
  private JTextField configPathField = new JTextField(20);
  private JLabel configModeLabel = new JLabel("Mode");
  private JTextField configModeField = new JTextField(2);
  //
  private DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("top");
  private DefaultMutableTreeNode apModes = new DefaultMutableTreeNode("panels");
  private DefaultMutableTreeNode alarmsNode = new DefaultMutableTreeNode("modes");
  private DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
  private JTree tree;
  private JScrollPane treePane;
  private CAPopupMenu caPopup = new CAPopupMenu();
  private CAPPopupMenu capPopup = new CAPPopupMenu();
  //////////////////////////////////////////////////////////////////////////////

  /** Constructs an instance. */
  public MonitorPanel() {
    // wire the config panel
    configPanel.setLayout(new BoxLayout(configPanel, BoxLayout.LINE_AXIS));
    configPanel.add(configPathLabel);
    configPanel.add(configPathField);
    configPanel.add(configModeLabel);
    configPanel.add(configModeField);
    configPathField.setEditable(false);
    configModeField.setEditable(false);
    // wire the tree
    rootNode.add(apModes);
    rootNode.add(alarmsNode);
    tree = new JTree(treeModel);
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
 
    treePane = new JScrollPane(tree);
    //
    setLayout(new BorderLayout());
    add(configPanel, BorderLayout.NORTH);
    add(treePane, BorderLayout.CENTER);
    //
    tree.addMouseListener(new MyMouseListener());
  }

  public void setConfigPath(String path) {
    configPathField.setText(path);
  }

  public void setConfigMode(String mode) {
    configModeField.setText(mode == null ? "" : mode);
  }

  public void setAlarmPanelNames(List<String> names) {
    apModes.removeAllChildren();
    // set names to current list
    for (String name : names) {
      addAPName(name, false);
    }
  }

  public DefaultMutableTreeNode addAPName(String alarmPanelName, boolean shouldBeVisible) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(alarmPanelName);

    treeModel.insertNodeInto(childNode, apModes, apModes.getChildCount());

    if (shouldBeVisible) {
      tree.scrollPathToVisible(new TreePath(childNode.getPath()));
    }

    return childNode;
  }

  public void setAlarmNames(List<String> names) {
    alarmsNode.removeAllChildren();
    // set names to current list
    for (String name : names) {
      addAlarmName(name, false);
    }
  }

  public DefaultMutableTreeNode addAlarmName(String alarmName, boolean shouldBeVisible) {
    DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(alarmName);

    treeModel.insertNodeInto(childNode, alarmsNode, alarmsNode.getChildCount());

    if (shouldBeVisible) {
      tree.scrollPathToVisible(new TreePath(childNode.getPath()));
    }

    return childNode;
  }

  @Override
  public void valueChanged(TreeSelectionEvent e) {
    //Returns the last path element of the selection.
    //This method is useful only when the selection model allows a single selection.
    DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

    if (node == null) //Nothing is selected.
    {
      return;
    }

    System.out.println("TreeSelectionListener.valueChanged(): node=" + node.getUserObject());
  }

  //////////////////////////////////////////////////////////////////////////////
  private static class MyDefaultMutableTreeNode extends DefaultMutableTreeNode {

    public MyDefaultMutableTreeNode(Object userObject) {
      super(userObject);
    }

    @Override
    public boolean isLeaf() {
      return false;
    }
  }
  //////////////////////////////////////////////////////////////////////////////

  /**
   * Listens for request to show popup
   */
  private class MyMouseListener extends MouseAdapter {

    @Override
    public void mousePressed(MouseEvent e) {
      maybeShowPopup(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
      maybeShowPopup(e);
    }

    private void maybeShowPopup(MouseEvent e) {
      if (e.isPopupTrigger()) {
 
        TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
 

        if (selPath != null) {
          DefaultMutableTreeNode node = (DefaultMutableTreeNode) selPath.getLastPathComponent();
          if (node.equals(alarmsNode)) {
            System.out.println("selected alarms node");
            caPopup.show(e.getComponent(), e.getX(), e.getY());

          } else if (node.equals(apModes)) {
            System.out.println("selected alarm panels node");
            capPopup.show(e.getComponent(), e.getX(), e.getY());

          } else if (alarmsNode.isNodeChild(node)) {
            System.out.println("selected alarm node");

          } else if (apModes.isNodeChild(node)) {
            System.out.println("selected alarm panel node");
          }
        }
 
      }
    }
  }
  //////////////////////////////////////////////////////////////////////////////

  private class CAPopupMenu extends JPopupMenu {

    private JMenuItem selectAlarmMenuItem = new JMenuItem("Create Alarm");

    public CAPopupMenu() {

      add(selectAlarmMenuItem);

      selectAlarmMenuItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
          System.out.println("creating alarm");
        }
      });

    }
  }  //////////////////////////////////////////////////////////////////////////////

  private class CAPPopupMenu extends JPopupMenu {

    private JMenuItem selectAlarmMenuItem = new JMenuItem("Create Alarm Panel");

    public CAPPopupMenu() {

      add(selectAlarmMenuItem);

      selectAlarmMenuItem.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
          System.out.println("creating alarm panel");
        }
      });

    }
  }
}

======================================================================
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Arrays;
import javax.swing.JFrame;

public class RunMonitorPanel {

  /** Construct an instance. */
  public RunMonitorPanel() {
  }

  /**
   * Center a Component on the screen.
   * @param target the component to be centered.
   */
  private static void centerOnScreen(Component target) {
    if (target != null) {
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension targetSize = target.getSize();

      if (targetSize.height > screenSize.height) {
        targetSize.height = screenSize.height;
      }

      if (targetSize.width > screenSize.width) {
        targetSize.width = screenSize.width;
      }

      int x = (screenSize.width - targetSize.width) / 2;
      int y = (screenSize.height - targetSize.height) / 2;

      target.setLocation(x, y);
    }
  }

  /**
   * Create and show the GUI.
   */
  private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("MonitorPanel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Set up the content pane.
    MonitorPanel panel = new MonitorPanel();
    frame.getContentPane().add(panel);
    // set some values for display
    panel.setConfigPath("/tmp/config");
    panel.setConfigMode("rw");
    String[] alarmNames = {"a1","a2","a3"};
    panel.setAlarmNames(Arrays.asList(alarmNames));
    String[] alarmPanelNames = {"ap1", "ap2", "ap3"};
    panel.setAlarmPanelNames(Arrays.asList(alarmPanelNames));

    // Pack and show the window.
    frame.pack();
    centerOnScreen(frame);
    frame.setVisible(true);
  }

  /**
   * Create and show the GUI.
   */
  public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        createAndShowGUI();
      }
    });
  }
}

==================================================================



Answer : GUI Tree Problem

I guess it should be minus 45:

WHERE providerID IN (  'A000000015210B','A000000003731B' )
  AND Input_Date < DATEADD(day, -45, Received_Date)

or

WHERE providerID IN (  'A000000015210B','A000000003731B' )
  AND Received_Date > DATEADD(day, 45, Input_Date)
Random Solutions  
 
programming4us programming4us