Search in sources :

Example 1 with OmegaTFileChooser

use of org.omegat.util.gui.OmegaTFileChooser in project omegat by omegat-org.

the class ProjectPropertiesDialog method doBrowseDirectoy.

/**
 * Browses for the directory.
 *
 * @param browseTarget
 *            customizes the messages depending on what is browsed for
 * @param field
 *            text field to write browsed folder to
 */
private void doBrowseDirectoy(int browseTarget, JTextField field) {
    if (field == null) {
        return;
    }
    String title;
    boolean fileMode = false;
    boolean glossaryFile = false;
    if (browseTarget == 6) {
        fileMode = true;
        glossaryFile = true;
    }
    switch(browseTarget) {
        case 1:
            title = OStrings.getString("PP_BROWSE_TITLE_SOURCE");
            break;
        case 2:
            title = OStrings.getString("PP_BROWSE_TITLE_TARGET");
            break;
        case 3:
            title = OStrings.getString("PP_BROWSE_TITLE_GLOS");
            break;
        case 4:
            title = OStrings.getString("PP_BROWSE_TITLE_TM");
            break;
        case 5:
            title = OStrings.getString("PP_BROWSE_TITLE_DICT");
            break;
        case 6:
            title = OStrings.getString("PP_BROWSE_W_GLOS");
            break;
        default:
            return;
    }
    OmegaTFileChooser browser = new OmegaTFileChooser();
    // String str = OStrings.getString("BUTTON_SELECT_NO_MNEMONIC");
    // browser.setApproveButtonText(str);
    browser.setDialogTitle(title);
    if (fileMode) {
        browser.setFileSelectionMode(OmegaTFileChooser.FILES_ONLY);
    } else {
        browser.setFileSelectionMode(OmegaTFileChooser.DIRECTORIES_ONLY);
    }
    // check if the current directory as specified by the field exists
    String curDir = field.getText();
    File curDirCheck = new File(curDir);
    if (fileMode && !StringUtil.isEmpty(curDirCheck.getName())) {
        String dirOnly = curDirCheck.getParent();
        dirOnly = (dirOnly != null) ? dirOnly : "";
        curDirCheck = new File(dirOnly);
    }
    // if the dir doesn't exist, use project dir and check if that exists
    if (!curDirCheck.exists() || !curDirCheck.isDirectory()) {
        curDir = projectProperties.getProjectRoot();
        curDirCheck = new File(curDir);
    }
    // if all fails, get last used dir from preferences
    if (!curDirCheck.exists() || !curDirCheck.isDirectory()) {
        switch(browseTarget) {
            case 1:
                curDir = Preferences.getPreference(Preferences.SOURCE_FOLDER);
                break;
            case 2:
                curDir = Preferences.getPreference(Preferences.TARGET_FOLDER);
                break;
            case 3:
                curDir = Preferences.getPreference(Preferences.GLOSSARY_FOLDER);
                break;
            case 4:
                curDir = Preferences.getPreference(Preferences.TM_FOLDER);
                break;
            case 5:
                curDir = Preferences.getPreference(Preferences.DICT_FOLDER);
                break;
            case 6:
                curDir = Preferences.getPreference(Preferences.GLOSSARY_FILE);
                break;
        }
    }
    if (fileMode) {
        File dirFile = new File(curDir);
        curDir = dirFile.getParent();
    }
    if (curDir.equals("")) {
        curDir = Preferences.getPreference(Preferences.CURRENT_FOLDER);
    }
    if (!curDir.equals("")) {
        File dir = new File(curDir);
        if (dir.exists() && dir.isDirectory()) {
            browser.setCurrentDirectory(dir);
        }
    }
    // show the browser
    int action = browser.showOpenDialog(this);
    // check if the selection has been approved
    if (action != javax.swing.JFileChooser.APPROVE_OPTION) {
        return;
    }
    // get the selected folder
    File dir = browser.getSelectedFile();
    if (dir == null) {
        return;
    }
    String str = dir.getAbsolutePath();
    if (!fileMode) {
        // Add file separator for directories
        str += File.separator;
    }
    // The writeable glossary file must end with .txt or utf8. Not .tab, because it not necessarily is .utf8
    if (glossaryFile && !str.endsWith(OConsts.EXT_TSV_TXT) && !str.endsWith(OConsts.EXT_TSV_UTF8)) {
        // Defaults to .txt
        str += OConsts.EXT_TSV_TXT;
    }
    // reset appropriate path - store preferred directory
    switch(browseTarget) {
        case 1:
            Preferences.setPreference(Preferences.SOURCE_FOLDER, browser.getSelectedFile().getParent());
            projectProperties.setSourceRoot(str);
            field.setText(projectProperties.getSourceRoot());
            if (new File(projectProperties.getSourceRoot()).exists() && new File(projectProperties.getSourceRoot()).isDirectory()) {
                field.setForeground(java.awt.SystemColor.textText);
            }
            break;
        case 2:
            Preferences.setPreference(Preferences.TARGET_FOLDER, browser.getSelectedFile().getParent());
            projectProperties.setTargetRoot(str);
            field.setText(projectProperties.getTargetRoot());
            if (new File(projectProperties.getTargetRoot()).exists() && new File(projectProperties.getTargetRoot()).isDirectory()) {
                field.setForeground(java.awt.SystemColor.textText);
            }
            break;
        case 3:
            Preferences.setPreference(Preferences.GLOSSARY_FOLDER, browser.getSelectedFile().getParent());
            projectProperties.setGlossaryRoot(str);
            field.setText(projectProperties.getGlossaryRoot());
            if (new File(projectProperties.getGlossaryRoot()).exists() && new File(projectProperties.getGlossaryRoot()).isDirectory()) {
                field.setForeground(java.awt.SystemColor.textText);
            }
            break;
        case 4:
            Preferences.setPreference(Preferences.TM_FOLDER, browser.getSelectedFile().getParent());
            projectProperties.setTMRoot(str);
            field.setText(projectProperties.getTMRoot());
            if (new File(projectProperties.getTMRoot()).exists() && new File(projectProperties.getTMRoot()).isDirectory()) {
                field.setForeground(java.awt.SystemColor.textText);
            }
            break;
        case 5:
            Preferences.setPreference(Preferences.DICT_FOLDER, browser.getSelectedFile().getParent());
            projectProperties.setDictRoot(str);
            field.setText(projectProperties.getDictRoot());
            if (new File(projectProperties.getDictRoot()).exists() && new File(projectProperties.getDictRoot()).isDirectory()) {
                field.setForeground(java.awt.SystemColor.textText);
            }
            break;
        case 6:
            Preferences.setPreference(Preferences.GLOSSARY_FILE, browser.getSelectedFile().getPath());
            projectProperties.setWriteableGlossary(str);
            field.setText(projectProperties.getWriteableGlossary());
            // The writable glosssary file must be inside the glossary dir
            if (new File(projectProperties.getWriteableGlossaryDir()).exists() && new File(projectProperties.getWriteableGlossaryDir()).isDirectory() && projectProperties.getWriteableGlossaryDir().contains(projectProperties.getGlossaryRoot())) {
                field.setForeground(java.awt.SystemColor.textText);
            }
            break;
    }
}
Also used : OmegaTFileChooser(org.omegat.util.gui.OmegaTFileChooser) File(java.io.File)

