Skip to content

Commit

Permalink
first draft of Bnd Effective Source tab
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Rueger <[email protected]>

add find text via CMD+F

Signed-off-by: Christoph Rueger <[email protected]>

add Syntax highlighting via SourceViewer

remove unnecessary code

remove some code which came in from JarPrintPage.java

Signed-off-by: Christoph Rueger <[email protected]>

fix resource bundle

found in org.eclipse.ui.texteditor.EditorMessages.BUNDLE_FOR_CONSTRUCTED_KEYS which is unfortunatelly not accessible (private)

Signed-off-by: Christoph Rueger <[email protected]>

add line numbers

Signed-off-by: Christoph Rueger <[email protected]>

rename tab just "Effective"

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed Dec 15, 2024
1 parent 8e5cfa7 commit 9c48429
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 3 deletions.
23 changes: 21 additions & 2 deletions bndtools.core/src/bndtools/editor/BndEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public class BndEditor extends ExtendedFormEditor implements IResourceChangeList
public static final String WORKSPACE_EDITOR = "bndtools.bndWorkspaceConfigEditor";

public static final String SOURCE_PAGE = "__source_page";
public static final String SOURCE_PAGE_EFFECTIVE = "__source_page_effective";

public static final String CONTENT_PAGE = "__content_page";
public static final String WORKSPACE_PAGE = "__workspace_page";
Expand All @@ -126,6 +127,7 @@ public class BndEditor extends ExtendedFormEditor implements IResourceChangeList
private final static Image buildFileImg = Icons.image("icons/bndtools-logo-16x16.png");

private BndSourceEditorPage sourcePage;
private BndSourceEffectivePage sourcePageEffective;
private Promise<Workspace> modelReady;

private IResource inputResource;
Expand All @@ -150,6 +152,7 @@ private void updateIncludedPages() {
requiredPageIds.addAll(getPagesBnd(path));
}
requiredPageIds.add(SOURCE_PAGE);
requiredPageIds.add(SOURCE_PAGE_EFFECTIVE);

