import com.acuilab.bchelper.main.dao.NodeDao;
import com.acuilab.bchelper.main.node.NodeListTopComponent;
import java.awt.event.ActionEvent;
import java.sql.SQLException;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.actions.Presenter;
import org.openide.util.lookup.Lookups;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
/**
*
* @author chia2
*/
public class NetworkChildren extends Children.Keys<Network> {
@Override
protected Node[] createNodes(Network key) {
return new Node[] { new NetworkNode(key, Lookups.singleton(key)) };
}
@Override
protected void addNotify() {
super.addNotify();
setKeys(NetworkManager.getDefault().getNetworkList());
}
private class NetworkNode extends AbstractNode {
private final Network key;
public NetworkNode(Network key, Lookup lookup) {
super(Children.LEAF, lookup);
setDisplayName(key.getSymbol());
setIconBaseWithExtension(key.getIconBaseWithExtension());
this.key = key;
}
@Override
public Action[] getActions(boolean context) {
Action selectNodeAction = new SelectNodeAction("选择节点", key);
Action manageNodeAction = new AbstractAction("管理节点...") {
@Override
public void actionPerformed(ActionEvent e) {
TopComponent tc = WindowManager.getDefault().findTopComponent(NodeListTopComponent.getId(key.getSymbol(), key.getNodeTool().getId()));
if(tc == null) {
tc = new NodeListTopComponent(key, key.getNodeTool());
}
tc.open();
tc.requestActive();
}
};
Action newWalletAction = new AbstractAction("创建钱包...") {
@Override
public void actionPerformed(ActionEvent e) {
// WalletListTopComponent tc = (WalletListTopComponent)WindowManager.getDefault().findTopComponent("WalletListTopComponent");
// if(tc != null) {
// tc.newWallet(key);
// }
}
};
newWalletAction.setEnabled(true);
Action importWalletAction = new AbstractAction("导入钱包...") {
@Override
public void actionPerformed(ActionEvent e) {
// WalletListTopComponent tc = (WalletListTopComponent)WindowManager.getDefault().findTopComponent("WalletListTopComponent");
// if(tc != null) {
// tc.importWallet(key);
// }
}
};
importWalletAction.setEnabled(true);
return new Action[] {
selectNodeAction,
manageNodeAction,
null,
newWalletAction,
importWalletAction};
}
}
private class SelectNodeAction extends AbstractAction implements Presenter.Popup {
private final Network key;
public SelectNodeAction(String name, Network key) {
super(name);
this.key = key;
}
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public JMenuItem getPopupPresenter() {
JMenu menu = new JMenu(this);
try {
List<com.acuilab.bchelper.main.node.Node> nodes = NodeDao.getByNetworkSymbol(key.getSymbol());
// ButtonGroup for radio buttons
ButtonGroup btnGroup = new ButtonGroup();
for(com.acuilab.bchelper.main.node.Node node : nodes) {
JRadioButtonMenuItem mi = new JRadioButtonMenuItem(node.getName(), node.isSelected());
mi.addActionListener((ActionEvent e) -> {
try {
// ①切换节点
key.setNode(node.getNodeInfo());
// ②重新设置当前使用的节点
NodeDao.setSelected(node.getNetworkSymbol(), node.getId());
// ③重新加载对应的结点列表
NodeListTopComponent tc = (NodeListTopComponent)WindowManager.getDefault().findTopComponent(NodeListTopComponent.getId(key.getSymbol(), key.getNodeTool().getId()));
if(tc != null) {
tc.reloadNodeList();
}
} catch (SQLException ex) {
Exceptions.printStackTrace(ex);
}
});
menu.add(mi);
btnGroup.add(mi);
}
} catch (SQLException ex) {
Exceptions.printStackTrace(ex);
}
return menu;
}
}
}