1 3 package com.sshtools.ui.swing; 4 5 import java.awt.BorderLayout ; 6 import java.awt.CardLayout ; 7 import java.awt.Component ; 8 import java.awt.event.ActionEvent ; 9 import java.util.Iterator ; 10 import java.util.Vector ; 11 12 import javax.swing.Action ; 13 import javax.swing.BorderFactory ; 14 import javax.swing.Icon ; 15 import javax.swing.JPanel ; 16 17 public class SideBarTabber extends JPanel implements Tabber { 18 19 private TabToolBar toolBar; 20 private Vector tabs; 21 private Vector actions; 22 private FolderBar folderBar; 23 private JPanel viewPane; 24 private CardLayout layout; 25 private ScrollingPanel scrolling; 26 27 30 public SideBarTabber() { 31 super(new BorderLayout ()); 32 tabs = new Vector (); 33 actions = new Vector (); 34 toolBar = new TabToolBar() { 35 public int getFixedWidth() { 36 return getFixedToolBarWidth(); 37 } 38 }; 39 scrolling = new ScrollingPanel(toolBar); 40 scrolling.setBorder(BorderFactory.createLoweredBevelBorder()); 41 folderBar = new FolderBar(" ", new EmptyIcon(32, 32)); 42 folderBar.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLoweredBevelBorder(), 43 BorderFactory.createEmptyBorder(0, 0, 4, 0))); 44 toolBar.setFolderBar(folderBar); 45 46 JPanel centerPane = new JPanel (new BorderLayout ()); 47 centerPane.setBorder(BorderFactory.createEmptyBorder(0, 4, 0, 0)); 48 viewPane = new JPanel (layout = new CardLayout ()); 49 centerPane.add(folderBar, BorderLayout.NORTH); 50 centerPane.add(viewPane, BorderLayout.CENTER); 51 add(scrolling, BorderLayout.WEST); 52 add(centerPane, BorderLayout.CENTER); 53 } 54 55 public int getFixedToolBarWidth() { 56 return toolBar.getFixedWidth(); 57 } 58 59 public void setFixedToolBarWidth(int fixedWidth) { 60 toolBar.setFixedWidth(fixedWidth); 61 } 62 63 68 public Tab getTabAt(int i) { 69 return (Tab) tabs.elementAt(i); 70 } 71 72 77 public Component getComponent() { 78 return this; 79 } 80 81 86 public void removeAllTabs() { 87 tabs.clear(); 88 actions.clear(); 89 viewPane.invalidate(); 90 viewPane.removeAll(); 91 folderBar.setAction(null); 92 toolBar.removeAllActions(); 93 viewPane.validate(); 94 95 } 96 97 102 public void applyTabs() { 103 for (int i = 0; i < tabs.size(); i++) { 104 ((Tab) tabs.elementAt(i)).applyTab(); 105 } 106 } 107 108 113 public boolean validateTabs() { 114 for (int i = 0; i < tabs.size(); i++) { 115 Tab t = (Tab) tabs.elementAt(i); 116 117 if (!t.validateTab()) { 118 return false; 119 } 120 } 121 122 return true; 123 } 124 125 130 public void addTab(Tab tab) { 131 addTab(tab, false); 132 } 133 134 138 public void addTab(Tab tab, boolean sel) { 139 String c = (tab.getTabCategory() == null) ? "Unknown" : tab.getTabCategory(); 140 TabAction action = new TabAction(tab.getTabIcon(), 141 tab.getTabLargeIcon(), 142 tab.getTabTitle(), 143 tab.getTabToolTipText(), 144 tab.getTabMnemonic(), 145 layout, 146 viewPane, 147 c); 148 tabs.addElement(tab); 149 actions.addElement(action); 150 viewPane.add(tab.getTabComponent(), tab.getTabTitle()); 151 toolBar.addAction(action); 152 153 if (sel || tabs.size() == 1) { 154 layout.show(viewPane, tab.getTabTitle()); 155 folderBar.setAction(action); 156 toolBar.setSelectedContext(c); 157 } 158 159 scrolling.setAvailableActions(); 160 } 161 162 164 class TabAction extends AppAction { 165 CardLayout layout; 166 JPanel viewPane; 167 168 TabAction(Icon icon, Icon largeIcon, String name, String description, int mnemonic, CardLayout layout, JPanel viewPane, 169 String category) { 170 super(name); 171 putValue(AppAction.LARGE_ICON, largeIcon); 172 putValue(AppAction.SMALL_ICON, icon); 173 putValue(AppAction.LONG_DESCRIPTION, description); 174 putValue(AppAction.CATEGORY, category); 175 this.layout = layout; 176 this.viewPane = viewPane; 177 } 178 179 public boolean checkAvailable() { 180 return true; 181 } 182 183 public void actionPerformed(ActionEvent evt) { 184 layout.show(viewPane, (String ) getValue(AppAction.NAME)); 185 } 186 } 187 188 191 public int getTabCount() { 192 return tabs.size(); 193 } 194 195 public void setSelectedTabClass(Class selectedTabClass) { 196 if (selectedTabClass != null) { 197 for (Iterator i = tabs.iterator(); i.hasNext();) { 198 Tab tab = (Tab) i.next(); 199 if (tab.getClass().equals(selectedTabClass)) { 200 String c = (tab.getTabCategory() == null) ? "Unknown" : tab.getTabCategory(); 201 layout.show(viewPane, tab.getTabTitle()); ; 202 folderBar.setAction((Action )actions.get(tabs.indexOf(tab))); 203 toolBar.setSelectedContext(c); 204 return; 205 206 } 207 } 208 } 209 } 210 } | Popular Tags |