Search in sources :

Example 1 with ProjectPropertiesDialog

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

the class ProjectUICommands method projectOpen.

/**
 * Open project. Does nothing if a project is already open and closeCurrent is false.
 *
 * @param projectDirectory
 *            project directory or null if user must choose it
 * @param closeCurrent
 *            whether or not to close the current project first, if any
 */
public static void projectOpen(final File projectDirectory, boolean closeCurrent) {
    UIThreadsUtil.mustBeSwingThread();
    if (Core.getProject().isProjectLoaded()) {
        if (closeCurrent) {
            // Register to try again after closing the current project.
            CoreEvents.registerProjectChangeListener(new IProjectEventListener() {

                public void onProjectChanged(PROJECT_CHANGE_TYPE eventType) {
                    if (eventType == PROJECT_CHANGE_TYPE.CLOSE) {
                        projectOpen(projectDirectory, false);
                        CoreEvents.unregisterProjectChangeListener(this);
                    }
                }
            });
            projectClose();
        }
        return;
    }
    final File projectRootFolder;
    if (projectDirectory == null) {
        // select existing project file - open it
        OmegaTFileChooser pfc = new OpenProjectFileChooser();
        if (OmegaTFileChooser.APPROVE_OPTION != pfc.showOpenDialog(Core.getMainWindow().getApplicationFrame())) {
            return;
        }
        projectRootFolder = pfc.getSelectedFile();
    } else {
        projectRootFolder = projectDirectory;
    }
    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);
            try {
                // convert old projects if need
                ConvertProject.convert(projectRootFolder);
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_CONVERT_PROJECT");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_CONVERT_PROJECT");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            // check if project okay
            ProjectProperties props;
            try {
                props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            try {
                boolean needToSaveProperties = false;
                if (props.hasRepositories()) {
                    // This is a remote project
                    if (!Core.getParams().containsKey(CLIParameters.NO_TEAM)) {
                        // Save repository mapping
                        List<RepositoryDefinition> repos = props.getRepositories();
                        // Update project.properties
                        mainWindow.showStatusMessageRB("TEAM_OPEN");
                        try {
                            RemoteRepositoryProvider remoteRepositoryProvider = new RemoteRepositoryProvider(props.getProjectRootDir(), props.getRepositories());
                            remoteRepositoryProvider.switchToVersion(OConsts.FILE_PROJECT, null);
                            // Overwrite omegat.project
                            remoteRepositoryProvider.copyFilesFromRepoToProject(OConsts.FILE_PROJECT);
                            // Reload project properties
                            props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
                            if (props.getRepositories() == null) {
                                // We have a 3.6 style project,
                                // so we restore the mapping we just lost
                                props.setRepositories(repos);
                                needToSaveProperties = true;
                            }
                        } catch (Exception e) {
                            Log.logErrorRB(e, "TF_PROJECT_PROPERTIES_ERROR");
                            Log.log(e);
                            throw new IOException(OStrings.getString("TF_PROJECT_PROPERTIES_ERROR") + "\n" + e.getMessage(), e);
                        }
                    }
                    // team project - non-exist directories could be created from repo
                    props.autocreateDirectories();
                } else {
                    // not a team project - ask for non-exist directories
                    while (!props.isProjectValid()) {
                        needToSaveProperties = true;
                        // something wrong with the project - display open dialog
                        // to fix it
                        ProjectPropertiesDialog prj = new ProjectPropertiesDialog(Core.getMainWindow().getApplicationFrame(), props, new File(projectRootFolder, OConsts.FILE_PROJECT).getAbsolutePath(), ProjectPropertiesDialog.Mode.RESOLVE_DIRS);
                        prj.setVisible(true);
                        props = prj.getResult();
                        prj.dispose();
                        if (props == null) {
                            // user clicks on 'Cancel'
                            mainWindow.setCursor(oldCursor);
                            return null;
                        }
                    }
                }
                final ProjectProperties propsP = props;
                Core.executeExclusively(true, () -> ProjectFactory.loadProject(propsP, true));
                if (needToSaveProperties) {
                    Core.getProject().saveProjectProperties();
                }
            } catch (Exception ex) {
                Log.logErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                Core.getMainWindow().displayErrorRB(ex, "PP_ERROR_UNABLE_TO_READ_PROJECT_FILE");
                mainWindow.setCursor(oldCursor);
                return null;
            }
            RecentProjects.add(projectRootFolder.getAbsolutePath());
            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 : RemoteRepositoryProvider(org.omegat.core.team2.RemoteRepositoryProvider) OmegaTFileChooser(org.omegat.util.gui.OmegaTFileChooser) ProjectProperties(org.omegat.core.data.ProjectProperties) IOException(java.io.IOException) Cursor(java.awt.Cursor) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) OpenProjectFileChooser(org.omegat.util.gui.OpenProjectFileChooser) ArrayList(java.util.ArrayList) List(java.util.List) ProjectPropertiesDialog(org.omegat.gui.dialogs.ProjectPropertiesDialog) IProjectEventListener(org.omegat.core.events.IProjectEventListener) File(java.io.File)

