Skip to content

Commit

Permalink
enable json schema selection
Browse files Browse the repository at this point in the history
  • Loading branch information
rainer-prosi committed Oct 16, 2024
1 parent 99883f0 commit c9b48d4
Show file tree
Hide file tree
Showing 15 changed files with 183 additions and 107 deletions.
10 changes: 5 additions & 5 deletions src/main/java/org/cip4/tools/jdfeditor/CheckJDFScrollPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,17 @@ public void drawCheckJDFOutputTree(final XMLDoc bugReport)
public void drawJSONSchemaOutputTree(final XMLDoc bugReport)
{
final KElement repRoot = bugReport.getRoot();
final JSONSchemaOutputWrapper checkJDFRoot = new JSONSchemaOutputWrapper(repRoot);
m_reportTree = new JTree(checkJDFRoot);
m_reportTree.setModel(new JDFTreeModel(checkJDFRoot, false));
final JSONSchemaOutputWrapper jsonOutputRoot = new JSONSchemaOutputWrapper(repRoot);
m_reportTree = new JTree(jsonOutputRoot);
m_reportTree.setModel(new JDFTreeModel(jsonOutputRoot, false));
m_reportTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
m_reportTree.setExpandsSelectedPaths(true);
m_reportTree.setEditable(false);
ToolTipManager.sharedInstance().registerComponent(m_reportTree);

setJSONSchemaOutputTree(checkJDFRoot);
setJSONSchemaOutputTree(jsonOutputRoot);
// m_reportTree.expandPath(new TreePath(bugReportRoot.getPath()));
m_reportTree.expandPath(new TreePath(checkJDFRoot.getPath()));
m_reportTree.expandPath(new TreePath(jsonOutputRoot.getPath()));

m_SelectionListener = new ValidationSelectionListener();
m_reportTree.addTreeSelectionListener(m_SelectionListener);
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/cip4/tools/jdfeditor/EditorButtonBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,10 @@ public EditorButtonBar(final JDFFrame frame)
}

/**
* Method drawButtonBar.
* Editor.java contains ICONS_PATH. This package can be found under JDFEditor/src/Java/org.cip4.jdfeditor.icons
* You can add icons to this packagae by putting your .gif files into the following folder. This folder comes from whereever you
* downloaded yoru code from SubVersion. My place (BIskey) is the reference point. You can make it whatever you like.
* C:\Subversion_Root\My_Root\JDFEditor\src\java\org\cip4\jdfeditor\icons
* After adding the icons to this folder, you need to come here and refresh the package w/in your Java editing software.
* Method drawButtonBar. Editor.java contains ICONS_PATH. This package can be found under JDFEditor/src/Java/org.cip4.jdfeditor.icons You can add icons to this packagae by
* putting your .gif files into the following folder. This folder comes from whereever you downloaded yoru code from SubVersion. My place (BIskey) is the reference point. You
* can make it whatever you like. C:\Subversion_Root\My_Root\JDFEditor\src\java\org\cip4\jdfeditor\icons After adding the icons to this folder, you need to come here and
* refresh the package w/in your Java editing software.
*/
public void drawButtonBar()
{
Expand Down Expand Up @@ -248,7 +246,8 @@ public void drawButtonBar()

/**
* Creates a JButton with the default settings.
* @param tip - The tool tip
*
* @param tip - The tool tip
* @param enabled - If the button is enabled or disabled initially
* @return The default JButton.
*/
Expand Down Expand Up @@ -352,6 +351,7 @@ public void setEnableOpen(final boolean mode)
///////////////////////////////////////////////////////////////
/**
* Mother of all action dispatchers
*
* @param e the event that gets checked
*/
@Override
Expand All @@ -370,7 +370,7 @@ else if (eSrc == m_openButton) // open document
}
else if (eSrc == m_saveButton) // save document
{
if (m_frame.getTitle().contains("Untitled.j") /*|| m_frame.getTitle().equalsIgnoreCase("Untitled.jmf")*/)
if (m_frame.getTitle().contains("Untitled.j") /* || m_frame.getTitle().equalsIgnoreCase("Untitled.jmf") */)
{
m_frame.saveAs();
}
Expand All @@ -391,11 +391,11 @@ else if (eSrc == m_convert2Jdf) // convert 2 JDF
}
else if (eSrc == m_convert2XJdf) // convert 2 XJDF
{
MainView.getModel().saveAsXJDF(null, true);
MainView.getModel().saveAsXJDF(null, false);
}
else if (eSrc == m_convert2JSON) // convert 2 json
{
MainView.getModel().saveAsJSON(null);
MainView.getModel().saveAsJSON(null, false);
}
else if (eSrc == m_upOneLevelButton) // navigate up
{
Expand Down
19 changes: 10 additions & 9 deletions src/main/java/org/cip4/tools/jdfeditor/EditorDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,19 @@ public boolean isJSONEnabled()
*/
public void setJson(final boolean json, final boolean checkFile)
{
if (jdfDoc != null && checkFile)
if (this.json != json)
{

final String extension = EditorUtils.getExtension(jdfDoc.getRoot(), json);
final String newExtension = UrlUtil.newExtension(getOriginalFileName(), extension);
if (!checkSave(UrlUtil.urlToFile(newExtension)))
return;
if (jdfDoc != null && checkFile)
{
if (!checkSave(UrlUtil.urlToFile(newExtension)))
return;
}
this.json = json;

jdfDoc.setOriginalFileName(newExtension);

final JDFFrame frame = MainView.getFrame();
frame.refreshView(this, null);

Expand All @@ -199,11 +202,7 @@ public void setJson(final boolean json, final boolean checkFile)
frame.getJDFTreeArea().drawTreeView(this);

frame.setEnableOpen(true);
saveFile(null);
}
else
{
this.json = json;
setDirtyFlag();
}
}

Expand Down Expand Up @@ -349,6 +348,8 @@ public void setModel(final JDFTreeModel model)
*/
public JDFTreeModel getModel()
{
if (jdfTreeModel == null)
createModel();
return jdfTreeModel;
}

Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/cip4/tools/jdfeditor/EditorFileChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JTextField;

import org.cip4.jdflib.core.VString;
import org.cip4.jdflib.util.StringUtil;
import org.cip4.tools.jdfeditor.util.ResourceUtil;

/**
* generic file chooser for the editor
*
* @author prosirai
*
*/
Expand Down Expand Up @@ -143,4 +145,23 @@ private void setFilters(final String fileTypes)
}
setFileFilter(xmlFilterAll);
}

