1 /*
   2  * Copyright 2001 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 package sun.jvm.hotspot.bugspot;
  26 
  27 import java.awt.*;
  28 import java.awt.event.*;
  29 import java.util.*;
  30 import javax.swing.*;
  31 import sun.jvm.hotspot.debugger.cdbg.*;
  32 import sun.jvm.hotspot.runtime.*;
  33 import sun.jvm.hotspot.ui.*;
  34 
  35 /** This panel contains a ListBox with all of the stack frames in a
  36     given thread. When a given entry is selected, an event is
  37     fired. */
  38 
  39 public class StackTracePanel extends JPanel {
  40   public interface Listener {
  41     public void frameChanged(CFrame fr, JavaVFrame jfr);
  42   }
  43 
  44   class Model extends AbstractListModel implements ComboBoxModel {
  45     private Object selectedItem;
  46     public Object getElementAt(int index) {
  47       if (trace == null) return null;
  48       return trace.get(index);
  49     }
  50     public int getSize() {
  51       if (trace == null) return 0;
  52       return trace.size();
  53     }
  54     public Object getSelectedItem() {
  55       return selectedItem;
  56     }
  57     public void setSelectedItem(Object item) {
  58       selectedItem = item;
  59     }
  60     public void dataChanged() {
  61       fireContentsChanged(this, 0, trace.size());
  62     }
  63   }
  64 
  65   private java.util.List trace;
  66   private Model model;
  67   private JComboBox list;
  68   private java.util.List listeners;
  69 
  70   public StackTracePanel() {
  71     super();
  72 
  73     model = new Model();
  74 
  75     // Build user interface
  76     setLayout(new BorderLayout());
  77     setBorder(GraphicsUtilities.newBorder(5));
  78     list = new JComboBox(model);
  79     list.setPrototypeDisplayValue("ZZZZZZZZZZZZZZZZZZZZZZZZZZZZ");
  80     add(list, BorderLayout.CENTER);
  81 
  82     // Add selection listener
  83     list.addItemListener(new ItemListener() {
  84         public void itemStateChanged(ItemEvent e) {
  85           if (e.getStateChange() == ItemEvent.SELECTED) {
  86             fireFrameChanged();
  87           }
  88         }
  89       });
  90   }
  91 
  92   /** Takes a List of StackTraceEntry objects */
  93   public void setTrace(java.util.List trace) {
  94     this.trace = trace;
  95     model.dataChanged();
  96     list.setSelectedIndex(0);
  97     fireFrameChanged();
  98   }
  99 
 100   public void addListener(Listener listener) {
 101     if (listeners == null) {
 102       listeners = new ArrayList();
 103     }
 104     listeners.add(listener);
 105   }
 106 
 107   protected void fireFrameChanged() {
 108     if (listeners != null) {
 109       StackTraceEntry entry = (StackTraceEntry) trace.get(list.getSelectedIndex());
 110       for (Iterator iter = listeners.iterator(); iter.hasNext(); ) {
 111         ((Listener) iter.next()).frameChanged(entry.getCFrame(), entry.getJavaFrame());
 112       }
 113     }
 114   }
 115 }