use of org.omegat.core.events.IProjectEventListener 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();
}
use of org.omegat.core.events.IProjectEventListener in project omegat by omegat-org.
the class ExternalFinder method generateIProjectEventListener.
private static IProjectEventListener generateIProjectEventListener() {
return new IProjectEventListener() {
private final List<Component> menuItems = new ArrayList<Component>();
@Override
public void onProjectChanged(final IProjectEventListener.PROJECT_CHANGE_TYPE eventType) {
switch(eventType) {
case LOAD:
onLoad();
break;
case CLOSE:
onClose();
break;
default:
}
}
private void onLoad() {
// clear old items
menuItems.clear();
// add finder items to menuItems
IExternalFinderItemMenuGenerator generator = new ExternalFinderItemMenuGenerator(ExternalFinderItem.TARGET.BOTH, false);
List<JMenuItem> newMenuItems = generator.generate();
JMenu toolsMenu = Core.getMainWindow().getMainMenu().getToolsMenu();
// Separator
Component separator = new JPopupMenu.Separator();
toolsMenu.add(separator);
menuItems.add(separator);
// add menuItems to menu
MenuItemPager pager = new MenuItemPager(toolsMenu);
for (JMenuItem component : newMenuItems) {
pager.add(component);
}
menuItems.addAll(pager.getFirstPage());
}
private void onClose() {
// remove menu items
final JMenu menu = Core.getMainWindow().getMainMenu().getToolsMenu();
menuItems.forEach(menu::remove);
menuItems.clear();
projectConfig = null;
}
};
}
use of org.omegat.core.events.IProjectEventListener in project omegat by omegat-org.
the class ScriptsMonitor method hookProjectEvent.
private void hookProjectEvent() {
if (m_projectEventListener != null) {
CoreEvents.unregisterProjectChangeListener(m_projectEventListener);
}
addEventScripts(EventType.PROJECT_CHANGED);
m_projectEventListener = new IProjectEventListener() {
@Override
public void onProjectChanged(PROJECT_CHANGE_TYPE eventType) {
HashMap<String, Object> binding = new HashMap<String, Object>();
binding.put("eventType", eventType);
for (ScriptItem si : m_eventsScript.get(EventType.PROJECT_CHANGED)) {
m_scriptingWindow.executeScriptFile(si, binding);
}
}
};
CoreEvents.registerProjectChangeListener(m_projectEventListener);
}
Aggregations