/**
* @param schemaPath
* @return the newly set schema file, if it is readable
*/
public static File getSchemaURL(final JTextField schemaPath)
{
final String s = schemaPath.getText();
if (!StringUtil.isEmpty(s))
{
final File f = new File(s);
if (f.canRead())
{
return f;
}
}
return null;
}

}
5 changes: 4 additions & 1 deletion src/main/java/org/cip4/tools/jdfeditor/JDFFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
import org.cip4.jdflib.util.StringUtil;
import org.cip4.jdflib.util.file.UserDir;
import org.cip4.tools.jdfeditor.controller.MainController;
import org.cip4.tools.jdfeditor.dialog.PreferenceDialog;
import org.cip4.tools.jdfeditor.model.enumeration.SettingKey;
import org.cip4.tools.jdfeditor.service.DocumentService;
import org.cip4.tools.jdfeditor.util.RecentFileUtil;
Expand Down Expand Up @@ -1816,14 +1817,16 @@ public int setJDFDoc(final JDFDoc doc, final String mimePackage)
if (doc != null)
{
i = documentService.indexOfJDF(doc, m_VjdfDocument);
final EditorDocument ed = new EditorDocument(doc, mimePackage);

if (i >= 0)
{
m_DocPos = i;
m_VjdfDocument.set(i, ed);
}
else
{
m_VjdfDocument.add(new EditorDocument(doc, mimePackage));
m_VjdfDocument.add(ed);
m_DocPos = m_VjdfDocument.size() - 1;
// make sure that we have a global dirty policy in force
doc.getCreateXMLDocUserData().setDirtyPolicy(EnumDirtyPolicy.Doc);
Expand Down
40 changes: 28 additions & 12 deletions src/main/java/org/cip4/tools/jdfeditor/JDFTreeModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,10 @@ boolean validateJSON()
{
final EditorDocument eDoc = MainView.getEditorDoc();
final String json = eDoc.getJSONString(eDoc.getJDFDoc().getRoot(), 0);
final JSONSchemaReader sr = new JSONSchemaReader(EditorUtils.RES_SCHEMA_JSON);
final JSONSchemaReader sr = new JSONSchemaReader(getJsonschemaURL());
final Collection<ValidationMessage> results = sr.checkJSON(json);
validationResult = null;
final KElement jsonSchemaResult = KElement.createRoot("JSONSchema", null);
jsonSchemaResult.setAttribute("SchemaLocation", EditorUtils.RES_SCHEMA_JSON);
jsonSchemaResult.setAttribute("SchemaLocation", getJsonschemaURL());
jsonSchemaResult.setAttribute("ValidationResult", ContainerUtil.isEmpty(results) ? "Valid" : "Error");
for (final ValidationMessage m : results)
{
Expand All @@ -310,6 +309,10 @@ boolean validateJSON()
final KElement error = jsonSchemaResult.appendElement(typ);
error.setAttribute("Message", m.toString());
error.setAttribute("Path", loc);
if (m.getInstanceNode() != null)
{
error.setNonEmpty("Value", m.getInstanceNode().asText());
}
}
final JDFFrame m_frame = MainView.getFrame();
m_frame.getBottomTabs().m_validErrScroll.drawJSONSchemaOutputTree(jsonSchemaResult.getOwnerDocument_KElement());
Expand All @@ -321,6 +324,16 @@ boolean validateJSON()
return ContainerUtil.isEmpty(results);
}

String getJsonschemaURL()
{
final String file = SettingService.getSettingService().getString(SettingKey.JSON_SCHEMA_URL);
if (!StringUtil.isEmpty(file))
{
return UrlUtil.fileToUrl(new File(file), false);
}
return EditorUtils.RES_SCHEMA_JSON;
}

protected boolean validateJDF()
{
final EditorDocument eDoc = MainView.getEditorDoc();
Expand Down Expand Up @@ -416,7 +429,6 @@ protected boolean validateXJDF()
final JDFDoc theDoc = eDoc.getJDFDoc();

validationResult = null;
XMLDoc schemaValidationResult = null;
try
{
final ByteArrayIOStream outStream = new ByteArrayIOStream();
Expand All @@ -435,7 +447,7 @@ protected boolean validateXJDF()

if (tmpDoc != null)
{
schemaValidationResult = tmpDoc.getValidationResult();
validationResult = tmpDoc.getValidationResult();
}
}
catch (final Exception e)
Expand All @@ -444,13 +456,13 @@ protected boolean validateXJDF()
}

final JDFFrame m_frame = MainView.getFrame();
m_frame.getBottomTabs().m_SchemaErrScroll.drawSchemaOutputTree(schemaValidationResult);
m_frame.getBottomTabs().m_SchemaErrScroll.drawSchemaOutputTree(validationResult);
if (MainView.getEditorDoc().getJDFTree() != null)
{
MainView.getEditorDoc().getJDFTree().repaint();
m_frame.getJDFTreeArea().goToPath(m_frame.getJDFTreeArea().getSelectionPath()); // TODO: what this code actually do ?
}
return schemaValidationResult == null || "Valid".equals(schemaValidationResult.getRoot().getAttribute("ValidationResult"));
return validationResult == null || "Valid".equals(validationResult.getRoot().getAttribute("ValidationResult"));
}

/**
Expand Down Expand Up @@ -1365,7 +1377,7 @@ public void saveAsXJDF(final TreePath selectionPath, final boolean reallysave)
if (xjdf)
{
log.info("converting JSON to XJDF");
eDoc.setJson(false, true);
eDoc.setJson(false, reallysave);
}
else
{
Expand Down Expand Up @@ -1406,10 +1418,10 @@ else if (e instanceof JDFJMF)
{
final JDFDoc doc = new JDFDoc(d);
fnNew = FilenameUtils.getName(fnNew);

doc.setOriginalFileName(EditorUtils.getNewPath(fnNew));
MainView.getFrame().setJDFDoc(doc, null);

MainView.getEditorDoc().setDirtyFlag();
MainView.getFrame().refreshView(MainView.getEditorDoc(), null);
}
}
else
Expand Down Expand Up @@ -1444,7 +1456,7 @@ else if (e instanceof JDFJMF)
* @param selectionPath
* @experimental
*/
public void saveAsJSON(final TreePath selectionPath)
public void saveAsJSON(final TreePath selectionPath, final boolean reallySave)
{
final JDFTreeNode node = selectionPath == null ? (JDFTreeNode) getRootNode() : (JDFTreeNode) selectionPath.getLastPathComponent();
if (node == null || !EditorUtils.isJSONEnabled(node.getName()))
Expand All @@ -1462,11 +1474,15 @@ public void saveAsJSON(final TreePath selectionPath)
{
ed.setJson(true, true);
frame.setEditorDoc(ed);
if (reallySave)
ed.saveFile(null);
}
}
else
{
eDoc.setJson(true, true);
eDoc.setJson(true, reallySave);
if (reallySave)
eDoc.saveFile(null);
}
log.info("converting " + JDFTreeNode.getName(node) + " to JSON ");
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/cip4/tools/jdfeditor/MessageSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* The CIP4 Software License, Version 1.0
*
*
* Copyright (c) 2001-2015 The International Cooperation for the Integration of
* Copyright (c) 2001-2024 The International Cooperation for the Integration of
* Processes in Prepress, Press and Postpress (CIP4). All rights
* reserved.
*
Expand Down Expand Up @@ -107,7 +107,6 @@ public void sendJMF()
final SendToDevice sendTo = new SendToDevice();
sendTo.trySend();
}

}

/**
Expand All @@ -125,13 +124,12 @@ boolean generateDoc()
else
{
doc = generateJMFDoc();

}
if (doc != null)
{
MainView.getFrame().setJDFDoc(doc, null);
final EditorDocument edNew = MainView.getEditorDoc();
edNew.setJson(json, json);
edNew.setJson(json, false);
MainView.getFrame().getJDFTreeArea().drawTreeView(edNew);
}
return doc != null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ else if (eSrc == m_saveXJDF)
}
else if (eSrc == m_saveJSON)
{
MainView.getModel().saveAsJSON(treeArea.getSelectionPath());
MainView.getModel().saveAsJSON(treeArea.getSelectionPath(), true);
}
else if (eSrc == m_saveJDF)
{
Expand Down
Loading

0 comments on commit c9b48d4

Please sign in to comment.