KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > subversion > ui > browser > CreateFolderAction


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.subversion.ui.browser;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyVetoException JavaDoc;
25 import java.sql.Date JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import org.netbeans.modules.subversion.RepositoryFile;
29 import org.netbeans.modules.subversion.ui.browser.RepositoryPathNode.RepositoryPathEntry;
30 import org.openide.DialogDescriptor;
31 import org.openide.DialogDisplayer;
32 import org.openide.ErrorManager;
33 import org.openide.NotifyDescriptor;
34 import org.openide.explorer.ExplorerManager;
35 import org.openide.nodes.Children;
36 import org.openide.nodes.Node;
37 import org.openide.util.RequestProcessor;
38 import org.tigris.subversion.svnclientadapter.SVNNodeKind;
39 import org.tigris.subversion.svnclientadapter.SVNRevision;
40
41 /**
42  * Creates a new folder in the browser
43  *
44  * @author Tomas Stupka
45  */

46 public class CreateFolderAction extends BrowserAction implements PropertyChangeListener JavaDoc {
47     private final String JavaDoc defaultFolderName;
48     
49     public CreateFolderAction(String JavaDoc defaultFolderName) {
50         this.defaultFolderName = defaultFolderName;
51         putValue(Action.NAME, org.openide.util.NbBundle.getMessage(RepositoryPathNode.class, "CTL_Action_MakeDir")); // NOI18N
52
setEnabled(false);
53     }
54     
55     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
56         if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
57             setEnabled(isEnabled());
58         }
59     }
60     
61     public boolean isEnabled() {
62         Browser browser = getBrowser();
63         if(browser == null) {
64             return false;
65         }
66         
67         if(browser.getBrowserPanel()==null) {
68             return false;
69         }
70         if(browser.getExplorerManager().getRootContext() == Node.EMPTY) {
71             return false;
72         }
73         Node[] nodes = getBrowser().getSelectedNodes();
74         if(nodes.length != 1) {
75             return false;
76         }
77         return nodes[0] instanceof RepositoryPathNode &&
78                ((RepositoryPathNode) nodes[0]).getEntry().getSvnNodeKind() == SVNNodeKind.DIR;
79     }
80
81     /**
82      * Configures this action with the actuall browser instance
83      */

84     public void setBrowser(Browser browser) {
85         Browser oldBrowser = getBrowser();
86         if(oldBrowser!=null) {
87             oldBrowser.removePropertyChangeListener(this);
88         }
89         browser.addPropertyChangeListener(this);
90         super.setBrowser(browser);
91     }
92     
93     public void actionPerformed(ActionEvent JavaDoc e) {
94         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
95             public void run() {
96                 Node[] nodes = getSelectedNodes();
97                 if(nodes.length > 1) {
98                     return;
99                 }
100
101                 RepositoryPathNode repositoryPathNode = (RepositoryPathNode) nodes[0];
102                 Children children = repositoryPathNode.getChildren();
103                 Node[] childNodes = children.getNodes();
104                 if(childNodes.length > 0) {
105                   try {
106                         // force listing of all child nodes ...
107
getExplorerManager().setSelectedNodes(new Node[] {childNodes[0]});
108                     } catch (PropertyVetoException JavaDoc ex) {
109                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
110
}
111                 }
112
113                 DialogDescriptor.InputLine input =
114                     new DialogDescriptor.InputLine(java.util.ResourceBundle.getBundle("org/netbeans/modules/subversion/ui/browser/Bundle").getString("CTL_Browser_NewFolder_Prompt"), java.util.ResourceBundle.getBundle("org/netbeans/modules/subversion/ui/browser/Bundle").getString("CTL_Browser_NewFolder_Title"));
115                 input.setInputText(defaultFolderName);
116                 DialogDisplayer.getDefault().notify(input);
117                 String JavaDoc newDir = input.getInputText().trim();
118                 if(input.getValue() == DialogDescriptor.CANCEL_OPTION ||
119                    input.getValue() == DialogDescriptor.CLOSED_OPTION ||
120                    newDir.equals("")) // NOI18N
121
{
122                     return;
123                 }
124
125                 children = repositoryPathNode.getChildren();
126                 if(children != null && children.getNodesCount() > 0) {
127                     childNodes = children.getNodes();
128                     for (int i = 0; i < childNodes.length; i++) {
129                         if(childNodes[i].getDisplayName().equals(newDir)) {
130                             JButton JavaDoc ok = new JButton JavaDoc(java.util.ResourceBundle.getBundle("org/netbeans/modules/subversion/ui/browser/Bundle").getString("CTL_Browser_OK"));
131                             NotifyDescriptor descriptor = new NotifyDescriptor(
132                                     org.openide.util.NbBundle.getMessage(CreateFolderAction.class, "MSG_Browser_FolderExists", newDir), // NOI18N
133
java.util.ResourceBundle.getBundle("org/netbeans/modules/subversion/ui/browser/Bundle").getString("MSG_Browser_WrongFolerName"), // NOI18N
134
NotifyDescriptor.DEFAULT_OPTION,
135                                     NotifyDescriptor.ERROR_MESSAGE,
136                                     new Object JavaDoc [] { ok },
137                                     ok);
138                             DialogDisplayer.getDefault().notify(descriptor);
139                             return;
140                         }
141                     }
142                 }
143
144                 RepositoryFile parentFile = repositoryPathNode.getEntry().getRepositoryFile();
145                 Node segmentNode = repositoryPathNode;
146                 String JavaDoc[] segments = newDir.split("/"); // NOI18N
147
for (int i = 0; i < segments.length; i++) {
148                     
149                     RepositoryFile newFile = parentFile.appendPath(segments[i]);
150                     RepositoryPathEntry entry = new RepositoryPathEntry(newFile, SVNNodeKind.DIR, new SVNRevision(0), new Date JavaDoc(System.currentTimeMillis()), ""); // XXX gget author
151
Node node = RepositoryPathNode.createBrowserPathNode(getBrowser(), entry);
152                     Node[] newChild = new Node[] {node};
153                     segmentNode.getChildren().add(newChild);
154                     segmentNode = node;
155                     parentFile = newFile;
156
157                     if( i == segments.length - 1 ) {
158                         // we are done, select the node ...
159
try {
160                             setSelectedNodes(newChild);
161                         } catch (PropertyVetoException JavaDoc ex) {
162                             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); // should not happen
163
}
164                     }
165                 }
166             }
167         });
168     }
169 }
Popular Tags