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

EditTokenDialog: Change GUI-Editor to Intellij GUI designer #3785

Merged
merged 18 commits into from
Dec 27, 2022
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
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ protobuf {
sourceSets {
main {
java {
srcDirs 'build/generated/source/proto/main/grpc'
srcDirs 'build/generated/source/proto/main/java'
}
}
Expand Down Expand Up @@ -281,6 +280,7 @@ repositories {
maven { url = 'https://maptool.craigs-stuff.net/repo/' }
maven { url = 'https://nerps.net/repo/' }
maven { url = 'https://jitpack.io' }
maven { url "https://www.jetbrains.com/intellij-repository/releases" }
}


Expand Down Expand Up @@ -448,6 +448,8 @@ dependencies {

implementation 'org.apache.commons:commons-compress:1.22'
implementation 'org.tukaani:xz:1.9'
// intellij forms runtime
implementation 'com.jetbrains.intellij.java:java-gui-forms-rt:223.7571.182'
}


Expand Down
83 changes: 73 additions & 10 deletions src/main/java/net/rptools/maptool/client/swing/AbeillePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
*/
package net.rptools.maptool.client.swing;

import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jeta.forms.components.panel.FormPanel;
import java.awt.Component;
import java.awt.GridLayout;
import com.jgoodies.forms.layout.FormLayout;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import javax.swing.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -54,7 +56,8 @@
@SuppressWarnings("serial")
public class AbeillePanel<T> extends JPanel {
private static final Logger log = LogManager.getLogger(AbeillePanel.class);
private final FormPanel panel;
private final Container panel;
private HashMap<String, Component> componentMap;
private T model;

static {
Expand All @@ -79,10 +82,19 @@ public void storeBindingInfo(Component view, BindingInfo info) {}
}

public AbeillePanel(String panelForm) {
setLayout(new GridLayout());
panel = new FormPanelI18N(panelForm);
panel = new FormPanel(panelForm);
init();
}

public AbeillePanel(JComponent mainPanel) {
panel = mainPanel;
init();
}

add(panel);
private void init() {
setLayout(new BorderLayout());
FormPanelI18N.translateComponent(panel);
add(panel, "Center");
}

public T getModel() {
Expand All @@ -102,13 +114,64 @@ protected void panelInit() {
}
}

protected void replaceComponent(String panelName, String name, Component component) {
panel.getFormAccessor(panelName).replaceBean(name, component);
panel.reset();
protected void replaceComponent(String panelName, String name, Component replacement) {
if (panel instanceof FormPanel formPanel) {
formPanel.getFormAccessor(panelName).replaceBean(name, replacement);
formPanel.reset();
} else {
var placeHolder = getComponent(name);
var container = (JPanel) getComponent(panelName);
Object constraints = null;
var layout = container.getLayout();
if (layout instanceof FormLayout formLayout) {
constraints = formLayout.getConstraints(placeHolder);
} else if (layout instanceof GridLayoutManager gridLayoutManager) {
constraints = gridLayoutManager.getConstraintsForComponent(placeHolder);
} else {
throw new RuntimeException(
"Replacement of components not implemented for layout: " + layout.getClass().getName());
}

container.remove(placeHolder);
container.add(replacement, constraints);
container.revalidate();
container.repaint();
componentMap.remove(name);
collectComponents(replacement);
}
}

private void createComponentMap() {
componentMap = new HashMap<>();
collectComponents(panel);
}

private void collectComponents(Component component) {
var name = component.getName();

if (name != null && !name.isEmpty()) {
componentMap.put(name, component);
}

if (component instanceof Container container) {
for (var comp : container.getComponents()) {
collectComponents(comp);
}
}
}

protected Component getComponent(String name) {
return panel.getComponentByName(name);
if (panel instanceof FormPanel formPanel) {
return formPanel.getComponentByName(name);
}

if (componentMap == null) {
createComponentMap();
}
if (componentMap.containsKey(name)) {
return (Component) componentMap.get(name);
}
return null;
}

/**
Expand Down
103 changes: 29 additions & 74 deletions src/main/java/net/rptools/maptool/client/swing/FormPanelI18N.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
*/
package net.rptools.maptool.client.swing;

import com.jeta.forms.components.colors.JETAColorWell;
import com.jeta.forms.components.image.ImageComponent;
import com.jeta.forms.components.label.JETALabel;
import com.jeta.forms.components.panel.FormPanel;
import com.jeta.forms.gui.form.FormAccessor;
import com.jeta.forms.gui.form.FormComponent;
import com.jeta.forms.gui.form.GridView;
import com.jeta.forms.store.properties.ListItemProperty;
import java.awt.*;
import java.util.Iterator;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import net.rptools.maptool.language.I18N;

Expand All @@ -46,93 +41,53 @@ public FormPanelI18N(String s) {
*
* @param comp the component to be translated
*/
private static void translateComponent(Component comp) {
protected static void translateComponent(Component comp) {
if (comp instanceof JLabel jLabel) {
jLabel.setText(I18N.getText(jLabel.getText()));
String tooltip = jLabel.getToolTipText();
if (tooltip != null) {
jLabel.setToolTipText(I18N.getText(tooltip));
}
}
if (comp instanceof JETALabel) {
JETALabel label = (JETALabel) comp;
label.setText(I18N.getText(label.getText()));
String tooltip = label.getToolTipText();
if (tooltip != null) {
label.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof AbstractButton) {
// Includes JToggleButton, JCheckBox, JButton
AbstractButton jButton = (AbstractButton) comp;
jButton.setText(I18N.getText(jButton.getText()));
String tooltip = jButton.getToolTipText();
if (tooltip != null) {
jButton.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof JSpinner) {
JSpinner jSpin = (JSpinner) comp;
String tooltip = jSpin.getToolTipText();
if (tooltip != null) {
jSpin.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof JComboBox) {
JComboBox<?> jComboBox = (JComboBox<?>) comp;
String tooltip = jComboBox.getToolTipText();
if (tooltip != null) {
jComboBox.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof JComboBox jComboBox) {
for (int i = 0; i < jComboBox.getItemCount(); ++i) {
ListItemProperty item = (ListItemProperty) jComboBox.getItemAt(i);
item.setLabel(I18N.getText(item.getLabel()));
}
} else if (comp instanceof JTextField) {
JTextField jTextField = (JTextField) comp;
String tooltip = jTextField.getToolTipText();
if (tooltip != null) {
jTextField.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof JList) {
JList<?> jList = (JList<?>) comp;
String tooltip = jList.getToolTipText();
if (tooltip != null) {
jList.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof JETAColorWell) {
JETAColorWell jetaColorWell = (JETAColorWell) comp;
String tooltip = jetaColorWell.getToolTipText();
if (tooltip != null) {
jetaColorWell.setToolTipText(I18N.getText(tooltip));
}
} else if (comp instanceof ImageComponent) {
ImageComponent jImage = (ImageComponent) comp;
String tooltip = jImage.getToolTipText();
if (tooltip != null) {
jImage.setToolTipText(I18N.getText(tooltip));
var comboBoxItem = jComboBox.getItemAt(i);
if (comboBoxItem instanceof ListItemProperty itemProperty) {
itemProperty.setLabel(I18N.getText(itemProperty.getLabel()));
} else if (comboBoxItem instanceof String string) {
jComboBox.removeItemAt(i);
jComboBox.insertItemAt(I18N.getText(string), i);
} else {
throw new RuntimeException(
"Untranslated type of JComboBox item: " + comboBoxItem.getClass().getName());
}
}
} else if (comp instanceof JTabbedPane) {
JTabbedPane jTabbedPane = (JTabbedPane) comp;
for (int i = 0; i < jTabbedPane.getTabRunCount(); i += 1) {
// Translate the tab titles
jTabbedPane.setTitleAt(i, I18N.getText(jTabbedPane.getTitleAt(i)));
}
for (Component subComp : jTabbedPane.getComponents()) {
// Recursively translate the sub components
translateComponent(subComp);
}
} else if (comp instanceof GridView) {
GridView gridView = (GridView) comp;
Iterator<?> iter = gridView.beanIterator();
while (iter.hasNext()) {
// Recursively translate the sub components
translateComponent((Component) iter.next());
}
Border border = gridView.getBorder();
// Translate the titled border, if any
}
if (comp instanceof JComponent jComponent) {
var border = jComponent.getBorder();
if (border instanceof TitledBorder) {
TitledBorder titledBorder = (TitledBorder) border;
titledBorder.setTitle(I18N.getText(titledBorder.getTitle()));
}
} else if (comp instanceof FormComponent) {
// Translate the GridView inside the form
FormComponent form = (FormComponent) comp;
translateComponent(form.getChildView());
} else if (comp instanceof JScrollPane) {
JScrollPane jScrollPane = (JScrollPane) comp;
// Translate the child of the scroll pane
translateComponent(jScrollPane.getViewport().getView());
String tooltip = jComponent.getToolTipText();
if (tooltip != null) {
jComponent.setToolTipText(I18N.getText(tooltip));
}
for (Component c : jComponent.getComponents()) {
translateComponent(c);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public class EditTokenDialog extends AbeillePanel<Token> {

/** Create a new token notes dialog. */
public EditTokenDialog() {
super("net/rptools/maptool/client/ui/forms/tokenPropertiesDialog.xml");
super(new TokenPropertiesDialog().$$$getRootComponent$$$());
panelInit();
}

Expand Down
Loading