Skip to content

Commit

Permalink
chore: update setup/teardown logic of acceptance test (#1230)
Browse files Browse the repository at this point in the history
* chore: update setup/teardown logic of acceptance test

- aligner: reduce dependency for Core
- EditorUtilsTest: update expectation for stability
- TestCoreGUI: add initialize once part

Signed-off-by: Hiroshi Miura <[email protected]>

* refactor: aligner initialization

- Improve acceptance test
- Improve AlignFilePickerController#show
 to split method into separate private methods

Signed-off-by: Hiroshi Miura <[email protected]>

* refactor: aligner initialization

- Improve acceptance test
- Improve AlignFilePickerController#show
 to split method into separate private methods

Signed-off-by: Hiroshi Miura <[email protected]>

* docs: improve TestCoreGUI javadoc

Signed-off-by: Hiroshi Miura <[email protected]>

* refactor: AlignFilePickerController

Signed-off-by: Hiroshi Miura <[email protected]>

* docs: explanation of expectation for EditorUtilsGUITest

Signed-off-by: Hiroshi Miura <[email protected]>

---------

Signed-off-by: Hiroshi Miura <[email protected]>
  • Loading branch information
miurahr authored Dec 21, 2024
1 parent c9b91fe commit 2165970
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 209 deletions.
347 changes: 185 additions & 162 deletions aligner/src/main/java/org/omegat/gui/align/AlignFilePickerController.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,122 @@ public void setDefaultSaveDir(String defaultSaveDir) {
* @param parent
* Parent window of file picker and align window
*/
@SuppressWarnings("serial")
public void show(final Component parent) {
JFrame frame = initGUI(parent);
frame.setLocationRelativeTo(parent);
frame.setVisible(true);
}

// public for test only
@SuppressWarnings("serial")
public JFrame initGUI(Component parent) {
final JFrame frame = new JFrame(BUNDLE.getString("ALIGNER_FILEPICKER"));
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
StaticUIUtils.setEscapeClosable(frame);
frame.setName("ALIGNER_FILEPICKER");
//
AlignFilePicker picker = new AlignFilePicker();
initializeFilePicker(picker);
// add the main listener.
picker.okButton.addActionListener(e -> {
picker.bottomPanel.remove(picker.messageTextArea);
picker.bottomPanel.add(picker.progressBar, BorderLayout.CENTER);
picker.bottomPanel.revalidate();
new SwingWorker<Aligner, Void>() {
@Override
protected Aligner doInBackground() throws Exception {
Aligner aligner = new Aligner(sourceFile, sourceLanguage, targetFile, targetLanguage);
aligner.loadFiles();
return aligner;
}

final AlignFilePicker picker = new AlignFilePicker();
@Override
protected void done() {
try {
Aligner aligner = get();
new AlignPanelController(aligner, defaultSaveDir).show(parent);
} catch (CancellationException e) {
// Ignore
} catch (Exception e) {
Log.log(e);
JOptionPane.showMessageDialog(frame, BUNDLE.getString("ALIGNER_ERROR_LOADING"),
BUNDLE.getString("ERROR_TITLE"), JOptionPane.ERROR_MESSAGE);
}
frame.dispose();
}
}.execute();
});
picker.cancelButton.addActionListener(e -> frame.dispose());
addListeners(picker, frame);
frame.getRootPane().setDefaultButton(picker.okButton);
updatePicker(picker);
frame.add(picker);
frame.pack();
return frame;
}

/**
* Initialize FilePicker.
* @return FilePicker frame.
*/
private void initializeFilePicker(AlignFilePicker picker) {
picker.setName("align_picker_panel");
picker.sourceLanguagePicker
.setModel(new DefaultComboBoxModel<>(new Vector<>(Language.getLanguages())));
picker.sourceLanguagePicker.setRenderer(new LanguageComboBoxRenderer());
picker.sourceLanguagePicker.setSelectedItem(sourceLanguage);
picker.sourceLanguagePicker.setName("sourceLanguagePicker");

picker.targetLanguagePicker
.setModel(new DefaultComboBoxModel<>(new Vector<>(Language.getLanguages())));
picker.targetLanguagePicker.setRenderer(new LanguageComboBoxRenderer());
picker.targetLanguagePicker.setSelectedItem(targetLanguage);
picker.targetLanguagePicker.setName("targetLanguagePicker");

picker.sourceChooseFileButton.setName("sourceChooseFileButton");
picker.targetChooseFileButton.setName("targetChooseFileButton");
picker.sourceLanguageFileField.setText(sourceFile);
picker.sourceLanguageFileField.setName("sourceLanguageFileField");

picker.targetLanguageFileField.setText(targetFile);
picker.targetLanguageFileField.setName("targetLanguageFileField");

picker.sourceLanguageFileField.setTransferHandler(languageFileFieldTransferHandler);
picker.targetLanguageFileField.setTransferHandler(languageFileFieldTransferHandler);

picker.okButton.setName("OK");
picker.cancelButton.setName("Cancel");
}

private final TransferHandler languageFileFieldTransferHandler = new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
try {
List<?> list = (List<?>) support.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor);
List<File> files = getSupportedFiles(list);
if (files.isEmpty()) {
return false;
}
JTextComponent field = (JTextComponent) support.getComponent();
field.setText(files.get(0).getAbsolutePath());
return true;
} catch (Exception e) {
Log.log(e);
return false;
}
}
};

