001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: KTabbedPane.java,v $ 023 Revision 1.2 2004/05/12 21:37:25 markl 024 comment block updates 025 026 Revision 1.1 2004/03/15 06:52:16 markl 027 New class. 028 ---------------------------------------------------------------------------- 029*/ 030 031package kiwi.ui; 032 033import javax.swing.*; 034import javax.swing.event.*; 035 036/** A specialization of <code>JTabbedPane</code> that provides a hook for data 037 * validation. The method <code>canLeaveTab()</code> can be overriden to 038 * provide data validation for the currently selected tab, thereby allowing or 039 * disallowing the user from selecting a different tab. 040 * 041 * @author Mark Lindner 042 * @since Kiwi 2.0 043 */ 044 045public class KTabbedPane extends JTabbedPane 046 { 047 private int curTab = 0; 048 private boolean firstTime = true, isAdjusting = false; 049 050 /** Construct a new <code>KTabbedPane</code>. */ 051 052 public KTabbedPane() 053 { 054 setOpaque(false); 055 056 addChangeListener(new ChangeListener() 057 { 058 public void stateChanged(ChangeEvent evt) 059 { 060 if(firstTime) 061 { 062 firstTime = false; 063 return; 064 } 065 066 if(isAdjusting) 067 return; 068 069 int index = getSelectedIndex(); 070 if(canLeaveTab(curTab, index)) 071 curTab = index; 072 else 073 { 074 isAdjusting = true; 075 setSelectedIndex(curTab); 076 isAdjusting = false; 077 } 078 } 079 }); 080 } 081 082 /** Reset the <code>KTabbedPane</code>. The first tab is made active. */ 083 084 public void reset() 085 { 086 isAdjusting = true; 087 setSelectedIndex(0); 088 isAdjusting = false; 089 } 090 091 /** Determine if the user is allowed to select a different tab. The default 092 * implementation simply returns <b>true</b>, but subclassers can provide 093 * input validation logic for the currently displayed tab. 094 * 095 * @param currentTab The index of the tab that is currently selected. 096 * @param newTab The tab that the user wishes to switch to. 097 * @return <b>true</b> if the new tab may be displayed, or <b>false</b> 098 * if the current tab should remain selected. 099 */ 100 101 protected boolean canLeaveTab(int currentTab, int newTab) 102 { 103 return(true); 104 } 105 106 } 107 108/* end of source file */