[ofbiz-framework] branch trunk updated: Improved: Open Screen file from browser (OFBIZ-12038)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[ofbiz-framework] branch trunk updated: Improved: Open Screen file from browser (OFBIZ-12038)

James Yong-2
This is an automated email from the ASF dual-hosted git repository.

jamesyong pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 0e40d9f  Improved: Open Screen file from browser (OFBIZ-12038)
0e40d9f is described below

commit 0e40d9f091aa2bd2a5fff3761049eb8ea10a68c1
Author: James Yong <[hidden email]>
AuthorDate: Thu Oct 8 15:28:43 2020 +0800

    Improved: Open Screen file from browser (OFBIZ-12038)
   
     Allow the opening of screen definition that contributed to the web page from the browser. Command to open the file is set at widget.dev.cmd.openSourceFile.
   
    Thanks Jacques for review.
---
 .../java/org/apache/ofbiz/common/CommonEvents.java | 40 ++++++++++++++++++----
 framework/widget/config/widget.properties          |  9 ++---
 .../renderer/macro/MacroScreenViewHandler.java     | 11 ++++++
 3 files changed, 49 insertions(+), 11 deletions(-)

diff --git a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
index 20ddb6d..89aad37 100644
--- a/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
+++ b/framework/common/src/main/java/org/apache/ofbiz/common/CommonEvents.java
@@ -26,8 +26,10 @@ import java.awt.geom.AffineTransform;
 import java.awt.image.BufferedImage;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.LineNumberReader;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
 import java.net.URL;
