Skip to content

Commit

Permalink
restructure form actions
Browse files Browse the repository at this point in the history
Resolves #199
  • Loading branch information
marhali committed Dec 7, 2022
1 parent f53b867 commit 414d413
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 51 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.*)
Expand Down
17 changes: 2 additions & 15 deletions src/main/java/de/marhali/easyi18n/dialog/AddDialog.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,8 +13,6 @@
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.
Expand All @@ -35,6 +32,8 @@ public AddDialog(@NotNull Project project, @Nullable KeyPath prefillKey, @Nullab
? new TranslationValue(ProjectSettingsService.get(project).getState().getPreviewLocale(), prefillLocale)
: null)
);

setTitle(bundle.getString("action.add"));
}

/**
Expand All @@ -45,18 +44,6 @@ public AddDialog(@NotNull Project project) {
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) {
Expand Down
14 changes: 4 additions & 10 deletions src/main/java/de/marhali/easyi18n/dialog/EditDialog.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions src/main/java/de/marhali/easyi18n/dialog/TranslationDialog.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -28,7 +28,7 @@
* 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");

Expand All @@ -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);
Expand Down Expand Up @@ -75,14 +77,6 @@ public void registerCallback(Consumer<TranslationUpdate> callback) {
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
Expand All @@ -95,7 +89,10 @@ public void registerCallback(Consumer<TranslationUpdate> callback) {
* 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) {
Expand All @@ -120,15 +117,16 @@ public void showAndHandle() {
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)
.getPanel();

panel.setMinimumSize(new Dimension(200, 150));

return configure(panel);
return panel;
}

private JComponent createLocalesPanel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,26 +12,19 @@
* 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);
}
}

0 comments on commit 414d413

Please sign in to comment.