📄 sharepanel.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.reflect.InvocationTargetException;
import java.io.File;
import java.io.IOException;
import net.jxta.peergroup.NetPeerGroupFactory;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.platform.NetworkManager;
import net.jxta.exception.PeerGroupException;
import net.jxta.impl.peergroup.Platform;
import net.jxta.impl.peergroup.GenericPeerGroup;
import net.jxta.share.*;
import net.jxta.share.metadata.*;
public class SharePanel extends JPanel
{
private CMS cms = null;
public SharePanel(PeerGroup netPeerGroup)
{
startJxta(netPeerGroup);
cms.addSearchListener(new MySearchListener()); //搜索监听
ShareSubPanel myPanel= new ShareSubPanel();
this.setLayout(new BorderLayout());
this.add(myPanel,java.awt.BorderLayout.CENTER);
myPanel.setVisible(true);
}
//初始化cms
private void startJxta(PeerGroup netPeerGroup)
{
try
{
cms = new CMS();
cms.init(netPeerGroup, null, null);
String homedir = System.getProperty("JXTA_HOME");
homedir = (homedir != null) ? homedir + "ShareDemo" : "ShareDemo";
//启动CMS,创建一个名为ShareDemo的目录来存储ContentAdvertisement
if(cms.startApp(new File(homedir)) == -1)
{
System.out.println("CMS initialization failed");
System.exit(-1);
}
}
catch ( PeerGroupException e)
{
System.out.println("fatal error : group creation failure");
e.printStackTrace();
System.exit(1);
}
}
//搜索监听的执行
class MySearchListener implements SearchListener
{
//收到list请求时调用该方法
public void queryReceived(String queryString)
{
System.out.println("List request with query \"" + queryString + "\" received.");
}
}
public class ShareSubPanel extends JPanel implements ActionListener
{
JFileChooser fc = new JFileChooser(new File("."));
JButton shareBtn= new JButton("添加");
JButton deleteBtn=new JButton("删除");
JButton clearBtn=new JButton("清空");
JButton showall=new JButton("显示所有");
List fileList= new java.awt.List();
public ShareSubPanel()
{
this.setVisible(true);
this.setBackground(Color.white);
this.setLayout(null);
this.add(shareBtn);
this.add(deleteBtn);
this.add(clearBtn);
this.add(fileList);
this.add(showall);
shareBtn.setBounds(375, 10, 70, 27);
deleteBtn.setBounds(375,47,70,27);
clearBtn.setBounds(375,84,70,27);
showall.setBounds(150,235,100,27);
fileList.setBounds(10,10,350,200);
shareBtn.addActionListener(this);
deleteBtn.addActionListener(this);
clearBtn.addActionListener(this);
showall.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==shareBtn) //添加事件
{
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
System.out.println("共享文件:" + file.getName());
try
{
cms.getContentManager().share(file);
//共享该文件并自动为其创建ContentAdvertisement
//ContentAdvertisement包含了在一个对等组中共享内容的信息
fileList.add(file.getPath());
//更新共享内容
} catch (IOException ex)
{
System.out.println("共享失败");
}
}
else
{
System.out.println("取消共享");
}
}
if(e.getSource()==deleteBtn) //删除事件
{
try {
cms.getContentManager().unshare(cms.getContentManager().getContent() [fileList.getSelectedIndex()]);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
fileList.remove(fileList.getSelectedIndex());
}
if(e.getSource()==clearBtn) //清空事件
{
fileList.removeAll();
Content[] tempContent= cms.getContentManager().getContent();
for(int i=0;i<tempContent.length;i++)
{
try {
cms.getContentManager().unshare(tempContent[i]);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
if(e.getSource()==showall) //显示所有事件
{
updateLocalFiles();
}
}
//刷新共享文件列表
private void updateLocalFiles()
{
Content[] content = cms.getContentManager().getContent();
//重新获得该对等点共享的所有内容
fileList.removeAll(); //清空列表显示
for (int i=0; i<content.length; i++) //将共享内容重新写入列表
{
fileList.add(content[i].getContentAdvertisement().getName());
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -