ActionHelper.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.action |
![]() |
![]() |
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.action;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.JPopupMenu;
import javax.swing.text.DefaultEditorKit;
import org.xnap.XNap;
import org.xnap.action.ToggleAction;
import org.xnap.gui.StatusBar;
import org.xnap.gui.component.XNapCheckBoxMenuItem;
import org.xnap.gui.component.XNapMenuItem;
import org.xnap.gui.menu.AbstractDynamicMenu;
import org.xnap.gui.util.IconHelper;
import org.xnap.util.launcher.Player;
import org.xnap.util.launcher.PlayerManager;
/**
* This class provides a set of static actions.
*/
public class ActionHelper
{
//--- Constant(s) ---
/**
* Default clipboard copy action that operates on any
* <code>JTextComponent</code>.
*/
public static Action copyAction;
static {
copyAction = new DefaultEditorKit.CopyAction();
copyAction.putValue(Action.NAME, XNap.tr("Copy"));
copyAction.putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Copies selected text to clipboard."));
copyAction.putValue(IconHelper.XNAP_ICON, "editcopy.png");
}
/**
* Default clipboard cut action that operates on any
* <code>JTextComponent</code>.
*/
public static Action cutAction;
static {
cutAction = new DefaultEditorKit.CutAction();
cutAction.putValue(Action.NAME, XNap.tr("Cut"));
cutAction.putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Moves selected text to clipboard."));
cutAction.putValue(IconHelper.XNAP_ICON, "editcut.png");
}
/**
* Default clipboard paste action that operates on any
* <code>JTextComponent</code>.
*/
public static Action pasteAction;
static {
pasteAction = new DefaultEditorKit.PasteAction();
pasteAction.putValue(Action.NAME, XNap.tr("Paste"));
pasteAction.putValue(Action.SHORT_DESCRIPTION,
XNap.tr("Pastes clipboard contents."));
pasteAction.putValue(IconHelper.XNAP_ICON, "editpaste.png");
}
public static Action pluginPreferencesDialogAction;
static {
pluginPreferencesDialogAction = new PluginPreferencesDialogAction();
pluginPreferencesDialogAction.setEnabled(false);
}
//--- Data field(s) ---
//--- Constructor(s) ---
//--- Method(s) ---
// public static boolean shouldEnable(IUser[] users, Class actionClass)
// {
// boolean isSupported = false;
// for (int i = 0; i < users.length; i++) {
// isSupported |= users[i].isActionSupported(actionClass);
// }
// return isSupported;
// }
/**
* Uses the ActionExtractor to extract the common actions from all objects
* in the array and adds them to the AbstractDynamicMenu as temporaries.
*
* @return -1, if nothing was added; the number of actions that were added
* to <code>jm</code>, otherwise
*/
public static int addCommonActions(AbstractDynamicMenu jm,
Object[] object, ActionExtractor ae,
int startIndex)
{
Collection actions = getCommonActions(object, ae);
if (actions != null) {
int index = startIndex;
for (Iterator i = actions.iterator(); i.hasNext();) {
Action action = (Action)i.next();
jm.addTemporary(createMenuItem(action), index);
index++;
}
return index - startIndex;
}
return -1;
}
public static int addCommonActions(AbstractDynamicMenu jm,
Object[] object, ActionExtractor ae)
{
return addCommonActions(jm, object, ae, 0);
}
/**
* Returns a collection of common actions in <code>objects</code>.
*/
public static Collection getCommonActions(Object[] items,
ActionExtractor ae)
{
if (items != null) {
if (items.length == 1) {
Action[] actions = ae.getActions(items[0]);
if (actions != null) {
return Arrays.asList(actions);
}
}
else {
// maps Action items to ActionContainer items
Hashtable containerByAction = new Hashtable();
// create action container
Action[] refActions = ae.getActions(items[0]);
if (refActions != null) {
for (int i = 0; i < refActions.length; i++) {
if (refActions[i] != null) {
containerByAction.put
(refActions[i],
new ActionContainer(refActions[i]));
}
}
}
for (int i = 1; (i < items.length
&& containerByAction.size() > 0); i++) {
Action[] actions = ae.getActions(items[i]);
if (actions != null && actions.length > 0) {
retainAll(containerByAction, actions);
}
else {
containerByAction.clear();
}
}
if (containerByAction.size() > 0) {
// restore order of refActions
LinkedList sorted = new LinkedList();
for (int i = 0; i < refActions.length; i++) {
if (refActions[i] != null
&& containerByAction.containsKey(refActions[i])) {
sorted.add(containerByAction.get(refActions[i]));
}
}
return sorted;
}
}
}
return null;
}
/**
* MenuItem factory.
*/
public static JComponent createMenuItem(Action action)
{
if (action == null) {
return new JPopupMenu.Separator();
}
else if (action instanceof ToggleAction) {
return new XNapCheckBoxMenuItem((ToggleAction)action);
}
else if (action instanceof SubmenuAction) {
return ((SubmenuAction)action).createMenu();
}
else {
return new XNapMenuItem(action);
}
}
/**
* This is O(n^2);
*/
private static void retainAll(Hashtable containerByAction,
Action[] actions)
{
for (Iterator it = containerByAction.keySet().iterator();
it.hasNext();) {
Object key = it.next();
boolean contained = false;
// searches array for action
for (int i = 0; i < actions.length && !contained; i++) {
if (actions[i] != null
&& key.equals(actions[i])
&& actions[i].equals(key)) {
ActionContainer ac
= (ActionContainer)containerByAction.get(key);
ac.add(actions[i]);
contained = true;
}
}
if (!contained) {
// action was not found
it.remove();
}
}
}
public interface ActionExtractor {
Action[] getActions(Object o);
}
/**
* Used by the Play...Actions for enqueueing files.
*
* A thread is used if when array size surpasses 5 elements.
*
* @param files array of files to be enqueued
* @param playFirst play first file
*/
public static void enqueueFiles(final File[] files,
final boolean playFirst)
{
if (files.length < 5) {
enqueue(files, playFirst);
}
else {
Thread t = new Thread("EnqueueThread")
{
public void run()
{
enqueue(files, playFirst);
}
};
t.start();
}
}
private static void enqueue(File[] files, boolean playFirst)
{
Player player = PlayerManager.getInstance().getDefaultPlayer();
if (player == null) {
StatusBar.setText(XNap.tr("Could not launch player"));
return;
}
for (int i = 0; i < files.length; i++) {
try {
if (files[i] != null && files[i].length() > 0
&& player.canPlay(files[i])) {
if (playFirst) {
player.open(files[i]);
playFirst = false;
}
else {
player.enqueue(files[i]);
}
}
}
catch (IOException e) {
}
}
}
public static boolean perform(Action[] actions, ActionEvent event, Class requiredInterface)
{
if (actions != null) {
for (int i = 0; i < actions.length; i++) {
if (actions[i] != null
&& actions[i].isEnabled()
&& requiredInterface.isInstance(actions[i])) {
actions[i].actionPerformed(event);
return true;
}
}
}
return false;
}
}
The table below shows all metrics for ActionHelper.java.




