XNapFrame.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui |
![]() |
![]() |
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;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.*;
import javax.help.HelpBroker;
import javax.swing.Action;
import javax.swing.*;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.action.ToggleAction;
import org.xnap.gui.component.ToggleableIconPane;
import org.xnap.gui.component.XNapToolBarButton;
import org.xnap.gui.component.XNapToolBarToggleButton;
import org.xnap.gui.theme.ThemeManager;
import org.xnap.gui.util.HelpManager;
import org.xnap.gui.util.*;
import org.xnap.io.Library;
import org.xnap.plugin.PluginManager;
import org.xnap.util.Preferences;
import org.xnap.util.SystemHelper;
import org.xnap.util.Updater;
import ziga.gui.WindowsDesktopIndicator;
public class XNapFrame extends JFrame
implements ChangeListener, PropertyChangeListener
{
//--- Constant(s) ---
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(XNapFrame.class);
private static Preferences prefs = Preferences.getInstance();
private static XNapFrame singleton = null;
private ToggleableIconPane pane;
private JSplitPane jspV;
private MainToolBar jtbMain;
private ChatPanel jpChat;
private HotlistPanel jpHotlist;
private LibraryPanel jpLibrary;
private SearchPanel jpSearch;
private TransferPanel jpTransfer;
private WindowsDesktopIndicator wdi;
//private HotlistDockAction acDockHotlist;
//--- Constructor(s) ---
public XNapFrame()
{
singleton = this;
setContentPane(new JPanel());
// install support for system tray icon on Windows
try {
SplashWindow.incProgress(1, XNap.tr("Enabling Window system tray icon"));
if (SystemHelper.isZigaDllLoaded) {
wdi = new WindowsDesktopIndicator(this);
wdi.show("xnap.ico", XNap.tr("XNap"));
wdi.hide();
}
}
catch (Exception e) {
logger.warn("Could not enable system tray through ziga.dll", e);
}
// register native mac os x support for application menu
if (SystemHelper.isMacOSX) {
SplashWindow.incProgress(1, XNap.tr("Registering Mac OS handler"));
try {
org.xnap.platform.macos.MacOS141Handler.register();
}
catch (Exception e) {
logger.warn("Could not register MacOSX 1.4.1 handler", e);
try {
org.xnap.platform.macos.MacOSHandler.register();
}
catch (Exception e2) {
logger.warn("could not register MacOSX handler", e2);
}
}
}
// swing initialization
ToolTipManager.sharedInstance().setDismissDelay(60 * 1000);
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
// moved to XNap.java
// SplashWindow.incProgress(5, XNap.tr("Setting up Look & Feel"));
// ThemeManager.initialize();
// updateLookAndFeel();
// wizard
if (!prefs.getSeenStartupWizard()) {
SplashWindow.incProgress(5, XNap.tr("Loading Wizard"));
StartupWizardDialog.showDialog(this, true);
}
initialize();
SplashWindow.incProgress(5, XNap.tr("Enabling plugins"));
PluginManager.getInstance().guiStarted();
Updater.guiStarted();
// show update notification
// if (prefs.getAutoVersionCheck() && prefs.shouldCheckForUpdate(7)) {
// UpdateDialogAction.doit(false);
// }
SplashWindow.setProgress(100, XNap.tr("Done"));
}
//--- Method(s) ---
private void initialize()
{
SplashWindow.incProgress(5, XNap.tr("Setting up Toolbars"));
initializeToolbars();
// the toolbars need to be setup before the menu for the
// Show...ToolBar actions to work
SplashWindow.incProgress(5, XNap.tr("Setting up Menu"));
initializeMenu();
SplashWindow.incProgress(5, XNap.tr("Setting up GUI"));
initializePanels();
SplashWindow.incProgress(5, XNap.tr("Setting up Status Bar"));
initializeStatusBar();
SplashWindow.incProgress(5, XNap.tr("Setting up Application"));
setTitle(XNap.tr("XNap - Java Peer-To-Peer Client"));
ImageIcon icon = IconHelper.getImage("16/xnap.png");
if (icon != null) {
setIconImage(icon.getImage());
}
SplashWindow.incProgress(5, XNap.tr("Setting up Hotlist"));
jpHotlist.add(new UploadSettingsPanel(), BorderLayout.SOUTH);
jspV = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
jspV.setBorder(GUIHelper.createEmptyBorder());
jspV.setDividerLocation(prefs.getHotlistDividerLocation());
jspV.setDividerSize(1);
// always resize pane
jspV.setResizeWeight(1.0);
jspV.add(pane);
jspV.add(jpHotlist);
SplashWindow.incProgress(5, XNap.tr("Setting up Main Window"));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(jspV, BorderLayout.CENTER);
getContentPane().add(jtbMain, BorderLayout.NORTH);
getContentPane().add(StatusBar.getInstance(), BorderLayout.SOUTH);
pack();
setBounds(prefs.getWindowX(), prefs.getWindowY(),
prefs.getWindowWidth(), prefs.getWindowHeight());
// close app if someone clicks on X
addWindowListener(new WindowListener());
HelpManager.enableHelpKeys(getRootPane(), "introduction", null);
// listen to a few properties
prefs.addPropertyChangeListener(this);
}
private void initializeMenu()
{
setJMenuBar(new MainMenuBar());
}
private void initializePanels()
{
// tabbed pane / icon split pane
pane = new ToggleableIconPane(prefs.getUseTabbedPane());
pane.addChangeListener(this);
updatePaneBorder();
// search
jpSearch = new SearchPanel();
jpSearch.setName("search");
insertTab(XNap.tr("Search"), IconHelper.getListIcon("find.png"),
jpSearch, 0);
SplashWindow.incProgress(5);
// transfer
jpTransfer = new TransferPanel();
jpTransfer.setName("transfer");
insertTab(XNap.tr("Transfer"),
IconHelper.getListIcon("connect_established.png"),
jpTransfer, 1);
SplashWindow.incProgress(5, XNap.tr("Setting up Chat Panel"));
// chat
updateChatPanel();
SplashWindow.incProgress(5, XNap.tr("Setting up Library Panel"));
// library
updateLibraryPanel();
// hotlist
jpHotlist = new HotlistPanel();
jpHotlist.setBorder(GUIHelper.createRaisedBorder());
jpHotlist.setVisible(prefs.getBoolean("showHotlist"));
}
private void initializeStatusBar()
{
JLabel jlPresence = new PresenceStatusPanel();
jlPresence.setName(XNap.tr("Presence"));
StatusBar.getInstance().appendComponent(jlPresence);
StatusPanel spLibrary = new StatusPanel();
spLibrary.setIcon(IconHelper.getStatusBarIcon("contents.png"));
spLibrary.setName(XNap.tr("Library"));
Library.getInstance().setStatusListener(spLibrary);
StatusBar.getInstance().appendComponent(spLibrary);
}
private void initializeToolbars()
{
// main toolbar
jtbMain = new MainToolBar();
jtbMain.setVisible(prefs.getBoolean("showMainToolbar"));
}
/**
* Kills the instance of XNapFrame.
*/
public static void disposeInstance()
{
Library.getInstance().setStatusListener(null);
if (singleton.wdi != null) {
singleton.wdi.hide();
Thread.yield();
}
singleton.dispose();
singleton = null;
}
/**
* Inserts a tab at <code>index</code>.
*/
public void insertTab(String title, Icon icon, JComponent component,
int index)
{
pane.insertTab(title, icon, component, index);
if (component instanceof JTabbedPane) {
((JTabbedPane)component).addChangeListener(this);
}
}
/**
* Adds a tab above the chat panel.
*/
public void insertTab(String title, Icon icon, JComponent component)
{
insertTab(title, icon, component, pane.getTabCount());
}
/**
* Removes <code>component</code>.
*/
public void removeTab(Component component)
{
int index = pane.indexOfComponent(component);
if (index != -1) {
pane.remove(component);
if (component instanceof JTabbedPane) {
((JTabbedPane)component).removeChangeListener(this);
}
}
}
/**
* Returns the instance of XNapFrame.
*
* @return null, if the gui was not started
*/
public static XNapFrame getInstance()
{
return singleton;
}
/**
* Returns the chat panel.
*/
public ChatPanel getChatPanel()
{
if (jpChat == null) {
jpChat = new ChatPanel();
jpChat.setName("chat");
jpChat.addChangeListener(this);
}
return jpChat;
}
/**
* Returns the library panel.
*/
public LibraryPanel getLibraryPanel()
{
if (jpLibrary == null) {
jpLibrary = new LibraryPanel();
jpLibrary.setName("library");
}
return jpLibrary;
}
/**
* Returns the hotlist panel.
*/
public HotlistPanel getHotlistPanel()
{
return jpHotlist;
}
/**
* Returns the main menu bar.
*/
public MainMenuBar getMainMenuBar()
{
return (MainMenuBar)getJMenuBar();
}
/**
* Returns the main tool bar.
*/
public MainToolBar getMainToolBar()
{
return jtbMain;
}
/**
* Returns the main pane.
*/
public ToggleableIconPane getPane()
{
return pane;
}
/**
* Returns the search panel.
*/
public SearchPanel getSearchPanel()
{
return jpSearch;
}
/**
* Returns the transfer panel.
*/
public TransferPanel getTransferPanel()
{
return jpTransfer;
}
/**
* Invoked by {@link XNap#startGUI(boolean)} after gui is
* visible. Checks if important preferences like directory
* settings are valid.
*/
public void guiVisible()
{
boolean showPrefs = false;
File f = new File(prefs.getDownloadDir());
if (!f.isDirectory() && !f.mkdirs()) {
Dialogs.error
(this,
XNap.tr("Could not create download directory."));
showPrefs = true;
}
f = new File(prefs.getIncompleteDir());
if (!f.isDirectory() && !f.mkdirs()) {
Dialogs.error
(this,
XNap.tr("Could not create incomplete directory."));
showPrefs = true;
}
if (showPrefs) {
PreferencesDialog.showDialog(this);
}
}
public void chatBlink()
{
if (pane.getSelectedComponent() != jpChat) {
int i = pane.indexOfComponent(jpChat);
if (i != -1) {
pane.blink(i, IconHelper.getListIcon("penguin.png"));
}
}
}
public void propertyChange(PropertyChangeEvent e)
{
String p = e.getPropertyName();
if (p.equals("lookAndFeel") || p.equals("theme")) {
// updating the look and feel seems to break when using
// the IconSplitPane view
pane.setTabbed(true);
updateLookAndFeel();
// reset tabbed pane, see above
pane.setTabbed(prefs.getUseTabbedPane());
}
else if (p.equals("useTabbedPane")) {
pane.setTabbed(prefs.getUseTabbedPane());
}
else if (p.equals("showHotlist")) {
if (jpHotlist.isVisible()) {
prefs.setHotlistDividerLocation(jspV.getDividerLocation());
}
else {
jspV.setDividerLocation(prefs.getHotlistDividerLocation());
}
jpHotlist.setVisible(prefs.getBoolean("showHotlist"));
if (jpHotlist.getWidth()
< jpHotlist.getMinimumSize().getWidth()) {
jspV.setDividerLocation
((int)(jspV.getWidth() - jpHotlist.getMinimumSize().getWidth()));
}
}
else if (p.equals("showChatPanel")) {
updateChatPanel();
}
else if (p.equals("showLibraryPanel")) {
updateLibraryPanel();
}
else if (p.equals("showMainToolbar")) {
jtbMain.setVisible(prefs.getBoolean("showMainToolbar"));
}
}
/**
* Cleanly exits XNap.
*
* @param showCloseDialog if true, a confirmation dialog will be shown
* @return false, if exit was cancelled by user
*/
public boolean exit(boolean showCloseDialog)
{
if (showCloseDialog && !Dialogs.showCloseDialog(this)) {
return false;
}
XNap.exit();
return true;
}
/**
* Cleanly exits the application. Prompts the user for confirmation if
* confirmation is turned on.
*
* @return false, if exit was cancelled by user
* @see #exit(boolean)
*/
public boolean exit()
{
return exit(Dialogs.getShowDialog("Close"));
}
public void stop()
{
PluginManager.getInstance().guiStopped();
// save gui prefs
jpSearch.savePrefs();
jpTransfer.savePrefs();
if (jpChat != null) {
jpChat.savePrefs();
}
if (jpLibrary != null) {
jpLibrary.savePrefs();
}
// save hotlist layout
if (jpHotlist.isVisible()) {
prefs.setHotlistDividerLocation(jspV.getDividerLocation());
}
// save window positions
// it seems that XNap shrinks on every launch
prefs.setWindowHeight(getBounds().getSize().height
+ prefs.getCorrectivePixels());
prefs.setWindowWidth(getBounds().getSize().width);
prefs.setWindowX(getBounds().getLocation().x);
prefs.setWindowY(getBounds().getLocation().y);
disposeInstance();
}
/**
* Changes the icons on the main toolbar if the panel is switched.
*/
public void stateChanged(ChangeEvent e)
{
Object source = pane.getSelectedComponent();
// remove previous panel icons
getMainToolBar().removeAllTemporaries();
if (source instanceof JTabbedPane) {
if (((JTabbedPane)source).getSelectedComponent() != null) {
source = ((JTabbedPane)source).getSelectedComponent();
}
}
else if (source == jpChat) {
if (pane.getSelectedComponent() != jpChat) {
return;
}
// FIX: the source of the event is set to jpChat instead of
// jtp (which is a member of jpChat). Java bug?
source = jpChat.getSelectedTab();
}
if (source instanceof ActionProvider) {
Action[] actions = ((ActionProvider)source).getActions();
if (actions != null) {
for (int i = 0; i < actions.length; i++) {
if (actions[i] != null) {
if (actions[i] instanceof ToggleAction) {
getMainToolBar().addTemporary
(new XNapToolBarToggleButton
((ToggleAction)actions[i]));
}
else {
getMainToolBar().addTemporary
(new XNapToolBarButton(actions[i]));
}
}
else {
JSeparator js = new JToolBar.Separator();
js.setOrientation(SwingConstants.VERTICAL);
getMainToolBar().addTemporary(js);
}
}
}
}
// redraw
getMainToolBar().updateUI();
}
private void updateChatPanel()
{
if (prefs.getBoolean("showChatPanel")) {
insertTab(XNap.tr("Chat"),
IconHelper.getListIcon("mail_generic2.png"),
getChatPanel(), 2);
}
else if (jpChat != null) {
pane.remove(jpChat);
}
}
private void updateLibraryPanel()
{
if (prefs.getBoolean("showLibraryPanel")) {
boolean chatVisible
= (jpChat != null && pane.indexOfComponent(jpChat) != -1);
insertTab(XNap.tr("Library"),
IconHelper.getListIcon("contents.png"),
getLibraryPanel(), chatVisible ? 3 : 2);
}
else if (jpLibrary != null) {
pane.remove(jpLibrary);
}
}
/**
* Switches the look and feel. */
public static void updateLookAndFeel()
{
ThemeManager.setTheme(prefs.getIconTheme());
if (!ThemeManager.setTheme(prefs.getTheme())) {
ThemeManager.setTheme(Preferences.DEFAULT_THEME);
}
try {
Class.forName(prefs.getLookAndFeel());
UIManager.setLookAndFeel(prefs.getLookAndFeel());
updatePaneBorder();
}
catch (Exception e) {
logger.warn("could not change look and feel", e);
}
if (getInstance() != null) {
SwingUtilities.updateComponentTreeUI(getInstance());
}
SplashWindow.updateComponentTree();
}
/**
* The jgoodies l&fs use embedded tabs, so we need an extra border.
*/
private static void updatePaneBorder()
{
if (getInstance() != null && getInstance().pane != null) {
getInstance().pane.setBorder
(prefs.getLookAndFeel().startsWith("com.jgoodies.plaf")
? GUIHelper.createRaisedBorder()
: GUIHelper.createEmptyBorder());
}
}
//--- Inner Class(es) ---
/**
* Handles the Windows system tray icon.
*/
private class WindowListener extends WindowAdapter
{
public void windowDeiconified(WindowEvent e)
{
}
public void windowIconified(WindowEvent e)
{
if (wdi != null) {
wdi.show("xnap.ico", XNap.tr("XNap"));
Thread.yield();
wdi.hideFrame();
}
}
public void windowClosing (WindowEvent evt)
{
exit();
}
}
}
The table below shows all metrics for XNapFrame.java.



