ShortcutPrefsPanel.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.prefs |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.prefs;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.util.Hashtable;
import javax.help.CSH;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import org.xnap.XNap;
import org.xnap.gui.AbstractSettingsPanel;
import org.xnap.gui.action.EnableAction;
import org.xnap.gui.component.KeyStrokePanel;
import org.xnap.gui.component.ValidatedTextField;
import org.xnap.gui.shortcut.Shortcut;
import org.xnap.gui.shortcut.ShortcutManager;
import org.xnap.gui.table.Column;
import org.xnap.gui.table.DefaultColumnTreeTableModel;
import org.xnap.gui.table.DefaultTreeTableNode;
import org.xnap.gui.table.JTreeTable;
import org.xnap.gui.table.LeafTreeTableNode;
import org.xnap.gui.table.TreeTableModel;
import org.xnap.gui.tree.StringTreeCellRenderer;
import org.xnap.gui.util.GUIHelper;
import org.xnap.gui.util.GridBagHelper;
/**
* Provides controls for shortcut appearance settings.
*/
public class ShortcutPrefsPanel extends AbstractSettingsPanel
implements TreeSelectionListener, ChangeListener
{
//--- Data field(s) ---
private JCheckBox jcShowIcons;
private JCheckBox jcShowSplash;
private JCheckBox jcShowTooltips;
private JCheckBox jcbUseSubMenuForPluginMenus;
private ValidatedTextField jteMaxConsoleLines;
private ValidatedTextField jteHistorySize;
private JRadioButton jrbFocusAlways;
private JRadioButton jrbFocusNever;
private ShortcutTableModel tm;
private KeyStrokePanel shortcutPanel;
private ShortcutNode currentNode;
//--- Constructor(s) ---
public ShortcutPrefsPanel()
{
setLayout(new GridBagLayout());
// help
CSH.setHelpIDString(this, "shortcuts");
tm = new ShortcutTableModel();
JTreeTable shortcutTable = tm.createTreeTable(prefs, "shortcut");
shortcutTable.setPreferredScrollableViewportSize(new Dimension(50, 50));
shortcutTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
shortcutTable.getTree().getSelectionModel().addTreeSelectionListener(this);
shortcutTable.getTree().setCellRenderer(new StringTreeCellRenderer(tm));
shortcutTable.getTableHeader().setReorderingAllowed(false);
// add shortcuts
Shortcut[] shortcuts = ShortcutManager.getInstance().getShortcuts();
if (shortcuts != null) {
for (int i = 0; i < shortcuts.length; i++) {
tm.add(shortcuts[i]);
}
}
GUIHelper.expandAllNodes(shortcutTable.getTree());
GridBagHelper.addPanel(this, new JScrollPane(shortcutTable));
shortcutPanel = new KeyStrokePanel();
shortcutPanel.addChangeListener(this);
setBorder(GUIHelper.createDefaultBorder(XNap.tr("Shortcut")));
// disable by default, will be enabled when a node is selected
EnableAction.setComponentEnabled(shortcutPanel, false);
GridBagHelper.add(this, shortcutPanel);
GUIHelper.setMnemonics(this);
}
public void apply()
{
if (currentNode != null) {
currentNode.setKeyStroke(shortcutPanel.getKeyStroke());
}
// traverse tree and commit all shortcuts
for (int i = tm.getChildCount(tm.getRoot()) - 1; i >= 0; i--) {
DefaultTreeTableNode node
= (DefaultTreeTableNode)tm.getChild(tm.getRoot(), i);
for (int j = node.getChildCount() - 1; j >= 0; j--) {
ShortcutNode child = (ShortcutNode)node.getChildAt(j);
child.getShortcut().setKeyStroke(child.getKeyStroke());
}
}
}
public String getTitle()
{
return XNap.tr("Shortcuts");
}
public void valueChanged(TreeSelectionEvent event)
{
if (currentNode != null) {
currentNode.setKeyStroke(shortcutPanel.getKeyStroke());
}
Object o = event.getPath().getLastPathComponent();
if (o instanceof ShortcutNode) {
currentNode = (ShortcutNode)o;
shortcutPanel.setKeyStroke(currentNode.getKeyStroke());
shortcutPanel.setDefaultKeyStroke
(currentNode.getShortcut().getDefaultKeyStroke());
EnableAction.setComponentEnabled(shortcutPanel, true);
}
else {
currentNode = null;
shortcutPanel.setKeyStroke(null);
shortcutPanel.setDefaultKeyStroke(null);
EnableAction.setComponentEnabled(shortcutPanel, false);
}
}
/**
* @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
*/
public void stateChanged(ChangeEvent e)
{
if (currentNode != null) {
currentNode.setKeyStroke(shortcutPanel.getKeyStroke());
tm.changed(currentNode);
}
}
private class ShortcutNode extends LeafTreeTableNode {
private KeyStroke stroke;
public ShortcutNode(Shortcut shortcut)
{
super(shortcut);
this.stroke = shortcut.getKeyStroke();
}
public void setKeyStroke(KeyStroke keyStroke)
{
this.stroke = keyStroke;
}
public KeyStroke getKeyStroke()
{
return stroke;
}
public Shortcut getShortcut()
{
return (Shortcut)getData();
}
}
private class ShortcutTableModel extends DefaultColumnTreeTableModel {
private Hashtable nodeByCategory = new Hashtable();
public ShortcutTableModel()
{
Column columns[] = new Column[] {
new Column("Action", TreeTableModel.class),
//new Column("Scope", String.class),
new Column("Shortcut", String.class),
};
addColumns(columns);
}
public void add(Shortcut shortcut)
{
DefaultTreeTableNode node = (DefaultTreeTableNode)
nodeByCategory.get(shortcut.getCategory());
if (node == null) {
node = new DefaultTreeTableNode(this, shortcut.getCategory());
nodeByCategory.put(shortcut.getCategory(), node);
add(node);
}
node.add(new ShortcutNode(shortcut));
}
public void changed(ShortcutNode node)
{
DefaultTreeTableNode parent = (DefaultTreeTableNode)
nodeByCategory.get(node.getShortcut().getCategory());
if (parent != null) {
parent.changedChildAt(parent.getIndexOfChildByData(node.getData()));
}
}
public Object getValueAt(Object node, int column)
{
if (node instanceof DefaultTreeTableNode) {
if (column == 0) {
return ((DefaultTreeTableNode)node).getData();
}
}
else if (node instanceof ShortcutNode) {
ShortcutNode sn = (ShortcutNode)node;
switch (column) {
case 0:
return sn.getShortcut().getDescription();
case 1:
return (sn.getKeyStroke() != null)
? KeyStrokePanel.toString(sn.getKeyStroke())
: null;
}
}
return null;
}
}
}
The table below shows all metrics for ShortcutPrefsPanel.java.



