Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor improvements #153

Merged
merged 3 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions plugins/de.cau.cs.kieler.kgraph.text/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Require-Bundle: org.eclipse.xtext;visibility:=reexport,
org.eclipse.emf.common,
org.eclipse.core.resources,
org.eclipse.elk.core;visibility:=reexport,
de.cau.cs.kieler.klighd.kgraph,
de.cau.cs.kieler.klighd.krendering;visibility:=reexport
de.cau.cs.kieler.klighd.kgraph;visibility:=reexport,
de.cau.cs.kieler.klighd.krendering;visibility:=reexport,
de.cau.cs.kieler.klighd;resolution:=optional
Import-Package: org.apache.log4j
DynamicImport-Package: de.cau.cs.kieler.klighd.test
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Modifier;
import java.util.Map;
import java.util.stream.Stream;

import org.eclipse.elk.graph.properties.IProperty;
import org.eclipse.elk.graph.properties.Property;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.EcoreUtil2;
import org.eclipse.xtext.linking.lazy.LazyLinkingResource;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.resource.SaveOptions;

import com.google.common.collect.ImmutableList;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

import de.cau.cs.kieler.klighd.kgraph.KGraphElement;
import de.cau.cs.kieler.klighd.kgraph.EMapPropertyHolder;
import de.cau.cs.kieler.klighd.kgraph.KLayoutData;
import de.cau.cs.kieler.klighd.kgraph.KNode;
import de.cau.cs.kieler.klighd.kgraph.PersistentEntry;
import de.cau.cs.kieler.klighd.kgraph.util.KGraphDataUtil;
import de.cau.cs.kieler.klighd.kgraph.util.KGraphUtil;
import de.cau.cs.kieler.klighd.krendering.KRendering;