@@ -464,21 +466,45 @@ public class CommonEvents {
             if (UtilValidate.isNotEmpty(sourceLocation) && sourceLocation.startsWith("component:")) {
                 try {
                     // find absolute path of file
-                    URL sourceFileUrl = FlexibleLocation.resolveLocation(sourceLocation);
-                    String location = sourceFileUrl.getFile();
+                    URL sourceFileUrl = null;
+                    String fragment = "";
+                    if (sourceLocation.contains("#")) {
+                        int indexOfHash = sourceLocation.indexOf("#");
+                        sourceFileUrl = FlexibleLocation.resolveLocation(sourceLocation.substring(0, indexOfHash));
+                        fragment = sourceLocation.substring(indexOfHash + 1);
+                    } else {
+                        sourceFileUrl = FlexibleLocation.resolveLocation(sourceLocation);
+                    }
+                    String platformSpecificPath = sourceFileUrl.getFile();
                     // ensure file separator in location is correct
-                    if (!location.contains(File.separator) && "\\".equals(File.separator)) {
-                        location = location.replaceAll("/", "\\\\");
+                    if (!platformSpecificPath.contains(File.separator) && "\\".equals(File.separator)) {
+                        platformSpecificPath = platformSpecificPath.replaceAll("/", "\\\\");
+                    }
+                    // get line number
+                    int lineNumber = 1;
+                    if (UtilValidate.isNotEmpty(fragment)) {
+                        try (LineNumberReader lnr = new LineNumberReader(new FileReader(platformSpecificPath))) {
+                            String line;
+                            while ((line = lnr.readLine()) != null) {
+                                if (line.matches(".*name=\"" + fragment + "\".*")) {
+                                    lineNumber = lnr.getLineNumber();
+                                    break;
+                                }
+                            }
+                        } catch (IOException e) {
+                            Debug.logError(e, MODULE);
+                        }
                     }
-                    location = "\"" + location + "\"";
                     // prepare content map for string expansion
                     Map<String, Object> sourceMap = new HashMap<>();
-                    sourceMap.put("sourceLocation", location);
+                    sourceMap.put("sourceLocation", platformSpecificPath);
+                    sourceMap.put("lineNumber", lineNumber);
                     // get command to run
                     String cmdTemplate = UtilProperties.getPropertyValue("widget", "widget.dev.cmd.openSourceFile");
                     String cmd = (String) FlexibleStringExpander.getInstance(cmdTemplate).expand(sourceMap);
                     // run command
-                    Process process = Runtime.getRuntime().exec(String.format(cmd, location));
+                    Debug.logInfo("Run command: " + cmd, MODULE);
+                    Process process = Runtime.getRuntime().exec(cmd);
                     // print result
                     BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
                     String line = "";
diff --git a/framework/widget/config/widget.properties b/framework/widget/config/widget.properties
index dc1be2c..85b424c 100644
--- a/framework/widget/config/widget.properties
+++ b/framework/widget/config/widget.properties
@@ -33,10 +33,11 @@ widget.verbose=true
 # SOURCE - Show named border with link to open the source code
 widget.dev.namedBorder=NONE
 
-# Command template to open file with editor. Make sure the program is in PATH.
-# idea ${sourceLocation}
-# eclipse ${sourceLocation}
-widget.dev.cmd.openSourceFile=idea ${sourceLocation}
+# Command template to open file with editor.
+# Make sure the editor is in PATH, and supports opening-of-file and go-to-line thru command line
+# idea --line ${lineNumber} "${sourceLocation}"
+# eclipse "${sourceLocation}:${lineNumber}"
+widget.dev.cmd.openSourceFile=idea --line ${lineNumber} "${sourceLocation}"
 
 # Default number of items to be displayed per page in a list form
 widget.form.defaultViewSize=20
diff --git a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroScreenViewHandler.java b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroScreenViewHandler.java
index 6972238..444eec2 100644
--- a/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroScreenViewHandler.java
+++ b/framework/widget/src/main/java/org/apache/ofbiz/widget/renderer/macro/MacroScreenViewHandler.java
@@ -36,12 +36,14 @@ import org.apache.ofbiz.base.util.collections.MapStack;
 import org.apache.ofbiz.webapp.view.AbstractViewHandler;
 import org.apache.ofbiz.webapp.view.ViewHandlerException;
 import org.apache.ofbiz.widget.model.ModelTheme;
+import org.apache.ofbiz.widget.model.ModelWidget;
 import org.apache.ofbiz.widget.renderer.FormStringRenderer;
 import org.apache.ofbiz.widget.renderer.MenuStringRenderer;
 import org.apache.ofbiz.widget.renderer.ScreenRenderer;
 import org.apache.ofbiz.widget.renderer.ScreenStringRenderer;
 import org.apache.ofbiz.widget.renderer.TreeStringRenderer;
 import org.apache.ofbiz.widget.renderer.VisualTheme;
+import org.apache.ofbiz.widget.renderer.html.HtmlWidgetRenderer;
 import org.xml.sax.SAXException;
 
 import freemarker.template.TemplateException;
@@ -113,7 +115,16 @@ public class MacroScreenViewHandler extends AbstractViewHandler {
             context.put("screens", screens);
             context.put("simpleEncoder", UtilCodec.getEncoder(visualTheme.getModelTheme().getEncoder(getName())));
             screenStringRenderer.renderScreenBegin(writer, context);
+            // render start of named border for screen
+            ModelWidget.NamedBorderType widgetNamedBorderType = ModelWidget.widgetNamedBorderType();
+            if (widgetNamedBorderType != ModelWidget.NamedBorderType.NONE) {
+                writer.append(HtmlWidgetRenderer.buildNamedBorder("Begin", "Screen", page, widgetNamedBorderType, request.getContextPath()));
+            }
             screens.render(page);
+            // render end of named border for screen
+            if (widgetNamedBorderType != ModelWidget.NamedBorderType.NONE) {
+                writer.append(HtmlWidgetRenderer.buildNamedBorder("End", "Screen", page, widgetNamedBorderType, request.getContextPath()));
+            }
             screenStringRenderer.renderScreenEnd(writer, context);
             writer.flush();
         } catch (TemplateException e) {