use of org.omegat.core.team2.IRemoteRepository2 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 {
File projectFile = new File(projectRootFolder, OConsts.FILE_PROJECT);
boolean needToSaveProperties = false;
File restoreOnFail = null;
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(), props);
remoteRepositoryProvider.switchToVersion(OConsts.FILE_PROJECT, null);
restoreOnFail = FileUtil.backupFile(projectFile);
// Overwrite omegat.project
remoteRepositoryProvider.copyFilesFromReposToProject(OConsts.FILE_PROJECT);
// Reload project properties
props = ProjectFileStorage.loadProjectProperties(projectRootFolder.getAbsoluteFile());
/*
* Repositories with a project by default have no mapping. When they are downloaded,
* the mapping is added locally.
* Every time we reopen the project, and we copy omegat.project from the remote project,
* we lose the locally defined mapping(s). We need to restore them and save the updated
* omegat.properties to disk.
* If the repository already contains the omegat.project file with mappings, then we
* use those. Local changes are overwritten like any other setting.
*/
if (props.getRepositories() == null) {
// We have a project without mappings
// so we restore the mapping we just lost
props.setRepositories(repos);
needToSaveProperties = true;
}
} catch (IRemoteRepository2.NetworkException e) {
// Do nothing. Network errors are handled in RealProject.
} catch (Exception e) {
Log.logErrorRB(e, "TF_PROJECT_PROPERTIES_ERROR");
throw 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, projectFile.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;
final File restoreOnFailFinal = restoreOnFail;
Core.executeExclusively(true, () -> {
boolean succeeded = ProjectFactory.loadProject(propsP, true);
if (restoreOnFailFinal != null) {
if (succeeded) {
Files.deleteIfExists(restoreOnFailFinal.toPath());
} else {
Files.move(restoreOnFailFinal.toPath(), projectFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
});
if (needToSaveProperties) {
Core.getProject().saveProjectProperties();
}
RecentProjects.add(projectRootFolder.getAbsolutePath());
} 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;
}
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();
}
use of org.omegat.core.team2.IRemoteRepository2 in project omegat by omegat-org.
the class RealProject method saveProject.
/**
* Saves the translation memory and preferences.
*
* This method must be executed in the Core.executeExclusively.
*/
public synchronized void saveProject(boolean doTeamSync) {
if (isSaving) {
return;
}
isSaving = true;
Log.logInfoRB("LOG_DATAENGINE_SAVE_START");
UIThreadsUtil.mustNotBeSwingThread();
Core.getAutoSave().disable();
try {
Core.getMainWindow().getMainMenu().getProjectMenu().setEnabled(false);
try {
Preferences.save();
try {
saveProjectProperties();
projectTMX.save(config, config.getProjectInternal() + OConsts.STATUS_EXTENSION, isProjectModified());
if (remoteRepositoryProvider != null && doTeamSync) {
tmxPrepared = null;
glossaryPrepared = null;
remoteRepositoryProvider.cleanPrepared();
Core.getMainWindow().showStatusMessageRB("TEAM_SYNCHRONIZE");
rebaseAndCommitProject(true);
setOnlineMode();
}
setProjectModified(false);
} catch (KnownException ex) {
throw ex;
} catch (IRemoteRepository2.NetworkException e) {
if (isOnlineMode) {
Log.logErrorRB("TEAM_NETWORK_ERROR", e.getCause());
setOfflineMode();
}
} catch (Exception e) {
Log.logErrorRB(e, "CT_ERROR_SAVING_PROJ");
Core.getMainWindow().displayErrorRB(e, "CT_ERROR_SAVING_PROJ");
}
LastSegmentManager.saveLastSegment();
// update statistics
StatsResult stat = CalcStandardStatistics.buildProjectStats(this);
stat.updateStatisticsInfo(hotStat);
String fn = config.getProjectInternal() + OConsts.STATS_FILENAME;
Statistics.writeStat(fn, stat.getTextData(config));
} finally {
Core.getMainWindow().getMainMenu().getProjectMenu().setEnabled(true);
}
CoreEvents.fireProjectChange(IProjectEventListener.PROJECT_CHANGE_TYPE.SAVE);
} finally {
Core.getAutoSave().enable();
}
Log.logInfoRB("LOG_DATAENGINE_SAVE_END");
isSaving = false;
}
use of org.omegat.core.team2.IRemoteRepository2 in project omegat by omegat-org.
the class RealProject method loadProject.
/**
* Load exist project in a "big" sense -- loads project's properties, glossaries, tms, source files etc.
*/
public synchronized void loadProject(boolean onlineMode) {
Log.logInfoRB("LOG_DATAENGINE_LOAD_START");
UIThreadsUtil.mustNotBeSwingThread();
// load new project
try {
if (!lockProject()) {
throw new KnownException("PROJECT_LOCKED");
}
isOnlineMode = onlineMode;
if (RuntimePreferences.isLocationSaveEnabled()) {
Preferences.setPreference(Preferences.CURRENT_FOLDER, new File(config.getProjectRoot()).getAbsoluteFile().getParent());
Preferences.save();
}
Core.getMainWindow().showStatusMessageRB("CT_LOADING_PROJECT");
if (remoteRepositoryProvider != null) {
try {
tmxPrepared = null;
glossaryPrepared = null;
remoteRepositoryProvider.switchAllToLatest();
} catch (IRemoteRepository2.NetworkException e) {
Log.logErrorRB("TEAM_NETWORK_ERROR", e.getCause());
setOfflineMode();
}
remoteRepositoryProvider.copyFilesFromReposToProject("");
// After adding filters.xml and segmentation.conf, we must reload them again
config.loadProjectFilters();
config.loadProjectSRX();
}
loadFilterSettings();
loadSegmentationSettings();
// load projectsave.tmx
loadTranslations();
loadSourceFiles();
// This MUST happen after calling loadTranslations()
if (remoteRepositoryProvider != null && isOnlineMode) {
Core.getMainWindow().showStatusMessageRB("TEAM_REBASE_AND_COMMIT");
rebaseAndCommitProject(true);
}
// after loadSourcefiles, the entries are filled. The list can now (and only now) be readonly.
allProjectEntries = Collections.unmodifiableList(allProjectEntries);
// and now we can set the importHandler, used by loadTM
importHandler = new ImportFromAutoTMX(this, allProjectEntries);
// imports translation from source files into ProjectTMX
importTranslationsFromSources();
// loads external tmx, and auto/enfoce tmx'es (appending to projectTMX)
loadTM();
loadOtherLanguages();
// build word count
StatsResult stat = CalcStandardStatistics.buildProjectStats(this);
stat.updateStatisticsInfo(hotStat);
String fn = config.getProjectInternal() + OConsts.STATS_FILENAME;
Statistics.writeStat(fn, stat.getTextData(config));
loaded = true;
// Project Loaded...
Core.getMainWindow().showStatusMessageRB(null);
setProjectModified(false);
} catch (OutOfMemoryError oome) {
// Fix for bug 1571944 @author Henry Pijffers
// (henry.pijffers@saxnot.com)
// Oh shit, we're all out of storage space!
// Of course we should've cleaned up after ourselves earlier,
// but since we didn't, do a bit of cleaning up now, otherwise
// we can't even inform the user about our slacking off.
allProjectEntries.clear();
projectFilesList.clear();
transMemories.clear();
projectTMX = null;
// There, that should do it, now inform the user
long memory = Runtime.getRuntime().maxMemory() / 1024 / 1024;
Log.logErrorRB("OUT_OF_MEMORY", memory);
Log.log(oome);
Core.getMainWindow().showErrorDialogRB("TF_ERROR", "OUT_OF_MEMORY", memory);
// Just quit, we can't help it anyway
System.exit(0);
} catch (Throwable e) {
Log.logErrorRB(e, "TF_LOAD_ERROR");
Core.getMainWindow().displayErrorRB(e, "TF_LOAD_ERROR");
if (!loaded) {
unlockProject();
}
}
Log.logInfoRB("LOG_DATAENGINE_LOAD_END");
}
Aggregations