KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > group > CreateGroupAction


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.group;
22
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30 import org.openide.ErrorManager;
31 import org.openide.awt.Mnemonics;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileStateInvalidException;
34 import org.openide.loaders.DataFolder;
35 import org.openide.loaders.DataObject;
36 import org.openide.nodes.Node;
37 import org.openide.nodes.NodeAcceptor;
38 import org.openide.loaders.DataFilter;
39 import org.openide.loaders.RepositoryNodeFactory;
40 import org.openide.nodes.NodeOperation;
41 import org.openide.util.HelpCtx;
42 import org.openide.util.NbBundle;
43 import org.openide.util.UserCancelException;
44 import org.openide.util.actions.CookieAction;
45
46
47 /**
48  * Creates group of data objects, i.e group data object, known
49  * as {@link GroupShadow}.
50  *
51  * @author Martin Ryzl
52  * @see GroupShadow
53  */

54 public class CreateGroupAction extends CookieAction {
55
56     /** Generated serail veriosn UID. */
57     static final long serialVersionUID =-280394671195477993L;
58     
59     
60     /** Performs action based on activated nodes. Implements superclass abstract method. */
61     protected void performAction(final Node[] nodes) {
62         try {
63             FileObject fo = selectFile();
64             ArrayList JavaDoc list;
65
66             if (fo != null) {
67                 list = new ArrayList JavaDoc();
68                 for(int i = 0; i < nodes.length; i++) {
69                     Object JavaDoc obj = nodes[i].getCookie(DataObject.class);
70                     if (obj != null) {
71                         list.add(GroupShadow.getLinkName(((DataObject)obj).getPrimaryFile()));
72                     }
73                 }
74                 GroupShadow.writeLinks(list, fo);
75             }
76         } catch(IOException JavaDoc ex) {
77             ErrorManager.getDefault().notify(ex);
78         }
79     }
80
81     /** Gets icon resource. Overrides suprclass method. */
82     protected String JavaDoc iconResource () {
83         return "org/netbeans/modules/group/resources/groupShadow.gif"; // NOI18N
84
}
85
86     /** Gets cookies which should contain nodes to enable the action. Implements superclass abstract method.
87      * @return <code>DataObject</code>.class as cookie */

88     protected Class JavaDoc[] cookieClasses() {
89         return new Class JavaDoc[] { DataObject.class };
90     }
91
92     /** Call super.enable and when it returns true,
93      * test if activated nodes are not on default file system. If yes return false.
94      * @return true when activated nodes are not on default filesystem and super class class is also true.
95      */

96     protected boolean enable (Node[] activatedNodes) {
97         boolean enable = super.enable (activatedNodes);
98
99         if ( enable ) { // if super call enable this action, test it more to default file system ...
100
// Fix #14740 disable action on SystemFileSystem.
101
for (int i = 0; i < activatedNodes.length; i++) {
102                 DataObject dataObject = (DataObject)activatedNodes[i].getCookie (DataObject.class);
103                 FileObject fileObject = dataObject.getPrimaryFile();
104                 try {
105                     if ( fileObject.isRoot() || fileObject.getFileSystem().isDefault() ) {
106                         enable = false;
107                         break;
108                     }
109                 } catch (FileStateInvalidException fsie) {
110                 }
111             } // for
112
}
113         
114         return enable;
115     }
116
117     /** Get mode telling which cookies have to be present the action to enable. Implements superclass abstract method.
118      * @return MODE_ALL */

119     protected int mode() {
120         return MODE_ALL;
121     }
122
123     /** Gets name of action. Implements superclass abstract method. */
124     public String JavaDoc getName() {
125         return NbBundle.getBundle(CreateGroupAction.class).getString("CreateGroup");
126     }
127
128     /** Gets help context for this action. Implements superclass abstract method. */
129     public HelpCtx getHelpCtx() {
130         return new HelpCtx(CreateGroupAction.class);
131     }
132
133     /** Lets the user to select a group shadow. Utilitiy method.
134      * @return <code>FileObject</code> for the filesystem.
135      * @exception IOException when error occures */

136     protected static FileObject selectFile() throws IOException JavaDoc {
137
138         InputPanel jp = new InputPanel();
139
140         try {
141             // repository
142
Node an = RepositoryNodeFactory.getDefault()
143                                            .repository(DataFilter.ALL);
144
145             NodeAcceptor na = new NodeAcceptor() {
146                                   public boolean acceptNodes(Node[] nodes) {
147                                       if (nodes == null || nodes.length != 1) {
148                                           return false;
149                                       }
150                                       DataFolder cookie = (DataFolder)nodes[0].getCookie (DataFolder.class);
151                                       return cookie != null && !cookie.getPrimaryFile ().isReadOnly ();
152                                   }
153                               };
154
155             // select file system
156
Node[] nodes = NodeOperation.getDefault().select(
157                                NbBundle.getBundle (CreateGroupAction.class).getString ("PROP_Select_File"),
158                                NbBundle.getBundle (CreateGroupAction.class).getString ("PROP_Look_In"),
159                                an, na, jp
160                            );
161
162             FileObject folder = ((DataFolder)nodes[0].getCookie(DataFolder.class)).getPrimaryFile();
163             return folder.createData(jp.getText(), GroupShadow.GS_EXTENSION );
164         } catch (UserCancelException ex) {
165             return null;
166         } catch (NullPointerException JavaDoc ex) {
167             // could occur if nodes[0].getCookie returns null, but
168
// it should not happen because of the filter
169
return null;
170         }
171     } // select file
172

173     
174     /** Input panel. */
175     private static class InputPanel extends JPanel JavaDoc {
176
177         /** Texfield. */
178         JTextField JavaDoc text;
179
180         /** Generated serial verison UID. */
181         static final long serialVersionUID =2856913107896554654L;
182         
183         
184         /** Constructor. */
185         public InputPanel () {
186             BorderLayout JavaDoc lay = new BorderLayout JavaDoc ();
187             lay.setVgap(5);
188             lay.setHgap(5);
189             setLayout (lay);
190
191             // label and text field with mnemonic
192
JLabel JavaDoc label = new JLabel JavaDoc();
193             Mnemonics.setLocalizedText(label, NbBundle.getBundle (CreateGroupAction.class).getString ("CTL_Group_Name"));
194
195             text = new JTextField JavaDoc ();
196             text.getAccessibleContext().setAccessibleDescription(NbBundle.getBundle (CreateGroupAction.class).getString ("ACS_TextField"));
197
198             label.setLabelFor(text);
199             add (BorderLayout.WEST, label);
200             add (BorderLayout.CENTER, text);
201         }
202
203         
204         /** Request focus delegates to texfield. */
205         public void requestFocus () {
206             text.requestFocus ();
207         }
208         
209         /** Request focus delegates to texfield. */
210         public boolean requestFocusInWindow () {
211             return text.requestFocusInWindow ();
212         }
213
214         /** Gets text from textfield. */
215         public String JavaDoc getText () {
216             return text.getText ();
217         }
218
219         /** Set text to textfield. */
220         public void setText (String JavaDoc s) {
221             setText (s);
222         }
223     } // End of InputPanel class.
224
}
225
Popular Tags