private void addListeners(AlignFilePicker picker, JFrame frame) {
picker.sourceLanguagePicker.addItemListener(e -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
Expand All @@ -194,11 +296,7 @@ public void show(final Component parent) {
}
updatePicker(picker);
});
picker.targetLanguagePicker
.setModel(new DefaultComboBoxModel<>(new Vector<>(Language.getLanguages())));
picker.targetLanguagePicker.setRenderer(new LanguageComboBoxRenderer());
picker.targetLanguagePicker.setSelectedItem(targetLanguage);
picker.targetLanguagePicker.setName("targetLanguagePicker");

picker.targetLanguagePicker.addItemListener(e -> {
if (e.getStateChange() != ItemEvent.SELECTED) {
return;
Expand Down Expand Up @@ -244,161 +342,16 @@ public void show(final Component parent) {
targetFile = file.getAbsolutePath();
}
});
picker.sourceChooseFileButton.setName("sourceChooseFileButton");
picker.targetChooseFileButton.setName("targetChooseFileButton");
picker.sourceLanguageFileField.setText(sourceFile);
picker.sourceLanguageFileField.setName("sourceLanguageFileField");
picker.sourceLanguageFileField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
update();
}

@Override
public void insertUpdate(DocumentEvent e) {
update();
}

@Override
public void changedUpdate(DocumentEvent e) {
update();
}

private void update() {
sourceFile = picker.sourceLanguageFileField.getText();
updatePicker(picker);
}
});
picker.targetLanguageFileField.setText(targetFile);
picker.targetLanguageFileField.setName("targetLanguageFileField");
picker.targetLanguageFileField.getDocument().addDocumentListener(new DocumentListener() {
@Override
public void removeUpdate(DocumentEvent e) {
update();
}

@Override
public void insertUpdate(DocumentEvent e) {
update();
}

@Override
public void changedUpdate(DocumentEvent e) {
update();
}

private void update() {
targetFile = picker.targetLanguageFileField.getText();
updatePicker(picker);
}
});

TransferHandler transferHandler = new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
try {
List<?> list = (List<?>) support.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor);
List<File> files = getSupportedFiles(list);
if (files.isEmpty()) {
return false;
}
JTextComponent field = (JTextComponent) support.getComponent();
field.setText(files.get(0).getAbsolutePath());
return true;
} catch (Exception e) {
Log.log(e);
return false;
}
}
};
picker.sourceLanguageFileField.setTransferHandler(transferHandler);
picker.targetLanguageFileField.setTransferHandler(transferHandler);

picker.setTransferHandler(new TransferHandler() {
@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
try {
List<?> list = (List<?>) support.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor);
List<File> files = getSupportedFiles(list);
if (files.isEmpty()) {
return false;
} else if (files.size() == 1) {
JTextComponent insertTarget = picker.sourceLanguageFileField;
if (picker.sourceLanguageFileField.getDocument().getLength() != 0
&& picker.targetLanguageFileField.getDocument().getLength() == 0) {
insertTarget = picker.targetLanguageFileField;
}
insertTarget.setText(files.get(0).getAbsolutePath());
} else {
picker.sourceLanguageFileField.setText(files.get(0).getAbsolutePath());
picker.targetLanguageFileField.setText(files.get(1).getAbsolutePath());
}
return true;
} catch (Exception e) {
Log.log(e);
return false;
}
}
});

