Search in sources :

Example 21 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class ProjectFileStorageTest method testFarAbsolutePaths.

@Test
public void testFarAbsolutePaths() throws Exception {
    File projFile = new File(PROJECT_DIR, "defaultdirs.project");
    Omegat omt = ProjectFileStorage.parseProjectFile(projFile);
    String prefix = repeat(OConsts.MAX_PARENT_DIRECTORIES_ABS2REL, "a/");
    File projRoot = Paths.get(tempDir.getAbsolutePath(), prefix, "root").toFile();
    projRoot.mkdirs();
    // Set project folders to absolute paths
    File srcDir = new File(tempDir, "source").getAbsoluteFile();
    File trgDir = new File(tempDir, "target").getAbsoluteFile();
    File dictDir = new File(tempDir, "dictionary").getAbsoluteFile();
    File glosDir = new File(tempDir, "glossary").getAbsoluteFile();
    File tmDir = new File(tempDir, "tm").getAbsoluteFile();
    omt.getProject().setSourceDir(srcDir.getPath());
    omt.getProject().setTargetDir(trgDir.getPath());
    omt.getProject().setDictionaryDir(dictDir.getPath());
    omt.getProject().setGlossaryDir(glosDir.getPath());
    omt.getProject().setTmDir(tmDir.getPath());
    // Make all the actual folders
    Arrays.asList(srcDir, trgDir, dictDir, glosDir, tmDir).forEach(File::mkdirs);
    // Load the ProjectProperties and verify that the project folders
    // are resolved correctly
    ProjectProperties props = ProjectFileStorage.loadPropertiesFile(projRoot, omt);
    props.verifyProject();
    // Write the project file out and read it again to make sure the
    // paths are correctly round-tripped. Since these are "far" paths
    // they should remain absolute.
    ProjectFileStorage.writeProjectFile(props);
    File outProjFile = new File(projRoot, OConsts.FILE_PROJECT);
    assertTrue(outProjFile.isFile());
    Omegat outOmt = ProjectFileStorage.parseProjectFile(outProjFile);
    assertEquals(ProjectFileStorage.normalizeSlashes(srcDir.getPath()), outOmt.getProject().getSourceDir());
    assertEquals(ProjectFileStorage.normalizeSlashes(trgDir.getPath()), outOmt.getProject().getTargetDir());
    assertEquals(ProjectFileStorage.normalizeSlashes(dictDir.getPath()), outOmt.getProject().getDictionaryDir());
    assertEquals(ProjectFileStorage.normalizeSlashes(glosDir.getPath()), outOmt.getProject().getGlossaryDir());
    assertEquals(ProjectFileStorage.normalizeSlashes(tmDir.getPath()), outOmt.getProject().getTmDir());
}
Also used : ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) Omegat(gen.core.project.Omegat) Test(org.junit.Test)

Example 22 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class GlossaryTextArea method showCreateGlossaryEntryDialog.

@Override
public void showCreateGlossaryEntryDialog(final Frame parent) {
    CreateGlossaryEntry d = createGlossaryEntryDialog;
    if (d != null) {
        d.requestFocus();
        return;
    }
    ProjectProperties props = Core.getProject().getProjectProperties();
    final File out = new File(props.getWriteableGlossary());
    final CreateGlossaryEntry dialog = new CreateGlossaryEntry(parent);
    String txt = dialog.getGlossaryFileText().getText();
    txt = MessageFormat.format(txt, out.getAbsolutePath());
    dialog.getGlossaryFileText().setText(txt);
    dialog.getSourceText().requestFocus();
    dialog.addWindowFocusListener(new WindowFocusListener() {

        @Override
        public void windowLostFocus(WindowEvent e) {
        }

        @Override
        public void windowGainedFocus(WindowEvent e) {
            Window w = e.getOppositeWindow();
            if (w != null) {
                String sel = getSelectedText(w.getMostRecentFocusOwner());
                if (!StringUtil.isEmpty(sel)) {
                    if (StringUtil.isEmpty(dialog.getSourceText().getText())) {
                        setText(dialog.getSourceText(), sel);
                    } else if (StringUtil.isEmpty(dialog.getTargetText().getText())) {
                        setText(dialog.getTargetText(), sel);
                    } else if (StringUtil.isEmpty(dialog.getCommentText().getText())) {
                        setText(dialog.getCommentText(), sel);
                    }
                }
            }
        }

        private String getSelectedText(Component comp) {
            String result = null;
            if (comp instanceof JTextComponent) {
                result = ((JTextComponent) comp).getSelectedText();
                if (!StringUtil.isEmpty(result)) {
                    result = EditorUtils.removeDirectionChars(result);
                }
            }
            return result;
        }

        private void setText(JTextComponent comp, String text) {
            comp.setText(text);
            comp.requestFocus();
            comp.selectAll();
        }
    });
    dialog.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosed(WindowEvent e) {
            createGlossaryEntryDialog = null;
            if (dialog.getReturnStatus() == CreateGlossaryEntry.RET_OK) {
                String src = StringUtil.normalizeUnicode(dialog.getSourceText().getText()).trim();
                String loc = StringUtil.normalizeUnicode(dialog.getTargetText().getText()).trim();
                String com = StringUtil.normalizeUnicode(dialog.getCommentText().getText()).trim();
                if (!StringUtil.isEmpty(src) && !StringUtil.isEmpty(loc)) {
                    try {
                        GlossaryReaderTSV.append(out, new GlossaryEntry(src, loc, com, true, out.getPath()));
                    } catch (Exception ex) {
                        Log.log(ex);
                    }
                }
            }
        }
    });
    StaticUIUtils.persistGeometry(dialog, Preferences.CREATE_GLOSSARY_GEOMETRY_PREFIX);
    dialog.setVisible(true);
    createGlossaryEntryDialog = dialog;
}
Also used : IMainWindow(org.omegat.gui.main.IMainWindow) Window(java.awt.Window) WindowAdapter(java.awt.event.WindowAdapter) JTextComponent(javax.swing.text.JTextComponent) ProjectProperties(org.omegat.core.data.ProjectProperties) CreateGlossaryEntry(org.omegat.gui.dialogs.CreateGlossaryEntry) WindowEvent(java.awt.event.WindowEvent) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) CreateGlossaryEntry(org.omegat.gui.dialogs.CreateGlossaryEntry) File(java.io.File) WindowFocusListener(java.awt.event.WindowFocusListener)

