1 11 package org.eclipse.compare.internal; 12 13 import java.io.*; 14 import java.util.ResourceBundle ; 15 import com.ibm.icu.text.MessageFormat; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.BusyIndicator; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.events.SelectionListener; 21 import org.eclipse.swt.layout.RowLayout; 22 import org.eclipse.swt.widgets.*; 23 import org.eclipse.ui.PlatformUI; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.compare.*; 26 import org.eclipse.compare.structuremergeviewer.ICompareInput; 27 28 32 public class BinaryCompareViewer extends AbstractViewer { 33 34 private static final String BUNDLE_NAME= "org.eclipse.compare.internal.BinaryCompareViewerResources"; 36 private static final int EOF= -1; 37 private ICompareInput fInput; 38 private ResourceBundle fBundle; 39 private boolean fLeftIsLocal; 40 41 private Composite fComposite; 42 private Label fMessage; 43 private Button fTextButton; 44 45 46 public BinaryCompareViewer(Composite parent, final CompareConfiguration cc) { 47 48 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, ICompareContextIds.BINARY_COMPARE_VIEW); 49 50 fBundle= ResourceBundle.getBundle(BUNDLE_NAME); 51 52 fComposite= new Composite(parent, SWT.NONE); 53 RowLayout rowLayout = new RowLayout(); 54 rowLayout.type = SWT.VERTICAL; 55 fComposite.setLayout(rowLayout); 56 57 fMessage= new Label(fComposite, SWT.WRAP); 58 fMessage.setData(CompareUI.COMPARE_VIEWER_TITLE, Utilities.getString(fBundle, "title")); 60 fLeftIsLocal= Utilities.getBoolean(cc, "LEFT_IS_LOCAL", false); 62 if (canShowAsText(cc)) { 63 fTextButton = new Button(fComposite, SWT.PUSH); 64 fTextButton.setText(Utilities.getString(fBundle, "compareAsText")); fTextButton.addSelectionListener(new SelectionListener() { 66 public void widgetSelected(SelectionEvent e) { 67 BusyIndicator.showWhile(Display.getCurrent(), new Runnable () { 68 public void run() { 69 handleShowAsText(cc); 70 } 71 }); 72 } 73 public void widgetDefaultSelected(SelectionEvent e) { 74 } 76 }); 77 fTextButton.setEnabled(false); 78 } 79 } 80 81 private boolean canShowAsText(CompareConfiguration cc) { 82 if (cc == null) 83 return false; 84 return Utilities.getAdapter(cc.getContainer(), ICompareAsText.class) != null; 85 } 86 87 protected void handleShowAsText(CompareConfiguration cc) { 88 ICompareAsText comparer = (ICompareAsText)Utilities.getAdapter(cc.getContainer(), ICompareAsText.class); 89 comparer.compareAsText(getInput()); 90 } 91 92 public Control getControl() { 93 return fComposite; 94 } 95 96 public void setInput(Object input) { 97 if (fComposite != null && input instanceof ICompareInput) { 98 fInput= (ICompareInput) input; 99 100 InputStream left= null; 101 InputStream right= null; 102 103 String message= null; 104 try { 105 left= getStream(fInput.getLeft()); 106 right= getStream(fInput.getRight()); 107 108 if (left != null && right != null) { 109 int pos= 0; 110 while (true) { 111 int l= left.read(); 112 int r= right.read(); 113 if (l != r) { 114 String format= Utilities.getString(fBundle, "diffMessageFormat"); message= MessageFormat.format(format, new String [] { Integer.toString(pos) } ); 116 break; 117 } 118 if (l == EOF) 119 break; 120 pos++; 121 } 122 } else if (left == null && right == null) { 123 message= Utilities.getString(fBundle, "deleteConflictMessage"); } else if (left == null) { 125 if (fLeftIsLocal) 126 message= Utilities.getString(fBundle, "deletedMessage"); else 128 message= Utilities.getString(fBundle, "addedMessage"); } else if (right == null) { 130 if (fLeftIsLocal) 131 message= Utilities.getString(fBundle, "addedMessage"); else 133 message= Utilities.getString(fBundle, "deletedMessage"); } 135 } catch (CoreException ex) { 136 message= Utilities.getString(fBundle, "errorMessage"); } catch (IOException ex) { 138 message= Utilities.getString(fBundle, "errorMessage"); } finally { 140 Utilities.close(left); 141 Utilities.close(right); 142 } 143 if (message != null) 144 fMessage.setText(message); 145 if (fTextButton != null) 146 fTextButton.setEnabled(true); 147 fComposite.layout(); 148 } 149 } 150 151 public Object getInput() { 152 return fInput; 153 } 154 155 private InputStream getStream(ITypedElement input) throws CoreException { 156 if (input instanceof IStreamContentAccessor) 157 return ((IStreamContentAccessor)input).getContents(); 158 return null; 159 } 160 } 161 | Popular Tags |