1 /*
2 * Copyright (c) 1995, 2006, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
206 }
207 if (constraints instanceof String) {
208 addLayoutComponent((String)constraints, comp);
209 } else {
210 throw new IllegalArgumentException("cannot add to layout: constraint must be a string");
211 }
212 }
213 }
214
215 /**
216 * @deprecated replaced by
217 * <code>addLayoutComponent(Component, Object)</code>.
218 */
219 @Deprecated
220 public void addLayoutComponent(String name, Component comp) {
221 synchronized (comp.getTreeLock()) {
222 if (!vector.isEmpty()) {
223 comp.setVisible(false);
224 }
225 for (int i=0; i < vector.size(); i++) {
226 if (((Card)vector.get(i)).name.equals(name)) {
227 ((Card)vector.get(i)).comp = comp;
228 return;
229 }
230 }
231 vector.add(new Card(name, comp));
232 }
233 }
234
235 /**
236 * Removes the specified component from the layout.
237 * If the card was visible on top, the next card underneath it is shown.
238 * @param comp the component to be removed.
239 * @see java.awt.Container#remove(java.awt.Component)
240 * @see java.awt.Container#removeAll()
241 */
242 public void removeLayoutComponent(Component comp) {
243 synchronized (comp.getTreeLock()) {
244 for (int i = 0; i < vector.size(); i++) {
245 if (((Card)vector.get(i)).comp == comp) {
246 // if we remove current component we should show next one
247 if (comp.isVisible() && (comp.getParent() != null)) {
248 next(comp.getParent());
249 }
250
251 vector.remove(i);
252
253 // correct currentCard if this is necessary
254 if (currentCard > i) {
255 currentCard--;
256 }
257 break;
258 }
259 }
260 }
261 }
262
263 /**
264 * Determines the preferred size of the container argument using
265 * this card layout.
510 parent.getComponent(currentCard).setVisible(true);
511 parent.validate();
512 }
513 }
514 }
515
516 /**
517 * Flips to the component that was added to this layout with the
518 * specified <code>name</code>, using <code>addLayoutComponent</code>.
519 * If no such component exists, then nothing happens.
520 * @param parent the parent container in which to do the layout
521 * @param name the component name
522 * @see java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
523 */
524 public void show(Container parent, String name) {
525 synchronized (parent.getTreeLock()) {
526 checkLayout(parent);
527 Component next = null;
528 int ncomponents = vector.size();
529 for (int i = 0; i < ncomponents; i++) {
530 Card card = (Card)vector.get(i);
531 if (card.name.equals(name)) {
532 next = card.comp;
533 currentCard = i;
534 break;
535 }
536 }
537 if ((next != null) && !next.isVisible()) {
538 ncomponents = parent.getComponentCount();
539 for (int i = 0; i < ncomponents; i++) {
540 Component comp = parent.getComponent(i);
541 if (comp.isVisible()) {
542 comp.setVisible(false);
543 break;
544 }
545 }
546 next.setVisible(true);
547 parent.validate();
548 }
549 }
550 }
557 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
558 }
559
560 /**
561 * Reads serializable fields from stream.
562 */
563 private void readObject(ObjectInputStream s)
564 throws ClassNotFoundException, IOException
565 {
566 ObjectInputStream.GetField f = s.readFields();
567
568 hgap = f.get("hgap", 0);
569 vgap = f.get("vgap", 0);
570
571 if (f.defaulted("vector")) {
572 // pre-1.4 stream
573 Hashtable<String, Component> tab = (Hashtable)f.get("tab", null);
574 vector = new Vector<>();
575 if (tab != null && !tab.isEmpty()) {
576 for (Enumeration<String> e = tab.keys() ; e.hasMoreElements() ; ) {
577 String key = (String)e.nextElement();
578 Component comp = (Component)tab.get(key);
579 vector.add(new Card(key, comp));
580 if (comp.isVisible()) {
581 currentCard = vector.size() - 1;
582 }
583 }
584 }
585 } else {
586 vector = (Vector)f.get("vector", null);
587 currentCard = f.get("currentCard", 0);
588 }
589 }
590
591 /**
592 * Writes serializable fields to stream.
593 */
594 private void writeObject(ObjectOutputStream s)
595 throws IOException
596 {
597 Hashtable<String, Component> tab = new Hashtable<>();
598 int ncomponents = vector.size();
599 for (int i = 0; i < ncomponents; i++) {
600 Card card = (Card)vector.get(i);
601 tab.put(card.name, card.comp);
602 }
603
604 ObjectOutputStream.PutField f = s.putFields();
605 f.put("hgap", hgap);
606 f.put("vgap", vgap);
607 f.put("vector", vector);
608 f.put("currentCard", currentCard);
609 f.put("tab", tab);
610 s.writeFields();
611 }
612 }
|
1 /*
2 * Copyright (c) 1995, 2014, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
206 }
207 if (constraints instanceof String) {
208 addLayoutComponent((String)constraints, comp);
209 } else {
210 throw new IllegalArgumentException("cannot add to layout: constraint must be a string");
211 }
212 }
213 }
214
215 /**
216 * @deprecated replaced by
217 * <code>addLayoutComponent(Component, Object)</code>.
218 */
219 @Deprecated
220 public void addLayoutComponent(String name, Component comp) {
221 synchronized (comp.getTreeLock()) {
222 if (!vector.isEmpty()) {
223 comp.setVisible(false);
224 }
225 for (int i=0; i < vector.size(); i++) {
226 if ((vector.get(i)).name.equals(name)) {
227 (vector.get(i)).comp = comp;
228 return;
229 }
230 }
231 vector.add(new Card(name, comp));
232 }
233 }
234
235 /**
236 * Removes the specified component from the layout.
237 * If the card was visible on top, the next card underneath it is shown.
238 * @param comp the component to be removed.
239 * @see java.awt.Container#remove(java.awt.Component)
240 * @see java.awt.Container#removeAll()
241 */
242 public void removeLayoutComponent(Component comp) {
243 synchronized (comp.getTreeLock()) {
244 for (int i = 0; i < vector.size(); i++) {
245 if ((vector.get(i)).comp == comp) {
246 // if we remove current component we should show next one
247 if (comp.isVisible() && (comp.getParent() != null)) {
248 next(comp.getParent());
249 }
250
251 vector.remove(i);
252
253 // correct currentCard if this is necessary
254 if (currentCard > i) {
255 currentCard--;
256 }
257 break;
258 }
259 }
260 }
261 }
262
263 /**
264 * Determines the preferred size of the container argument using
265 * this card layout.
510 parent.getComponent(currentCard).setVisible(true);
511 parent.validate();
512 }
513 }
514 }
515
516 /**
517 * Flips to the component that was added to this layout with the
518 * specified <code>name</code>, using <code>addLayoutComponent</code>.
519 * If no such component exists, then nothing happens.
520 * @param parent the parent container in which to do the layout
521 * @param name the component name
522 * @see java.awt.CardLayout#addLayoutComponent(java.awt.Component, java.lang.Object)
523 */
524 public void show(Container parent, String name) {
525 synchronized (parent.getTreeLock()) {
526 checkLayout(parent);
527 Component next = null;
528 int ncomponents = vector.size();
529 for (int i = 0; i < ncomponents; i++) {
530 Card card = vector.get(i);
531 if (card.name.equals(name)) {
532 next = card.comp;
533 currentCard = i;
534 break;
535 }
536 }
537 if ((next != null) && !next.isVisible()) {
538 ncomponents = parent.getComponentCount();
539 for (int i = 0; i < ncomponents; i++) {
540 Component comp = parent.getComponent(i);
541 if (comp.isVisible()) {
542 comp.setVisible(false);
543 break;
544 }
545 }
546 next.setVisible(true);
547 parent.validate();
548 }
549 }
550 }
557 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
558 }
559
560 /**
561 * Reads serializable fields from stream.
562 */
563 private void readObject(ObjectInputStream s)
564 throws ClassNotFoundException, IOException
565 {
566 ObjectInputStream.GetField f = s.readFields();
567
568 hgap = f.get("hgap", 0);
569 vgap = f.get("vgap", 0);
570
571 if (f.defaulted("vector")) {
572 // pre-1.4 stream
573 Hashtable<String, Component> tab = (Hashtable)f.get("tab", null);
574 vector = new Vector<>();
575 if (tab != null && !tab.isEmpty()) {
576 for (Enumeration<String> e = tab.keys() ; e.hasMoreElements() ; ) {
577 String key = e.nextElement();
578 Component comp = tab.get(key);
579 vector.add(new Card(key, comp));
580 if (comp.isVisible()) {
581 currentCard = vector.size() - 1;
582 }
583 }
584 }
585 } else {
586 vector = (Vector)f.get("vector", null);
587 currentCard = f.get("currentCard", 0);
588 }
589 }
590
591 /**
592 * Writes serializable fields to stream.
593 */
594 private void writeObject(ObjectOutputStream s)
595 throws IOException
596 {
597 Hashtable<String, Component> tab = new Hashtable<>();
598 int ncomponents = vector.size();
599 for (int i = 0; i < ncomponents; i++) {
600 Card card = vector.get(i);
601 tab.put(card.name, card.comp);
602 }
603
604 ObjectOutputStream.PutField f = s.putFields();
605 f.put("hgap", hgap);
606 f.put("vgap", vgap);
607 f.put("vector", vector);
608 f.put("currentCard", currentCard);
609 f.put("tab", tab);
610 s.writeFields();
611 }
612 }
|