1 package com.sshtools.ui.swing; 2 3 import java.awt.Component ; 4 import java.awt.Dimension ; 5 import java.awt.Insets ; 6 import java.awt.event.ActionEvent ; 7 import java.awt.event.ActionListener ; 8 import java.awt.event.MouseAdapter ; 9 import java.awt.event.MouseEvent ; 10 import javax.swing.AbstractButton ; 11 import javax.swing.Action ; 12 import javax.swing.JButton ; 13 import javax.swing.JPopupMenu ; 14 import javax.swing.JToolBar ; 15 import javax.swing.SwingConstants ; 16 17 public class ActionToolBar extends JToolBar { 18 private ExpandToolBarToggleButton button; 19 20 private int buttonIndex; 21 22 25 public ActionToolBar() { 26 super(); 27 init(); 28 } 29 30 33 public ActionToolBar(int orientation) { 34 super(orientation); 35 init(); 36 } 37 38 41 public ActionToolBar(String name) { 42 super(name); 43 init(); 44 } 45 46 50 public ActionToolBar(String name, int orientation) { 51 super(name, orientation); 52 init(); 53 } 54 55 public void setWrap(boolean wrap) { 56 boolean oldWrap = isWrap(); 57 if (wrap != oldWrap) { 58 ((ActionToolBarLayout) getLayout()).setWrap(wrap); 59 checkButton(); 60 } 61 } 62 63 public boolean isWrap() { 64 return getLayout() instanceof ActionToolBarLayout && ((ActionToolBarLayout) getLayout()).isWrap(); 65 } 66 67 private void init() { 68 ActionToolBarLayout layout = new ActionToolBarLayout(); 69 setLayout(layout); 70 checkButton(); 71 setWrap(false); 72 } 73 74 private void checkButton() { 75 invalidate(); 76 if(getLayout() instanceof ActionToolBarLayout) { if (button != null) { 78 remove(button); 79 ((ActionToolBarLayout) getLayout()).setExpandComponent(null); 80 buttonIndex = -1; 81 } 82 if (!isWrap()) { 83 button = new ExpandToolBarToggleButton(); 84 button.setFocusPainted(false); 85 ((ActionToolBarLayout) getLayout()).setExpandComponent(button); 86 add(button); 87 buttonIndex = getComponentIndex(button); 88 } 89 } 90 validate(); 91 repaint(); 92 } 93 94 class ExpandToolBarToggleButton extends JButton implements ActionListener { 95 JPopupMenu popup; 96 97 ExpandToolBarToggleButton() { 98 super(new ArrowIcon(SwingConstants.SOUTH)); 99 popup = new JPopupMenu (); 100 addActionListener(this); 101 addMouseListener(new MouseAdapter () { 102 103 public void mouseEntered(MouseEvent e) { 104 if(isEnabled()) { 105 setBorderPainted(true); 106 setContentAreaFilled(true); 107 } 108 } 109 110 public void mouseExited(MouseEvent e) { 111 setBorderPainted(false); 112 setContentAreaFilled(false); 113 } 114 }); 115 setBorderPainted(false); 116 setContentAreaFilled(false); 117 } 118 119 public Insets getMargin() { 120 return new Insets (0, 0, 0, 0); 121 } 122 123 public boolean isRequestFocusEnabled() { 124 return false; 125 } 126 127 public boolean isFocusTraversable() { 128 return false; 129 } 130 131 public void actionPerformed(ActionEvent evt) { 132 int overun = ((ActionToolBarLayout) ActionToolBar.this.getLayout()) 133 .getOverunIndex(); 134 if (overun != -1) { 135 popup.invalidate(); 136 popup.removeAll(); 137 int count = ActionToolBar.this.getComponentCount(); 138 Component c; 139 for (int i = overun; i < count; i++) { 140 c = ActionToolBar.this.getComponent(i); 141 if (c != this) { 142 if (c instanceof ToolBarSeparator) { 143 popup.addSeparator(); 144 } else { 145 AbstractButton button = (AbstractButton ) c; 146 Action appAction = (Action ) button.getAction(); 147 if (appAction != null) { 148 popup.add(appAction); 149 } 150 } 151 } 152 } 153 popup.show(this, getSize().width - popup.getPreferredSize().width, 154 getSize().height); 155 } 156 } 157 } 158 159 public Dimension getMinimumSize() { 160 return new Dimension (1, super.getMinimumSize().height); 161 } 162 163 168 public void remove(Component comp) { 169 if (comp != button) { 170 super.remove(comp); 171 } 172 } 173 174 179 public void remove(int index) { 180 if (index != buttonIndex) { 181 super.remove(index); 182 } 183 } 184 185 190 public void removeAll() { 191 super.removeAll(); 192 checkButton(); 193 } 194 } | Popular Tags |