001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.component; 020 021import icy.gui.component.button.IcyButton; 022import icy.resource.ResourceUtil; 023 024import java.awt.Color; 025import java.awt.Component; 026import java.awt.event.ActionEvent; 027import java.awt.event.ActionListener; 028import java.util.EventListener; 029 030import javax.swing.BorderFactory; 031import javax.swing.Box; 032import javax.swing.BoxLayout; 033import javax.swing.Icon; 034import javax.swing.JLabel; 035import javax.swing.JPanel; 036import javax.swing.JTabbedPane; 037import javax.swing.SwingConstants; 038 039/** 040 * @deprecated Use {@link CloseableTabbedPane} instead 041 */ 042@Deprecated 043public class CloseTabbedPane extends JTabbedPane 044{ 045 public static interface CloseTabbedPaneListener extends EventListener 046 { 047 public void tabClosed(int index, String title); 048 } 049 050 /** 051 * 052 */ 053 private static final long serialVersionUID = 7946889981661387490L; 054 055 private class CloseTabComponent extends JPanel 056 { 057 /** 058 * 059 */ 060 private static final long serialVersionUID = 4841789742300589373L; 061 062 final private IcyButton closeButton; 063 final private JLabel label; 064 final private Component sep; 065 066 public CloseTabComponent(String title, Icon icon) 067 { 068 super(); 069 070 setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); 071 setBorder(BorderFactory.createEmptyBorder()); 072 setOpaque(false); 073 074 label = new JLabel(title, icon, SwingConstants.CENTER); 075 label.setOpaque(false); 076 077 sep = Box.createHorizontalStrut(6); 078 079 closeButton = new IcyButton(ResourceUtil.ICON_DELETE, 12); 080 closeButton.setFlat(true); 081 // closeButton.setContentAreaFilled(false); 082 closeButton.setToolTipText("close"); 083 closeButton.setOpaque(false); 084 085 closeButton.addActionListener(new ActionListener() 086 { 087 @Override 088 public void actionPerformed(ActionEvent actionevent) 089 { 090 final int index = indexOfTabComponent(CloseTabComponent.this); 091 092 if (index != -1) 093 { 094 CloseTabbedPane.this.removeTabAt(index); 095 CloseTabbedPane.this.fireTabClosed(index, getTitle()); 096 } 097 } 098 }); 099 100 add(label); 101 add(sep); 102 add(closeButton); 103 104 validate(); 105 } 106 107 public boolean isClosable() 108 { 109 return closeButton.isVisible(); 110 } 111 112 public void setClosable(boolean value) 113 { 114 sep.setVisible(value); 115 closeButton.setVisible(value); 116 } 117 118 public String getTitle() 119 { 120 return label.getText(); 121 } 122 123 public void setTitle(String title) 124 { 125 label.setText(title); 126 } 127 128 public void setIcon(Icon icon) 129 { 130 label.setIcon(icon); 131 } 132 133 public void setDisabledIcon(Icon disabledIcon) 134 { 135 label.setDisabledIcon(disabledIcon); 136 } 137 138 public void setBackgroundAll(Color background) 139 { 140 label.setBackground(background); 141 closeButton.setBackground(background); 142 } 143 144 public void setForegroundAll(Color foreground) 145 { 146 label.setForeground(foreground); 147 closeButton.setForeground(foreground); 148 } 149 } 150 151 /** 152 * {@link JTabbedPane} 153 */ 154 public CloseTabbedPane() 155 { 156 super(); 157 } 158 159 /** 160 * {@link JTabbedPane} 161 */ 162 public CloseTabbedPane(int tabPlacement) 163 { 164 super(tabPlacement); 165 } 166 167 /** 168 * {@link JTabbedPane} 169 */ 170 public CloseTabbedPane(int tabPlacement, int tabLayoutPolicy) 171 { 172 super(tabPlacement, tabLayoutPolicy); 173 } 174 175 /** 176 * Returns the 'closable' state of tab component at <code>index</code>. 177 * 178 * @param index 179 * the tab index where the check state is queried 180 * @return true if tab component at <code>index</code> can be closed (close button visible).<br> 181 * Returns false otherwise 182 * @exception IndexOutOfBoundsException 183 * if index is out of range (index < 0 || index >= tab count) 184 * @see #setTabClosable(int, boolean) 185 */ 186 public boolean isTabClosable(int index) 187 { 188 return ((CloseTabComponent) getTabComponentAt(index)).isClosable(); 189 } 190 191 /** 192 * Set the 'closable' state of tab component at <code>index</code>. 193 * 194 * @param index 195 * the tab index we want to set the 'closable' state 196 * @param value 197 * true if the tab should be 'closable' (close button visible), false otherwise. 198 * @exception IndexOutOfBoundsException 199 * if index is out of range (index < 0 || index >= tab count) 200 * @see #isTabClosable(int) 201 */ 202 public void setTabClosable(int index, boolean value) 203 { 204 ((CloseTabComponent) getTabComponentAt(index)).setClosable(value); 205 } 206 207 @Override 208 public void setIconAt(int index, Icon icon) 209 { 210 super.setIconAt(index, icon); 211 212 final CloseTabComponent comp = (CloseTabComponent) getTabComponentAt(index); 213 214 if (comp != null) 215 comp.setIcon(icon); 216 } 217 218 @Override 219 public void setDisabledIconAt(int index, Icon disabledIcon) 220 { 221 super.setDisabledIconAt(index, disabledIcon); 222 223 final CloseTabComponent comp = (CloseTabComponent) getTabComponentAt(index); 224 225 if (comp != null) 226 comp.setDisabledIcon(disabledIcon); 227 } 228 229 @Override 230 public void setBackgroundAt(int index, Color background) 231 { 232 super.setBackgroundAt(index, background); 233 234 final CloseTabComponent comp = (CloseTabComponent) getTabComponentAt(index); 235 236 if (comp != null) 237 comp.setBackgroundAll(background); 238 } 239 240 @Override 241 public void setForegroundAt(int index, Color foreground) 242 { 243 super.setForegroundAt(index, foreground); 244 245 final CloseTabComponent comp = (CloseTabComponent) getTabComponentAt(index); 246 247 if (comp != null) 248 comp.setForegroundAll(foreground); 249 } 250 251 @Override 252 public void setTitleAt(int index, String title) 253 { 254 super.setTitleAt(index, title); 255 256 final CloseTabComponent comp = (CloseTabComponent) getTabComponentAt(index); 257 258 if (comp != null) 259 comp.setTitle(title); 260 } 261 262 @Override 263 public void insertTab(String title, Icon icon, Component component, String tip, int index) 264 { 265 super.insertTab(title, icon, component, tip, index); 266 267 setTabComponentAt(index, new CloseTabComponent(title, icon)); 268 } 269 270 protected void fireTabClosed(int index, String text) 271 { 272 for (CloseTabbedPaneListener l : listenerList.getListeners(CloseTabbedPaneListener.class)) 273 l.tabClosed(index, text); 274 275 } 276 277 public void addCloseTabbedPaneListener(CloseTabbedPaneListener l) 278 { 279 listenerList.add(CloseTabbedPaneListener.class, l); 280 } 281 282 public void removeCloseTabbedPaneListener(CloseTabbedPaneListener l) 283 { 284 listenerList.remove(CloseTabbedPaneListener.class, l); 285 } 286}