picker.okButton.addActionListener(e -> {
picker.bottomPanel.remove(picker.messageTextArea);
picker.bottomPanel.add(picker.progressBar, BorderLayout.CENTER);
picker.bottomPanel.revalidate();
new SwingWorker<Aligner, Void>() {
@Override
protected Aligner doInBackground() throws Exception {
Aligner aligner = new Aligner(sourceFile, sourceLanguage, targetFile, targetLanguage);
aligner.loadFiles();
return aligner;
}

@Override
protected void done() {
try {
Aligner aligner = get();
new AlignPanelController(aligner, defaultSaveDir).show(parent);
} catch (CancellationException e) {
// Ignore
} catch (Exception e) {
Log.log(e);
JOptionPane.showMessageDialog(frame, BUNDLE.getString("ALIGNER_ERROR_LOADING"),
BUNDLE.getString("ERROR_TITLE"), JOptionPane.ERROR_MESSAGE);
}
frame.dispose();
}
}.execute();
});
picker.cancelButton.addActionListener(e -> frame.dispose());
picker.okButton.setName("OK");
picker.cancelButton.setName("Cancel");

frame.getRootPane().setDefaultButton(picker.okButton);

updatePicker(picker);

frame.add(picker);
frame.pack();
frame.setLocationRelativeTo(parent);
frame.setVisible(true);
picker.sourceLanguageFileField.getDocument().addDocumentListener(new AlignFilePickerDocumentListener(() -> {
sourceFile = picker.sourceLanguageFileField.getText();
updatePicker(picker);
}));
picker.targetLanguageFileField.getDocument().addDocumentListener(new AlignFilePickerDocumentListener(() -> {
targetFile = picker.targetLanguageFileField.getText();
updatePicker(picker);
}));
picker.setTransferHandler(new PickerTransferHandler(picker));
}

private void updatePicker(final AlignFilePicker picker) {
Expand Down Expand Up @@ -483,6 +436,76 @@ public boolean accept(File f) {
return null;
}

private static class AlignFilePickerDocumentListener implements DocumentListener {
private final Runnable runnable;

AlignFilePickerDocumentListener(Runnable runnable) {
this.runnable = runnable;
}

@Override
public void removeUpdate(DocumentEvent e) {
update();
}

@Override
public void insertUpdate(DocumentEvent e) {
update();
}

@Override
public void changedUpdate(DocumentEvent e) {
update();
}

private void update() {
runnable.run();
}
}

@SuppressWarnings("serial")
private static class PickerTransferHandler extends TransferHandler {
private final AlignFilePicker picker;

PickerTransferHandler(final AlignFilePicker picker) {
this.picker = picker;
}

@Override
public boolean canImport(TransferSupport support) {
return support.isDataFlavorSupported(DataFlavor.javaFileListFlavor);
}

@Override
public boolean importData(TransferSupport support) {
if (!canImport(support)) {
return false;
}
try {
List<?> list = (List<?>) support.getTransferable()
.getTransferData(DataFlavor.javaFileListFlavor);
List<File> files = getSupportedFiles(list);
if (files.isEmpty()) {
return false;
} else if (files.size() == 1) {
JTextComponent insertTarget = picker.sourceLanguageFileField;
if (picker.sourceLanguageFileField.getDocument().getLength() != 0
&& picker.targetLanguageFileField.getDocument().getLength() == 0) {
insertTarget = picker.targetLanguageFileField;
}
insertTarget.setText(files.get(0).getAbsolutePath());
} else {
picker.sourceLanguageFileField.setText(files.get(0).getAbsolutePath());
picker.targetLanguageFileField.setText(files.get(1).getAbsolutePath());
}
return true;
} catch (Exception e) {
Log.log(e);
return false;
}
}
}

/**
* Entry point for debugging or standalone use. Optionally accepts four
* arguments to pre-fill the picker:
Expand All @@ -494,7 +517,7 @@ public boolean accept(File f) {
* </ol>
*
* @param args command arguments.
* @throws Exception when failed to ininitalize OmegaT core.
* @throws Exception when failed to ininitalize OmegaT core.
*/
public static void main(String[] args) throws Exception {
System.setProperty("apple.laf.useScreenMenuBar", "true");
Expand Down
Loading

0 comments on commit 2165970

Please sign in to comment.