Example 23 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class ProjectMedProcessing method createMed.

public static void createMed(File medZip, ProjectProperties props) throws Exception {
    if (medZip.getAbsolutePath().startsWith(props.getProjectRoot())) {
        throw new Exception("Med can't be inside project");
    }
    File sourceMedFile = getOriginMedFile(props);
    Properties translationContent = new Properties();
    String slang = props.getSourceLanguage().getLanguage();
    String tlang = props.getTargetLanguage().getLanguage();
    String medName = medZip.getName().replaceAll("\\.zip$", "");
    try (ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(medZip))) {
        if (sourceMedFile != null) {
            String oldMedName = sourceMedFile.getName().replaceAll("\\.zip$", "");
            String[] skipPrefixes = new String[] { oldMedName + "/doc/main/" + slang + "/", oldMedName + "/doc/main/" + tlang + "/" };
            String[] skipFiles = new String[] { oldMedName + "/dossier/workflow/translation", oldMedName + "/doc/support/" + slang + "/tm/" + props.getProjectName() + OConsts.LEVEL2_TMX + OConsts.TMX_EXTENSION, oldMedName + "/doc/support/" + slang + "/term/glossary.txt" };
            try (ZipFile zip = new ZipFile(sourceMedFile)) {
                // extract old file
                ZipEntry tre = zip.getEntry(oldMedName + "/dossier/workflow/translation");
                if (tre == null) {
                    throw new Exception("Wrong MED zip structure");
                }
                try (InputStream in = zip.getInputStream(tre)) {
                    translationContent.load(in);
                }
                for (Enumeration<? extends ZipEntry> en = zip.entries(); en.hasMoreElements(); ) {
                    ZipEntry e = en.nextElement();
                    if (e.isDirectory()) {
                        continue;
                    }
                    boolean add = true;
                    for (String skip : skipFiles) {
                        if (e.getName().equals(skip)) {
                            add = false;
                            break;
                        }
                    }
                    if (add) {
                        for (String skip : skipPrefixes) {
                            if (e.getName().startsWith(skip)) {
                                add = false;
                                break;
                            }
                        }
                    }
                    if (!add) {
                        continue;
                    }
                    String newName = medName + e.getName().substring(oldMedName.length());
                    outZip.putNextEntry(new ZipEntry(newName));
                    try (InputStream in = zip.getInputStream(e)) {
                        IOUtils.copy(in, outZip);
                    }
                }
            }
        }
        translationContent.put("slang", slang);
        translationContent.put("tlang", tlang);
        outZip.putNextEntry(new ZipEntry(medName + "/dossier/workflow/translation"));
        translationContent.store(outZip, null);
        // source files
        packDir(props.getSourceDir().getAsFile(), medName + "/doc/main/" + slang + "/", outZip);
        // translations
        packDir(props.getTargetDir().getAsFile(), medName + "/doc/main/" + tlang + "/", outZip);
        // level2.tmx
        outZip.putNextEntry(new ZipEntry(medName + "/doc/support/" + slang + "/tm/" + props.getProjectName() + OConsts.LEVEL2_TMX + OConsts.TMX_EXTENSION));
        FileUtils.copyFile(new File(props.getProjectRootDir(), props.getProjectName() + OConsts.LEVEL2_TMX + OConsts.TMX_EXTENSION), outZip);
        // statistics
        outZip.putNextEntry(new ZipEntry(medName + "/dossier/post/" + OConsts.STATS_FILENAME));
        FileUtils.copyFile(new File(props.getProjectInternalDir(), OConsts.STATS_FILENAME), outZip);
        // writable glossary
        if (props.getWritableGlossaryFile().getAsFile().exists()) {
            outZip.putNextEntry(new ZipEntry(medName + "/doc/support/" + slang + "/term/glossary.txt"));
            FileUtils.copyFile(props.getWritableGlossaryFile().getAsFile(), outZip);
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) Properties(java.util.Properties) ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 24 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class ConvertProject26to37team method convert.

private static void convert(File projectRootFolder) throws Exception {
    ProjectProperties props = ProjectFileStorage.loadProjectProperties(projectRootFolder);
    String version;
    String url;
    RepositoryDefinition def = new RepositoryDefinition();
    if (isSVNDirectory(projectRootFolder)) {
        url = getSVNUrl(projectRootFolder);
        def.setType("svn");
        version = getSVNTmxVersion(projectRootFolder);
    } else {
        url = getGITUrl(projectRootFolder);
        def.setType("git");
        version = getGITTmxVersion(projectRootFolder);
    }
    if (url == null) {
        throw new Exception("Repository URL not defined");
    }
    def.setUrl(url);
    saveVersion(projectRootFolder, "omegat/project_save.tmx", version);
    // map full project
    RepositoryMapping map = new RepositoryMapping();
    map.setLocal("");
    map.setRepository("");
    def.getMapping().add(map);
    props.setRepositories(new ArrayList<RepositoryDefinition>());
    props.getRepositories().add(def);
    ProjectFileStorage.writeProjectFile(props);
    // all data saved - remove old repository
    FileUtils.deleteDirectory(new File(projectRootFolder, ".svn"));
    FileUtils.deleteDirectory(new File(projectRootFolder, ".git"));
}
Also used : RepositoryDefinition(gen.core.project.RepositoryDefinition) RepositoryMapping(gen.core.project.RepositoryMapping) ProjectProperties(org.omegat.core.data.ProjectProperties) File(java.io.File) IOException(java.io.IOException)

Example 25 with ProjectProperties

use of org.omegat.core.data.ProjectProperties in project omegat by omegat-org.

the class ProjectUICommands method projectEditProperties.

public static void projectEditProperties() {
    UIThreadsUtil.mustBeSwingThread();
    if (!Core.getProject().isProjectLoaded()) {
        return;
    }
    // commit the current entry first
    Core.getEditor().commitAndLeave();
    // displaying the dialog to change paths and other properties
    ProjectPropertiesDialog prj = new ProjectPropertiesDialog(Core.getMainWindow().getApplicationFrame(), Core.getProject().getProjectProperties(), Core.getProject().getProjectProperties().getProjectName(), ProjectPropertiesDialog.Mode.EDIT_PROJECT);
    prj.setVisible(true);
    final ProjectProperties newProps = prj.getResult();
    prj.dispose();
    if (newProps == null) {
        return;
    }
    int res = JOptionPane.showConfirmDialog(Core.getMainWindow().getApplicationFrame(), OStrings.getString("MW_REOPEN_QUESTION"), OStrings.getString("MW_REOPEN_TITLE"), JOptionPane.YES_NO_OPTION);
    if (res != JOptionPane.YES_OPTION) {
        return;
    }
    new SwingWorker<Void, Void>() {

        int previousCurEntryNum = Core.getEditor().getCurrentEntryNumber();

        protected Void doInBackground() throws Exception {
            Core.executeExclusively(true, () -> {
                Core.getProject().saveProject(true);
                ProjectFactory.closeProject();
                ProjectFactory.loadProject(newProps, true);
            });
            return null;
        }

        protected void done() {
            try {
                get();
                // Make sure to update Editor title
                SwingUtilities.invokeLater(() -> {
                    // activate entry later - after project will be loaded
                    Core.getEditor().gotoEntry(previousCurEntryNum);
                    Core.getEditor().requestFocus();
                });
            } catch (Exception ex) {
                processSwingWorkerException(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
            }
        }
    }.execute();
}
Also used : ProjectPropertiesDialog(org.omegat.gui.dialogs.ProjectPropertiesDialog) ProjectProperties(org.omegat.core.data.ProjectProperties) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

ProjectProperties (org.omegat.core.data.ProjectProperties)29 File (java.io.File)20 Test (org.junit.Test)11 IOException (java.io.IOException)10 ExecutionException (java.util.concurrent.ExecutionException)6 KnownException (org.omegat.core.KnownException)6 Omegat (gen.core.project.Omegat)4 Cursor (java.awt.Cursor)4 List (java.util.List)4 Language (org.omegat.util.Language)4 ArrayList (java.util.ArrayList)3 NotLoadedProject (org.omegat.core.data.NotLoadedProject)3 ProjectPropertiesDialog (org.omegat.gui.dialogs.ProjectPropertiesDialog)3 RepositoryDefinition (gen.core.project.RepositoryDefinition)2 RepositoryMapping (gen.core.project.RepositoryMapping)2 InputStream (java.io.InputStream)2 Properties (java.util.Properties)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 Before (org.junit.Before)2