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

feat: implement node type translation action #239

Merged
merged 1 commit into from
Jun 2, 2024
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: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

## Unreleased

### Added

- Action to create and update translations for a node type

## 1.21.0 - 2024-06-01

- Add JSON schema for node type presets (needs separate settings file named `Settings.Presets.*`)
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/de/vette/idea/neos/LocaleDialogWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package de.vette.idea.neos;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder;
import de.vette.idea.neos.lang.xliff.XliffBundle;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.ResourceBundle;

Check warning on line 12 in src/main/java/de/vette/idea/neos/LocaleDialogWrapper.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unused import

Unused import `import java.util.ResourceBundle;`
import java.util.regex.Pattern;

public class LocaleDialogWrapper extends DialogWrapper {
JBTextField textField = new JBTextField();

Pattern pattern = Pattern.compile("^[A-Za-z]{2,4}([_-][A-Za-z]{4})?([_-]([A-Za-z]{2}|[0-9]{3}))?$");

public LocaleDialogWrapper(Project project, String title) {
super(project);
setTitle(title);
init();
}

@Override
protected @Nullable JComponent createCenterPanel() {
FormBuilder formBuilder = FormBuilder.createFormBuilder();
formBuilder.addLabeledComponent("Locale", this.textField);
return formBuilder.getPanel();
}

public String getText() {
return this.textField.getText().trim();
}

@Override
protected @Nullable ValidationInfo doValidate() {
if (!pattern.matcher(this.getText()).matches()) {
return new ValidationInfo(XliffBundle.message("settings.dialog.validation.error"), this.textField);
}

return null;
}

@Override
public @Nullable JComponent getPreferredFocusedComponent() {
return this.textField;
}
}
5 changes: 5 additions & 0 deletions src/main/java/de/vette/idea/neos/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.LinkedList;
import java.util.List;

@State(name = "NeosPluginSettings")
public class Settings implements PersistentStateComponent<Settings> {

public boolean pluginEnabled = false;
public boolean dismissEnableNotification = false;
public boolean excludePackageSymlinks = false;

public List<String> locales = new LinkedList<>();

public static Settings getInstance(@NotNull Project project) {
return project.getService(Settings.class);
}
Expand Down
76 changes: 74 additions & 2 deletions src/main/java/de/vette/idea/neos/SettingsForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,30 @@

import com.intellij.ide.actions.ShowSettingsUtilImpl;
import com.intellij.openapi.project.Project;
import com.intellij.ui.AnActionButton;
import com.intellij.ui.AnActionButtonRunnable;
import com.intellij.ui.CollectionListModel;
import com.intellij.ui.ToolbarDecorator;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBList;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.jetbrains.php.frameworks.PhpFrameworkConfigurable;
import de.vette.idea.neos.lang.xliff.XliffBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

public class SettingsForm implements PhpFrameworkConfigurable {
private JCheckBox pluginEnabled;
private JCheckBox excludePackageSymlinks;
private CollectionListModel<String> locales;
private final Project project;

public SettingsForm(@NotNull final Project project) {
Expand Down Expand Up @@ -66,8 +77,17 @@
public JComponent createComponent() {
pluginEnabled = new JCheckBox("Enable plugin for this project");
excludePackageSymlinks = new JCheckBox("Exclude symlinked packages");
locales = new CollectionListModel<>();

GridLayoutManager layout = new GridLayoutManager(2,1);
JBList localeList = new JBList<String>();
localeList.setModel(this.locales);

Check warning on line 83 in src/main/java/de/vette/idea/neos/SettingsForm.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unchecked warning

Unchecked call to 'setModel(ListModel)' as a member of raw type 'javax.swing.JList'

ToolbarDecorator editableList = ToolbarDecorator.createDecorator(localeList);

Check warning on line 85 in src/main/java/de/vette/idea/neos/SettingsForm.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unchecked warning

Unchecked assignment: 'com.intellij.ui.components.JBList' to 'javax.swing.JList'
editableList.disableUpDownActions();
editableList.setPreferredSize(new Dimension(150, 100));
editableList.setAddAction(new AddAction(this.locales));

GridLayoutManager layout = new GridLayoutManager(5,1);
GridConstraints c = new GridConstraints();
c.setAnchor(GridConstraints.ANCHOR_NORTHWEST);
c.setRow(0);
Expand All @@ -77,12 +97,37 @@

c.setRow(1);
panel1.add(excludePackageSymlinks, c);

c.setRow(2);
panel1.add(new Spacer(), c);

c.setRow(3);
panel1.add(new JBLabel(XliffBundle.message("settings.locales.label")), c);

c.setRow(4);
panel1.add(editableList.createPanel(), c);

return panel1;
}

@Override
public boolean isModified() {
return !pluginEnabled.isSelected() == getSettings().pluginEnabled || !excludePackageSymlinks.isSelected() == getSettings().excludePackageSymlinks;
int i = 0;

if (getSettings().locales.size() != locales.getSize()) {
return true;
}

for (String item : locales.getItems()) {
if (!item.equals(getSettings().locales.get(i))) {
return true;
}

i++;
}

return !pluginEnabled.isSelected() == getSettings().pluginEnabled
|| !excludePackageSymlinks.isSelected() == getSettings().excludePackageSymlinks;
}

@Override
Expand All @@ -93,6 +138,7 @@

getSettings().pluginEnabled = pluginEnabled.isSelected();
getSettings().excludePackageSymlinks = excludePackageSymlinks.isSelected();
getSettings().locales = new ArrayList<>(locales.getItems());
}

@Override
Expand All @@ -103,6 +149,9 @@
private void updateUIFromSettings() {
pluginEnabled.setSelected(getSettings().pluginEnabled);
excludePackageSymlinks.setSelected(getSettings().excludePackageSymlinks);
getSettings().locales.forEach(s -> {
locales.add(s);
});
}

private Settings getSettings() {
Expand All @@ -112,4 +161,27 @@
public static void show(@NotNull Project project) {
ShowSettingsUtilImpl.showSettingsDialog(project, "Neos.SettingsForm", null);
}


class AddAction implements AnActionButtonRunnable {

CollectionListModel list;

public AddAction(CollectionListModel list) {
this.list = list;
}

@Override
public void run(AnActionButton anActionButton) {
LocaleDialogWrapper dialog = new LocaleDialogWrapper(project, "Add Locale");
if (dialog.showAndGet()) {
String text = dialog.getText();
if (text.isEmpty()) {
return;
}

this.list.add(text);

Check warning on line 183 in src/main/java/de/vette/idea/neos/SettingsForm.java

View workflow job for this annotation

GitHub Actions / Qodana Community for JVM

Unchecked warning

Unchecked call to 'add(T)' as a member of raw type 'com.intellij.ui.CollectionListModel'
}
}
}
}
Loading
Loading