ToggleableIconPane.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.component |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.component;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.Timer;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
* Provides a container that can display multiple panels. The view can be
* switched between a {@link javax.swing.JTabbedPane JTabbedPane} view and
* an {@link IconSplitPane} view.
*/
public class ToggleableIconPane extends JPanel implements ChangeListener
{
//--- Constant(s) ---
public static final int BLINK_INTERVAL = 500;
//--- Data field(s) ---
/**
* ChangeListeners.
*/
Vector cls = new Vector();
IconSplitPane ipane = null;
int dividerSize;
JTabbedPane tpane = null;
boolean useTabbedPane;
/**
* Maps descriptions to components.
*/
Hashtable componentByDescription = new Hashtable();
Timer timer;
/**
* Always set to non null value to make life easy.
*/
Blinker blinker = new Blinker();
private Hashtable clientProperties;
//--- Constructor(s) ---
/**
* Constructs a ToggleableIconPane in tabbed mode if tabbed is true and
* given dividerSize for the IconSplitPane.
*
* @param tabbed if true the TabbedPane is the active view, otherwise it's
* the IconSplitPane
* @param dividerSize the size of the divider for the IconSplitPane view
*/
public ToggleableIconPane(boolean tabbed, int dividerSize)
{
this.dividerSize = dividerSize;
setLayout(new BorderLayout());
setTabbed(tabbed);
addChangeListener(this);
}
/**
* Constructs a ToggleableIconPane in tabbed mode if tabbed is true.
*
* @param tabbed if true the TabbedPane is the active view, otherwise it is
* the IconSplitPane
*/
public ToggleableIconPane(boolean tabbed)
{
this(tabbed, -1);
}
//--- Method(s) ---
/**
* Inserts a component, at index, represented by a description and/or
* icon.
*
* @param description the title for this component
* @param icon the icon to be displayed representing the component
* @param c the component to be displayed when the respective tab/icon is
* clicked
* @param index the position where the component is inserted
*/
public void insertTab(String description, Icon icon, JComponent c,
int index)
{
componentByDescription.put(description, c);
if (useTabbedPane)
tpane.insertTab(description, icon, c, null, index);
else
ipane.insertTab(description, icon, c, index);
if (index <= blinker.index) {
blinker.index++;
}
}
/**
* Adds a component, represented by a description and/or icon.
*
* @param description the title for this component
* @param icon the icon to be displayed representing the component
* @param c the component to be displayed when the respective tab/icon is
* clicked
*/
public void addTab(String description, Icon icon, JComponent c)
{
componentByDescription.put(description, c);
if (useTabbedPane)
tpane.addTab(description, icon, c);
else
ipane.addTab(description, icon, c);
}
/**
* Returns all the components in this container.
*
* @return an array of all child components of this container
*/
public Component[] getTabs()
{
if (useTabbedPane)
return tpane.getComponents();
else
return ipane.getTabs();
}
/**
* Returns the index of the tab/icon for the specified component.
*
* Returns the index of the tab for the specified component. Returns -1 if
* there is no tab for this component.
*
* @param component - the component for the tab/icon
*
* @return the first tab which matches this component, or -1 if there is
* no tab/icon for this component.
*/
public int indexOfComponent(Component c)
{
if (useTabbedPane)
return tpane.indexOfComponent(c);
else
return ipane.indexOfComponent(c);
}
/**
* Sets the divider size for the IconSplitPane view.
*
* @param newSize an integer giving the size of the divider in pixels
*/
public void setDividerSize(int newSize)
{
dividerSize = newSize;
}
/**
* Removes the specified component from the ToggleableIconPane.
*
* @param c the componennt which is to be removed
*/
public void remove(Component c)
{
Object key = null;
for (Enumeration e = componentByDescription.keys();
e.hasMoreElements();) {
Object o = e.nextElement();
if (componentByDescription.get(o) == c) {
key = o;
break;
}
}
if (key != null) {
componentByDescription.remove(key);
}
if (useTabbedPane)
tpane.remove(c);
else
ipane.remove(c);
blinker.index--;
}
/**
* Adds a ChangeListener to this toggableiconpane.
*
* @param l the changelistener to add
*/
public void addChangeListener(ChangeListener l)
{
cls.add(l);
if (useTabbedPane)
tpane.addChangeListener(l);
else
ipane.addChangeListener(l);
}
/**
* Removes a ChangeListener from this toggableiconpane.
*
* @param l the changelistener to remove
*/
public void removeChangeListener(ChangeListener l) {
cls.remove(l);
if (useTabbedPane)
tpane.removeChangeListener(l);
else
ipane.removeChangeListener(l);
}
/**
* Returns the icon at index i.
*
* @param i the index of the item being queried
* @return the icon at index i
*/
public Icon getIconAt(int i)
{
if (useTabbedPane)
return tpane.getIconAt(i);
else
return ipane.getIconAt(i);
}
/**
* Sets the icon at index i.
*
* @param i the index of the item being set
* @param icon the icon to be set
*/
public void setIconAt(int i, Icon icon)
{
if (useTabbedPane)
tpane.setIconAt(i, icon);
else
ipane.setIconAt(i, icon);
}
/**
* Sets a blinking icon for the component at index i.
*
* There is only one icon blinking at a time.
*
* @param i the index of the item being blinked
* @param blinkIcon the icon which blinks.
*/
public void blink(int i, Icon blinkIcon)
{
unblink();
blinker = new Blinker(i, blinkIcon);
timer = new Timer(BLINK_INTERVAL, blinker);
timer.start();
}
/**
* Stops blinking and restores the original icon for the blinking tab/icon.
*/
public void unblink()
{
if (timer != null) {
timer.stop();
setIconAt(blinker.index, blinker.savedIcon);
timer = null;
}
}
/**
* Returns the index of the currently selected component.
*
* Returns -1 if there is no currently selected component.
*
* @return the index of the selected component
*/
public int getSelectedIndex()
{
if (useTabbedPane)
return tpane.getSelectedIndex();
else
return ipane.getSelectedIndex();
}
/**
* Returns the currently selected component for this toggableiconpane.
*
* Returns null if there is no currently selected component.
*
* @return the component corresponding to the selected tab/icon
*/
public Component getSelectedComponent()
{
if (useTabbedPane)
return tpane.getSelectedComponent();
else
return ipane.getSelectedComponent();
}
/**
* Returns the number of child components.
*/
public int getTabCount()
{
if (useTabbedPane)
return tpane.getTabCount();
else
return ipane.getTabCount();
}
public void putTabClientProperty(Object key, Object value)
{
if (clientProperties == null) {
clientProperties = new Hashtable();
}
clientProperties.put(key, value);
updateClientProperties();
}
/**
* Selects the tab/icon having the given description.
*/
public void setSelectedComponent(String description)
{
setSelectedComponent((Component)componentByDescription.get(description));
}
/**
* Selects the given child component.
*/
public void setSelectedComponent(Component c)
{
if (useTabbedPane)
tpane.setSelectedComponent(c);
else
ipane.setSelectedComponent(c);
}
/**
* Toggles between the TabbedPane view and the IconSplitPane view.
*
* @param newValue whether or not the new view is the tabbed view
*/
public void setTabbed(boolean newValue)
{
useTabbedPane = newValue;
if (useTabbedPane && tpane == null) {
tpane = new JTabbedPane();
if (ipane != null) {
super.remove(ipane);
Component c = ipane.getSelectedComponent();
for (Iterator i = cls.iterator(); i.hasNext();) {
ChangeListener l = (ChangeListener)i.next();
ipane.removeChangeListener(l);
tpane.addChangeListener(l);
}
while(ipane.getTabCount() > 0) {
String title = ipane.getTitleAt(0);
Icon icon = ipane.getIconAt(0);
Component component = ipane.getTabAt(0);
ipane.removeTabAt(0);
tpane.addTab(title, icon, component);
}
ipane = null;
tpane.setSelectedComponent(c);
}
super.add(tpane, BorderLayout.CENTER);
}
else if (!useTabbedPane && ipane == null) {
ipane = new IconSplitPane();
if (dividerSize != -1) {
ipane.setDividerSize(dividerSize);
}
if (tpane != null) {
super.remove(tpane);
Component c = tpane.getSelectedComponent();
for (Iterator i = cls.iterator(); i.hasNext();) {
ChangeListener l = (ChangeListener)i.next();
tpane.removeChangeListener(l);
ipane.addChangeListener(l);
}
while(tpane.getTabCount() > 0) {
String title = tpane.getTitleAt(0);
Icon icon = tpane.getIconAt(0);
Component component = tpane.getComponentAt(0);
tpane.removeTabAt(0);
ipane.addTab(title, icon, component);
}
tpane = null;
ipane.setSelectedComponent(c);
}
super.add(ipane, BorderLayout.CENTER);
}
updateClientProperties();
}
/**
* Implements the {@link ChangeListener} interface.
*/
public void stateChanged(ChangeEvent e)
{
if (timer != null && getSelectedIndex() == blinker.index) {
unblink();
}
}
protected void updateClientProperties()
{
if (clientProperties != null) {
for (Iterator i = clientProperties.keySet().iterator(); i.hasNext();) {
Object key = i.next();
if (useTabbedPane)
tpane.putClientProperty(key, clientProperties.get(key));
else
ipane.putClientProperty(key, clientProperties.get(key));
}
}
}
protected class Blinker implements ActionListener
{
public Icon blinkIcon;
public Icon savedIcon;
public int index = 0;
public Blinker(int index, Icon blinkIcon)
{
this.index = index;
this.blinkIcon = blinkIcon;
savedIcon = getIconAt(index);
}
public Blinker()
{
}
public void actionPerformed(ActionEvent e)
{
Icon icon = (getIconAt(index) == savedIcon)
? blinkIcon : savedIcon;
setIconAt(index, icon);
}
}
}
The table below shows all metrics for ToggleableIconPane.java.




