restructure form actions

Resolves #199
This commit is contained in:
marhali 2022-12-07 21:41:26 +01:00
parent f53b867713
commit 414d413434
5 changed files with 26 additions and 51 deletions

View File

@ -6,6 +6,9 @@
### Added
- Support of path variables for the locales directory configuration @SIMULATAN
### Changed
- Restructure form actions to improve user experience
## [4.3.1]
### Fixed
- Support for all 2022.3 builds (223.*)

View File

@ -1,7 +1,6 @@
package de.marhali.easyi18n.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import de.marhali.easyi18n.model.action.TranslationCreate;
@ -14,8 +13,6 @@ import de.marhali.easyi18n.settings.ProjectSettingsService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
/**
* Dialog to create a new translation with all associated locale values.
* Supports optional prefill technique for translation key or locale value.
@ -35,6 +32,8 @@ public class AddDialog extends TranslationDialog {
? new TranslationValue(ProjectSettingsService.get(project).getState().getPreviewLocale(), prefillLocale)
: null)
);
setTitle(bundle.getString("action.add"));
}
/**
@ -45,18 +44,6 @@ public class AddDialog extends TranslationDialog {
this(project, new KeyPath(), "");
}
@Override
protected @NotNull DialogBuilder configure(@NotNull JComponent centerPanel) {
DialogBuilder builder = new DialogBuilder();
builder.setTitle(bundle.getString("action.add"));
builder.removeAllActions();
builder.addOkAction();
builder.addCancelAction();
builder.setCenterPanel(centerPanel);
return builder;
}
@Override
protected @Nullable TranslationUpdate handleExit(int exitCode) {
if(exitCode == DialogWrapper.OK_EXIT_CODE) {

View File

@ -1,7 +1,6 @@
package de.marhali.easyi18n.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import de.marhali.easyi18n.dialog.descriptor.DeleteActionDescriptor;
@ -27,18 +26,13 @@ public class EditDialog extends TranslationDialog {
*/
public EditDialog(@NotNull Project project, @NotNull Translation origin) {
super(project, origin);
setTitle(bundle.getString("action.edit"));
}
@Override
protected @NotNull DialogBuilder configure(@NotNull JComponent centerPanel) {
DialogBuilder builder = new DialogBuilder();
builder.setTitle(bundle.getString("action.edit"));
builder.removeAllActions();
builder.addCancelAction();
builder.addActionDescriptor(new DeleteActionDescriptor());
builder.addOkAction();
builder.setCenterPanel(centerPanel);
return builder;
protected Action @NotNull [] createLeftSideActions() {
return new Action[]{ new DeleteActionDescriptor(this) };
}
@Override

View File

@ -1,7 +1,7 @@
package de.marhali.easyi18n.dialog;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.Consumer;
@ -28,7 +28,7 @@ import java.util.*;
* Base for add and edit translation dialogs.
* @author marhali
*/
abstract class TranslationDialog {
abstract class TranslationDialog extends DialogWrapper {
protected static final ResourceBundle bundle = ResourceBundle.getBundle("messages");
@ -48,6 +48,8 @@ abstract class TranslationDialog {
* @param origin Prefill translation
*/
protected TranslationDialog(@NotNull Project project, @NotNull Translation origin) {
super(project);
this.project = project;
this.settings = ProjectSettingsService.get(project).getState();
this.converter = new KeyPathConverter(settings);
@ -75,14 +77,6 @@ abstract class TranslationDialog {
callbacks.add(callback);
}
/**
* Implementation needs to configure the dialog. E.g. title, actions, ...
* The implementation needs to set the provided centerPanel as the view panel.
* @param centerPanel GUI to set on the dialog builder
* @return configured dialog builder
*/
protected abstract @NotNull DialogBuilder configure(@NotNull JComponent centerPanel);
/**
* Implementation needs to handle exit
* @param exitCode See {@link com.intellij.openapi.ui.DialogWrapper} for exit codes
@ -95,7 +89,10 @@ abstract class TranslationDialog {
* Internally, the {@link #handleExit(int)} method will be called to determine finalization logic.
*/
public void showAndHandle() {
int exitCode = createDialog().show();
init();
show();
int exitCode = getExitCode();
TranslationUpdate update = handleExit(exitCode);
if(update != null) {
@ -120,7 +117,8 @@ abstract class TranslationDialog {
return new Translation(key, value);
}
private DialogBuilder createDialog() {
@Override
protected @Nullable JComponent createCenterPanel() {
JPanel panel = FormBuilder.createFormBuilder()
.addLabeledComponent(bundle.getString("translation.key"), keyField, true)
.addComponent(createLocalesPanel(), 12)
@ -128,7 +126,7 @@ abstract class TranslationDialog {
panel.setMinimumSize(new Dimension(200, 150));
return configure(panel);
return panel;
}
private JComponent createLocalesPanel() {

View File

@ -1,7 +1,7 @@
package de.marhali.easyi18n.dialog.descriptor;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.NotNull;
import javax.swing.*;
import java.awt.event.ActionEvent;
@ -12,26 +12,19 @@ import java.util.ResourceBundle;
* Action can be monitored using the exit code for the opened dialog. See EXIT_CODE.
* @author marhali
*/
public class DeleteActionDescriptor extends AbstractAction implements DialogBuilder.ActionDescriptor {
public class DeleteActionDescriptor extends AbstractAction {
public static final int EXIT_CODE = 10;
private DialogWrapper dialogWrapper;
private final DialogWrapper dialog;
public DeleteActionDescriptor() {
public DeleteActionDescriptor(@NotNull DialogWrapper dialog) {
super(ResourceBundle.getBundle("messages").getString("action.delete"));
this.dialog = dialog;
}
@Override
public void actionPerformed(ActionEvent e) {
if(dialogWrapper != null) {
dialogWrapper.close(EXIT_CODE, false);
}
}
@Override
public Action getAction(DialogWrapper dialogWrapper) {
this.dialogWrapper = dialogWrapper;
return this;
dialog.close(EXIT_CODE, false);
}
}