use of org.eclipse.core.runtime.OperationCanceledException in project che by eclipse.
the class AddImportsOperation method run.
/**
* Runs the operation.
*
* @param monitor The progress monitor
* @throws CoreException if accessing the CU or rewritting the imports fails
* @throws OperationCanceledException Runtime error thrown when operation is canceled.
*/
public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
try {
monitor.beginTask(CodeGenerationMessages.AddImportsOperation_description, 4);
CompilationUnit astRoot = SharedASTProvider.getAST(fCompilationUnit, SharedASTProvider.WAIT_YES, new SubProgressMonitor(monitor, 1));
if (astRoot == null)
throw new OperationCanceledException();
ImportRewrite importRewrite = StubUtility.createImportRewrite(astRoot, true);
MultiTextEdit res = new MultiTextEdit();
TextEdit edit = evaluateEdits(astRoot, importRewrite, fSelectionOffset, fSelectionLength, new SubProgressMonitor(monitor, 1));
if (edit == null) {
return;
}
res.addChild(edit);
TextEdit importsEdit = importRewrite.rewriteImports(new SubProgressMonitor(monitor, 1));
res.addChild(importsEdit);
fResultingEdit = res;
if (fApply) {
JavaModelUtil.applyEdit(fCompilationUnit, res, fDoSave, new SubProgressMonitor(monitor, 1));
}
} finally {
monitor.done();
}
}
use of org.eclipse.core.runtime.OperationCanceledException in project translationstudio8 by heartsome.
the class NewProjectWizard method copySourceFile.
/**
* 将源文件复制到项目中
* @param srcFiles
* 源文件路径列表 ;
* @param project
* 项目
* @param folderName
* 目录名称
* @throws CoreException
*/
private void copySourceFile(final IProject project, String folderName, final List<String> srcFiles, IProgressMonitor monitor) throws CoreException {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(Messages.getString("wizard.NewProjectWizard.task3"), srcFiles.size());
monitor.setTaskName(Messages.getString("wizard.NewProjectWizard.task3"));
final IProgressMonitor sMonitor = monitor;
final IFolder folder = project.getFolder(folderName);
Display.getDefault().asyncExec(new Runnable() {
public void run() {
for (int i = 0; i < srcFiles.size(); i++) {
if (sMonitor.isCanceled()) {
try {
project.delete(true, sMonitor);
} catch (CoreException e) {
e.printStackTrace();
}
throw new OperationCanceledException();
}
File file = new File(srcFiles.get(i));
String fileName = file.getName();
InputStream inputStream;
try {
inputStream = new FileInputStream(file);
IFile iFile = folder.getFile(fileName);
if (iFile.exists()) {
if (!MessageDialog.openConfirm(getShell(), Messages.getString("wizard.NewProjectWizard.msgTitle"), Messages.getString("wizard.NewProjectWizard.msg1"))) {
continue;
}
}
iFile.create(inputStream, false, null);
sourcefiles.add(iFile);
sMonitor.worked(1);
} catch (Exception e) {
logger.error("", e);
MessageDialog.openInformation(getShell(), Messages.getString("wizard.NewProjectWizard.msgTitle"), MessageFormat.format(Messages.getString("wizard.NewProjectWizard.msg2"), fileName));
continue;
} finally {
sMonitor.done();
}
}
}
});
}
use of org.eclipse.core.runtime.OperationCanceledException in project translationstudio8 by heartsome.
the class NewProjectWizard method initProjectConfig.
/**
* 构建项目配置信息
* @param project
* ;
* @throws CoreException
*/
private void initProjectConfig(final IProject project, IProgressMonitor monitor) {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
final IProgressMonitor sMonitor = monitor;
sMonitor.beginTask(Messages.getString("wizard.NewProjectWizard.task2"), 3);
sMonitor.worked(1);
Display.getDefault().asyncExec(new Runnable() {
public void run() {
ProjectInfoBean bean = new ProjectInfoBean();
bean.setProjectName(project.getName());
bean.setMapField(firstPage.getFieldMap());
bean.setMapAttr(firstPage.getAttributeMap());
bean.setSourceLang(secondPage.getSrcLanguage());
bean.setTargetLang(secondPage.getTargetlanguage());
for (AbstractNewProjectWizardPage extensionPage : extensionPages) {
if (extensionPage.getPageType().equals("TM")) {
bean.setTmDb(extensionPage.getSelectedDatabase());
} else if (extensionPage.getPageType().equals("TB")) {
bean.setTbDb(extensionPage.getSelectedDatabase());
}
}
sMonitor.worked(1);
ProjectConfiger projCfg = ProjectConfigerFactory.getProjectConfiger(project);
sMonitor.worked(1);
projCfg.updateProjectConfig(bean);
// 记住当前语言信息
IPreferenceStore ps = Activator.getDefault().getPreferenceStore();
ps.setValue(IPreferenceConstants.NEW_PROJECT_SRC_LANG, bean.getSourceLang().getCode());
List<Language> targetLangList = bean.getTargetLang();
StringBuffer bf = new StringBuffer();
for (Language lang : targetLangList) {
bf.append(lang.getCode());
bf.append(",");
}
ps.setValue(IPreferenceConstants.NEW_PROJECT_TGT_LANG, bf.substring(0, bf.lastIndexOf(",")));
if (sMonitor.isCanceled()) {
try {
project.delete(true, sMonitor);
} catch (CoreException e) {
logger.error("", e);
}
throw new OperationCanceledException();
}
}
});
sMonitor.done();
}
use of org.eclipse.core.runtime.OperationCanceledException in project translationstudio8 by heartsome.
the class ConverterUtil method convert2Tbx.
public static File convert2Tbx(String fileName, IProgressMonitor monitor) throws ImportException {
File file = new File(fileName);
if (!file.exists()) {
throw new ImportException(fileName + Messages.getString("converter.ConverterUtil.msg"));
}
if (fileName.toLowerCase().endsWith(".tbx")) {
return null;
}
AbstractConverter conveter = ConverterFactory.getFile2TbxConverter(fileName);
if (null == conveter) {
return null;
}
File createTempFile = null;
boolean hasError = true;
monitor.beginTask("", 1);
try {
createTempFile = File.createTempFile("Tbx_", "" + System.currentTimeMillis() + ".tbx");
conveter.doConvert(createTempFile.getAbsolutePath(), new SubProgressMonitor(monitor, 1));
hasError = false;
} catch (OperationCanceledException e) {
throw e;
} catch (IOException e) {
LOGGER.error("", e);
throw new ImportException(e.getMessage().replace("\n", " "));
} catch (Exception e) {
LOGGER.error("", e);
throw new ImportException(e.getMessage().replace("\n", " "));
} finally {
if (hasError && null != createTempFile) {
createTempFile.delete();
}
monitor.done();
}
return createTempFile;
}
use of org.eclipse.core.runtime.OperationCanceledException in project translationstudio8 by heartsome.
the class ImportTbx method saveTermEntry.
/**
* 覆盖或者忽略
* @param headerId
* ;
* @throws SQLException
* @throws NavException
* @throws XPathEvalException
* @throws XPathParseException
* @throws ParseException
* @throws EntityException
* @throws EOFException
* @throws EncodingException
* @throws IOException
* @throws ModifyException
* @throws TranscodeException
*/
private void saveTermEntry(String srcLang, int headerId) throws XPathParseException, XPathEvalException, NavException, SQLException, EncodingException, EOFException, EntityException, ParseException, TranscodeException, ModifyException, IOException {
AutoPilot ap = new AutoPilot(vu.getVTDNav());
// term entry
ap.selectXPath("/martif/text/body/termEntry");
AutoPilot _ap = new AutoPilot(vu.getVTDNav());
_ap.declareXPathNameSpace("xml", VTDUtils.XML_NAMESPACE_URL);
// langSet == sourceLang
_ap.selectXPath("./langSet[@xml:lang='" + srcLang + "']/tig/term|./ntig/termGrp/term");
AutoPilot langSetAp = new AutoPilot(vu.getVTDNav());
langSetAp.selectXPath("./langSet");
AutoPilot termAp = new AutoPilot(vu.getVTDNav());
// Term
termAp.selectXPath("./tig/term|./ntig/termGrp/term");
List<String> textDataIds = new ArrayList<String>();
while (ap.evalXPath() != -1) {
// 循环termEntry节点
if (monitor != null && monitor.isCanceled()) {
throw new OperationCanceledException();
}
if (importStrategy == Constants.IMPORT_MODEL_ALWAYSADD) {
// 始终增加模式
saveTermEntryWithAdd(headerId, langSetAp, termAp);
if (monitor != null) {
monitor.worked(1);
}
continue;
}
// 判断该TermEntry节点是否重复
vu.getVTDNav().push();
while (_ap.evalXPath() != -1) {
String pureText = DocUtils.getTmxTbxPureText(vu);
pureText = pureText.trim();
textDataIds = dbOperator.getTextDataId(pureText.hashCode(), Utils.convertLangCode(srcLang), "B");
if (textDataIds.size() > 0) {
// 当前TermEntry重复
break;
}
}
_ap.resetXPath();
vu.getVTDNav().pop();
// System.out.println(textDataIds);
if (textDataIds.size() == 0) {
// 在库中没有重复的TermEntry
saveTermEntryWithAdd(headerId, langSetAp, termAp);
} else {
if (importStrategy == Constants.IMPORT_MODEL_IGNORE) {
// 忽略当前TermEntry
if (monitor != null) {
monitor.worked(1);
}
continue;
} else {
// 合并
List<String> termEntryPks = new ArrayList<String>();
// 获取源文相同的TermEntry Pk
termEntryPks = dbOperator.getTextDataGroupIdByTextId(textDataIds);
// System.out.println(termEntryPks);
// 获取TermEntry的内容
List<Map<String, String>> dbTermEntrys = dbOperator.getBTermEntryByPk(termEntryPks);
if (dbTermEntrys.size() == 1) {
// 在库中只有一个TermEntry和当前TermEntry重复
if (importStrategy == Constants.IMPORT_MODEL_MERGE) {
saveTermEntryWithMerge(dbTermEntrys.get(0), langSetAp, termAp);
} else if (importStrategy == Constants.IMPORT_MODEL_OVERWRITE) {
saveTermEntryWithOverwrite(dbTermEntrys.get(0), langSetAp, termAp);
}
} else {
// 标记是否已经执行过更新
boolean updateflag = false;
for (int i = 0; i < dbTermEntrys.size(); i++) {
Map<String, String> dbTermEntry = dbTermEntrys.get(i);
if (dbTermEntry.get("CID").equals(getElementAttribute("id"))) {
updateflag = true;
if (importStrategy == Constants.IMPORT_MODEL_MERGE) {
saveTermEntryWithMerge(dbTermEntry, langSetAp, termAp);
} else if (importStrategy == Constants.IMPORT_MODEL_OVERWRITE) {
saveTermEntryWithOverwrite(dbTermEntry, langSetAp, termAp);
}
}
}
if (!updateflag) {
// 没有执行更新,即没有在库中找到和当前TermEntry ID一样的
saveTermEntryWithAdd(headerId, langSetAp, termAp);
}
}
}
}
if (monitor != null) {
monitor.worked(1);
}
}
}
Aggregations