From c0daad44016f53e30efcbb01c1da21c3619639aa Mon Sep 17 00:00:00 2001 From: John Dallaway Date: Sun, 11 Jun 2023 11:31:05 +0100 Subject: [PATCH] Accommodate archive files in Default Binary File Editor --- .../cdt/internal/core/model/Archive.java | 5 +++-- .../cdt/internal/core/model/Binary.java | 5 +++-- .../ui/editor/DefaultBinaryFileEditor.java | 22 ++++++++++--------- .../cdt/internal/ui/util/EditorUtility.java | 21 +++++++++++------- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Archive.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Archive.java index 8683ce004f8..e6f5d4fc66d 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Archive.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Archive.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -11,6 +11,7 @@ * Contributors: * QNX Software Systems - Initial API and implementation * Anton Leherbauer (Wind River Systems) + * John Dallaway - Adapt for IBinaryFile (#413) *******************************************************************************/ package org.eclipse.cdt.internal.core.model; @@ -88,7 +89,7 @@ public boolean computeChildren(OpenableInfo info, IResource res) { @Override public T getAdapter(Class adapter) { - if (IBinaryArchive.class.equals(adapter)) { + if (adapter.isAssignableFrom(IBinaryArchive.class)) { return adapter.cast(getBinaryArchive()); } return super.getAdapter(adapter); diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java index 0616dc4d205..4bd564eeca7 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/model/Binary.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -12,6 +12,7 @@ * QNX Software Systems - Initial API and implementation * Markus Schorn (Wind River Systems) * Anton Leherbauer (Wind River Systems) + * John Dallaway - Adapt for IBinaryFile (#413) *******************************************************************************/ package org.eclipse.cdt.internal.core.model; @@ -214,7 +215,7 @@ protected IBinaryObject getBinaryObject() { @Override public T getAdapter(Class adapter) { - if (IBinaryObject.class.equals(adapter)) { + if (adapter.isAssignableFrom(IBinaryObject.class)) { return adapter.cast(getBinaryObject()); } return super.getAdapter(adapter); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DefaultBinaryFileEditor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DefaultBinaryFileEditor.java index a4b390fe217..947fb828bc7 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DefaultBinaryFileEditor.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/editor/DefaultBinaryFileEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2015 Wind River Systems, Inc. and others. + * Copyright (c) 2007, 2023 Wind River Systems, Inc. and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -10,6 +10,7 @@ * * Contributors: * Anton Leherbauer (Wind River Systems) - initial API and implementation + * John Dallaway - support both IArchive and IBinary as input (#413) *******************************************************************************/ package org.eclipse.cdt.internal.ui.editor; @@ -19,6 +20,7 @@ import org.eclipse.cdt.core.IBinaryParser; import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.IArchive; import org.eclipse.cdt.core.model.IBinary; import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.resources.FileStorage; @@ -60,7 +62,7 @@ public class DefaultBinaryFileEditor extends AbstractTextEditor implements IReso * A storage editor input for binary files. */ public static class BinaryFileEditorInput extends PlatformObject implements IStorageEditorInput { - private final IBinary fBinary; + private final ICElement fBinary; private IStorage fStorage; /** @@ -68,7 +70,7 @@ public static class BinaryFileEditorInput extends PlatformObject implements ISto * * @param binary */ - public BinaryFileEditorInput(IBinary binary) { + public BinaryFileEditorInput(ICElement binary) { fBinary = binary; } @@ -120,11 +122,11 @@ public String getToolTipText() { @Override public IStorage getStorage() throws CoreException { if (fStorage == null) { - IBinaryParser.IBinaryObject object = fBinary.getAdapter(IBinaryParser.IBinaryObject.class); - if (object != null) { - IGnuToolFactory factory = object.getBinaryParser().getAdapter(IGnuToolFactory.class); + IBinaryParser.IBinaryFile file = fBinary.getAdapter(IBinaryParser.IBinaryFile.class); + if (file != null) { + IGnuToolFactory factory = file.getBinaryParser().getAdapter(IGnuToolFactory.class); if (factory != null) { - Objdump objdump = factory.getObjdump(object.getPath()); + Objdump objdump = factory.getObjdump(file.getPath()); if (objdump != null) { try { // limit editor to X MB, if more - users should use objdump in command @@ -139,7 +141,7 @@ public IStorage getStorage() throws CoreException { System.arraycopy(message.getBytes(), 0, output, limitBytes - message.length(), message.length()); } - fStorage = new FileStorage(new ByteArrayInputStream(output), object.getPath()); + fStorage = new FileStorage(new ByteArrayInputStream(output), file.getPath()); } catch (IOException exc) { CUIPlugin.log(exc); } @@ -172,8 +174,8 @@ protected IDocument createDocument(Object element) throws CoreException { IFile file = ResourceUtil.getFile(element); if (file != null) { ICElement cElement = CoreModel.getDefault().create(file); - if (cElement instanceof IBinary) { - element = new BinaryFileEditorInput((IBinary) cElement); + if (cElement instanceof IArchive || cElement instanceof IBinary) { + element = new BinaryFileEditorInput(cElement); } } return super.createDocument(element); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java index 8827a8e46f9..99890cbe8ba 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/util/EditorUtility.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2020 QNX Software Systems and others. + * Copyright (c) 2000, 2023 QNX Software Systems and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,6 +15,7 @@ * Anton Leherbauer (Wind River Systems) * Ed Swartz (Nokia) * Alexander Fedorov (ArSysOp) - Bug 561993 - Remove dependency to com.ibm.icu from CDT UI + * John Dallaway - Support both IArchive and IBinary storage requests (#413) *******************************************************************************/ package org.eclipse.cdt.internal.ui.util; @@ -36,6 +37,7 @@ import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.model.IIncludeReference; +import org.eclipse.cdt.core.model.IOpenable; import org.eclipse.cdt.core.model.ISourceRange; import org.eclipse.cdt.core.model.ISourceReference; import org.eclipse.cdt.core.model.ITranslationUnit; @@ -751,15 +753,18 @@ private static String appendModifierString(String modifierString, int modifier) new String[] { modifierString, newModifierString }); } - public static IStorage getStorage(IBinary bin) { + public static IStorage getStorage(ICElement element) { IStorage store = null; - try { - IBuffer buffer = bin.getBuffer(); - if (buffer != null) { - store = new FileStorage(new ByteArrayInputStream(buffer.getContents().getBytes()), bin.getPath()); + if (element instanceof IOpenable openable) { + try { + IBuffer buffer = openable.getBuffer(); + if (buffer != null) { + store = new FileStorage(new ByteArrayInputStream(buffer.getContents().getBytes()), + element.getPath()); + } + } catch (CModelException e) { + // nothing; } - } catch (CModelException e) { - // nothing; } return store; }