📄 sharedemo.java
字号:
/* * Copyright (c) 2001 Sun Microsystems, Inc. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Sun Microsystems, Inc. for Project JXTA." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", * nor may "JXTA" appear in their name, without prior written * permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL SUN MICROSYSTEMS OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of Project JXTA. For more * information on Project JXTA, please see * <http://www.jxta.org/>. * * This license is based on the BSD license adopted by the Apache Foundation. * * $Id: ShareDemo.java,v 1.9 2002/04/20 18:26:02 rmjohnson Exp $ * */package net.jxta.test;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.PeerGroup;import net.jxta.peergroup.PeerGroupFactory;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.*;/** * Simple application that demonstrates how to share a file using CMS. * * @see SearchDemo * @see MetadataShareDemo * @see net.jxta.share.ContentManager * @see net.jxta.share.CMS * @version $Revision: 1.9 $ */public class ShareDemo { private PeerGroup netPeerGroup = null; private CMS cms = null; static public void main(String args[]) { //start ShareDemo new ShareDemo(); } public ShareDemo() { startJxta(); //adds a search listener that monitors incoming list requests cms.addSearchListener(new MySearchListener()); ShareWindow window = new ShareWindow(); window.setVisible(true); } /** * initializes NetPeerGroup and the CMS */ private void startJxta() { try { // create, and Start the default jxta NetPeerGroup netPeerGroup = PeerGroupFactory.newNetPeerGroup(); //uncomment the following line if you want to start the app defined // the NetPeerGroup Advertisement (by default it's the shell) // in this case we want use jxta directly. // netPeerGroup.startApp(null); //instanciate and initialize a content management service for //the NetPeerGroup cms = new CMS(); cms.init(netPeerGroup, null, null); //set up a ShareDemo directory inside the JXTA_HOME directory String homedir = System.getProperty("JXTA_HOME"); homedir = (homedir != null) ? homedir + "ShareDemo" : "ShareDemo"; //start CMS, creating a directory named ShareDemo to store the // ContentAdvertisement cache in. if(cms.startApp(new File(homedir)) == -1) { System.out.println("CMS initialization failed"); System.exit(-1); } } catch ( PeerGroupException e) { // could not instanciate the group, print the stack and exit System.out.println("fatal error : group creation failure"); e.printStackTrace(); System.exit(1); } } /** * A simple implementation of a SearchListener. */ class MySearchListener implements SearchListener { /** * This method is called every time a list request is recieved. This * implementation prints the query string to standard output when it is * called. */ public void queryReceived(String queryString) { System.out.println("List request with query \"" + queryString + "\" received."); } } /** * Inner class that defines the ShareDemo GUI */ public class ShareWindow extends Frame implements ActionListener { JFileChooser fc = new JFileChooser(new File(".")); Button shareButton; List fileList; public ShareWindow() { super("Share Demo"); setSize(450, 250); addWindowListener(new WindowMonitor()); Panel toolbar = new Panel(); toolbar.setLayout(new FlowLayout(FlowLayout.LEFT)); shareButton = new Button("Share"); shareButton.addActionListener(this); toolbar.add(shareButton); add(toolbar, BorderLayout.NORTH); fileList = new java.awt.List(); add(fileList, BorderLayout.CENTER); //immediately fill the list with content that is being shared updateLocalFiles(); } public void actionPerformed(ActionEvent e) { System.out.println(e.getActionCommand()); //handle the event of the "Share" button being clicked if (e.getSource().equals(shareButton)) { //prompt the user to choose a file to share int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //this is where a real application would open the file. System.out.println("Sharing: " + file.getName() + "."); try { //ContentManager.share() will automatically create a //ContentAdvertisement for a file and begin sharing it. //If more control in the construction of the //ContentAdvertisement is needed, there are also //overloaded versions of share() that allow the //advertised name, content type, and other metadata to //in the advertisement to be specified. cms.getContentManager().share(file); //update the list of shared content updateLocalFiles(); } catch (IOException ex) { System.out.println("Share command failed."); } } else { System.out.println("Share command cancelled by user."); } } } /** * Refreshes the list of shared content */ private void updateLocalFiles() { //ContentManager.getContent() retrieves all of the content that is // being shared by this peer. Content[] content = cms.getContentManager().getContent(); //erase the list of shared content... fileList.removeAll(); //...and repopulate it for (int i=0; i<content.length; i++) { fileList.add(content[i].getContentAdvertisement().getName()); } } } /** * A window adapter to take care of cleanup */ class WindowMonitor extends WindowAdapter { public void windowClosing(WindowEvent e) { Window w = e.getWindow(); w.setVisible(false); w.dispose(); System.exit(0); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -