1 19 20 package org.netbeans.modules.xml.xam.ui.category; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.event.ActionEvent ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import javax.swing.AbstractAction ; 30 import javax.swing.Action ; 31 import javax.swing.ButtonGroup ; 32 import javax.swing.JPanel ; 33 import javax.swing.JToggleButton ; 34 import javax.swing.JToolBar ; 35 import javax.swing.UIManager ; 36 import javax.swing.border.Border ; 37 import org.netbeans.modules.xml.xam.ui.search.DefaultSearchControlPanel; 38 39 44 public class DefaultCategoryPane extends AbstractCategoryPane { 45 46 private JPanel visualComponent; 47 48 private DefaultSearchControlPanel searchComponent; 49 50 private JPanel categoryPanel; 51 52 private List <ShowCategoryAction> categoryActions; 53 54 private ButtonGroup buttonGroup; 55 56 59 public DefaultCategoryPane() { 60 categoryActions = new ArrayList <ShowCategoryAction>(); 61 initComponents(); 62 } 63 64 public void addCategory(Category category) { 65 categoryActions.add(new ShowCategoryAction(category, this)); 66 } 67 68 public Component getComponent() { 69 return visualComponent; 70 } 71 72 public SearchComponent getSearchComponent() { 73 return searchComponent; 74 } 75 76 79 private void initComponents() { 80 buttonGroup = new ButtonGroup (); 82 83 visualComponent = new JPanel (new GridBagLayout ()) { 85 86 private static final long serialVersionUID = 1L; 87 88 @Override 89 public void requestFocus() { 90 super.requestFocus(); 91 Category cat = getCategory(); 93 if (cat != null) { 94 cat.getComponent().requestFocus(); 95 } 96 } 97 98 @Override 99 public boolean requestFocusInWindow() { 100 boolean retVal = super.requestFocusInWindow(); 101 Category cat = getCategory(); 103 if (cat != null) { 104 return cat.getComponent().requestFocusInWindow(); 105 } 106 return retVal; 107 } 108 }; 109 110 GridBagConstraints gbc = new GridBagConstraints (); 112 gbc.fill = GridBagConstraints.BOTH; 113 gbc.gridwidth = GridBagConstraints.REMAINDER; 114 gbc.weightx = 1.0d; 115 gbc.weighty = 1.0d; 116 categoryPanel = new JPanel (new BorderLayout ()); 117 visualComponent.add(categoryPanel, gbc); 118 119 searchComponent = new DefaultSearchControlPanel(this); 121 searchComponent.hideComponent(); 122 gbc.anchor = GridBagConstraints.WEST; 123 gbc.fill = GridBagConstraints.HORIZONTAL; 124 gbc.weightx = 0.0d; 125 gbc.weighty = 0.0d; 126 visualComponent.add(searchComponent.getComponent(), gbc); 127 } 128 129 public void populateToolbar(JToolBar toolbar) { 130 Category selected = getCategory(); 131 Border border = UIManager.getBorder("nb.tabbutton.border"); for (ShowCategoryAction action : categoryActions) { 134 JToggleButton button = new JToggleButton (action); 135 button.setText(null); 138 button.getAccessibleContext().setAccessibleName(action.getCategory().getTitle()); 139 button.setRolloverEnabled(true); 140 if (border != null) { 141 button.setBorder(border); 142 } 143 buttonGroup.add(button); 144 if (selected == null) { 145 if (buttonGroup.getButtonCount() == 1) { 146 button.setSelected(true); 148 action.actionPerformed(null); 150 } 151 } else { 152 if (action.getCategory().equals(selected)) { 153 button.setSelected(true); 154 } 155 } 156 toolbar.add(button); 157 } 158 } 159 160 @Override 161 public void setCategory(Category category) { 162 Category oldcat = getCategory(); 163 super.setCategory(category); 164 if (oldcat != null) { 165 Component oldcomp = oldcat.getComponent(); 166 categoryPanel.remove(oldcomp); 167 oldcat.componentHidden(); 168 } 169 Component newcomp = category.getComponent(); 170 categoryPanel.add(newcomp, BorderLayout.CENTER); 171 category.componentShown(); 172 categoryPanel.validate(); 173 categoryPanel.repaint(); 174 searchComponent.updateSearchProviders(); 175 } 176 177 180 private static class ShowCategoryAction extends AbstractAction { 181 private static final long serialVersionUID = 1L; 182 183 private transient Category category; 184 185 private transient CategoryPane pane; 186 187 193 public ShowCategoryAction(Category category, CategoryPane pane) { 194 this.category = category; 195 this.pane = pane; 196 putValue(Action.NAME, category.getTitle()); 197 putValue(Action.SHORT_DESCRIPTION, category.getDescription()); 198 putValue(Action.SMALL_ICON, category.getIcon()); 199 } 200 201 public void actionPerformed(ActionEvent e) { 202 pane.setCategory(category); 203 } 204 205 210 public Category getCategory() { 211 return category; 212 } 213 } 214 } 215 | Popular Tags |