KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > localhistory > ui > view > LocalHistoryDiffView


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.localhistory.ui.view;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 import java.beans.PropertyChangeListener JavaDoc;
27 import java.io.*;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.SwingUtilities JavaDoc;
30 import org.netbeans.api.diff.Diff;
31 import org.netbeans.api.diff.DiffView;
32 import org.netbeans.api.diff.StreamSource;
33 import org.netbeans.api.diff.Difference;
34 import org.netbeans.modules.localhistory.store.StoreEntry;
35 import org.netbeans.modules.versioning.util.NoContentPanel;
36 import org.openide.ErrorManager;
37 import org.openide.filesystems.FileObject;
38 import org.openide.filesystems.FileUtil;
39 import org.openide.explorer.ExplorerManager;
40 import org.openide.nodes.Node;
41 import org.openide.util.NbBundle;
42 import org.openide.util.RequestProcessor;
43 import org.openide.util.Lookup;
44 import org.openide.util.lookup.Lookups;
45
46 /**
47  *
48  * @author Tomas Stupka
49  */

50 public class LocalHistoryDiffView implements PropertyChangeListener JavaDoc, ActionListener JavaDoc {
51            
52     private DiffPanel panel;
53     private Component JavaDoc diffComponent;
54     private DiffView diffView;
55     
56     /** Creates a new instance of LocalHistoryView */
57     public LocalHistoryDiffView() {
58         panel = new DiffPanel();
59         panel.nextButton.addActionListener(this);
60         panel.prevButton.addActionListener(this);
61         showNoContent(NbBundle.getMessage(LocalHistoryDiffView.class, "MSG_DiffPanel_NoVersion"));
62     }
63         
64     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
65         if(ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) {
66             selectionChanged(evt);
67         }
68     }
69       
70     JPanel JavaDoc getPanel() {
71         return panel;
72     }
73     
74     private void selectionChanged(PropertyChangeEvent JavaDoc evt) {
75         Node[] newSelection = ((Node[]) evt.getNewValue());
76         if(newSelection == null || newSelection.length == 0) {
77             return;
78         }
79
80         StoreEntry se = newSelection[0].getLookup().lookup(StoreEntry.class);
81         
82         if( se == null ) {
83             showNoContent(NbBundle.getMessage(LocalHistoryDiffView.class, "MSG_DiffPanel_IllegalSelection"));
84             return;
85         }
86         refreshDiffPanel(se);
87     }
88     
89     private void refreshDiffPanel(StoreEntry se) {
90         DiffPrepareTask prepareTask = new DiffPrepareTask(se);
91         RequestProcessor.Task task = RequestProcessor.getDefault().create(prepareTask);
92         task.schedule(0);
93     }
94
95     private class DiffPrepareTask implements Runnable JavaDoc {
96         
97         private final StoreEntry entry;
98
99         public DiffPrepareTask(final StoreEntry se) {
100             entry = se;
101         }
102
103         public void run() {
104             final Diff diff = Diff.getDefault();
105             
106             // XXX how to get the mimetype
107

108             SwingUtilities.invokeLater(new Runnable JavaDoc() {
109                 public void run() {
110                     try {
111                         
112                         StreamSource ss1 = StreamSource.createSource("historyfile", entry.getFile().getName() + " " + StoreEntryNode.getFormatedDate(entry), entry.getMIMEType(), new InputStreamReader(entry.getStoreFileInputStream()));
113                         
114                         String JavaDoc title;
115                         StreamSource ss2;
116                         File file = entry.getFile();
117                         if(file.exists()) {
118                             title = NbBundle.getMessage(LocalHistoryDiffView.class, "LBL_Diff_CurrentFile");
119                             ss2 = new LHStreamSource(file, title, entry.getMIMEType());
120                         } else {
121                             title = NbBundle.getMessage(LocalHistoryDiffView.class, "LBL_Diff_FileDeleted");
122                             ss2 = StreamSource.createSource("currentfile", title, entry.getMIMEType(), new StringReader(""));
123                         }
124                         
125                         diffView = diff.createDiff(ss1, ss2);
126                         
127                         setDiffComponent(diffView.getComponent());
128                         if(diffView.getDifferenceCount() > 0) {
129                             setCurrentDifference(0);
130                         } else {
131                             refreshNavigationButtons();
132                         }
133                         panel.revalidate();
134                         panel.repaint();
135
136                     } catch (IOException ioe) {
137                         ErrorManager.getDefault().notify(ioe);
138                     }
139                 }
140             });
141         }
142     }
143     
144     private void showNoContent(String JavaDoc s) {
145         setDiffComponent(new NoContentPanel(s));
146     }
147
148     private void setDiffComponent(Component JavaDoc component) {
149         //int dl = panel.splitPane.getDividerLocation();
150
if(diffComponent != null) {
151             panel.diffPanel.remove(diffComponent);
152         }
153         panel.diffPanel.add(component, BorderLayout.CENTER);
154         diffComponent = component;
155         panel.diffPanel.revalidate();
156         panel.diffPanel.repaint();
157         //panel.splitPane.setDividerLocation(dl);
158
}
159     
160     public void actionPerformed(ActionEvent JavaDoc evt) {
161         if(evt.getSource() == panel.nextButton) {
162             onNextButton();
163         } else if(evt.getSource() == panel.prevButton) {
164             onPrevButton();
165         }
166     }
167
168     private void onNextButton() {
169         if(diffView == null) {
170             return;
171         }
172         int nextDiffernce = diffView.getCurrentDifference() + 1;
173         if(nextDiffernce < diffView.getDifferenceCount()) {
174             setCurrentDifference(nextDiffernce);
175         }
176     }
177
178     private void onPrevButton() {
179         if(diffView == null) {
180             return;
181         }
182         int prevDiffernce = diffView.getCurrentDifference() - 1;
183         if(prevDiffernce > -1) {
184             setCurrentDifference(prevDiffernce);
185         }
186     }
187     
188     private void setCurrentDifference(int idx) {
189         diffView.setCurrentDifference(idx);
190         refreshNavigationButtons();
191     }
192     
193     private void refreshNavigationButtons() {
194         int currentDifference = diffView.getCurrentDifference();
195         panel.prevButton.setEnabled(currentDifference > 0);
196         panel.nextButton.setEnabled(currentDifference < diffView.getDifferenceCount() - 1);
197     }
198     
199     private class LHStreamSource extends StreamSource {
200         
201         private final File file;
202         private final String JavaDoc title;
203         private final String JavaDoc mimeType;
204
205         public LHStreamSource(File file, String JavaDoc title, String JavaDoc mimeType) {
206             this.file = file;
207             this.title = title;
208             this.mimeType = mimeType;
209         }
210
211         public boolean isEditable() {
212             return true;
213         }
214
215         public Lookup getLookup() {
216             FileObject fo = FileUtil.toFileObject(file);
217             if (fo != null) {
218                 return Lookups.fixed(fo);
219             } else {
220                 return Lookups.fixed();
221             }
222         }
223
224         public String JavaDoc getName() {
225             return title;
226         }
227
228         public String JavaDoc getTitle() {
229             return title;
230         }
231
232         public String JavaDoc getMIMEType() {
233             return mimeType;
234         }
235
236         public Reader createReader() throws IOException {
237             return new FileReader(file);
238         }
239
240         public Writer createWriter(Difference[] conflicts) throws IOException {
241             return null;
242         }
243     }
244 }
245
Popular Tags