Skip to content

Commit

Permalink
Plugin compatibility for OmegaT version pre/post 4.3.0
Browse files Browse the repository at this point in the history
Copied the method Core.executeExclusively(boolean,
RunnableWithException) to allow the plugin to run in old (pre 4.3.0) and
new versions of OmegaT.
  • Loading branch information
briacp committed Nov 7, 2019
1 parent ed3d6e7 commit aa8f1ce
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## [v1.3.0]

- Copied the method Core.executeExclusively(boolean, RunnableWithException) to allow the plugin to run in old (pre 4.3.0) and new versions of OmegaT.

## [v1.2.1]

- Create `.empty` file when a directory containing only excluded file is empty.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ repositories {
}

def compileDependencies = [
'org.omegat:omegat:4.3.0',
'org.omegat:omegat:4.1.3-02',
'commons-io:commons-io:2.4',
'commons-lang:commons-lang:2.6'
]
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pluginMainClass=net.briac.omegat.plugin.omt.ManageOMTPackage
pluginName=OMT Package Plugin
version=1.2.1
version=1.3.0
omegatPluginDir=C:/Users/briac/AppData/Roaming/OmegaT/plugins/
114 changes: 85 additions & 29 deletions src/main/java/net/briac/omegat/plugin/omt/ManageOMTPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,44 @@
**************************************************************************/
package net.briac.omegat.plugin.omt;

import static org.omegat.core.Core.getMainWindow;

import java.awt.Cursor;
import java.awt.Desktop;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import org.apache.commons.io.FileUtils;
import groovyjarjarcommonscli.CommandLine;
import groovyjarjarcommonscli.CommandLineParser;
import groovyjarjarcommonscli.BasicParser;
import groovyjarjarcommonscli.HelpFormatter;
import groovyjarjarcommonscli.OptionBuilder;
import groovyjarjarcommonscli.Option;
import groovyjarjarcommonscli.Options;
import groovyjarjarcommonscli.ParseException;
import org.omegat.core.Core;
import org.omegat.core.CoreEvents;
import org.omegat.core.data.ProjectProperties;
Expand All @@ -39,28 +68,25 @@
import org.omegat.gui.scripting.IScriptLogger;
import org.omegat.gui.scripting.ScriptItem;
import org.omegat.gui.scripting.ScriptRunner;
import org.omegat.util.*;
import org.omegat.util.FileUtil;
import org.omegat.util.Log;
import org.omegat.util.OConsts;
import org.omegat.util.Preferences;
import org.omegat.util.StaticUtils;
import org.omegat.util.gui.OmegaTFileChooser;
import org.omegat.util.gui.UIThreadsUtil;
import org.openide.awt.Mnemonics;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

import static org.omegat.core.Core.getMainWindow;
import groovyjarjarcommonscli.BasicParser;
import groovyjarjarcommonscli.CommandLine;
import groovyjarjarcommonscli.CommandLineParser;
import groovyjarjarcommonscli.HelpFormatter;
import groovyjarjarcommonscli.Option;
import groovyjarjarcommonscli.OptionBuilder;
import groovyjarjarcommonscli.Options;
import groovyjarjarcommonscli.ParseException;

@SuppressWarnings("deprecation")
public class ManageOMTPackage {

public static final String PROPERTY_POST_PACKAGE_SCRIPT = "post-package-script";
Expand All @@ -78,14 +104,16 @@ public class ManageOMTPackage {

protected static final ResourceBundle res = ResourceBundle.getBundle("omt-package", Locale.getDefault());

private static final ReentrantLock EXCLUSIVE_RUN_LOCK = new ReentrantLock();

private static JMenuItem importOMT;
private static JMenuItem exportOMT;

private static boolean cliMode = false;

private static Properties pluginProps = new Properties();

@SuppressWarnings("static-access")
@SuppressWarnings({ "static-access" })
public static void main(String[] args) throws Exception {
File configFile = new File(StaticUtils.getConfigDir(), CONFIG_FILE);
String projectDirectoryName = null;
Expand Down Expand Up @@ -357,13 +385,12 @@ protected Void doInBackground() throws Exception {
mainWindow.setCursor(hourglassCursor);

mainWindow.showStatusMessageRB("MW_STATUS_SAVING");

Core.executeExclusively(true, () -> {
ManageOMTPackage.executeExclusively(true, () -> {
Core.getProject().saveProject(true);
});

if (Boolean.parseBoolean(pluginProps.getProperty(PROPERTY_GENERATE_TARGET, "false"))) {
Core.executeExclusively(true, () -> {
ManageOMTPackage.executeExclusively(true, () -> {
try {
Core.getProject().compileProject(".*");
} catch (Exception ex) {
Expand Down Expand Up @@ -653,5 +680,34 @@ private static void createEmptyFile(final ZipOutputStream out, Path entry) throw
out.putNextEntry(new ZipEntry(emptyDirFile.replace("\\", "/")));
out.closeEntry();
}


/** This is copied from org.omegat.core.Core.executeExclusively(boolean, RunnableWithException) to allow the
* plugin to run in old (pre 4.3.0) and new versions of OmegaT.*/
public static void executeExclusively(boolean waitForUnlock, RunnableWithException run)
throws Exception {
if (!EXCLUSIVE_RUN_LOCK.tryLock(waitForUnlock ? 180000 : 1, TimeUnit.MILLISECONDS)) {
Exception ex = new TimeoutException("Timeout waiting for previous exclusive execution");
Exception cause = new Exception("Previous exclusive execution");
if (runningStackTrace != null) {
cause.setStackTrace(runningStackTrace);
ex.initCause(cause);
}
throw ex;
}
try {
runningStackTrace = new Exception().getStackTrace();
run.run();
} finally {
runningStackTrace = null;
EXCLUSIVE_RUN_LOCK.unlock();
}
}

private static StackTraceElement[] runningStackTrace;

public interface RunnableWithException {
void run() throws Exception;
}

}

0 comments on commit aa8f1ce

Please sign in to comment.