Search in sources :

Example 1 with ChooseMedProject

use of org.omegat.gui.dialogs.ChooseMedProject in project omegat by omegat-org.

the class ProjectUICommands method projectCreateMED.

public static void projectCreateMED() {
    UIThreadsUtil.mustBeSwingThread();
    if (!Core.getProject().isProjectLoaded()) {
        return;
    }
    // commit the current entry first
    Core.getEditor().commitAndLeave();
    // ask for new MED file
    ChooseMedProject ndm = new ChooseMedProject();
    // default name
    String zipName = null;
    try {
        File origin = ProjectMedProcessing.getOriginMedFile(Core.getProject().getProjectProperties());
        if (origin != null) {
            zipName = origin.getName();
        }
    } catch (Exception ex) {
    }
    if (zipName == null) {
        zipName = Core.getProject().getProjectProperties().getProjectName() + "-MED.zip";
    }
    ndm.setSelectedFile(new File(Core.getProject().getProjectProperties().getProjectRootDir().getParentFile(), zipName));
    int ndmResult = ndm.showSaveDialog(Core.getMainWindow().getApplicationFrame());
    if (ndmResult != OmegaTFileChooser.APPROVE_OPTION) {
        // user press 'Cancel' in project creation dialog
        return;
    }
    // add .zip extension if there is no
    final File med = ndm.getSelectedFile().getName().toLowerCase(Locale.ENGLISH).endsWith(".zip") ? ndm.getSelectedFile() : new File(ndm.getSelectedFile().getAbsolutePath() + ".zip");
    new SwingWorker<Void, Void>() {

        protected Void doInBackground() throws Exception {
            IMainWindow mainWindow = Core.getMainWindow();
            Cursor hourglassCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
            Cursor oldCursor = mainWindow.getCursor();
            mainWindow.setCursor(hourglassCursor);
            mainWindow.showStatusMessageRB("MW_STATUS_SAVING");
            Core.executeExclusively(true, () -> {
                Core.getProject().saveProject(true);
                try {
                    Core.getProject().compileProject(".*");
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            });
            ProjectMedProcessing.createMed(med, Core.getProject().getProjectProperties());
            mainWindow.showStatusMessageRB("MW_STATUS_SAVED");
            mainWindow.setCursor(oldCursor);
            return null;
        }

        protected void done() {
            try {
                get();
                SwingUtilities.invokeLater(Core.getEditor()::requestFocus);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
            }
        }
    }.execute();
}
Also used : ChooseMedProject(org.omegat.gui.dialogs.ChooseMedProject) Cursor(java.awt.Cursor) File(java.io.File) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with ChooseMedProject

use of org.omegat.gui.dialogs.ChooseMedProject in project omegat by omegat-org.

the class ProjectUICommands method projectOpenMED.

public static void projectOpenMED() {
    UIThreadsUtil.mustBeSwingThread();
    if (Core.getProject().isProjectLoaded()) {
        return;
    }
    // ask for MED file
    ChooseMedProject ndm = new ChooseMedProject();
    int ndmResult = ndm.showOpenDialog(Core.getMainWindow().getApplicationFrame());
    if (ndmResult != OmegaTFileChooser.APPROVE_OPTION) {
        // user press 'Cancel' in project creation dialog
        return;
    }
    final File med = ndm.getSelectedFile();
    // ask for new project dir
    NewProjectFileChooser ndc = new NewProjectFileChooser();
    int ndcResult = ndc.showSaveDialog(Core.getMainWindow().getApplicationFrame());
    if (ndcResult != OmegaTFileChooser.APPROVE_OPTION) {
        // user press 'Cancel' in project creation dialog
        return;
    }
    final File dir = ndc.getSelectedFile();
    new SwingWorker<Void, Void>() {

        protected Void doInBackground() throws Exception {
            dir.mkdirs();
            final ProjectProperties newProps = new ProjectProperties(dir);
            ProjectMedProcessing.extractFromMed(med, newProps);
            // create project
            try {
                ProjectFactory.createProject(newProps);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
            }
            RecentProjects.add(dir.getAbsolutePath());
            return null;
        }

        protected void done() {
            try {
                get();
                SwingUtilities.invokeLater(Core.getEditor()::requestFocus);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
            }
        }
    }.execute();
}
Also used : ChooseMedProject(org.omegat.gui.dialogs.ChooseMedProject) NewProjectFileChooser(org.omegat.gui.dialogs.NewProjectFileChooser) ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 ExecutionException (java.util.concurrent.ExecutionException)2 KnownException (org.omegat.core.KnownException)2 ChooseMedProject (org.omegat.gui.dialogs.ChooseMedProject)2 Cursor (java.awt.Cursor)1 ProjectProperties (org.omegat.core.data.ProjectProperties)1 NewProjectFileChooser (org.omegat.gui.dialogs.NewProjectFileChooser)1