View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.action;
21  
22  import java.io.File;
23  import java.util.Iterator;
24  import java.util.LinkedList;
25  
26  import javax.swing.Action;
27  
28  /***
29   * Provides a helper class for the file actions. All
30   * <code>PasteFileAction</code> should call {@link #addPasteListener} to get
31   * enabled or disabled when actions are pending.
32   *
33   * <p>All calls to this class need to be swing synchronized.
34   *
35   * @see CopyFileAction
36   * @see CutFileAction
37   * @see PasteFileAction
38   */
39  public class FileActionManager {
40      
41      //--- Constant(s) ---
42  
43      /***
44       * The action type for no pending action.
45       */
46      public static final int ACTION_DO_NOTHING = 0;
47  
48      /***
49       * The action type for a copy operation.
50       */
51      public static final int ACTION_COPY = 1;
52  
53      /***
54       * The action type for a move operation.
55       */
56      public static final int ACTION_MOVE = 2;
57  
58      //--- Data field(s) ---
59  
60      private static int action = 0;
61      private static File[] files = null;
62  
63      /***
64       * A list of <code>Action</code> objects.
65       */
66      private static LinkedList pasteActions = new LinkedList();
67  
68      //--- Constructor(s) ---
69  
70      private FileActionManager()
71      {
72      }
73  
74      //--- Method(s) ---
75  
76      /***
77       * Adds a listener. The <code>action</code> object will be enabled, when
78       * an action is pending and disabled after the paste action has been
79       * performed.
80       */
81      public static void addPasteListener(Action action)
82      {
83  	pasteActions.add(action);
84  	action.setEnabled(getFiles() != null);
85      }
86  
87      /***
88       * Returns the pending action.
89       *
90       * @return the action type 
91       */
92      public static int getAction()
93      {
94  	return action;
95      }
96  
97      /***
98       * Returns the pending <code>File</code> objects.
99       */
100     public static File[] getFiles()
101     {
102 	return files;
103     }
104 
105     /***
106      * Sets the <code>File</code> objects and <code>action</code> that a 
107      * subsequent <code>PasteFileAction</code> would perform.
108      */
109     public static void setFiles(File[] files, int action)
110     {
111 	FileActionManager.files = files;
112 	FileActionManager.action = action;
113 
114 	for (Iterator i = pasteActions.iterator(); i.hasNext();) {
115 	    ((PasteFileAction)i.next()).setEnabled(files != null);
116 	}
117     }
118 
119     /***
120      * Cancels the pending action.
121      */
122     public static void unsetFiles()
123     {
124 	setFiles(null, ACTION_DO_NOTHING);
125     }
126 
127 }