1 /*
2  * Copyright (c) 2002, 2004, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  *
23  */
24 
25 package sun.jvm.hotspot.ui.classbrowser;
26 
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 import javax.swing.event.*;
31 import sun.jvm.hotspot.oops.*;
32 import sun.jvm.hotspot.ui.*;
33 import sun.jvm.hotspot.ui.action.*;
34 import sun.jvm.hotspot.utilities.*;
35 
36 import com.sun.java.swing.ui.StatusBar;
37 import com.sun.java.swing.ui.CommonToolBar;
38 
39 import com.sun.java.swing.action.ActionManager;
40 import com.sun.java.swing.action.DelegateAction;
41 
42 public class ClassBrowserPanel extends JPanel implements ActionListener {
43    private StatusBar           statusBar;
44    private ClassBrowserToolBar toolBar;
45    private JSplitPane          splitPane;
46    private SAEditorPane        classesEditor;
47    private SAEditorPane        contentEditor;
48    private HTMLGenerator       htmlGen;
49 
ClassBrowserPanel()50    public ClassBrowserPanel() {
51       htmlGen = new HTMLGenerator();
52 
53       HyperlinkListener hyperListener = new HyperlinkListener() {
54                          public void hyperlinkUpdate(HyperlinkEvent e) {
55                             if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
56                                contentEditor.setText(htmlGen.genHTMLForHyperlink(e.getDescription()));
57                             }
58                          }
59                       };
60 
61       classesEditor = new SAEditorPane();
62       classesEditor.addHyperlinkListener(hyperListener);
63 
64       contentEditor = new SAEditorPane();
65       contentEditor.addHyperlinkListener(hyperListener);
66 
67       JPanel topPanel = new JPanel();
68       topPanel.setLayout(new BorderLayout());
69       topPanel.add(new JScrollPane(classesEditor), BorderLayout.CENTER);
70 
71       JPanel bottomPanel = new JPanel();
72       bottomPanel.setLayout(new BorderLayout());
73       bottomPanel.add(new JScrollPane(contentEditor), BorderLayout.CENTER);
74 
75       splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, topPanel, bottomPanel);
76       splitPane.setDividerLocation(0);
77 
78       setLayout(new BorderLayout());
79       add(splitPane, BorderLayout.CENTER);
80       statusBar = new StatusBar();
81       add(statusBar, BorderLayout.SOUTH);
82       toolBar = new ClassBrowserToolBar(statusBar);
83       add(toolBar, BorderLayout.NORTH);
84       registerActions();
85    }
86 
setClassesText(String text)87    public void setClassesText(String text) {
88       classesEditor.setText(text);
89       splitPane.setDividerLocation(0.5);
90    }
91 
setContentText(String text)92    public void setContentText(String text) {
93       contentEditor.setText(text);
94       splitPane.setDividerLocation(0.5);
95    }
96 
97    private class ClassBrowserToolBar extends CommonToolBar {
98        private JTextField searchTF;
99 
ClassBrowserToolBar(StatusBar status)100        public ClassBrowserToolBar(StatusBar status) {
101           super(HSDBActionManager.getInstance(), status);
102        }
103 
addComponents()104        protected void addComponents() {
105           searchTF = new JTextField();
106           searchTF.setToolTipText("Find classes");
107 
108           // Pressing Enter on the text field will search
109           InputMap im = searchTF.getInputMap();
110           im.put(KeyStroke.getKeyStroke("ENTER"), "enterPressed");
111           ActionMap am = searchTF.getActionMap();
112           am.put("enterPressed", manager.getAction(FindClassesAction.VALUE_COMMAND));
113 
114           add(searchTF);
115           addButton(manager.getAction(FindClassesAction.VALUE_COMMAND));
116        }
117 
getFindText()118        public String getFindText() {
119           return searchTF.getText();
120        }
121    }
122 
123    //
124    // ActionListener implementation and actions support
125    //
126 
actionPerformed(ActionEvent evt)127    public void actionPerformed(ActionEvent evt) {
128       String command = evt.getActionCommand();
129 
130       if (command.equals(FindClassesAction.VALUE_COMMAND)) {
131          findClasses();
132       }
133    }
134 
registerActions()135    protected void registerActions() {
136       registerAction(FindClassesAction.VALUE_COMMAND);
137    }
138 
registerAction(String actionName)139    private void registerAction(String actionName) {
140       ActionManager manager = ActionManager.getInstance();
141       DelegateAction action = manager.getDelegateAction(actionName);
142       action.addActionListener(this);
143    }
144 
findClasses()145    private void findClasses() {
146       String findText = toolBar.getFindText();
147       if (findText == null || findText.equals("")) {
148          return;
149       }
150 
151       setContentText(htmlGen.genHTMLForWait("Finding classes ..."));
152       InstanceKlass[] klasses = SystemDictionaryHelper.findInstanceKlasses(findText);
153       if (klasses.length == 0) {
154          setContentText(htmlGen.genHTMLForMessage("No class found with name containing '" + findText + "'"));
155       } else {
156          setContentText(htmlGen.genHTMLForKlassNames(klasses));
157       }
158    }
159 }
160