use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class ConverterViewModel method reverseConvert.
/**
* 逆向转换
* @param sourceItem
* @param monitor
* @return ;
*/
private Map<String, String> reverseConvert(IConversionItem sourceItem, IProgressMonitor monitor) throws ConverterException {
Map<String, String> result = null;
String sourcePathStr = configBean.getSource();
String targetFile = ConverterUtil.toLocalPath(configBean.getTarget());
boolean convertSuccessful = false;
File target = null;
try {
target = new File(targetFile);
if (!target.exists()) {
try {
File parent = target.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
target.createNewFile();
} catch (IOException e) {
if (LOGGER.isErrorEnabled()) {
String msg = Messages.getString("model.ConverterViewModel.logger4");
Object[] args = { targetFile };
LOGGER.error(new MessageFormat(msg).format(args), e);
}
}
}
Converter converter = getConverter();
if (converter == null) {
// Build a message
String message = Messages.getString("model.ConverterViewModel.msg3") + configBean.getFileType();
// Build a new IStatus
IStatus status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, message);
throw new ConverterNotFoundException(status);
}
ConverterContext converterContext = new ConverterContext(configBean);
final Map<String, String> configuration = converterContext.getReverseConvertConfiguraion();
if (configBean.getFileType().equals("x-msoffice2003")) {
IPreferenceStore ps = net.heartsome.cat.ts.ui.Activator.getDefault().getPreferenceStore();
String path = ps.getString(ITranslationPreferenceConstants.PATH_OF_OPENOFFICE);
String port = ps.getString(ITranslationPreferenceConstants.PORT_OF_OPENOFFICE);
configuration.put("ooPath", path);
configuration.put("ooPort", port);
}
result = converter.convert(configuration, monitor);
convertSuccessful = true;
} catch (OperationCanceledException e) {
// 捕获用户取消操作的异常
LOGGER.info(Messages.getString("model.ConverterViewModel.logger2"), e);
throw e;
} catch (ConverterNotFoundException e) {
// Let the StatusManager handle the Status and provide a hint
StatusManager.getManager().handle(e.getStatus(), StatusManager.LOG | StatusManager.SHOW);
if (LOGGER.isErrorEnabled()) {
LOGGER.error(e.getMessage(), e);
}
throw e;
} catch (ConverterException e) {
if (LOGGER.isErrorEnabled()) {
LOGGER.error(MessageFormat.format(Messages.getString("model.ConverterViewModel.logger3"), sourcePathStr), e);
}
throw e;
} finally {
if (!convertSuccessful) {
// 在转换失败或用户取消转换时,清除目标文件和骨架文件
if (target != null && target.exists()) {
target.delete();
}
}
sourceItem.refresh();
}
return result;
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class IConverterCallerImpl method openConverter.
/**
* (non-Javadoc)
* @see net.heartsome.cat.ts.ui.extensionpoint.IConverterCaller#openConverter(java.util.List)
*/
public void openConverter(List<IFile> files) {
if (files == null || files.size() == 0) {
return;
}
final Shell shell = Display.getCurrent().getActiveShell();
ArrayList<IFile> list = new ArrayList<IFile>();
ArrayList<IFile> wrongFiles = new ArrayList<IFile>();
for (IFile file : files) {
String fileExtension = file.getFileExtension();
if (getLegalFileExtensions() == null || getLegalFileExtensions().length == 0) {
// 未限制后缀名的情况
list.add(file);
} else {
// 限制了后缀名的情况
if (fileExtension == null) {
// 无后缀名的文件
fileExtension = "";
}
if (CommonFunction.containsIgnoreCase(getLegalFileExtensions(), fileExtension)) {
list.add(file);
} else {
wrongFiles.add(file);
}
}
}
if (!wrongFiles.isEmpty()) {
StringBuffer msg = new StringBuffer(Messages.getString("extension.IConverterCallerImpl.msg1"));
for (IFile iFile : wrongFiles) {
msg.append("\n").append(iFile.getFullPath().toOSString());
}
if (!MessageDialog.openConfirm(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle1"), msg.toString())) {
return;
}
}
ArrayList<ConverterViewModel> converterViewModels = new ArrayList<ConverterViewModel>();
for (int i = 0; i < list.size(); i++) {
Object adapter = Platform.getAdapterManager().getAdapter(list.get(i), IConversionItem.class);
IConversionItem sourceItem = null;
if (adapter instanceof IConversionItem) {
sourceItem = (IConversionItem) adapter;
}
ConverterViewModel converterViewModel = new ConverterViewModel(Activator.getContext(), Converter.DIRECTION_POSITIVE);
// 记住所选择的文件
converterViewModel.setConversionItem(sourceItem);
converterViewModels.add(converterViewModel);
}
IProject project = list.get(0).getProject();
ConversionWizard wizard = new ConversionWizard(converterViewModels, project);
TSWizardDialog dialog = new TSWizardDialog(shell, wizard);
int result = dialog.open();
if (result == IDialogConstants.OK_ID) {
final List<ConverterViewModel> models = wizard.getConverterViewModels();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
monitor.setTaskName(Messages.getString("extension.IConverterCallerImpl.task1"));
monitor.beginTask(Messages.getString("extension.IConverterCallerImpl.task2"), models.size());
for (ConverterViewModel converterViewModel : models) {
try {
IProgressMonitor subMonitor = Progress.getSubMonitor(monitor, 1);
subMonitor.setTaskName(Messages.getString("extension.IConverterCallerImpl.task3") + converterViewModel.getConversionItem().getLocation().toOSString());
converterViewModel.convertWithoutJob(subMonitor);
} catch (ConverterException e) {
final String message = e.getMessage();
LOGGER.error("", e);
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle2"), message);
}
});
} catch (Exception e) {
final String message = e.getMessage();
LOGGER.error("", e);
Display.getDefault().syncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle2"), message);
}
});
}
}
monitor.done();
}
};
try {
new ProgressMonitorDialog(shell).run(true, true, runnable);
} catch (InvocationTargetException e) {
MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle2"), e.getMessage());
LOGGER.error("", e);
} catch (InterruptedException e) {
MessageDialog.openInformation(shell, Messages.getString("extension.IConverterCallerImpl.msgTitle2"), e.getMessage());
LOGGER.error(e.getMessage());
}
}
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class PreviewTranslationHandler method preview.
/**
* 预览单个文件
* @param file
* IFile 对象
* @param subMonitor
* 进度监视器
* @throws ExecutionException
* ;
*/
private void preview(IFile file, IProgressMonitor subMonitor) throws ExecutionException {
ConverterViewModel model = getConverterViewModel(file);
if (model != null) {
try {
Map<String, String> result = model.convertWithoutJob(subMonitor);
String targetFile = result.get(Converter.ATTR_TARGET_FILE);
if (targetFile == null) {
MessageDialog.openError(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), Messages.getString("handler.PreviewTranslationHandler.msg4"));
} else {
final IFile input = root.getFileForLocation(new Path(targetFile));
Display.getDefault().syncExec(new Runnable() {
public void run() {
try {
// 使用外部编辑器(系统默认编辑器)打开文件。
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
Assert.isNotNull(page, Messages.getString("handler.PreviewTranslationHandler.msg5"));
IDE.openEditor(page, input, IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID);
} catch (PartInitException e) {
MessageDialog.openInformation(shell, Messages.getString("handler.PreviewTranslationHandler.msgTitle"), Messages.getString("handler.PreviewTranslationHandler.msg6") + e.getMessage());
LOGGER.error("", e);
}
}
});
}
} catch (ConverterException e) {
throw new ExecutionException(e.getMessage(), e);
}
}
subMonitor.done();
}
use of net.heartsome.cat.converter.ConverterException in project translationstudio8 by heartsome.
the class ConverterViewModel method convert.
/**
* 根据用户的选择和配置信息,执行文件的转换功能
* @return ;
*/
public Map<String, String> convert() {
// System.out.print(getConfigBean().toString());
// 以用户最后在配置对话框所选择的源文件为准
JobRunnable runnalbe = new JobRunnable() {
private Map<String, String> conversionResult;
public IStatus run(IProgressMonitor monitor) {
IStatus result = Status.OK_STATUS;
try {
conversionResult = convertWithoutJob(monitor);
} catch (OperationCanceledException e) {
LOGGER.info(Messages.getString("model.ConverterViewModel.logger2"), e);
result = Status.CANCEL_STATUS;
} catch (ConverterException e) {
String msg = Messages.getString("model.ConverterViewModel.logger3");
Object[] args = { getConfigBean().getSource() };
LOGGER.error(new MessageFormat(msg).format(args), e);
result = e.getStatus();
} finally {
ConverterViewModel.this.close();
}
return result;
}
public void showResults(IStatus status) {
IAction action = getRunnableCompletedAction(status);
if (action != null) {
action.run();
}
}
public IAction getRunnableCompletedAction(IStatus status) {
return new ConversionCompleteAction(Messages.getString("model.ConverterViewModel.msg1"), status, conversionResult);
}
};
Job conversionJob = JobFactoryFacade.createJob(Display.getDefault(), "conversion job", runnalbe);
conversionJob.setUser(true);
conversionJob.setRule(conversionItem.getProject());
conversionJob.schedule();
return null;
}
Aggregations