xref: /titanic_41/usr/src/cmd/krb5/kadmin/gui/visualrt/sunsoft/jws/visual/rt/awt/CardPanel.java (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * ident	"%Z%%M%	%I%	%E% SMI"
24  *
25  * Copyright (c) 2000 by Sun Microsystems, Inc.
26  * All rights reserved.
27  */
28 
29 /*
30  *        Copyright (C) 1996  Active Software, Inc.
31  *                  All rights reserved.
32  *
33  * @(#) CardPanel.java 1.8 - last change made 06/17/97
34  */
35 
36 package sunsoft.jws.visual.rt.awt;
37 
38 import sunsoft.jws.visual.rt.base.Global;
39 
40 import java.awt.*;
41 import java.util.*;
42 
43 public class CardPanel extends VJPanel {
44     public static final int FETCHCARD = 68496;
45     public static final int CURRENTCARD = 68497;
46 
47     protected Vector tabs;
48     private Label cardLabel;
49     private CardLayout cardLayout;
50     private String currentCard;
51     private Hashtable cards;
52 
CardPanel()53     public CardPanel() {
54         cardLayout = new CardLayout();
55         setLayout(cardLayout);
56 
57         tabs = new Vector();
58         cards = new Hashtable();
59 
60         cardLabel = newCardLabel();
61         add(cardLabel, /* NOI18N */"foobar");
62         // JDK1.1 requires a constraint
63     }
64 
newCardLabel()65     protected Label newCardLabel() {
66         return new Label(Global.getMsg(
67 /* JSTYLED */
68 				       "sunsoft.jws.visual.rt.awt.CardPanel.Card__Panel"));
69     }
70 
add(String name, Component comp)71     public Component add(String name, Component comp) {
72         boolean isFront =
73 	    (currentCard != null && cards.get(currentCard) == comp);
74 
75         super.add(name, comp);
76 
77         if (isFront) {
78             comp.show();
79             currentCard = name;
80         }
81 
82         return comp;
83     }
84 
addTab(String name)85     public void addTab(String name) {
86         tabs.addElement(name);
87     }
88 
addTab(String name, int index)89     public void addTab(String name, int index) {
90         tabs.insertElementAt(name, index);
91     }
92 
getTab(int index)93     public String getTab(int index) {
94         int size = tabs.size();
95         if (index >= 0 && index < size)
96             return (String)tabs.elementAt(index);
97         else
98             return null;
99     }
100 
getTabIndex(String name)101     public int getTabIndex(String name) {
102         return tabs.indexOf(name);
103     }
104 
removeTab(String name)105     public void removeTab(String name) {
106         tabs.removeElement(name);
107         removeCard(name);
108     }
109 
renameTab(String oldName, String newName)110     public void renameTab(String oldName, String newName) {
111         int index = tabs.indexOf(oldName);
112         if (index != -1) {
113             tabs.removeElementAt(index);
114             tabs.insertElementAt(newName, index);
115             renameCard(oldName, newName);
116         }
117     }
118 
removeAllTabs()119     public void removeAllTabs() {
120         tabs.removeAllElements();
121         removeAllCards();
122     }
123 
tabs()124     public Enumeration tabs() {
125         return tabs.elements();
126     }
127 
addCard(String name, Component card)128     public Component addCard(String name, Component card) {
129         if (cardLabel.getParent() == this)
130             remove(cardLabel);
131 
132         if (!tabs.contains(name)) {
133 	    /* BEGIN JSTYLED */
134 	    throw new Error(Global.fmtMsg("sunsoft.jws.visual.rt.awt.CardPanel.FMT.0", /* NOI18N */"\r\n",
135 					  Global.getMsg("sunsoft.jws.visual.rt.awt.CardPanel.________There__is__no__tab__.0"), name, /* NOI18N */"\".\r\n",
136 					  Global.getMsg("sunsoft.jws.visual.rt.awt.CardPanel.________You__must__call__ad.1")));
137 	    /* END JSTYLED */
138         }
139 
140         cards.put(name, card);
141 
142         // The first card that is added will be shown by the cardLayout,
143         // and thusly it must become the currentCard.
144         if (currentCard == null) {
145             currentCard = name;
146             postEvent(new Event(this, CURRENTCARD, name));
147         }
148 
149         return add(name, card);
150     }
151 
getCard(String name)152     public Component getCard(String name) {
153         return (Component)cards.get(name);
154     }
155 
getCardName(Component comp)156     public String getCardName(Component comp) {
157         Enumeration e = cards.keys();
158         while (e.hasMoreElements()) {
159             String key = (String)e.nextElement();
160             if (cards.get(key) == comp)
161                 return key;
162         }
163 
164         return null;
165     }
166 
167     // Call renameTab to rename the card
renameCard(String oldName, String newName)168     private void renameCard(String oldName, String newName) {
169         Component comp = (Component)cards.get(oldName);
170 
171         if (comp != null) {
172             cards.remove(oldName);
173             cards.put(newName, comp);
174 
175             remove(comp);
176             add(newName, comp);
177 
178             if (oldName.equals(currentCard)) {
179                 currentCard = newName;
180                 comp.show();
181             }
182         }
183     }
184 
185     // Call removeTab to remove the card
removeCard(String name)186     private void removeCard(String name) {
187         if (name.equals(currentCard))
188             currentCard = null;
189 
190         Component comp = (Component)cards.get(name);
191         if (comp != null) {
192             cards.remove(name);
193             remove(comp);
194         }
195 
196         if (cardLabel.getParent() != this &&
197 	    countComponents() == 0)
198 	    add(cardLabel, /* NOI18N */"foobar");
199         // JDK1.1 requires a constraint
200     }
201 
202     // Call removeAllTabs to remove all the cards
removeAllCards()203     private void removeAllCards() {
204         cards.clear();
205 
206         if (cardLabel.getParent() != this)
207             add(cardLabel, /* NOI18N */"foobar");
208     }
209 
getCurrentCard()210     public String getCurrentCard() {
211         return currentCard;
212     }
213 
show(String name)214     public void show(String name) {
215         if (cards.get(name) == null) {
216             postEvent(new Event(this, FETCHCARD, name));
217         }
218 
219         if (cards.get(name) != null) {
220             currentCard = name;
221             cardLayout.show(this, name);
222             postEvent(new Event(this, CURRENTCARD, name));
223         }
224     }
225 
first()226     public void first() {
227         if (tabs.size() > 0)
228             show((String)tabs.elementAt(0));
229     }
230 
next()231     public void next() {
232         int index = frontIndex();
233         if (tabs.size() > (index+1))
234             show((String)tabs.elementAt(index+1));
235     }
236 
previous()237     void previous() {
238         int index = frontIndex();
239         if (tabs.size() > 0 && (index-1) >= 0)
240             show((String)tabs.elementAt(index-1));
241     }
242 
last()243     public void last() {
244         int index = tabs.size()-1;
245         if (index >= 0)
246             show((String)tabs.elementAt(index));
247     }
248 
frontIndex()249     private int frontIndex() {
250         if (currentCard == null)
251             return -1;
252         else
253             return tabs.indexOf(currentCard);
254     }
255 }
256