diff options
author | Andre Weinand | 2003-03-24 15:38:57 +0000 |
---|---|---|
committer | Andre Weinand | 2003-03-24 15:38:57 +0000 |
commit | 8f49559469b644c3f26c8331b96fa805ff34ff7d (patch) | |
tree | 1677dba15f54347d728855d64b2d33e9f11f3fae /examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples | |
parent | aea06383149db74eb28e030bf6dac5bb0c6ae11f (diff) | |
download | eclipse.platform.team-8f49559469b644c3f26c8331b96fa805ff34ff7d.tar.gz eclipse.platform.team-8f49559469b644c3f26c8331b96fa805ff34ff7d.tar.xz eclipse.platform.team-8f49559469b644c3f26c8331b96fa805ff34ff7d.zip |
initial checkin
Diffstat (limited to 'examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples')
4 files changed, 268 insertions, 0 deletions
diff --git a/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/CompareExampleMessages.properties b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/CompareExampleMessages.properties new file mode 100644 index 000000000..3ebe0141c --- /dev/null +++ b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/CompareExampleMessages.properties @@ -0,0 +1,16 @@ +############################################################################### +# Copyright (c) 2000, 2003 IBM Corporation and others. +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Common Public License v1.0 +# which accompanies this distribution, and is available at +# http://www.eclipse.org/legal/cpl-v10.html +# +# Contributors: +# IBM Corporation - initial API and implementation +############################################################################### +# +# Resource strings +# +KeyValuePairStructureCreator.title= Key/Value Pair Compare +KeyValuePairStructureCreator.CoreException.message= CoreException +KeyValuePairStructureCreator.BadLocationException.message= BadLocationException diff --git a/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/KeyValuePairStructureCreator.java b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/KeyValuePairStructureCreator.java new file mode 100644 index 000000000..657b5d28b --- /dev/null +++ b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/KeyValuePairStructureCreator.java @@ -0,0 +1,155 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.compare.examples.structurecreator; + +import org.eclipse.swt.graphics.*; + +import org.eclipse.jface.text.*; + +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Status; + +import org.eclipse.compare.*; +import org.eclipse.compare.structuremergeviewer.*; + + +/** + * This structure creator parses input containing key/value pairs + * and returns the pairs as a tree of <code>IStructureComparator</code>s. + * Each key/value pair must be on a separate line and keys are separated + * from values by a '='. + * <p> + * Example: + * <pre> + * foo=bar + * name=joe + * </pre> + */ +public class KeyValuePairStructureCreator implements IStructureCreator { + + static class KeyValueNode extends DocumentRangeNode implements ITypedElement { + + String fValue; + + public KeyValueNode(String id, String value, IDocument doc, int start, int length) { + super(0, id, doc, start, length); + fValue= value; + } + + String getValue() { + return fValue; + } + + /** + * @see ITypedElement#getName + */ + public String getName() { + return this.getId(); + } + + /** + * Every key/value pair is of type "txt" so that the builtin TextMergeViewer is used automatically. + * @see ITypedElement#getType + */ + public String getType() { + return "txt"; //$NON-NLS-1$ + } + + /** + * @see ITypedElement#getImage + */ + public Image getImage() { + return CompareUI.getImage(getType()); + } + }; + + + public KeyValuePairStructureCreator() { + } + + /** + * This title will be shown in the title bar of the structure compare pane. + */ + public String getName() { + return Util.getString("KeyValuePairStructureCreator.title"); //$NON-NLS-1$ + } + + /** + * Returns a node. + */ + public IStructureComparator getStructure(Object input) { + + if (!(input instanceof IStreamContentAccessor)) + return null; + + IStreamContentAccessor sca= (IStreamContentAccessor) input; + + try { + String contents= Util.readString(sca.getContents()); + if (contents == null) + contents= ""; //$NON-NLS-1$ + Document doc= new Document(contents); + + KeyValueNode root= new KeyValueNode("root", "", doc, 0, doc.getLength()); //$NON-NLS-1$ //$NON-NLS-2$ + + for (int i= 0; i < doc.getNumberOfLines(); i++) { + + IRegion r= doc.getLineInformation(i); + String s= doc.get(r.getOffset(), r.getLength()); + int start= r.getOffset(); + + String key= ""; //$NON-NLS-1$ + String value= ""; //$NON-NLS-1$ + int pos= s.indexOf('='); + if (pos >= 0) { + key= s.substring(0, pos); + value= s.substring(pos+1); + } else { + key= s; + } + if (key.length() > 0) + root.addChild(new KeyValueNode(key, value, doc, start, s.length())); + } + return root; + } catch (CoreException ex) { + String message= Util.getString("KeyValuePairStructureCreator.CoreException.message"); //$NON-NLS-1$ + CompareUI.getPlugin().getLog().log(new Status(Status.ERROR, CompareUI.PLUGIN_ID, 0, message, ex)); + } catch (BadLocationException ex) { + String message= Util.getString("KeyValuePairStructureCreator.BadLocationException.message"); //$NON-NLS-1$ + CompareUI.getPlugin().getLog().log(new Status(Status.ERROR, CompareUI.PLUGIN_ID, 0, message, ex)); + } + + return null; + } + + public void save(IStructureComparator structure, Object input) { + if (input instanceof IEditableContent && structure instanceof KeyValueNode) { + IDocument doc= ((KeyValueNode)structure).getDocument(); + IEditableContent bca= (IEditableContent) input; + String c= doc.get(); + bca.setContent(c.getBytes()); + } + } + + public String getContents(Object node, boolean ignoreWhitespace) { + if (node instanceof KeyValueNode) { + String s= ((KeyValueNode)node).getValue(); + if (ignoreWhitespace) + s= s.trim(); + return s; + } + return null; + } + + public IStructureComparator locate(Object path, Object source) { + return null; + } +} diff --git a/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/TextMergeViewerCreator.java b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/TextMergeViewerCreator.java new file mode 100644 index 000000000..2565e4544 --- /dev/null +++ b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/TextMergeViewerCreator.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.compare.examples.structurecreator; + +import org.eclipse.swt.widgets.Composite; + +import org.eclipse.jface.viewers.Viewer; + +import org.eclipse.compare.*; +import org.eclipse.compare.contentmergeviewer.TextMergeViewer; + +/** + * A factory object for the <code>TextMergeViewer</code>. + * This indirection is necessary because only objects with a default + * constructor can be created via an extension point + * (this precludes Viewers). + */ +public class TextMergeViewerCreator implements IViewerCreator { + + public Viewer createViewer(Composite parent, CompareConfiguration mp) { + return new TextMergeViewer(parent, mp); + } +} diff --git a/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/Util.java b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/Util.java new file mode 100644 index 000000000..04ae99f8a --- /dev/null +++ b/examples/org.eclipse.compare.examples/src/org/eclipse/compare/examples/structurecreator/Util.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.compare.examples.structurecreator; + +import java.io.*; +import java.util.*; + +import org.eclipse.core.resources.ResourcesPlugin; + + +public class Util { + + private static final String RESOURCE_BUNDLE= "org.eclipse.compare.examples.structurecreator.CompareExampleMessages"; //$NON-NLS-1$ + + private static ResourceBundle fgResourceBundle= ResourceBundle.getBundle(RESOURCE_BUNDLE); + + public static String getString(String key) { + try { + return fgResourceBundle.getString(key); + } catch (MissingResourceException e) { + return "!" + key + "!"; //$NON-NLS-2$ //$NON-NLS-1$ + } + } + + /** + * Reads the contents of the given input stream into a string. + * The function assumes that the input stream uses the platform's default encoding + * (<code>ResourcesPlugin.getEncoding()</code>). + * Returns null if an error occurred. + */ + static String readString(InputStream is) { + if (is == null) + return null; + BufferedReader reader= null; + try { + StringBuffer buffer= new StringBuffer(); + char[] part= new char[2048]; + int read= 0; + reader= new BufferedReader(new InputStreamReader(is, ResourcesPlugin.getEncoding())); + + while ((read= reader.read(part)) != -1) + buffer.append(part, 0, read); + + return buffer.toString(); + + } catch (IOException ex) { + } finally { + if (reader != null) { + try { + reader.close(); + } catch (IOException ex) { + // we don't log IOException when closing a file + } + } + } + return null; + } +} |