KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > TabbedPanel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.ui;
35
36 import javax.swing.*;
37 import java.awt.event.*;
38 import java.awt.*;
39
40 /**
41  * Extended by all panels that can dynamically be added or removed from the
42  * _tabbedPane in MainFrame. Provides a boolean indicating if the panel is being
43  * displayed, and a close button. Attaches an action to the close button which
44  * calls the _close method. This method can be overwritten in a subclass if
45  * needed.
46  * @version $Id: TabbedPanel.java 3886 2006-06-14 15:47:42Z rcartwright $
47  */

48 public abstract class TabbedPanel extends JPanel {
49   // indicates whether this tab is displayed in the tabbed pane
50
protected boolean _displayed;
51   // button which removes this pane's tab
52
protected JButton _closeButton;
53   // panel that has _closeButton in the north so it can't be stretched
54
// vertically
55
protected JPanel _closePanel;
56   // the panel that the subclasses of TabbedPanel can use
57
protected JPanel _mainPanel;
58   // used to be able to reference removeTab
59
protected MainFrame _frame;
60   // string to be displayed on the tab
61
private String JavaDoc _name;
62
63   /**
64    * Constructor.
65    * @param frame MainFrame displaying the tab
66    * @param name Name to display for the tab
67    */

68   public TabbedPanel(MainFrame frame, String JavaDoc name) {
69     _frame = frame;
70     _name = name;
71     _setUpPanes();
72     _displayed = false;
73   }
74
75   /**
76    * Puts the close panel in the east of this panel and puts the main panel in the
77    * center. Also adds the action to the close button.
78    */

79   private void _setUpPanes() {
80     this.setFocusCycleRoot(true);
81     this.setLayout(new BorderLayout());
82
83     _mainPanel = new JPanel();
84     _closePanel = new JPanel(new BorderLayout());
85     _closeButton = new CommonCloseButton(_closeListener);
86     _closePanel.add(_closeButton, BorderLayout.NORTH);
87     add(_closePanel, BorderLayout.EAST);
88     add(_mainPanel, BorderLayout.CENTER);
89   }
90
91   /** Defines the action that takes place upon clicking the close button. */
92   private final ActionListener _closeListener =
93     new ActionListener() {
94     public void actionPerformed(ActionEvent e) {
95       _close();
96     }
97   };
98
99   /** Visibly closes the panel and removes it from the frame. */
100   protected void _close() {
101      _displayed = false;
102      _frame.removeTab(this);
103   }
104
105   /** @return whether this tabbedPanel is displayed in a tab */
106   public boolean isDisplayed() { return _displayed; }
107
108   /**
109    * @return the display name of this tab
110    */

111   public String JavaDoc getName() {
112     return _name;
113   }
114
115   /**
116    * Sets whether the tab is displayed. Doesn't actually show or hide the tab.
117    */

118   public void setDisplayed(boolean displayed) {
119     _displayed = displayed;
120   }
121
122   JPanel getMainPanel() {
123     return _mainPanel;
124   }
125
126   /**
127    * This is overridden so that when switch previous pane focus is called
128    * on the currentDefPane, the caret will move here on the first call.
129    */

130   public boolean requestFocusInWindow() {
131     super.requestFocusInWindow();
132     return _mainPanel.requestFocusInWindow();
133   }
134 }
Popular Tags