1 19 package org.netbeans.modules.localhistory.ui.view; 20 21 import java.awt.BorderLayout ; 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.beans.PropertyChangeEvent ; 26 import java.beans.PropertyChangeListener ; 27 import java.io.*; 28 import javax.swing.JPanel ; 29 import javax.swing.SwingUtilities ; 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 50 public class LocalHistoryDiffView implements PropertyChangeListener , ActionListener { 51 52 private DiffPanel panel; 53 private Component diffComponent; 54 private DiffView diffView; 55 56 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 evt) { 65 if(ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 66 selectionChanged(evt); 67 } 68 } 69 70 JPanel getPanel() { 71 return panel; 72 } 73 74 private void selectionChanged(PropertyChangeEvent 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 { 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 108 SwingUtilities.invokeLater(new Runnable () { 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 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 s) { 145 setDiffComponent(new NoContentPanel(s)); 146 } 147 148 private void setDiffComponent(Component component) { 149 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 } 159 160 public void actionPerformed(ActionEvent 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 title; 203 private final String mimeType; 204 205 public LHStreamSource(File file, String title, String 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 getName() { 225 return title; 226 } 227 228 public String getTitle() { 229 return title; 230 } 231 232 public String 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 |