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

work on getting all the embedded screens from a root screen #3155

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.csstudio.display.builder.model.WidgetProperty;
import org.csstudio.display.builder.model.persist.ModelReader;
import org.csstudio.display.builder.model.properties.ActionInfos;
import org.csstudio.display.builder.model.properties.FilenameWidgetProperty;
import org.csstudio.display.builder.model.spi.ActionInfo;
import org.csstudio.display.builder.model.util.ModelResourceUtil;
import org.csstudio.display.actions.OpenDisplayAction;
Expand All @@ -31,13 +32,17 @@ public class ProcessOPI {

private final File rootFile;
private final Set<File> allLinkedFiles;
private final Set<File> allEmbeddedFiles;



/**
* @param rootFile Start of the navigation tree
*/
public ProcessOPI(File rootFile) {
this.rootFile = rootFile;
this.allLinkedFiles = new HashSet<>();
this.allEmbeddedFiles = new HashSet<>();
}

/**
Expand All @@ -52,13 +57,14 @@ public Set<File> process() {
}, () -> {
throw new UnsupportedOperationException("File extension unknown");
});
System.out.println("Processing file : " + this.rootFile);
logger.log(Level.INFO, "Processing file : " + this.rootFile);
getAllLinkedFiles(this.rootFile);
return this.allLinkedFiles;
}

private synchronized void getAllLinkedFiles(File file) {
System.out.println("Calculating linked files for " + file.getName());
public synchronized void getAllLinkedFiles(File file) {
logger.log(Level.INFO, "Calculating linked files for " + file.getName());

Set<File> linkedFiles = getLinkedFiles(file);
linkedFiles.forEach(f -> {
if (allLinkedFiles.contains(f) || f.equals(rootFile)) {
Expand All @@ -71,6 +77,13 @@ private synchronized void getAllLinkedFiles(File file) {
});
}

/**
* A Utility method which creates a list of all the files that can be launched from a given OPI.
* It only inlcudes files launched via actions.
*
* @param file root OPI file
* @return a unique Set of all files that can be reached from root OPI file
*/
public static synchronized Set<File> getLinkedFiles(File file) {
Set<File> result = new HashSet<>();
try {
Expand Down Expand Up @@ -113,6 +126,78 @@ public static synchronized Set<File> getLinkedFiles(File file) {
return result;
}


/**
* Gets All the files embedded in the rootFile
* This call should be made on a separate thread since it may take some time to process all the linked files
*/
public Set<File> processEmbedded() {
getExtensionByStringHandling(this.rootFile.getName()).ifPresentOrElse(ext -> {
if (!ext.equalsIgnoreCase("bob") && !ext.equalsIgnoreCase("opi")) {
throw new UnsupportedOperationException("File extension " + ext + " is not supported. The supported extensions are .bob and .opi.");
}
}, () -> {
throw new UnsupportedOperationException("File extension unknown");
});
logger.log(Level.INFO, "Processing file : " + this.rootFile);
getAllEmbeddedFiles(this.rootFile);
return this.allEmbeddedFiles;
}

public synchronized void getAllEmbeddedFiles(File file) {
logger.log(Level.INFO, "Calculating embedded files for " + file.getName());

Set<File> embeddedFiles = getEmbeddedFiles(file);
embeddedFiles.forEach(f -> {
if (allEmbeddedFiles.contains(f) || f.equals(rootFile)) {
// Already handled skip it
} else {
// Find all the linked files for this file
allEmbeddedFiles.add(f);
getAllEmbeddedFiles(f);
}
});
}

/**
* A Utility method which creates a list of all the embedded files that can be launched from a given OPI.
*
* @param file root OPI file
* @return a unique Set of all embedded files in root OPI file
*/
public static synchronized Set<File> getEmbeddedFiles(File file) {
Set<File> result = new HashSet<>();
try {
ModelReader reader = new ModelReader(new FileInputStream(file));
DisplayModel model = reader.readModel();
List<Widget> children = model.getChildren();

children.forEach(widget -> {
// Find all the action properties
Optional<WidgetProperty<Object>> foundfile = widget.checkProperty("file");

foundfile.ifPresent(f -> {
try {
FilenameWidgetProperty wp = (FilenameWidgetProperty) widget.getProperty("file");
// // For display path, use the combined macros...
// String expanded_path = MacroHandler.replace(widget.getEffectiveMacros(), f.getPath());
// // .. but fall back to properties
// expanded_path = MacroHandler.replace(widget.getMacrosOrProperties(), expanded_path);

String resource = ModelResourceUtil.resolveResource(file.getPath(), f.getValue().toString());
result.add(new File(resource));
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to resolve macros for : " + f, e);
}
});
});
return result;
} catch (Exception e) {
logger.log(Level.WARNING, "Failed to getLinkedFiles for file " + file.getPath(), e);
}
return result;
}

private Optional<String> getExtensionByStringHandling(String filename) {
return Optional.ofNullable(filename)
.filter(f -> f.contains("."))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.phoebus.applications.display.navigation;


import org.junit.jupiter.api.Test;

import java.io.File;
import java.util.HashSet;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* A utility Class for handling the navigation of .bob files
*/
public class ProcessEmbeddedOPITest {


@Test
public void testEmbeddedList()
{
// The child_1 and grandchild_1_1 have a cyclic link. The test ensures that the checking for linked files
// does avoid entering an infinite loop
File file = new File(getClass().getClassLoader().getResource("bob/embedded/root_embedded.bob").getFile());
ProcessOPI processOPI = new ProcessOPI(file);
Set<File> result = processOPI.processEmbedded();

result.forEach(System.out::println);
// Expected results
Set<File> expectedFiles = new HashSet<>();
expectedFiles.add(new File(getClass().getClassLoader().getResource("bob/embedded/level_1_embedded.bob").getFile()));
expectedFiles.add(new File(getClass().getClassLoader().getResource("bob/embedded/level_2_embsedded.bob").getFile()));

assertThat(result, is(expectedFiles));
// assertTrue("Failed to find the linked files. expected " + " found " , result.c);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Saved on 2024-10-03 15:21:50 by shroffk-->
<display version="2.0.0">
<name>level_1_embedded</name>
<width>400</width>
<height>150</height>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Level 1</text>
<width>400</width>
<height>50</height>
<font>
<font name="Oddball" family="Comic Sans MS" style="REGULAR" size="40.0">
</font>
</font>
<horizontal_alignment>1</horizontal_alignment>
<vertical_alignment>1</vertical_alignment>
</widget>
<widget type="embedded" version="2.0.0">
<name>Embedded Display</name>
<file>level_2_embedded.bob</file>
<y>50</y>
<height>50</height>
</widget>
<widget type="embedded" version="2.0.0">
<name>Embedded Display_1</name>
<file>level_2_embedded.bob</file>
<y>100</y>
<height>50</height>
</widget>
</display>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Saved on 2024-10-03 15:20:01 by shroffk-->
<display version="2.0.0">
<name>level_2_embedded</name>
<width>400</width>
<height>50</height>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Level 2</text>
<width>400</width>
<height>50</height>
<font>
<font name="Header 1" family="Liberation Sans" style="BOLD" size="22.0">
</font>
</font>
<horizontal_alignment>1</horizontal_alignment>
<vertical_alignment>1</vertical_alignment>
</widget>
</display>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Saved on 2024-10-03 15:25:39 by shroffk-->
<display version="2.0.0">
<name>Display</name>
<widget type="embedded" version="2.0.0">
<name>Embedded Display</name>
<file>level_1_embedded.bob</file>
<y>50</y>
<height>150</height>
</widget>
<widget type="embedded" version="2.0.0">
<name>Embedded Display_1</name>
<file>level_1_embedded.bob</file>
<x>400</x>
<y>50</y>
<height>150</height>
</widget>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>ROOT</text>
<x>190</x>
<width>350</width>
<height>50</height>
<font>
<font family="Liberation Sans" style="BOLD_ITALIC" size="24.0">
</font>
</font>
<horizontal_alignment>1</horizontal_alignment>
</widget>
</display>
31 changes: 31 additions & 0 deletions app/display/navigation/src/test/resources/bob/root_embedded.bob
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--Saved on 2024-10-03 15:24:06 by shroffk-->
<display version="2.0.0">
<name>root_embedded</name>
<height>300</height>
<widget type="label" version="2.0.0">
<name>Label</name>
<text>Root</text>
<width>800</width>
<height>50</height>
<font>
<font family="Liberation Sans" style="BOLD_ITALIC" size="24.0">
</font>
</font>
<horizontal_alignment>1</horizontal_alignment>
<vertical_alignment>1</vertical_alignment>
</widget>
<widget type="embedded" version="2.0.0">
<name>Embedded Display</name>
<file>embedded/level_1_embedded.bob</file>
<y>50</y>
<height>150</height>
</widget>
<widget type="embedded" version="2.0.0">
<name>Embedded Display_1</name>
<file>embedded/level_1_embedded.bob</file>
<x>400</x>
<y>50</y>
<height>150</height>
</widget>
</display>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
</execution>
</executions>
<configuration>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
<dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
<prefix>git</prefix>
<verbose>false</verbose>
Expand Down
Loading