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

Binding expression edition support (no resolution / string only) #279

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -724,11 +724,16 @@ private void mouseDoubleClickedOnGlassLayer(MouseEvent e) {
final DesignHierarchyMask m
= new DesignHierarchyMask(hitObject);
// Do not allow inline editing of the I18N value
if (m.isResourceKey() == false) {
if (m.isResourceKey() == false && m.isBindingExpression() == false) {
handleInlineEditing((FXOMInstance) selectAndMoveGesture.getHitObject());
} else {
final MessageLog ml = contentPanelController.getEditorController().getMessageLog();
ml.logWarningMessage("log.warning.inline.edit.internationalized.strings");
if (m.isResourceKey()) {
ml.logWarningMessage("log.warning.inline.edit.internationalized.strings");
}
if (m.isBindingExpression()) {
ml.logWarningMessage("log.warning.inline.edit.bindingexpression.strings");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ public boolean isResourceKey(final DisplayOption option) {
}
return option == INFO && mask.isResourceKey();
}

public boolean isBindingExpression(final DisplayOption option) {
// Place holder items do not have display info
if (mask == null) {
return false;
}
return option == INFO && mask.isBindingExpression();
}

public boolean isPlaceHolder() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,7 @@ private void filterMouseEvent(final MouseEvent me) {
final DisplayOption option = panelController.getDisplayOption();
if (item.hasDisplayInfo(option)
&& item.isResourceKey(option) == false // Do not allow inline editing of the I18N value
&& item.isBindingExpression(option) == false // Do not allow inline editing of the binding expression value
&& displayInfoLabel.isHover()) {
startEditingDisplayInfo();
// Consume the event so the native expand/collapse behavior is not performed
Expand Down Expand Up @@ -759,8 +760,8 @@ private void updateLayout(HierarchyItem item) {

final DisplayOption option = panelController.getDisplayOption();
final String displayInfo = item.getDisplayInfo(option);
// Do not allow inline editing of the I18N value
if (item.isResourceKey(option)) {
// Do not allow inline editing of the I18N value or binding expression
if (item.isResourceKey(option) || item.isBindingExpression(option)) {
displayInfoLabel.getStyleClass().removeAll(HIERARCHY_READWRITE_LABEL);
} else {
if (displayInfoLabel.getStyleClass().contains(HIERARCHY_READWRITE_LABEL) == false) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,25 @@
public class I18nStringEditor extends PropertyEditor {

private static final String PERCENT_STR = "%"; //NOI18N
private static final String EXPR_STR = "${"; //NOI18N
private static final String EXPR_STR_END = "}"; //NOI18N
private TextInputControl textNode = new TextField();
private HBox i18nHBox = null;
private HBox exprHBox = null;
private EventHandler<ActionEvent> valueListener;
private final MenuItem i18nMenuItem = new MenuItem();
private final String I18N_ON = I18N.getString("inspector.i18n.on");
private final String I18N_OFF = I18N.getString("inspector.i18n.off");
private final MenuItem exprMenuItem = new MenuItem();
private final String EXPR_ON = I18N.getString("inspector.bindingexpression.on");
private final String EXPR_OFF = I18N.getString("inspector.bindingexpression.off");
private final MenuItem multilineMenuItem = new MenuItem();
private final String MULTI_LINE = I18N.getString("inspector.i18n.multiline");
private final String SINGLE_LINE = I18N.getString("inspector.i18n.singleline");
private boolean multiLineSupported = false;
// Specific states
private boolean i18nMode = false;
private boolean exprMode = false;
private boolean multiLineMode = false;

public I18nStringEditor(ValuePropertyMetadata propMeta, Set<Class<?>> selectedClasses, boolean multiLineSupported) {
Expand All @@ -86,6 +93,7 @@ private void initialize(boolean multiLineSupported) {
setTextEditorBehavior(this, textNode, valueListener);

getMenu().getItems().add(i18nMenuItem);
getMenu().getItems().add(exprMenuItem);
getMenu().getItems().add(multilineMenuItem);

i18nMenuItem.setOnAction(e -> {
Expand All @@ -97,6 +105,15 @@ private void initialize(boolean multiLineSupported) {
I18nStringEditor.this.getCommitListener().handle(null);
updateMenuItems();
});
exprMenuItem.setOnAction(e -> {
if (!exprMode) {
setValue(new PrefixedValue(PrefixedValue.Type.BINDING_EXPRESSION, I18N.getString("inspector.bindingexpression.dummyvalue")).toString());
} else {
setValue(""); //NOEXPR
}
I18nStringEditor.this.getCommitListener().handle(null);
updateMenuItems();
});
multilineMenuItem.setOnAction(e -> {
if (!multiLineMode) {
switchToTextArea();
Expand All @@ -121,6 +138,8 @@ public Object getValue() {
String val = textNode.getText();
if (i18nMode) {
val = new PrefixedValue(PrefixedValue.Type.RESOURCE_KEY, val).toString();
} else if (exprMode) {
val = new PrefixedValue(PrefixedValue.Type.BINDING_EXPRESSION, val).toString();
Copy link
Collaborator

@abhinayagarwal abhinayagarwal Dec 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to use 'spaces' instead of 'tabs'. There are multiple other places where tabs have been introduced.

} else {
val = EditorUtils.getPlainString(val);
}
Expand All @@ -143,39 +162,42 @@ public void setValue(Object value) {
PrefixedValue prefixedValue = new PrefixedValue(val);
String suffix = prefixedValue.getSuffix();

// Handle i18n
if (prefixedValue.isResourceKey()) {
if (!i18nMode) {
wrapInHBox();
i18nMode = true;
}
} else if (i18nMode) {
if (i18nMode) {
// no percent + i18nMode
unwrapHBox();
unwrapI18nFromHBox();
i18nMode = false;
}

if (exprMode) {
// no percent + i18nMode
unwrapExprFromHBox();
exprMode = false;
}

if (multiLineMode) {
multiLineMode = false;
switchToTextField();
}

// Handle i18n
if (prefixedValue.isResourceKey()) {
wrapI18nInHBox();
i18nMode = true;
}

// Handle expression
if (prefixedValue.isBindingExpression()) {
wrapExprInHBox();
exprMode = true;
}

// Handle multi-line
if (containsLineFeed(prefixedValue.toString())) {
if (i18nMode) {
// multi-line + i18n ==> set as i18n only
multiLineMode = false;
switchToTextField();
} else {
if (!multiLineMode) {
multiLineMode = true;
switchToTextArea();
}
}
} else {
// no line feed
if (multiLineMode) {
multiLineMode = false;
switchToTextField();
}
multiLineMode = true;
switchToTextArea();
}

if (i18nMode) {
if (i18nMode || exprMode) {
textNode.setText(suffix);
} else {
// We may have other special characters (@, $, ...) to display in the text field
Expand All @@ -195,6 +217,8 @@ public Node getValueEditor() {
Node valueEditor;
if (i18nMode) {
valueEditor = i18nHBox;
} else if (exprMode) {
valueEditor = exprHBox;
} else {
valueEditor = textNode;
}
Expand Down Expand Up @@ -240,7 +264,7 @@ protected void switchToTextField() {
textNode = textField;
}

private void wrapInHBox() {
private void wrapI18nInHBox() {
i18nHBox = new HBox();
i18nHBox.setAlignment(Pos.CENTER);
EditorUtils.replaceNode(textNode, i18nHBox, null);
Expand All @@ -253,12 +277,33 @@ private void wrapInHBox() {
textNode.setPrefWidth(30.0);
HBox.setHgrow(textNode, Priority.ALWAYS);
}

private void wrapExprInHBox() {
exprHBox = new HBox();
exprHBox.setAlignment(Pos.CENTER);
EditorUtils.replaceNode(textNode, exprHBox, null);
Label expreLabel = new Label(EXPR_STR);
Label expreSuffixLabel = new Label(EXPR_STR_END);
expreLabel.getStyleClass().add("symbol-prefix"); //NOI18N
exprHBox.getChildren().addAll(expreLabel, textNode, expreSuffixLabel);
HBox.setHgrow(expreLabel, Priority.NEVER);
HBox.setHgrow(expreSuffixLabel, Priority.NEVER);
// we have to set a small pref width for the text node else it will
// revert to it's API set pref width which is too wide
textNode.setPrefWidth(30.0);
HBox.setHgrow(textNode, Priority.ALWAYS);
}

private void unwrapHBox() {
private void unwrapI18nFromHBox() {
i18nHBox.getChildren().remove(textNode);
EditorUtils.replaceNode(i18nHBox, textNode, null);
}


private void unwrapExprFromHBox() {
exprHBox.getChildren().remove(textNode);
EditorUtils.replaceNode(exprHBox, textNode, null);
}

private static boolean containsLineFeed(String str) {
return str.contains("\n"); //NOI18N
}
Expand All @@ -271,20 +316,32 @@ public void requestFocus() {
private void updateMenuItems() {
if (i18nMode) {
i18nMenuItem.setText(I18N_OFF);
exprMenuItem.setDisable(true);
multilineMenuItem.setDisable(true);
} else {
i18nMenuItem.setText(I18N_ON);
multilineMenuItem.setDisable(false);
}

if (exprMode) {
exprMenuItem.setText(EXPR_OFF);
i18nMenuItem.setDisable(true);
multilineMenuItem.setDisable(true);
} else {
exprMenuItem.setText(EXPR_ON);
}

if (multiLineMode) {
multilineMenuItem.setText(SINGLE_LINE);
i18nMenuItem.setDisable(true);
exprMenuItem.setDisable(true);
} else {
multilineMenuItem.setText(MULTI_LINE);
i18nMenuItem.setDisable(false);
}

i18nMenuItem.setDisable(exprMode || multiLineMode);
exprMenuItem.setDisable(i18nMode || multiLineMode);
multilineMenuItem.setDisable(exprMode || i18nMode);

if (!multiLineSupported) {
multilineMenuItem.setDisable(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private void cssMenuUpdate() {
}

protected boolean isSetValueDone() {
boolean done = !isHandlingError() && (isBinding() || isEditing());
boolean done = !isHandlingError() && isEditing();
return done;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright (c) 2020, 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 java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;

import javafx.fxml.FXMLLoader;

/**
*
*/
public class BindingExpressionDisabler {

public static void disable(FXOMDocument fxomDocument) {
assert fxomDocument != null;

final List<FXOMObject> candidates = new ArrayList<>();
if (fxomDocument.getFxomRoot() != null) {
candidates.add(fxomDocument.getFxomRoot());
}

while (candidates.isEmpty() == false) {
final FXOMObject candidate = candidates.get(0);
candidates.remove(0);

if (candidate instanceof FXOMInstance) {
final FXOMInstance inst = (FXOMInstance)candidate;
final Object sceneGraphObject = inst.getSceneGraphObject();

for (Map.Entry<PropertyName, FXOMProperty> e:inst.getProperties().entrySet()) {
FXOMProperty property = e.getValue();
PropertyName propertyName = e.getKey();

if (property instanceof FXOMPropertyT) {
FXOMPropertyT propertyT = (FXOMPropertyT)property;
if (propertyT.getValue().startsWith(FXMLLoader.BINDING_EXPRESSION_PREFIX)) {
try {
propertyName.setValue(sceneGraphObject, propertyT.getValue());
} catch (Exception ex) {
// Let the exception be, the binding expression can't be escaped
// due to the property type not accepting string value
// catching the exception allow the process to go on
}
}
}
}
}

candidates.addAll(candidate.getChildObjects());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ public void refresh(FXOMDocument document) {
refreshDocument(document, newDocument);
}
backup.restore();

BindingExpressionDisabler.disable(document);

synchronizeDividerPositions(document);
} catch (RuntimeException | IOException x) {
final StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ public T getValue(FXOMInstance fxomInstance) {
} else if (fxomProperty instanceof FXOMPropertyT) {
final FXOMPropertyT fxomPropertyT = (FXOMPropertyT) fxomProperty;
final PrefixedValue pv = new PrefixedValue(fxomPropertyT.getValue());
if (pv.isBindingExpression()) {
result = getDefaultValue();
} else {
result = makeValueFromProperty(fxomPropertyT);
}
result = makeValueFromProperty(fxomPropertyT);
} else if (fxomProperty instanceof FXOMPropertyC) {
final FXOMPropertyC fxomPropertyC = (FXOMPropertyC) fxomProperty;
assert fxomPropertyC.getValues().isEmpty() == false;
Expand Down
Loading