Skip to content

598: Developed flexible way to add placeholders to dialog window fields that inherited from the JTextComponent #938

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

Merged
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 @@ -6,6 +6,7 @@
package com.magento.idea.magento2plugin.actions.generation.dialog;

import com.intellij.openapi.util.Pair;
import com.magento.idea.magento2plugin.actions.generation.dialog.prompt.PlaceholderInitializerUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.reflection.ExtractComponentFromFieldUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.util.DialogFieldErrorUtil;
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.TypeFieldsRulesParser;
Expand Down Expand Up @@ -250,6 +251,12 @@ private int getParentTabPaneForComponent(final @NotNull Container component) {
return getParentTabPaneForComponent(parent);
}

@Override
public void setVisible(final boolean status) {
new PlaceholderInitializerUtil(this).initialize();
super.setVisible(status);
}

/**
* Listener that helps focus on a field for dialogues after it is opened.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

package com.magento.idea.magento2plugin.actions.generation.dialog.prompt;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import javax.swing.text.JTextComponent;
import org.jdesktop.swingx.prompt.PromptSupport;
import org.jetbrains.annotations.NotNull;

public final class PlaceholderInitializerUtil {

private static final String PLACEHOLDER_CLIENT_PROPERTY = "promptText";

private final Class<?> type;
private final Object object;

/**
* PlaceholderInitializerUtil constructor.
*
* @param object Object
*/
public PlaceholderInitializerUtil(final @NotNull Object object) {
this.object = object;
type = object.getClass();
}

/**
* Initialize placeholders for supported field types if specified promptText client property.
*/
public void initialize() {
for (final Field field : type.getDeclaredFields()) {
final JTextComponent target = getTarget(field);

if (target == null) {
continue;
}
final Object promptProperty = target.getClientProperty(
PLACEHOLDER_CLIENT_PROPERTY
);

if (promptProperty == null) {
continue;
}
PromptSupport.setPrompt((String) promptProperty, target);
}
}

private JTextComponent getTarget(final Field field) {
if (Modifier.isStatic(field.getModifiers())) {
return null;
}
final boolean isAccessible = field.canAccess(object);
field.setAccessible(true);

try {
final Object value = field.get(object);

return value instanceof JTextComponent ? (JTextComponent) value : null;
} catch (IllegalAccessException exception) {
return null;
} finally {
field.setAccessible(isAccessible);
}
}
}