Skip to content

Commit

Permalink
Fix for issue #404
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-Loeffler committed Sep 7, 2021
1 parent 3d04752 commit 3b2e343
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2021, Gluon and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* - Neither the name of Oracle Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.oracle.javafx.scenebuilder.kit.fxom;

class FXMLPropertiesDisabler {

/**
*
* On MacOS, when loading a FXML with a menu bar where useSystemMenuBarProperty()
* is enabled, the menu in the FXML will hide the menu of SceneBuilder.
* In this case, SceneBuilder becomes unusable.
*
* Setting the property here to false has the advantage, that the FXML to be saved
* will still contain the defined property BUT the SceneBuilder menu bar will remain
* visible.
*
* The modification of properties which are not desired to be active while
* editing must happen before loading the FXML using the FXMLLoader.
*
* Here a disconnect between the FXOM and FXML is created as the state of the
* useSystemMenuBarProperty is now different in both models.
*
* @param fxmlText FXML source to be modified
* @return FXML source with all properties disabled (=false) where WYSIWYG editing is not suitable.
*
*/
public String disableUseSystemMenuBarProperty(String fxmlText) {
return fxmlText.replace("useSystemMenuBar=\"true\"",
"useSystemMenuBar=\"false\"");
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 Gluon and/or its affiliates.
* Copyright (c) 2017, 2019, 2021, Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -44,6 +44,7 @@
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Set;

import com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform;
import javafx.beans.property.ReadOnlyIntegerProperty;
Expand Down Expand Up @@ -80,16 +81,23 @@ public class FXOMDocument {

private List<Class<?>> initialDeclaredClasses;

public FXOMDocument(String fxmlText, URL location, ClassLoader classLoader, ResourceBundle resources, boolean normalize) throws IOException {
public FXOMDocument(String fxmlText, URL location, ClassLoader classLoader, ResourceBundle resources, FXOMDocumentSwitch... switches) throws IOException {
this.glue = new GlueDocument(fxmlText);
this.location = location;
this.classLoader = classLoader;
this.resources = resources;
initialDeclaredClasses = new ArrayList<>();
if (this.glue.getRootElement() != null) {

String fxmlTextToLoad = fxmlText;
if (!Set.of(switches).contains(FXOMDocumentSwitch.FOR_PREVIEW)) {
final FXMLPropertiesDisabler FXMLPropertiesDisabler = new FXMLPropertiesDisabler();
fxmlTextToLoad = FXMLPropertiesDisabler.disableUseSystemMenuBarProperty(fxmlText);
}
final FXOMLoader loader = new FXOMLoader(this);
loader.load(fxmlText);
if (normalize) {
loader.load(fxmlTextToLoad);

if (!Set.of(switches).contains(FXOMDocumentSwitch.NON_NORMALIZED)) {
final FXOMNormalizer normalizer = new FXOMNormalizer(this);
normalizer.normalize();
}
Expand All @@ -102,13 +110,7 @@ public FXOMDocument(String fxmlText, URL location, ClassLoader classLoader, Reso

hasGluonControls = fxmlText.contains(EditorPlatform.GLUON_PACKAGE);
}


public FXOMDocument(String fxmlText, URL location, ClassLoader classLoader, ResourceBundle resources) throws IOException {
this(fxmlText, location, classLoader, resources, true /* normalize */);
}



public FXOMDocument() {
this.glue = new GlueDocument();
}
Expand Down Expand Up @@ -443,4 +445,9 @@ public static interface SceneGraphHolder {
public void fxomDocumentWillRefreshSceneGraph(FXOMDocument fxomDocument);
public void fxomDocumentDidRefreshSceneGraph(FXOMDocument fxomDocument);
}

public enum FXOMDocumentSwitch {
NON_NORMALIZED,
FOR_PREVIEW;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public void readPropertyAttribute(String name, Class<?> staticClass, String fxml
} else if (currentTransientNode instanceof TransientProperty) {
final TransientProperty transientProperty = (TransientProperty) currentTransientNode;
transientProperty.getCollectedProperties().add(fxomProperty);
} else if(currentTransientNode instanceof TransientIntrinsic) {
} else if(currentTransientNode instanceof TransientIntrinsic) {
final TransientIntrinsic transientIntrinsic = (TransientIntrinsic) currentTransientNode;
transientIntrinsic.getProperties().add(fxomProperty);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Gluon and/or its affiliates.
* Copyright (c) 2019, 2021, Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -32,6 +32,7 @@
*/
package com.oracle.javafx.scenebuilder.kit.fxom;

import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.FXOMDocumentSwitch;
import com.oracle.javafx.scenebuilder.kit.metadata.Metadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.ValuePropertyMetadata;
import com.oracle.javafx.scenebuilder.kit.metadata.property.value.DoubleArrayPropertyMetadata;
Expand Down Expand Up @@ -65,7 +66,7 @@ public void refresh(FXOMDocument document) {
document.getLocation(),
document.getClassLoader(),
document.getResources(),
false /* normalized */);
FXOMDocumentSwitch.NON_NORMALIZED);
final TransientStateBackup backup = new TransientStateBackup(document);
// if the refresh should not take place (e.g. due to an error), remove a property from intrinsic
if (newDocument.getSceneGraphRoot() == null && newDocument.getFxomRoot() == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2019, Gluon and/or its affiliates.
* Copyright (c) 2016, 2019, 2021, Gluon and/or its affiliates.
* Copyright (c) 2012, 2014, Oracle and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
Expand Down Expand Up @@ -38,6 +38,7 @@
import com.oracle.javafx.scenebuilder.kit.editor.EditorPlatform.Theme;
import com.oracle.javafx.scenebuilder.kit.editor.panel.util.AbstractWindowController;
import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument.FXOMDocumentSwitch;
import com.oracle.javafx.scenebuilder.kit.i18n.I18N;
import com.oracle.javafx.scenebuilder.kit.util.MathUtils;

Expand Down Expand Up @@ -214,7 +215,8 @@ public void openDialog() {
clone = new FXOMDocument(fxomDocument.getFxmlText(false),
fxomDocument.getLocation(),
fxomDocument.getClassLoader(),
fxomDocument.getResources());
fxomDocument.getResources(),
FXOMDocumentSwitch.FOR_PREVIEW);
clone.setSampleDataEnabled(fxomDocument.isSampleDataEnabled());
} catch (IOException ex) {
throw new RuntimeException("Bug in PreviewWindowController::openDialog", ex); //NOI18N
Expand Down Expand Up @@ -280,7 +282,8 @@ public void run() {
clone = new FXOMDocument(fxomDocument.getFxmlText(false),
fxomDocument.getLocation(),
fxomDocument.getClassLoader(),
fxomDocument.getResources());
fxomDocument.getResources(),
FXOMDocumentSwitch.FOR_PREVIEW);
clone.setSampleDataEnabled(fxomDocument.isSampleDataEnabled());
} catch (IOException ex) {
throw new RuntimeException("Bug in PreviewWindowController::requestUpdate", ex); //NOI18N
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2021, Gluon and/or its affiliates.
* All rights reserved. Use is subject to license terms.
*
* This file is available and licensed under the following license:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
* - Neither the name of Oracle Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.oracle.javafx.scenebuilder.kit.fxom;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Files;

import org.junit.Test;

public class FXMLPropertiesDisablerTest {

private FXMLPropertiesDisabler classUnderTest = new FXMLPropertiesDisabler();

@Test
public void that_property_value_is_set_to_false() throws Exception {
String fxmlText = readResourceText("ContainerWithMenu_SystemMenuBarEnabled.fxml");
assertTrue("ensures that test resource is correct",
fxmlText.contains("<MenuBar useSystemMenuBar=\"true\" VBox.vgrow=\"NEVER\" fx:id=\"theMenuBar\">"));
String modfiedFxmlText = classUnderTest.disableUseSystemMenuBarProperty(fxmlText);
assertTrue(modfiedFxmlText.contains("<MenuBar useSystemMenuBar=\"false\" VBox.vgrow=\"NEVER\" fx:id=\"theMenuBar\">"));
}

private String readResourceText(String resourceName) throws Exception {
File fxmlFileName = new File(getClass().getResource(resourceName).toURI());
return Files.readString(fxmlFileName.toPath());
}

}
Loading

0 comments on commit 3b2e343

Please sign in to comment.