KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > versioning > system > cvss > ui > history > ViewRevisionAction


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 package org.netbeans.modules.versioning.system.cvss.ui.history;
20
21 import org.netbeans.modules.versioning.spi.VCSContext;
22 import org.netbeans.modules.versioning.util.Utils;
23 import org.netbeans.modules.versioning.system.cvss.VersionsCache;
24 import org.openide.util.NbBundle;
25 import org.openide.util.HelpCtx;
26 import org.openide.DialogDescriptor;
27 import org.openide.DialogDisplayer;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.loaders.DataObject;
30 import org.openide.loaders.DataObjectNotFoundException;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33
34 import javax.swing.*;
35 import java.io.*;
36 import java.awt.event.ActionEvent JavaDoc;
37 import java.awt.Dialog JavaDoc;
38
39 /**
40  * Allows to view a specific revision of the given file.
41  *
42  * @author Maros Sandor
43  */

44 public class ViewRevisionAction extends AbstractAction implements Runnable JavaDoc {
45     
46     private final VCSContext ctx;
47     private ViewRevisionPanel settings;
48
49     public ViewRevisionAction(VCSContext ctx) {
50         this(Utils.getActionName(ViewRevisionAction.class, "CTL_MenuItem_ViewRevision", ctx), ctx);
51     }
52
53     public ViewRevisionAction(String JavaDoc name, VCSContext ctx) {
54         super(name);
55         this.ctx = ctx;
56     }
57
58     public boolean isEnabled() {
59         return ctx.getRootFiles().size() > 0;
60     }
61
62     public void actionPerformed(ActionEvent JavaDoc e) {
63
64         String JavaDoc title = NbBundle.getMessage(ViewRevisionAction.class, "CTL_ViewRevisionDialog_Title", Utils.getContextDisplayName(ctx)); // NOI18N
65

66         settings = new ViewRevisionPanel(ctx);
67         
68         JButton view = new JButton(NbBundle.getMessage(ViewRevisionAction.class, "CTL_ViewRevisionDialog_Action_View")); // NOI18N
69
settings.putClientProperty("OKButton", view); // NOI18N
70
settings.refreshComponents();
71         view.setToolTipText(NbBundle.getMessage(ViewRevisionAction.class, "TT_ViewRevisionDialog_Action_View")); // NOI18N
72
DialogDescriptor descriptor = new DialogDescriptor(
73                 settings,
74                 title,
75                 true,
76                 new Object JavaDoc [] { view, DialogDescriptor.CANCEL_OPTION },
77                 view,
78                 DialogDescriptor.DEFAULT_ALIGN,
79                 new HelpCtx(ViewRevisionAction.class),
80                 null);
81         descriptor.setClosingOptions(null);
82         Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(descriptor);
83         dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(ViewRevisionAction.class, "ACSD_ViewRevisionDialog")); // NOI18N
84
dialog.setVisible(true);
85         if (descriptor.getValue() != view) return;
86
87         settings.saveSettings();
88         Utils.createTask(this).schedule(0);
89     }
90
91     public void run() {
92         final String JavaDoc revision = settings.getRevision();
93         File tempFolder = Utils.getTempFolder();
94         tempFolder.deleteOnExit();
95         for (File file : ctx.getRootFiles()) {
96             if (file.isDirectory()) continue;
97             try {
98                 File original = VersionsCache.getInstance().getRemoteFile(file, revision, null);
99                 File daoFile = new File(tempFolder, file.getName());
100                 Utils.copyStreamsCloseAll(new FileOutputStream(daoFile), new FileInputStream(original));
101                 daoFile.deleteOnExit();
102                 final FileObject fo = FileUtil.toFileObject(daoFile);
103                 try {
104                     DataObject dao = DataObject.find(fo);
105                     final EditorCookie ec = dao.getCookie(EditorCookie.class);
106                     if (ec != null) {
107                         SwingUtilities.invokeLater(new Runnable JavaDoc() {
108                             public void run() {
109                                 Utils.openFile(fo, revision);
110                             }
111                         });
112                     }
113                 } catch (DataObjectNotFoundException e) {
114                     // no data obejct for the file, ignore it
115
}
116             } catch (Exception JavaDoc e) {
117                 // the file cannot be opened, ignore
118
}
119         }
120     }
121
122 }
123
Popular Tags