Example 2 with OmegaTFileChooser

use of org.omegat.util.gui.OmegaTFileChooser 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 3 with OmegaTFileChooser

use of org.omegat.util.gui.OmegaTFileChooser in project omegat by omegat-org.

the class SearchWindowController method doBrowseDirectory.

// /////////////////////////////////////////////////////////////
// internal functions
private void doBrowseDirectory() {
    OmegaTFileChooser browser = new OmegaTFileChooser();
    // String str = OStrings.getString("BUTTON_SELECT");
    // browser.setApproveButtonText(str);
    browser.setDialogTitle(OStrings.getString("SW_TITLE"));
    browser.setFileSelectionMode(OmegaTFileChooser.DIRECTORIES_ONLY);
    String curDir = form.m_dirField.getText();
    if (!curDir.equals("")) {
        File dir = new File(curDir);
        if (dir.exists() && dir.isDirectory()) {
            browser.setCurrentDirectory(dir);
        }
    }
    browser.showOpenDialog(form);
    File dir = browser.getSelectedFile();
    if (dir == null) {
        return;
    }
    String str = dir.getAbsolutePath() + File.separator;
    form.m_dirField.setText(str);
}
Also used : OmegaTFileChooser(org.omegat.util.gui.OmegaTFileChooser) File(java.io.File)

Example 4 with OmegaTFileChooser

use of org.omegat.util.gui.OmegaTFileChooser in project omegat by omegat-org.

the class ProjectUICommands method doPromptImportSourceFiles.

/**
 * Imports the file/files/folder into project's source files.
 */
public static void doPromptImportSourceFiles() {
    OmegaTFileChooser chooser = new OmegaTFileChooser();
    chooser.setMultiSelectionEnabled(true);
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.setDialogTitle(OStrings.getString("TF_FILE_IMPORT_TITLE"));
    int result = chooser.showOpenDialog(Core.getMainWindow().getApplicationFrame());
    if (result == OmegaTFileChooser.APPROVE_OPTION) {
        File[] selFiles = chooser.getSelectedFiles();
        projectImportFiles(Core.getProject().getProjectProperties().getSourceRoot(), selFiles);
    }
}
Also used : OmegaTFileChooser(org.omegat.util.gui.OmegaTFileChooser) File(java.io.File)

Aggregations

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