1 21 package com.sshtools.ui.swing; 22 23 import java.awt.CardLayout ; 24 import java.awt.Color ; 25 import java.awt.Dimension ; 26 import java.awt.GridBagConstraints ; 27 import java.awt.GridBagLayout ; 28 import java.awt.Insets ; 29 import java.awt.event.ActionEvent ; 30 import java.util.Iterator ; 31 import java.util.Vector ; 32 33 import javax.swing.JButton ; 34 import javax.swing.JPanel ; 35 import javax.swing.JViewport ; 36 import javax.swing.SwingConstants ; 37 import javax.swing.UIManager ; 38 39 45 public class TabToolBar extends JPanel { 46 private Vector contextList; 48 private ContextPanel selectedContext; 49 private GridBagConstraints gBC; 50 private CardLayout cardLayout; 51 private JPanel cardPanel; 52 private FolderBar folderBar; 53 private int fixedWidth = -1; 54 private Color toolBarBackground; 55 56 59 public TabToolBar() { 60 super(new GridBagLayout ()); 61 62 toolBarBackground = UIManager.getColor("Button.background").darker(); 63 64 contextList = new Vector (); 66 cardLayout = new CardLayout (); 67 cardPanel = new JPanel (cardLayout); 68 cardPanel.setOpaque(false); 69 cardPanel.setBackground(toolBarBackground); 70 71 gBC = new GridBagConstraints (); 73 gBC.anchor = GridBagConstraints.NORTH; 74 gBC.fill = GridBagConstraints.HORIZONTAL; 75 76 setBackground(toolBarBackground); 77 setOpaque(true); 78 } 79 80 public void setFixedWidth(int fixedWidth) { 81 this.fixedWidth = fixedWidth; 82 doLayout(); 83 } 84 85 public int getFixedWidth() { 86 return fixedWidth; 87 } 88 89 94 public void setSelectedContext(String context) { 95 ContextPanel p = null; 96 97 for (Iterator i = contextList.iterator(); i.hasNext();) { 98 ContextPanel z = (ContextPanel) i.next(); 99 100 if (context.equals(z.name)) { 101 p = z; 102 } 103 } 104 105 if (p != null) { 106 selectedContext = p; 107 cardLayout.show(cardPanel, context); 108 makeToolBar(); 109 } 110 } 111 112 119 public void setFolderBar(FolderBar folderBar) { 120 this.folderBar = folderBar; 121 } 122 123 128 public FolderBar getFolderBar() { 129 return folderBar; 130 } 131 132 143 public void addAction(AppAction action) { 144 if ((getFolderBar() != null) && (getFolderBar().getAction() == null)) { 147 getFolderBar().setAction(action); 148 } 149 150 String context = (String ) action.getValue(AppAction.CATEGORY); 152 153 if (context == null) { 154 throw new IllegalArgumentException ("AppAction.CONTEXT parameter of action must not be null"); 155 } 156 157 ContextPanel contextPanel = null; 158 159 for (int i = 0; (i < contextList.size()) && (contextPanel == null); i++) { 160 ContextPanel p = (ContextPanel) contextList.elementAt(i); 161 162 if (p.name.equals(context)) { 163 contextPanel = p; 164 } 165 } 166 167 if (contextPanel == null) { 168 contextPanel = new ContextPanel(context); 169 cardPanel.add(contextPanel.name, contextPanel); 170 contextList.addElement(contextPanel); 171 } 172 173 if (selectedContext == null) { 174 selectedContext = contextPanel; 175 } 176 177 TabActionButton button = contextPanel.addIcon(action); 179 180 if (getParent() instanceof JViewport && ((JViewport ) getParent()).getView() instanceof ScrollingPanel) { 181 ((ScrollingPanel) ((JViewport ) getParent()).getView()).setIncrement(button.getPreferredSize().height); 182 } 183 184 makeToolBar(); 185 } 186 187 190 public void removeAllActions() { 191 contextList.clear(); 192 cardPanel.removeAll(); 193 selectedContext = null; 194 makeToolBar(); 195 } 196 197 private void makeToolBar() { 198 invalidate(); 200 removeAll(); 201 for (int i = 0; i < contextList.size(); i++) { 202 ContextPanel p = (ContextPanel) contextList.elementAt(i); 203 204 UIUtil.jGridBagAdd(this, new TabButton(p.getContextAction()), gBC, GridBagConstraints.REMAINDER); 206 207 if (p == selectedContext) { 209 cardLayout.show(cardPanel, p.name); 210 211 gBC.weighty = 1.0; 212 UIUtil.jGridBagAdd(this, cardPanel, gBC, GridBagConstraints.REMAINDER); 213 gBC.weighty = 0.0; 214 } 215 } 216 217 validate(); 218 repaint(); 219 } 220 221 public class ContextPanel extends JPanel { 223 String name; 224 AppAction action; 225 GridBagConstraints gBC; 226 227 public ContextPanel(String name) { 228 super(new ListLayout()); 229 this.name = name; 230 setOpaque(false); 231 setBackground(toolBarBackground); 232 action = new ContextAction(name, this); 233 } 234 235 public AppAction getContextAction() { 236 return action; 237 } 238 239 public TabActionButton addIcon(AppAction action) { 240 TabActionButton b = new TabActionButton(action); 241 add(b); 242 243 return b; 244 } 245 } 246 247 public class ContextAction extends AppAction { 248 ContextPanel context; 249 250 ContextAction(String name, ContextPanel context) { 251 super(); 252 putValue(AppAction.NAME, name); 253 this.context = context; 254 } 255 256 public void actionPerformed(ActionEvent evt) { 257 selectedContext = context; 258 makeToolBar(); 259 } 260 261 public boolean checkAvailable() { 262 return true; 263 } 264 } 265 266 public class TabButton extends JButton { 267 public TabButton(AppAction a) { 268 super(a); 269 setFocusPainted(false); 270 setDefaultCapable(false); 271 setMargin(new Insets (1, 1, 1, 1)); 272 } 273 } 274 275 public class TabActionButton extends ActionButton { 276 277 public TabActionButton(final AppAction a) { 278 super(a, true, true); 279 setHorizontalTextPosition(SwingConstants.CENTER); 280 setVerticalTextPosition(SwingConstants.BOTTOM); 281 } 282 283 public Dimension getMaximimumSize() { 284 return fixedWidth == -1 ? super.getMaximumSize() : new Dimension (fixedWidth, super.getMaximumSize().height); 285 } 286 287 public Dimension getMinimumSize() { 288 return fixedWidth == -1 ? super.getMinimumSize() : new Dimension (fixedWidth, super.getMinimumSize().height); 289 } 290 291 public Dimension getPreferredSize() { 292 return fixedWidth == -1 ? super.getPreferredSize() : new Dimension (fixedWidth, super.getPreferredSize().height); 293 } 294 } 295 } 296 | Popular Tags |