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 java.awt.Color; 022import java.awt.Component; 023import java.awt.event.ActionEvent; 024import java.awt.event.ActionListener; 025 026import javax.swing.BorderFactory; 027import javax.swing.BoxLayout; 028import javax.swing.Icon; 029import javax.swing.JCheckBox; 030import javax.swing.JLabel; 031import javax.swing.JPanel; 032import javax.swing.JTabbedPane; 033import javax.swing.SwingConstants; 034 035/** 036 * Basically a JTabbedPane with checkbox in tab. 037 * 038 * @author Stephane 039 */ 040public class CheckTabbedPane extends JTabbedPane 041{ 042 /***/ 043 private static final long serialVersionUID = 1274171822668858593L; 044 045 private class CheckTabComponent extends JPanel 046 { 047 /** 048 * 049 */ 050 private static final long serialVersionUID = 4841789742300589373L; 051 052 final private JCheckBox checkBox; 053 final private JLabel label; 054 055 public CheckTabComponent(String title, Icon icon) 056 { 057 super(); 058 059 setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS)); 060 setBorder(BorderFactory.createEmptyBorder()); 061 setOpaque(false); 062 063 checkBox = new JCheckBox(null, null, defaultSelected); 064 checkBox.setBorder(BorderFactory.createEmptyBorder()); 065 checkBox.setFocusable(false); 066 checkBox.setToolTipText("enable / disable"); 067 checkBox.setOpaque(false); 068 069 checkBox.addActionListener(new ActionListener() 070 { 071 @Override 072 public void actionPerformed(ActionEvent actionevent) 073 { 074 CheckTabbedPane.this.fireStateChanged(); 075 } 076 }); 077 078 label = new JLabel(" " + title, icon, SwingConstants.CENTER); 079 label.setOpaque(false); 080 081 add(checkBox); 082 add(label); 083 084 validate(); 085 } 086 087 public boolean isSelected() 088 { 089 return checkBox.isSelected(); 090 } 091 092 public void setSelected(boolean value) 093 { 094 checkBox.setSelected(value); 095 } 096 097 public void setTitle(String title) 098 { 099 label.setText(" " + title); 100 } 101 102 public void setIcon(Icon icon) 103 { 104 label.setIcon(icon); 105 } 106 107 public void setDisabledIcon(Icon disabledIcon) 108 { 109 label.setDisabledIcon(disabledIcon); 110 } 111 112 public void setBackgroundAll(Color background) 113 { 114 checkBox.setBackground(background); 115 label.setBackground(background); 116 } 117 118 public void setForegroundAll(Color foreground) 119 { 120 checkBox.setForeground(foreground); 121 label.setForeground(foreground); 122 } 123 } 124 125 /** 126 * default checkbox selected state 127 */ 128 boolean defaultSelected; 129 130 /** 131 * Constructor. 132 * 133 * @param defaultSelected 134 * by default checkbox is selected 135 * @see JTabbedPane 136 */ 137 public CheckTabbedPane(int tabPlacement, boolean defaultSelected) 138 { 139 super(tabPlacement); 140 141 this.defaultSelected = defaultSelected; 142 } 143 144 public boolean isDefaultSelected() 145 { 146 return defaultSelected; 147 } 148 149 public void setDefaultSelected(boolean defaultSelected) 150 { 151 this.defaultSelected = defaultSelected; 152 } 153 154 @Override 155 protected void fireStateChanged() 156 { 157 // just to avoid warning 158 super.fireStateChanged(); 159 } 160 161 /** 162 * Returns the check state of tab component at <code>index</code>. 163 * 164 * @param index 165 * the tab index where the check state is queried 166 * @return true if tab component at <code>index</code> is checked, false 167 * otherwise 168 * @exception IndexOutOfBoundsException 169 * if index is out of range (index < 0 || index >= tab count) 170 * @see #setTabChecked(int, boolean) 171 */ 172 public boolean isTabChecked(int index) 173 { 174 return ((CheckTabComponent) getTabComponentAt(index)).isSelected(); 175 } 176 177 /** 178 * Set the check state of tab component at <code>index</code>. 179 * 180 * @param index 181 * the tab index we want to set the check state 182 * @param value 183 * the check state 184 * @exception IndexOutOfBoundsException 185 * if index is out of range (index < 0 || index >= tab count) 186 * @see #isTabChecked(int) 187 */ 188 public void setTabChecked(int index, boolean value) 189 { 190 ((CheckTabComponent) getTabComponentAt(index)).setSelected(value); 191 } 192 193 @Override 194 public void setIconAt(int index, Icon icon) 195 { 196 super.setIconAt(index, icon); 197 198 ((CheckTabComponent) getTabComponentAt(index)).setIcon(icon); 199 } 200 201 @Override 202 public void setDisabledIconAt(int index, Icon disabledIcon) 203 { 204 super.setDisabledIconAt(index, disabledIcon); 205 206 ((CheckTabComponent) getTabComponentAt(index)).setDisabledIcon(disabledIcon); 207 } 208 209 @Override 210 public void setBackgroundAt(int index, Color background) 211 { 212 super.setBackgroundAt(index, background); 213 214 ((CheckTabComponent) getTabComponentAt(index)).setBackgroundAll(background); 215 } 216 217 @Override 218 public void setForegroundAt(int index, Color foreground) 219 { 220 super.setForegroundAt(index, foreground); 221 222 ((CheckTabComponent) getTabComponentAt(index)).setForegroundAll(foreground); 223 } 224 225 @Override 226 public void setTitleAt(int index, String title) 227 { 228 super.setTitleAt(index, title); 229 230 ((CheckTabComponent) getTabComponentAt(index)).setTitle(title); 231 } 232 233 @Override 234 public void insertTab(String title, Icon icon, Component component, String tip, int index) 235 { 236 super.insertTab(title, icon, component, tip, index); 237 238 setTabComponentAt(index, new CheckTabComponent(title, icon)); 239 } 240}