KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > compare > internal > BinaryCompareViewer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.compare.internal;
12
13 import java.io.*;
14 import java.util.ResourceBundle JavaDoc;
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 /**
29  * A simple compare viewer for binary files.
30  * Shows the position of the first non-matching byte.
31  */

32 public class BinaryCompareViewer extends AbstractViewer {
33
34     private static final String JavaDoc BUNDLE_NAME= "org.eclipse.compare.internal.BinaryCompareViewerResources"; //$NON-NLS-1$
35

36     private static final int EOF= -1;
37     private ICompareInput fInput;
38     private ResourceBundle JavaDoc 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")); //$NON-NLS-1$
59

60         fLeftIsLocal= Utilities.getBoolean(cc, "LEFT_IS_LOCAL", false); //$NON-NLS-1$
61

62         if (canShowAsText(cc)) {
63             fTextButton = new Button(fComposite, SWT.PUSH);
64             fTextButton.setText(Utilities.getString(fBundle, "compareAsText")); //$NON-NLS-1$
65
fTextButton.addSelectionListener(new SelectionListener() {
66                 public void widgetSelected(SelectionEvent e) {
67                     BusyIndicator.showWhile(Display.getCurrent(), new Runnable JavaDoc() {
68                         public void run() {
69                             handleShowAsText(cc);
70                         }
71                     });
72                 }
73                 public void widgetDefaultSelected(SelectionEvent e) {
74                     // Do nothing
75
}
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 JavaDoc input) {
97         if (fComposite != null && input instanceof ICompareInput) {
98             fInput= (ICompareInput) input;
99             
100             InputStream left= null;
101             InputStream right= null;
102             
103             String JavaDoc 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 JavaDoc format= Utilities.getString(fBundle, "diffMessageFormat"); //$NON-NLS-1$
115
message= MessageFormat.format(format, new String JavaDoc[] { 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"); //$NON-NLS-1$
124
} else if (left == null) {
125                     if (fLeftIsLocal)
126                         message= Utilities.getString(fBundle, "deletedMessage"); //$NON-NLS-1$
127
else
128                         message= Utilities.getString(fBundle, "addedMessage"); //$NON-NLS-1$
129
} else if (right == null) {
130                     if (fLeftIsLocal)
131                         message= Utilities.getString(fBundle, "addedMessage"); //$NON-NLS-1$
132
else
133                         message= Utilities.getString(fBundle, "deletedMessage"); //$NON-NLS-1$
134
}
135             } catch (CoreException ex) {
136                 message= Utilities.getString(fBundle, "errorMessage"); //$NON-NLS-1$
137
} catch (IOException ex) {
138                 message= Utilities.getString(fBundle, "errorMessage"); //$NON-NLS-1$
139
} 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 JavaDoc 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