package com.onaro.sanscreen.client; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; import java.util.ResourceBundle; import javax.swing.JComponent; import javax.swing.JScrollPane; import javax.swing.UIManager; import org.w3c.dom.Element; import com.onaro.commons.prefs.PreferencesWrapper; import com.onaro.commons.swing.OnaroSwingUtilities; import com.onaro.sanscreen.client.prefs.ClientPrefs; import com.onaro.sanscreen.client.view.ActionFactory; import com.onaro.util.jfc.collapsible.CollapsiblePane; import com.onaro.util.jfc.collapsible.CollapsiblePanels; import com.onaro.util.jfc.collapsible.HyperlinkButton; public class LegacyShortcutManager extends ShortcutManager { /** * Map from the group id to the button for the group; */ private Map groupIdToGroupMap; /** * Contains all shortcut groups. */ private JScrollPane scroll; private CollapsiblePanels groupPanels; private Element shortcutsElement; private ResourceBundle resources; private boolean initialized = false; /** * The preferneces for this class's package, used to restore user preferences. */ static final PreferencesWrapper preferences = ClientPrefs.getClient(); public LegacyShortcutManager(ActionFactory actionFactory, Element shortcutsElement, ResourceBundle resources) { super(actionFactory); this.shortcutsElement = shortcutsElement; this.resources = resources; groupIdToGroupMap = new HashMap(); } private void initialize() { if (!initialized) { addShortcutGroup(shortcutsElement, resources); processShortcutExtensions(); OnaroSwingUtilities.invokeNowOrLater(new Runnable() { public void run() { for (CollapsiblePane group : groupIdToGroupMap.values()) { String groupId = group.getName(); group.setExpanded(preferences.getBoolean(groupId, false)); } } }); initialized = true; } } @Override protected void addShortcutComponent(ShortcutAction shortcutAction) { String groupId = shortcutAction.getGroupId(); // Retrieve the appropriate group for this action CollapsiblePane group = groupIdToGroupMap.get(groupId); // Create the shortcut button HyperlinkButton button = new HyperlinkButton(shortcutAction); button.setName(shortcutAction.getId()); group.add(button); } @Override protected void addShortcutGroupComponent(ShortcutGroupAction shortcutGroupAction) { String groupId = shortcutGroupAction.getGroupId(); // Get the display name of the group from the action final String groupName = (String) shortcutGroupAction.getValue(ShortcutGroupAction.NAME); // Create the group's collapsible pane final CollapsiblePane groupPane = new CollapsiblePane(groupName); groupPane.setName(groupId); // Register the group in the map groupIdToGroupMap.put(groupId, groupPane); // Add the group to the groups panel getGroupPanels().add(groupPane); groupPane.addExpandedActionListener(new ExpandedStateRecorder(groupId)); } private CollapsiblePanels getGroupPanels() { if (groupPanels == null) { groupPanels = new CollapsiblePanels(); groupPanels.setBackground(UIManager.getDefaults().getColor("BasicPanel.background")); //$NON-NLS-1$ } return groupPanels; } private JScrollPane getScroll() { if (scroll == null) { scroll = new JScrollPane(); scroll.setViewportView(getGroupPanels()); scroll.setBorder(UIManager.getDefaults().getBorder("BasicPanel.border")); //$NON-NLS-1$ scroll.getVerticalScrollBar().setUnitIncrement(15); } return scroll; } /** * Gets the shortcut panel. * @return the view */ public JComponent getComponent() { initialize(); return getScroll(); } private class ExpandedStateRecorder implements ActionListener { String groupId; public ExpandedStateRecorder(String groupId) { this.groupId = groupId; } public void actionPerformed(ActionEvent e) { preferences.putBoolean(groupId, ((CollapsiblePane)e.getSource()).isExpanded()); } } }