GiFTPlugin.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.plugin.gift |
![]() |
![]() |
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.plugin.gift;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.swing.Icon;
import javax.swing.*;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.XNapFacade;
import org.xnap.gui.AbstractPreferencesDialog;
import org.xnap.gui.DefaultPrefsWizardDialog;
import org.xnap.gui.StatusBar;
import org.xnap.gui.XNapFrame;
import org.xnap.gui.component.XNapMenu;
import org.xnap.gui.component.XNapMenuItem;
import org.xnap.gui.event.AbstractPreferencesDialogListener;
import org.xnap.gui.util.IconHelper;
import org.xnap.plugin.AbstractPlugin;
import org.xnap.plugin.gift.action.ConnectToGiFTAction;
import org.xnap.plugin.gift.action.DisconnectFromGiFTAction;
import org.xnap.plugin.gift.action.ShowPreferencesAction;
import org.xnap.plugin.gift.action.ShutdownGiFTAction;
import org.xnap.plugin.gift.action.StartGiFTAction;
import org.xnap.plugin.gift.action.UpdateGiFTStatsAction;
import org.xnap.plugin.gift.gui.GiFTDaemonPanel;
import org.xnap.plugin.gift.gui.GiFTPreferencesPanel;
import org.xnap.plugin.gift.gui.GiFTStatusPanel;
import org.xnap.plugin.gift.net.GiFTDaemon;
import org.xnap.plugin.gift.net.GiFTDaemonListener;
import org.xnap.plugin.gift.net.GiFTSearch;
import org.xnap.plugin.gift.net.event.ErrorEvent;
import org.xnap.plugin.gift.net.event.OfflineEvent;
import org.xnap.plugin.gift.net.event.OnlineEvent;
import org.xnap.plugin.gift.net.event.StatsEvent;
import org.xnap.plugin.gift.util.GiFTPreferences;
import org.xnap.search.MediaType;
import org.xnap.search.Search;
import org.xnap.search.SearchFilter;
import org.xnap.search.SearchManager;
import org.xnap.search.SearchProvider;
import org.xnap.util.Preferences;
import org.xnap.util.Scheduler;
/**
* GiFTPlugin
*
* @author $author$
* @version $Revision: 1.3 $
*/
public class GiFTPlugin extends AbstractPlugin
implements GiFTDaemonListener, SearchProvider {
private StartGiFTAction acStart;
private XNapMenu jmGift;
private Process giftProcess;
public static Icon ICON_16
= IconHelper.getIcon("gift.png", 16, false);
//~ Static fields/initializers ---------------------------------------------
private PreferencesDialogListener listener;
private GiFTStatusPanel spGift;
private ShutdownGiFTAction acShutdown;
private DisconnectFromGiFTAction acDisconnect;
private ConnectToGiFTAction acConnect;
private ShowPreferencesAction acConfigure;
private UpdateGiFTStatsAction acUpdate;
private static GiFTPlugin singleton;
private GiFTDaemon daemon;
private boolean connected = false;
//~ Instance fields --------------------------------------------------------
private Logger logger = Logger.getLogger(GiFTPlugin.class);
private GiFTPreferences giftPrefs = GiFTPreferences.getInstance();
private JTabbedPane jtpGift;
private GiFTDaemonPanel jpDaemon;
//~ Constructors -----------------------------------------------------------
/**
* Creates a new GiFTPlugin object.
*/
public GiFTPlugin() {
singleton = this;
acConnect = new ConnectToGiFTAction();
acDisconnect = new DisconnectFromGiFTAction();
acShutdown = new ShutdownGiFTAction();
acUpdate = new UpdateGiFTStatsAction();
acConfigure = new ShowPreferencesAction();
acStart = new StartGiFTAction();
}
//~ Methods ----------------------------------------------------------------
/**
* Singleton getInstance
*
* @return GiFTPlugin
*/
public static GiFTPlugin getInstance() {
return singleton;
}
/**
* @see xnap.plugin.gift.net.event.listener.NetworkEventListener#attached(xnap.plugin.gift.net.event.OnlineEvent)
*/
public void attached(OnlineEvent evt) {
SearchManager.getInstance().add(this);
acConnect.setEnabled(false);
acDisconnect.setEnabled(true);
if (giftPrefs.getStartDaemon())
acShutdown.setEnabled(true);
else acShutdown.setEnabled(false);
acUpdate.setEnabled(true);
}
/**
* @see xnap.plugin.gift.net.event.listener.NetworkEventListener#detached(xnap.plugin.gift.net.event.OfflineEvent)
*/
public void detached(OfflineEvent evt) {
SearchManager.getInstance().remove(this);
acConnect.setEnabled(true);
acDisconnect.setEnabled(false);
acShutdown.setEnabled(false);
acUpdate.setEnabled(false);
}
/**
* @see xnap.plugin.gift.net.event.listener.ErrorEventListener#onError(xnap.plugin.gift.net.event.ErrorEvent)
*/
public void onError(ErrorEvent evt) {
logger.debug(evt.getErrorMessage(), evt.getException());
}
/**
* @see xnap.plugin.INetworkPlugin#search(xnap.util.SearchFilter, int)
*/
public Search search(SearchFilter filter) {
GiFTSearch search = new GiFTSearch(filter, daemon);
return search;
}
/**
* @see xnap.plugin.IPlugin#start()
*/
public void start() {
singleton = this;
startGiftConnection();
}
public void startGiftConnection() {
acStart.setEnabled(false);
acShutdown.setEnabled(false);
acConnect.setEnabled(true);
String command = giftPrefs.getGiftDaemon();
if (giftPrefs.getStartDaemon() && command.length() > 0) {
String[] args = new String[] { command, "-d" };
try {
logger.debug("launching giFT daemon");
giftProcess = Runtime.getRuntime().exec
(args, null, (new File(command)).getParentFile());
acStart.setEnabled(false);
acShutdown.setEnabled(true);
}
catch (IOException ie) {
logger.debug("Could not launch giFT daemon", ie);
}
}
daemon = new GiFTDaemon(giftPrefs.getGiftHost(),
giftPrefs.getDaemonPort(),
Preferences.getInstance().getUsername());
daemon.addDaemonListener(this);
// start core after 5 seconds delay
if (giftPrefs.getAutoconnect()) {
Scheduler.run(5 * 1000, new Runnable() {
public void run()
{
daemon.start();
}
});
}
}
public void stopGiftConnection() {
if (giftProcess != null) {
daemon.stop(true);
Scheduler.run(5 * 1000, new Runnable() {
public void run()
{
if (giftProcess != null) {
giftProcess.destroy();
}
giftProcess = null;
}
});
} else {
daemon.stop(false);
}
acShutdown.setEnabled(false);
acConnect.setEnabled(false);
String command = giftPrefs.getGiftDaemon();
if (giftPrefs.getStartDaemon() && command.length() > 0)
acStart.setEnabled(true);
}
/**
* @see xnap.plugin.gift.net.event.listener.NetworkEventListener#statsUpdate(xnap.plugin.gift.net.event.StatsEvent)
*/
public void statsUpdate(StatsEvent evt) {
}
/**
* @see xnap.plugin.IPlugin#stop()
*/
public void stop() {
stopGiftConnection();
// we can not cleanly unload the plugin...
//singleton = null;
}
/**
* @see xnap.search.SearchProvider#getName()
*/
public String getName() {
return "giFT";
}
/**
* @see xnap.search.SearchProvider#getSupportedMediaTypes()
*/
public MediaType[] getSupportedMediaTypes() {
return new MediaType[] {SearchManager.MEDIA_ANYTHING};
}
/**
* @see xnap.plugin.Plugin#startGUI()
*/
public void startGUI() {
spGift = new GiFTStatusPanel(daemon);
daemon.addDaemonListener(spGift);
StatusBar.getInstance().addComponent(spGift);
listener = new PreferencesDialogListener();
XNapFacade.addPluginPreferencesDialogListener(listener);
initializeMenu();
XNapFrame.getInstance().getMainMenuBar().addPluginMenu(jmGift);
jtpGift = new JTabbedPane();
jpDaemon = new GiFTDaemonPanel(daemon);
daemon.addDaemonListener(jpDaemon);
jtpGift.addTab(XNap.tr("Daemon"), jpDaemon);
XNapFrame.getInstance().insertTab("giFT", IconHelper.getListIcon("gift.png"), jtpGift);
/* show prefs panel in dialog when plugin is started for the first
time. */
if (!giftPrefs.getBoolean("seenStartupWizard")) {
showPrefsDialog();
}
}
/**
* @see xnap.plugin.Plugin#stopGUI()
*/
public void stopGUI() {
XNapFrame.getInstance().getMainMenuBar().removePluginMenu(jmGift);
jmGift = null;
daemon.removeDaemonListener(spGift);
StatusBar.getInstance().removeComponent(spGift);
spGift = null;
daemon.removeDaemonListener(jpDaemon);
listener.dispose();
XNapFacade.removePluginPreferencesDialogListener(listener);
listener = null;
XNapFrame.getInstance().removeTab(jtpGift);
jtpGift = null;
}
private void initializeMenu()
{
jmGift = new XNapMenu(getName());
jmGift.setIcon(IconHelper.getMenuIcon("gift.png"));
jmGift.add(new XNapMenuItem(acConnect));
jmGift.add(new XNapMenuItem(acDisconnect));
jmGift.add(new XNapMenuItem(acUpdate));
jmGift.addSeparator();
jmGift.add(new XNapMenuItem(acStart));
jmGift.add(new XNapMenuItem(acShutdown));
jmGift.addSeparator();
jmGift.add(new XNapMenuItem(acConfigure));
}
public void showPrefsDialog()
{
DefaultPrefsWizardDialog dfd
= new DefaultPrefsWizardDialog(giftPrefs, "seenStartupWizard");
dfd.addPanel(new GiFTPreferencesPanel());
dfd.pack();
dfd.show(XNapFrame.getInstance());
}
/**
*
*/
public ConnectToGiFTAction getConnectAction()
{
return acConnect;
}
public GiFTDaemon getDaemon()
{
return daemon;
}
/**
*
*/
public DisconnectFromGiFTAction getDisconnectAction()
{
return acDisconnect;
}
/**
*
*/
public ShutdownGiFTAction getShutdownAction()
{
return acShutdown;
}
/**
*
*/
public StartGiFTAction getStartAction()
{
return acStart;
}
/**
*
*/
public UpdateGiFTStatsAction getUpdateAction()
{
return acUpdate;
}
private class PreferencesDialogListener
extends AbstractPreferencesDialogListener
{
public void addPanels(AbstractPreferencesDialog dialog, List panels)
{
GiFTPreferencesPanel jpp = new GiFTPreferencesPanel();
panels.add(jpp);
panels.add(dialog.addPanel(jpp, "gift.png"));
}
}
/**
* @see org.xnap.plugin.gift.net.GiFTDaemonListener#statusChanged(org.xnap.plugin.gift.net.GiFTDaemon, java.lang.String)
*/
public synchronized void statusChanged(final GiFTDaemon daemon)
{
if (daemon.isConnected()) {
if (!connected) {
connected = true;
SearchManager.getInstance().add(this);
}
}
else {
if (connected) {
connected = false;
SearchManager.getInstance().remove(this);
}
}
Runnable runner = new Runnable()
{
public void run()
{
acConnect.setEnabled(daemon.isDisconnected());
acDisconnect.setEnabled(daemon.isConnected());
}
};
SwingUtilities.invokeLater(runner);
}
}
The table below shows all metrics for GiFTPlugin.java.




