KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > actions > DeleteLocalAction


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
20 package org.netbeans.modules.versioning.system.cvss.ui.actions;
21
22 import org.openide.filesystems.FileObject;
23 import org.openide.filesystems.FileUtil;
24 import org.openide.filesystems.FileLock;
25 import org.openide.ErrorManager;
26 import org.openide.NotifyDescriptor;
27 import org.openide.DialogDisplayer;
28 import org.openide.nodes.Node;
29 import org.openide.util.*;
30 import org.openide.util.NbBundle;
31 import org.openide.util.RequestProcessor;
32 import org.netbeans.modules.versioning.system.cvss.FileInformation;
33 import org.netbeans.lib.cvsclient.admin.StandardAdminHandler;
34
35 import javax.swing.*;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Delete action enabled only for new local files only.
42  * It eliminates <tt>CVS/Entries</tt> scheduling if exists too.
43  *
44  * @author Petr Kuzel
45  */

46 public final class DeleteLocalAction extends AbstractSystemAction {
47
48     public static final int LOCALLY_DELETABLE_MASK = FileInformation.STATUS_NOTVERSIONED_NEWLOCALLY | FileInformation.STATUS_VERSIONED_ADDEDLOCALLY;
49
50     public void performCvsAction(Node[] nodes) {
51         NotifyDescriptor descriptor = new NotifyDescriptor.Confirmation(NbBundle.getMessage(DeleteLocalAction.class, "CTL_DeleteLocal_Prompt"));
52         descriptor.setTitle(NbBundle.getMessage(DeleteLocalAction.class, "CTL_DeleteLocal_Title"));
53         descriptor.setMessageType(JOptionPane.WARNING_MESSAGE);
54         descriptor.setOptionType(NotifyDescriptor.YES_NO_OPTION);
55
56         Object JavaDoc res = DialogDisplayer.getDefault().notify(descriptor);
57         if (res != NotifyDescriptor.YES_OPTION) {
58             return;
59         }
60
61         final File JavaDoc [] files = getContext(nodes).getFiles();
62         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
63             public void run() {
64                 async(files);
65             }
66         });
67     }
68
69     protected int getFileEnabledStatus() {
70         return LOCALLY_DELETABLE_MASK;
71     }
72
73     protected String JavaDoc getBaseName(Node [] activatedNodes) {
74         return "Delete"; // NOI18N
75
}
76
77     protected boolean asynchronous() {
78         return false;
79     }
80     
81     public void async(File JavaDoc[] files) {
82         for (int i = 0; i < files.length; i++) {
83             File JavaDoc file = files[i];
84             StandardAdminHandler entries = new StandardAdminHandler();
85             FileObject fo = FileUtil.toFileObject(file);
86             if (fo != null) {
87                 FileLock lock = null;
88                 try {
89                     lock = fo.lock();
90                     entries.removeEntry(file);
91                     fo.delete(lock);
92                 } catch (IOException JavaDoc e) {
93                     ErrorManager err = ErrorManager.getDefault();
94                     err.annotate(e, NbBundle.getMessage(DeleteLocalAction.class, "BK0001", file.getAbsolutePath()));
95                     err.notify(e);
96                 } finally {
97                     if (lock != null) {
98                         lock.releaseLock();
99                     }
100                 }
101             }
102         }
103     }
104 }
105
Popular Tags