// Remove pages no longer required and remember the rest in a map
int i = 0;
Expand All @@ -167,8 +170,13 @@ private void updateIncludedPages() {
// Cache new pages
for (String pageId : requiredPageIds) {
if (!pageCache.containsKey(pageId)) {
IFormPage page = SOURCE_PAGE.equals(pageId) ? sourcePage
: pageFactories.get(pageId)
IFormPage page;
if (SOURCE_PAGE.equals(pageId))
page = sourcePage;
else if (SOURCE_PAGE_EFFECTIVE.equals(pageId))
page = sourcePageEffective;
else
page = pageFactories.get(pageId)
.createPage(this, model, pageId);
pageCache.put(pageId, page);
}
Expand All @@ -184,13 +192,17 @@ private void updateIncludedPages() {
if (existingPointer >= getPageCount()) {
if (SOURCE_PAGE.equals(requiredId))
addPage(sourcePage, getEditorInput());
else if (SOURCE_PAGE_EFFECTIVE.equals(requiredId))
addPage(sourcePageEffective, getEditorInput());
else
addPage(pageCache.get(requiredId));
} else {
IFormPage existingPage = (IFormPage) pages.get(existingPointer);
if (!requiredId.equals(existingPage.getId())) {
if (SOURCE_PAGE.equals(requiredId))
addPage(existingPointer, sourcePage, getEditorInput());
else if (SOURCE_PAGE_EFFECTIVE.equals(requiredId))
addPage(existingPointer, sourcePageEffective, getEditorInput());
else
addPage(existingPointer, pageCache.get(requiredId));
}
Expand All @@ -204,6 +216,7 @@ private void updateIncludedPages() {

// Set the source page title
setPageText(sourcePage.getIndex(), "Source");
setPageText(sourcePageEffective.getIndex(), "Effective");

}

Expand All @@ -225,6 +238,7 @@ private static boolean isExtWorkspaceConfig(String path, String projectName) {
private IHandlerActivation resolveHandlerActivation;
private JobChangeAdapter resolveJobListener;


/*
* (non-Javadoc)
* @see bndtools.editor.IBndEditor#doSave(org.eclipse.core.runtime.
Expand Down Expand Up @@ -255,6 +269,7 @@ public void doSave(IProgressMonitor monitor) {
public void commitDirtyPages() {
if (sourcePage.isActive() && sourcePage.isDirty()) {
sourcePage.commit(true);
sourcePageEffective.setInput(this.model);
} else {
commitPages(true);
sourcePage.refresh();
Expand Down Expand Up @@ -677,6 +692,7 @@ private Promise<Workspace> loadEditModel(File inputFile, BndEditModel model) thr
private void initPages(IEditorSite site, IEditorInput input) throws PartInitException {
// Initialise pages
sourcePage = new BndSourceEditorPage(SOURCE_PAGE, this);
sourcePageEffective = new BndSourceEffectivePage(this, SOURCE_PAGE_EFFECTIVE, "Effective");
pageFactories.put(WORKSPACE_PAGE, WorkspacePage.MAIN_FACTORY);
pageFactories.put(WORKSPACE_EXT_PAGE, WorkspacePage.EXT_FACTORY);
pageFactories.put(CONTENT_PAGE, BundleContentPage.FACTORY);
Expand All @@ -700,6 +716,8 @@ private void initPages(IEditorSite site, IEditorInput input) throws PartInitExce
}
sourcePage.init(site, input);
sourcePage.initialize(this);
sourcePageEffective.init(site, input);
sourcePageEffective.initialize(this);
}

private void setPartNameForInput(IEditorInput input) {
Expand Down Expand Up @@ -784,6 +802,7 @@ public void resourceChanged(IResourceChangeEvent event) {
SWTConcurrencyUtil.execForDisplay(display, true, () -> {
setPartNameForInput(newInput);
sourcePage.setInput(newInput);
sourcePageEffective.setInput(this.model);
});
}
} else {
Expand Down
200 changes: 200 additions & 0 deletions bndtools.core/src/bndtools/editor/BndSourceEffectivePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
package bndtools.editor;

import java.util.ResourceBundle;
import java.util.Set;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.jdt.ui.JavaUI;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IFindReplaceTarget;
import org.eclipse.jface.text.TextViewer;
import org.eclipse.jface.text.source.CompositeRuler;
import org.eclipse.jface.text.source.LineNumberRulerColumn;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormEditor;
import org.eclipse.ui.forms.editor.FormPage;
import org.eclipse.ui.forms.widgets.Form;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.ScrolledForm;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.texteditor.FindReplaceAction;

import aQute.bnd.build.model.BndEditModel;
import aQute.bnd.exceptions.Exceptions;
import aQute.bnd.osgi.Processor;
import bndtools.editor.completion.BndSourceViewerConfiguration;

/**
* The 'Effective source' tab in a Bnd Editor which renders an effective
* readonly view of all properties of the BndEditModel. This means that it shows
* all available properties which are pulled in via various include-mechanisms.
* This it is a useful debugging tool to make things visible to the human eye.
*/
public class BndSourceEffectivePage extends FormPage {


private final BndEditor bndEditor;
private BndEditModel editModel;
private SourceViewer viewer;
private StyledText styledText;
private boolean loading;

public BndSourceEffectivePage(FormEditor formEditor, String id, String title) {
super(formEditor, id, title);
this.bndEditor = ((BndEditor) formEditor);
this.editModel = bndEditor.getModel();
}

@Override
protected void createFormContent(IManagedForm managedForm) {
FormToolkit toolkit = managedForm.getToolkit();
ScrolledForm scrolledForm = managedForm.getForm();
Form form = scrolledForm.getForm();
toolkit.setBorderStyle(SWT.NULL);

Composite body = form.getBody();
body.setLayout(new FillLayout());

// ruler for line numbers
CompositeRuler ruler = new CompositeRuler();
LineNumberRulerColumn ln = new LineNumberRulerColumn();
ln.setForeground(Display.getCurrent()
.getSystemColor(SWT.COLOR_DARK_GRAY));
ruler.addDecorator(0, ln);

this.viewer = new SourceViewer(body, ruler, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL) {
@Override
protected boolean canPerformFind() {
return true;
}
};
viewer.setDocument(new Document());
viewer.configure(new BndSourceViewerConfiguration(bndEditor, JavaUI.getColorManager()));
styledText = viewer.getTextWidget();
styledText.setEditable(false);
styledText.setFont(JFaceResources.getTextFont());

activateFindAndReplace(viewer, getSite(), this);


}

/**
* Setup Eclipse's built in Search / Replace dialog. Also see
* {@link #getAdapter(Class)}
*
* @param textViewer
* @param site
* @param page
*/
private static void activateFindAndReplace(TextViewer textViewer, IWorkbenchPartSite site,
IWorkbenchPart page) {
FindReplaceAction findReplaceAction = new FindReplaceAction(
ResourceBundle.getBundle("org.eclipse.ui.texteditor.ConstructedEditorMessages"), "Editor.FindReplace.",
page);
IHandlerService handlerService = site.getService(IHandlerService.class);
IHandler handler = new AbstractHandler() {

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
if (textViewer != null && textViewer.getDocument() != null) {
findReplaceAction.run();
}
return null;
}
};

handlerService.activateHandler("org.eclipse.ui.edit.findReplace", handler);
}


@Override
public boolean isEditor() {
return true;
}

@Override
public void setActive(boolean active) {
super.setActive(active);

IActionBars actionBars = getEditorSite().getActionBars();

if (active) {
update();
} else {
}
}

private static String print(BndEditModel model) throws Exception {
if (model == null) {
return "...";
}

try {
StringBuilder sb = new StringBuilder();

Processor p = new BndEditModel(model, true).getProperties();
Set<String> propertyKeys = p.getPropertyKeys(true);
for (String k : propertyKeys) {

String value = p.getProperty(k);
sb.append(k)
.append(": ")
.append(value)
.append("\n");
}

return sb.toString();
} catch (Exception e) {
throw Exceptions.duck(e);
}

}


private void update() {
if (loading || styledText == null || !isActive()) {
return;
}
loading = true;
try {
String text = print(editModel);
viewer.setDocument(new Document(text));
styledText.setFocus();
} catch (Exception e) {
throw Exceptions.duck(e);
}

}

public void setInput(BndEditModel model) {
this.editModel = model;
loading = false;
update();
}

@SuppressWarnings("unchecked")
@Override
public <T> T getAdapter(Class<T> adapter) {
if (IFindReplaceTarget.class.equals(adapter)) {
// needed for find/replace via CMD+F / Ctrl+F
return (T) viewer.getFindReplaceTarget();
}
return super.getAdapter(adapter);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
public abstract class ExtendedFormEditor extends FormEditor {

private ITextEditor sourcePage;

private ImageDescriptor baseImageDescriptor;
private Image titleImage;

Expand Down Expand Up @@ -116,4 +115,5 @@ protected void setSourcePage(ITextEditor sourcePage) {
this.sourcePage = sourcePage;
}


}

0 comments on commit 9c48429

Please sign in to comment.