KickJava   Java API By Example, From Geeks To Geeks.

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


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.diff;
21
22 import org.netbeans.modules.versioning.system.cvss.CvsModuleConfig;
23 import org.openide.windows.TopComponent;
24 import org.openide.util.NbBundle;
25 import org.openide.util.HelpCtx;
26 import org.openide.awt.UndoRedo;
27 import org.netbeans.modules.versioning.system.cvss.*;
28 import org.netbeans.modules.versioning.system.cvss.util.Context;
29
30 import javax.swing.*;
31 import java.awt.BorderLayout JavaDoc;
32 import java.io.*;
33 import java.util.List JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36
37 /**
38  * Performs diff action by fetching the appropriate file from repository
39  * and shows the diff. It actually does not execute "cvs diff" but rather uses our
40  * own diff algorithm.
41  *
42  * @author Maros Sandor
43  */

44 public class DiffExecutor {
45
46     private final Context context;
47     private final String JavaDoc contextName;
48
49     public DiffExecutor(Context context, String JavaDoc contextName) {
50         this.context = context;
51         this.contextName = contextName;
52     }
53
54     public DiffExecutor(String JavaDoc contextName) {
55         this.contextName = contextName;
56         this.context = null;
57     }
58
59     /**
60      * Opens GUI component that shows differences between current base revisions
61      * and HEAD revisions.
62      */

63     public void showRemoteDiff(ExecutorGroup group) {
64         showDiff(Setup.DIFFTYPE_REMOTE, group);
65     }
66     
67     /**
68      * Opens GUI component that shows differences between current working files
69      * and HEAD revisions.
70      */

71     public void showAllDiff(ExecutorGroup group) {
72         showDiff(Setup.DIFFTYPE_ALL, group);
73     }
74     
75     /**
76      * Opens GUI component that shows differences between current working files
77      * and repository versions they are based on.
78      */

79     public void showLocalDiff(ExecutorGroup group) {
80         showDiff(Setup.DIFFTYPE_LOCAL, group);
81     }
82
83     public void showDiff(File file, String JavaDoc rev1, String JavaDoc rev2) {
84         DiffMainPanel panel = new DiffMainPanel(file, rev1, rev2);
85         openDiff(panel, null);
86     }
87
88     private void showDiff(int type, ExecutorGroup group) {
89         VersionsCache.getInstance().purgeVolatileRevisions();
90         DiffMainPanel panel = new DiffMainPanel(context, type, contextName, group); // spawns bacground DiffPrepareTask
91
openDiff(panel, group);
92     }
93     
94     private void openDiff(final JComponent c, final ExecutorGroup group) {
95         SwingUtilities.invokeLater(new Runnable JavaDoc() {
96             public void run() {
97                 DiffTopComponent tc = new DiffTopComponent(c);
98                 tc.setName(NbBundle.getMessage(DiffExecutor.class, "CTL_DiffPanel_Title", contextName));
99                 tc.open();
100                 tc.requestActive();
101                 tc.setGroup(group);
102             }
103         });
104     }
105
106     /**
107      * Utility method that returns all non-excluded modified files that are
108      * under given roots (folders) and have one of specified statuses.
109      *
110      * @param context context to search
111      * @param includeStatus bit mask of file statuses to include in result
112      * @return File [] array of Files having specified status
113      */

114     public static File [] getModifiedFiles(Context context, int includeStatus) {
115         CvsFileTableModel model = CvsVersioningSystem.getInstance().getFileTableModel(context, includeStatus);
116         CvsFileNode [] nodes = model.getNodes();
117         List JavaDoc<File> files = new ArrayList JavaDoc<File>();
118         for (int i = 0; i < nodes.length; i++) {
119             File file = nodes[i].getFile();
120             if (CvsModuleConfig.getDefault().isExcludedFromCommit(file) == false) {
121                 files.add(file);
122             }
123         }
124         // ensure that command roots (files that were explicitly selected by user) are included in Diff
125
FileStatusCache cache = CvsVersioningSystem.getInstance().getStatusCache();
126         File [] rootFiles = context.getRootFiles();
127         for (int i = 0; i < rootFiles.length; i++) {
128             File file = rootFiles[i];
129             if (file.isFile() && (cache.getStatus(file).getStatus() & includeStatus) != 0 && !files.contains(file)) {
130                 files.add(file);
131             }
132         }
133         return (File[]) files.toArray(new File[files.size()]);
134     }
135
136     private static class DiffTopComponent extends TopComponent implements DiffSetupSource {
137
138         public DiffTopComponent() {
139         }
140         
141         public DiffTopComponent(JComponent c) {
142             setLayout(new BorderLayout JavaDoc());
143             c.putClientProperty(TopComponent.class, this);
144             add(c, BorderLayout.CENTER);
145             getAccessibleContext().setAccessibleName(NbBundle.getMessage(DiffTopComponent.class, "ACSN_Diff_Top_Component")); // NOI18N
146
getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(DiffTopComponent.class, "ACSD_Diff_Top_Component")); // NOI18N
147
}
148         
149         public UndoRedo getUndoRedo() {
150             DiffMainPanel mainPanel = ((DiffMainPanel) getComponent(0));
151             return mainPanel.getUndoRedo();
152         }
153
154         public int getPersistenceType(){
155             return TopComponent.PERSISTENCE_NEVER;
156         }
157
158         protected void componentClosed() {
159             ((DiffMainPanel) getComponent(0)).componentClosed();
160             super.componentClosed();
161         }
162
163         protected String JavaDoc preferredID(){
164             return "DiffExecutorTopComponent"; // NOI18N
165
}
166         
167         public HelpCtx getHelpCtx() {
168             return new HelpCtx(getClass());
169         }
170
171         protected void componentActivated() {
172             super.componentActivated();
173             DiffMainPanel mainPanel = ((DiffMainPanel) getComponent(0));
174             mainPanel.requestActive();
175         }
176
177         public Collection JavaDoc<Setup> getSetups() {
178             DiffSetupSource mainPanel = ((DiffSetupSource) getComponent(0));
179             return mainPanel.getSetups();
180         }
181
182         public String JavaDoc getSetupDisplayName() {
183             DiffSetupSource mainPanel = ((DiffSetupSource) getComponent(0));
184             return mainPanel.getSetupDisplayName();
185
186         }
187
188         public void setGroup(ExecutorGroup group) {
189             DiffMainPanel mainPanel = ((DiffMainPanel) getComponent(0));
190             mainPanel.setGroup(group);
191         }
192     }
193     
194 }
195
Popular Tags