Search in sources :

Example 86 with OperationCanceledException

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();
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ImportRewrite(org.eclipse.jdt.core.dom.rewrite.ImportRewrite) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 87 with OperationCanceledException

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();
                }
            }
        }
    });
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IFile(org.eclipse.core.resources.IFile) CoreException(org.eclipse.core.runtime.CoreException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) FileInputStream(java.io.FileInputStream) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IFolder(org.eclipse.core.resources.IFolder)

Example 88 with OperationCanceledException

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();
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) AbstractNewProjectWizardPage(net.heartsome.cat.ts.ui.extensionpoint.AbstractNewProjectWizardPage) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ProjectInfoBean(net.heartsome.cat.common.bean.ProjectInfoBean) Language(net.heartsome.cat.common.locale.Language) CoreException(org.eclipse.core.runtime.CoreException) ProjectConfiger(net.heartsome.cat.ts.core.file.ProjectConfiger) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) List(java.util.List) IPreferenceStore(org.eclipse.jface.preference.IPreferenceStore)

Example 89 with OperationCanceledException

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;
}
Also used : ImportException(net.heartsome.cat.common.core.exception.ImportException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IOException(java.io.IOException) AbstractConverter(net.heartsome.cat.document.converter.AbstractConverter) File(java.io.File) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ImportException(net.heartsome.cat.common.core.exception.ImportException) IOException(java.io.IOException)

Example 90 with OperationCanceledException

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);
        }
    }
}
Also used : AutoPilot(com.ximpleware.AutoPilot) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)134 CoreException (org.eclipse.core.runtime.CoreException)38 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)37 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)34 ArrayList (java.util.ArrayList)25 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)24 IOException (java.io.IOException)21 IFile (org.eclipse.core.resources.IFile)21 IStatus (org.eclipse.core.runtime.IStatus)21 InvocationTargetException (java.lang.reflect.InvocationTargetException)20 Status (org.eclipse.core.runtime.Status)16 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)16 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)16 File (java.io.File)15 SubMonitor (org.eclipse.core.runtime.SubMonitor)10 Job (org.eclipse.core.runtime.jobs.Job)9 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)9 IProject (org.eclipse.core.resources.IProject)8 HashSet (java.util.HashSet)7 IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)7