Example 2 with ProjectPropertiesDialog

use of org.omegat.gui.dialogs.ProjectPropertiesDialog 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)

Example 3 with ProjectPropertiesDialog

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

the class ProjectUICommands method projectCreate.

public static void projectCreate() {
    UIThreadsUtil.mustBeSwingThread();
    if (Core.getProject().isProjectLoaded()) {
        return;
    }
    // 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();
            // ask about new project properties
            ProjectProperties props = new ProjectProperties(dir);
            props.setSourceLanguage(Preferences.getPreferenceDefault(Preferences.SOURCE_LOCALE, "EN-US"));
            props.setTargetLanguage(Preferences.getPreferenceDefault(Preferences.TARGET_LOCALE, "EN-GB"));
            ProjectPropertiesDialog newProjDialog = new ProjectPropertiesDialog(Core.getMainWindow().getApplicationFrame(), props, dir.getAbsolutePath(), ProjectPropertiesDialog.Mode.NEW_PROJECT);
            newProjDialog.setVisible(true);
            newProjDialog.dispose();
            IMainWindow mainWindow = Core.getMainWindow();
            Cursor hourglassCursor = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
            Cursor oldCursor = mainWindow.getCursor();
            mainWindow.setCursor(hourglassCursor);
            final ProjectProperties newProps = newProjDialog.getResult();
            if (newProps == null) {
                // user clicks on 'Cancel'
                dir.delete();
                mainWindow.setCursor(oldCursor);
                return null;
            }
            final String projectRoot = newProps.getProjectRoot();
            if (!StringUtil.isEmpty(projectRoot)) {
                // 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());
            mainWindow.setCursor(oldCursor);
            return null;
        }
    }.execute();
}
Also used : NewProjectFileChooser(org.omegat.gui.dialogs.NewProjectFileChooser) ProjectPropertiesDialog(org.omegat.gui.dialogs.ProjectPropertiesDialog) ProjectProperties(org.omegat.core.data.ProjectProperties) Cursor(java.awt.Cursor) File(java.io.File) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IOException (java.io.IOException)3 ExecutionException (java.util.concurrent.ExecutionException)3 KnownException (org.omegat.core.KnownException)3 ProjectProperties (org.omegat.core.data.ProjectProperties)3 ProjectPropertiesDialog (org.omegat.gui.dialogs.ProjectPropertiesDialog)3 Cursor (java.awt.Cursor)2 File (java.io.File)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 IProjectEventListener (org.omegat.core.events.IProjectEventListener)1 RemoteRepositoryProvider (org.omegat.core.team2.RemoteRepositoryProvider)1 NewProjectFileChooser (org.omegat.gui.dialogs.NewProjectFileChooser)1 OmegaTFileChooser (org.omegat.util.gui.OmegaTFileChooser)1 OpenProjectFileChooser (org.omegat.util.gui.OpenProjectFileChooser)1