/**
* A customized {@link LazyLinkingResource} handling the (de-) serialization of
Expand All @@ -50,27 +54,41 @@
*/
public class KGraphResource extends LazyLinkingResource {

/**
* Default values for {@link KGraphElement}'s, see
* {@link de.cau.cs.kieler.klighd.xtext.transformations.KGraphDiagramSynthesis
* KGraphDiagramSynthesis}
*/
private static final IProperty<Boolean> DEFAULTS =
new Property<Boolean>("de.cau.cs.kieler.kgraphsynthesis.defaults", false);

/**
* KLighD offers the possibility to display tooltips that need to be parsed
* from persistent entries and added to the {@link KGraphElement}'s properties.
*/
private static final IProperty<String> TOOLTIP =
new Property<String>("klighd.tooltip");

private static final Predicate<EMapPropertyHolder> HANDLED_TYPES_FILTER =
e -> e instanceof KLayoutData || e instanceof KRendering;

/**
* Additional properties known to the kgraph text format that are no layout options. However,
* they are made available through content assist and are parsed properly.
*/
public static final IProperty<?>[] ADDITIONAL_PROPERTIES = ImmutableList.of(DEFAULTS, TOOLTIP).toArray(
new IProperty<?>[2]);
public static final IProperty<?>[] ADDITIONAL_PROPERTIES = getAdditionalProperties();

private static IProperty<?>[] getAdditionalProperties() {
try {
// traditionally we didn't have a hard dependency from the textual syntax implementation
// to the KLighD base package, so try to access the 'KlighdProperties' and its members via reflection
// (the optional dependency on 'de.cau.cs.kieler.klighd' will bring it into the runtime class path)
return Stream.of(
Class.forName("de.cau.cs.kieler.klighd.util.KlighdProperties").getDeclaredFields()
).filter(
f -> Modifier.isPublic(f.getModifiers()) && Modifier.isStatic(f.getModifiers())
&& IProperty.class.isAssignableFrom(f.getType())
).map(
f -> {
try {
return f.get(null);
} catch (IllegalArgumentException | IllegalAccessException e) {
return null;
}
}
).filter(
Predicates.notNull()
).toArray(IProperty<?>[]::new);

} catch (Throwable t) {
return new IProperty<?>[0];
}
};

/**
* {@inheritDoc}<br>
Expand All @@ -83,7 +101,7 @@ protected void doLoad(final InputStream inputStream, final Map<?, ?> options) th
EObject o = this.getContents().get(0);
if (o instanceof KNode) {
// parse persisted key-value pairs using KIML's layout data service
KGraphDataUtil.loadDataElements((KNode) o, ADDITIONAL_PROPERTIES);
KGraphDataUtil.loadDataElements((KNode) o, HANDLED_TYPES_FILTER, ADDITIONAL_PROPERTIES);
// validate layout data and references and fill in missing data
KGraphUtil.validate((KNode) o);
}
Expand Down Expand Up @@ -119,6 +137,7 @@ public void update(final int offset, final int replacedTextLength, final String
* {@link IProperty}s into
* {@link PersistentEntry}s.
*/
@SuppressWarnings("deprecation")
public void doSave(final OutputStream outputStream, final Map<?, ?> options) throws IOException {

if (!this.getContents().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,12 @@ public void setImage(final URL url) {
setImage(url.openStream()).close();

KlighdPiccoloPlugin.getDefault().getImageRegistry().put(imageKey,
ImageDescriptor.createFromImageData(imageData));
ImageDescriptor.createFromImageDataProvider(
// lambda resembles the behavior of deprecated constructor
// org.eclipse.jface.resource.ImageDataImageDescriptor#ImageDataImageDescriptor(ImageData)
i -> i == 100 ? imageData : null
)
);

} catch (final Exception e) {
final String msg =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.eclipse.elk.graph.properties.IPropertyHolder;
import org.eclipse.elk.graph.properties.MapPropertyHolder;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
Expand Down Expand Up @@ -544,10 +545,25 @@ public void doSaveAs() {
* @return the loaded model for convenience
*/
protected Object loadModel() throws PartInitException {
final EObject model = loadModel(getEditorInput());
this.model = model;
this.resourceSet = model.eResource().getResourceSet();
configureResourceSet(this.resourceSet);
return model;
}

/**
* Load a model from the editor input. The result is put into {@link #model}.
*
* @throws PartInitException
* if loading the model fails
*
* @return the loaded model for convenience
*/
protected static EObject loadModel(final IEditorInput input) throws PartInitException {
// get a URI or an input stream from the editor input
URI uri = null;
InputStream inputStream = null;
final IEditorInput input = getEditorInput();
if (input instanceof IFileEditorInput) {
final IFile file = ((IFileEditorInput) input).getFile();
uri = URI.createPlatformResourceURI(file.getFullPath().toString(), false);
Expand All @@ -571,8 +587,7 @@ protected Object loadModel() throws PartInitException {

final Resource resource;
try {
resourceSet = new ResourceSetImpl();
configureResourceSet(resourceSet);
final ResourceSet resourceSet = new ResourceSetImpl();
if (inputStream != null) {
// load a stream-based resource
uri = URI.createFileURI("temp.xmi");
Expand All @@ -593,9 +608,7 @@ protected Object loadModel() throws PartInitException {
throw new PartInitException("The resource is empty.");
}
// default behavior: get the first element in the resource
model = resource.getContents().get(0);

return model;
return resource.getContents().get(0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.part.ResourceTransfer;
import org.eclipse.ui.part.ViewPart;

import de.cau.cs.kieler.klighd.IDiagramWorkbenchPart;
import de.cau.cs.kieler.klighd.IViewer;
import de.cau.cs.kieler.klighd.Klighd;
import de.cau.cs.kieler.klighd.LightDiagramLayoutConfig;
import de.cau.cs.kieler.klighd.ViewContext;
import de.cau.cs.kieler.klighd.internal.ILayoutConfigProvider;
Expand All @@ -48,6 +54,7 @@
import de.cau.cs.kieler.klighd.ui.internal.options.DiagramSideBar;
import de.cau.cs.kieler.klighd.ui.printing.PrintAction;
import de.cau.cs.kieler.klighd.ui.viewers.UiContextViewer;
import de.cau.cs.kieler.klighd.util.KlighdSynthesisProperties;
import de.cau.cs.kieler.klighd.viewers.ContextViewer;

/**
Expand Down Expand Up @@ -314,9 +321,20 @@ protected void addButtons() {
toolBar.add(new Action("Refresh diagram",
KlighdUIPlugin.getImageDescriptorFromKlighdBase("icons/full/elcl16/refresh.gif")) {

final DiagramViewPart part = DiagramViewPart.this;

@Override
public void run() {
if (part.getViewContext() == null) {
final Object model = part.loadModel();
if (model != null)
part.initialize(model, DEFAULT_NAME, configureKlighdProperties());

return;
}

new LightDiagramLayoutConfig(DiagramViewPart.this.getViewContext())
.model(part.loadModel())
.animate(false)
.performUpdate();
}
Expand Down Expand Up @@ -346,6 +364,34 @@ public void run() {
menu.add(resetLayoutOptionsAction);
}

protected Object loadModel() {
try {
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
final IWorkbenchPage page = window.getActivePage() != null ? window.getActivePage() : window.getPages()[0];
final IEditorInput input = page == null
? null : page.getActiveEditor() != null
? page.getActiveEditor().getEditorInput() : page.getEditorReferences().length == 0
? null : page.getEditorReferences()[0].getEditorInput();
return input == null ? null : DiagramEditorPart.loadModel(input);

} catch (PartInitException e) {
Klighd.handle(e, KlighdUIPlugin.PLUGIN_ID);
return null;
}
}

/**
* Returns a configuration for the KLighD view. Override this method to use a custom
* configuration.
*
* @return KLighD configuration or <code>null</code>.
*
* @see {@link KlighdSynthesisProperties} for more details
*/
protected KlighdSynthesisProperties configureKlighdProperties() {
return null;
}

/**
* Registers the default print support, may be overriden if necessary.
*/
Expand Down