Search in sources :

Example 96 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project translationstudio8 by heartsome.

the class NumberOrTagConsisQAHandler method beginQA.

private void beginQA(final ArrayList<IFile> selectIFiles, final QAModel model, final boolean isNumberQA, final QAResult qaResult) {
    final String titile = isNumberQA ? Messages.getString("qa.NumberOrTagConsisQAHandler.jobTitle1") : Messages.getString("qa.NumberOrTagConsisQAHandler.jobTitle2");
    Job job = new Job(titile) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            // 解析一格,其余九格
            monitor.beginTask(titile, 10 * selectIFiles.size());
            xmlHandler = new QAXmlHandler();
            // 首先解析文件,如果为false,则退出
            if (!openFile(selectIFiles, monitor)) {
                return Status.OK_STATUS;
            }
            int allTUSize = 0;
            for (IFile iFile : selectIFiles) {
                allTUSize += xmlHandler.getTuSizeMap().get(iFile.getLocation().toOSString());
            }
            initWorkInterval(allTUSize);
            // 开始进行数据处理
            IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 9, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
            subMonitor.beginTask(titile + "...", allTUSize % workInterval == 0 ? (allTUSize / workInterval) : (allTUSize / workInterval) + 1);
            Map<String, ArrayList<String>> languageList = xmlHandler.getLanguages();
            int lineNumber = 0;
            int traversalTuIndex = 0;
            IFile iFile;
            for (Entry<String, ArrayList<String>> langEntry : languageList.entrySet()) {
                String srcLang = langEntry.getKey();
                for (String tgtLang : langEntry.getValue()) {
                    List<String> rowIdsList = xmlHandler.getAllRowIdsByLanguages(srcLang.toUpperCase(), tgtLang.toUpperCase());
                    model.setRowIdsList(rowIdsList);
                    // 开始针对每一个文本段进行检查
                    for (String rowId : rowIdsList) {
                        traversalTuIndex++;
                        // 行号
                        lineNumber = rowIdsList.indexOf(rowId) + 1;
                        String filePath = RowIdUtil.getFileNameByRowId(rowId);
                        iFile = ResourceUtils.fileToIFile(filePath);
                        String langPair = srcLang + Hyphen + tgtLang;
                        QATUDataBean tuDataBean = xmlHandler.getFilteredTUText(filePath, RowIdUtil.parseRowIdToXPath(rowId), model.getNotInclude());
                        if (tuDataBean == null) {
                            if (!xmlHandler.monitorWork(subMonitor, traversalTuIndex, workInterval, false)) {
                                return Status.CANCEL_STATUS;
                            }
                            continue;
                        }
                        if (!tuDataBean.isPassFilter()) {
                            if (!xmlHandler.monitorWork(subMonitor, traversalTuIndex, workInterval, false)) {
                                return Status.CANCEL_STATUS;
                            }
                            continue;
                        } else if (tuDataBean.getTgtContent() == null || "".equals(tuDataBean.getTgtContent())) {
                            // 因为文本段完整性检查要判断译文是否为空的情况,所以,如果译文为空,只有文本段完整性要进行检查,其他检查项都跳过。
                            continue;
                        }
                        tuDataBean.setLineNumber(lineNumber + "");
                        tuDataBean.setFileName(iFile.getName());
                        tuDataBean.setSrcLang(srcLang);
                        tuDataBean.setTgtLang(tgtLang);
                        if (isNumberQA) {
                            NumberConsistenceQA numberQA = new NumberConsistenceQA();
                            qaResult.setMultiOper(model.getMultiOper());
                            numberQA.setQaResult(qaResult);
                            numberQA.startQA(model, subMonitor, iFile, xmlHandler, tuDataBean);
                        } else {
                            TagConsistenceQA tagQA = new TagConsistenceQA();
                            qaResult.setMultiOper(model.getMultiOper());
                            tagQA.setQaResult(qaResult);
                            tagQA.startQA(model, subMonitor, iFile, xmlHandler, tuDataBean);
                        }
                        qaResult.sendDataToViewer(null);
                        if (!xmlHandler.monitorWork(subMonitor, traversalTuIndex, workInterval, false)) {
                            return Status.CANCEL_STATUS;
                        }
                    }
                }
                if (!xmlHandler.monitorWork(subMonitor, traversalTuIndex, workInterval, false)) {
                    return Status.CANCEL_STATUS;
                }
            }
            qaResult.informQAEndFlag();
            subMonitor.done();
            monitor.done();
            return Status.OK_STATUS;
        }
    };
    // 当程序退出时,检测当前 job 是否正常关闭
    CommonFunction.jobCantCancelTip(job);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void running(IJobChangeEvent event) {
            ProgressIndicatorManager.displayProgressIndicator();
            super.running(event);
        }

        @Override
        public void done(IJobChangeEvent event) {
            ProgressIndicatorManager.hideProgressIndicator();
            super.done(event);
        }
    });
    job.setUser(true);
    job.schedule();
}
Also used : NumberConsistenceQA(net.heartsome.cat.ts.ui.qa.NumberConsistenceQA) IFile(org.eclipse.core.resources.IFile) QATUDataBean(net.heartsome.cat.ts.core.qa.QATUDataBean) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) QAXmlHandler(net.heartsome.cat.ts.core.qa.QAXmlHandler) ArrayList(java.util.ArrayList) TagConsistenceQA(net.heartsome.cat.ts.ui.qa.TagConsistenceQA) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 97 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project translationstudio8 by heartsome.

the class QualityAssurance method beginQA.

//------------------下面是优化品质检查的试用代码
public void beginQA(final QAResult qaResult) {
    this.qaResult = qaResult;
    handler = new QAXmlHandler();
    Job job = new Job(Messages.getString("qa.all.qa")) {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            long time1 = System.currentTimeMillis();
            // 品质检查项的总数
            int fileNum = model.getQaXlfList().size();
            // 定义的进度条总共五格,其中,解析文件一格,进行检查四格
            monitor.beginTask(Messages.getString("qa.QualityAssurance.tip1"), fileNum * 5);
            //先将所有的文件进行解析
            if (!openFile(monitor)) {
                monitor.done();
                return Status.CANCEL_STATUS;
            }
            if (model.getQaXlfList().size() == 0) {
                MessageDialog.openInformation(shell, _INFO, Messages.getString("qa.QualityAssurance.tip2"));
                return Status.CANCEL_STATUS;
            }
            initWorkInterval();
            QARealization realization = null;
            //先遍历每个文件
            for (int fileIndex = 0; fileIndex < model.getQaXlfList().size(); fileIndex++) {
                final IFile iFile = model.getQaXlfList().get(fileIndex);
                String xlfPath = iFile.getLocation().toOSString();
                String iFileFullPath = iFile.getFullPath().toOSString();
                IProgressMonitor subMonitor = new SubProgressMonitor(monitor, 4, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
                int allTuSum = handler.getNodeCount(xlfPath, "/xliff/file/body//trans-unit");
                subMonitor.beginTask("", allTuSum % workInterval == 0 ? (allTuSum / workInterval) : (allTuSum / workInterval) + 1);
                subMonitor.setTaskName(MessageFormat.format(Messages.getString("qa.QualityAssurance.tip3"), new Object[] { Messages.getString("qa.all.qa"), iFileFullPath }));
                int fileNodeSum = handler.getNodeCount(xlfPath, "/xliff/file");
                //行号
                int lineNumber = 0;
                //遍历tu节点的序列号
                int traversalTuIndex = 0;
                for (int fileNodeIdx = QAConstant.QA_FIRST; fileNodeIdx <= fileNodeSum; fileNodeIdx++) {
                    //获取语言对
                    String srcLang = handler.getNodeAttribute(xlfPath, "/xliff/file[" + fileNodeIdx + "]", "source-language");
                    String tgtLang = handler.getNodeAttribute(xlfPath, "/xliff/file[" + fileNodeIdx + "]", "target-language");
                    String langPair = srcLang + Hyphen + tgtLang;
                    int curFileTUNodeSum = handler.getNodeCount(xlfPath, "/xliff/file[" + fileNodeIdx + "]/body//trans-unit");
                    for (int tuIndex = QAConstant.QA_FIRST; tuIndex <= curFileTUNodeSum; tuIndex++) {
                        traversalTuIndex++;
                        String tuXpath = "/xliff/file[" + fileNodeIdx + "]/body/descendant::trans-unit[" + tuIndex + "]";
                        QATUDataBean dataBean = handler.getFilteredTUText(xlfPath, tuXpath, model.getNotInclude());
                        //如果返回的map为null,则进行下一循环,行号也不加自加,这样可以保持这里的行号与界面上的行号一致性,方便定位,出现这种情况的可能很小,因为 rowID 的过滤已经处理过了。
                        if (dataBean == null) {
                            if (!monitorWork(subMonitor, traversalTuIndex, false)) {
                                closeDB();
                                return Status.CANCEL_STATUS;
                            }
                            continue;
                        }
                        lineNumber++;
                        if (lineNumber == 73) {
                            System.out.println("开始处理。。。。");
                        }
                        // 未通过 过滤器过滤的情况,
                        if (!dataBean.isPassFilter()) {
                            if (!monitorWork(subMonitor, traversalTuIndex, false)) {
                                closeDB();
                                return Status.CANCEL_STATUS;
                            }
                            continue;
                        }
                        dataBean.setLineNumber(lineNumber + "");
                        dataBean.setFileName(iFile.getName());
                        dataBean.setSrcLang(srcLang);
                        dataBean.setTgtLang(tgtLang);
                        dataBean.setXlfPath(xlfPath);
                        for (int i = 0; i < model.getBatchQAItemIdList().size(); i++) {
                            final String qaItemId = model.getBatchQAItemIdList().get(i);
                            realization = getClassInstance(qaItemId);
                            // 若没有该项检查的实例,提示出错
                            if (realization == null) {
                                Display.getDefault().asyncExec(new Runnable() {

                                    public void run() {
                                        MessageDialog.openError(shell, _ERROR, MessageFormat.format(Messages.getString("qa.QualityAssurance.tip4"), new Object[] { model.getQaItemId_Name_Class().get(qaItemId).get(QAConstant.QA_ITEM_NAME) }));
                                    }
                                });
                                closeDB();
                                return Status.CANCEL_STATUS;
                            }
                            // 开始进行该项文件的该项检查
                            final String result = realization.startQA(model, subMonitor, iFile, handler, dataBean);
                            // 当未设置术语库、拼写检查词典配置 错误时才会返回 null,所以这时可以直接删。
                            if (result == null) {
                                model.getBatchQAItemIdList().remove(qaItemId);
                                i--;
                            }
                            if (monitor.isCanceled()) {
                                return Status.CANCEL_STATUS;
                            }
                        }
                        // UNDO 发现这里很耗时,需要处理。
                        qaResult.sendDataToViewer(null);
                        if (!monitorWork(subMonitor, traversalTuIndex, false)) {
                            closeDB();
                            return Status.CANCEL_STATUS;
                        }
                    }
                    if (!monitorWork(subMonitor, traversalTuIndex, true)) {
                        closeDB();
                        return Status.CANCEL_STATUS;
                    }
                }
                subMonitor.done();
            }
            closeDB();
            qaResult.informQAEndFlag();
            monitor.done();
            System.out.println("所用时间为" + (System.currentTimeMillis() - time1));
            return Status.OK_STATUS;
        }
    };
    // 当程序退出时,检测当前 job 是否正常关闭
    CommonFunction.jobCantCancelTip(job);
    job.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void running(IJobChangeEvent event) {
            ProgressIndicatorManager.displayProgressIndicator();
            super.running(event);
        }

        @Override
        public void done(IJobChangeEvent event) {
            ProgressIndicatorManager.hideProgressIndicator();
            super.done(event);
        }
    });
    job.setUser(true);
    job.schedule();
}
Also used : IFile(org.eclipse.core.resources.IFile) QATUDataBean(net.heartsome.cat.ts.core.qa.QATUDataBean) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) QAXmlHandler(net.heartsome.cat.ts.core.qa.QAXmlHandler) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Job(org.eclipse.core.runtime.jobs.Job)

Example 98 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project cubrid-manager by CUBRID.

the class ImportDataFromFileDialog method updateData.

/**
	 *
	 * Update data
	 *
	 * @param parameterList List<PstmtParameter>
	 */
private void updateData(List<PstmtParameter> parameterList) {
    taskList.clear();
    String fileName = fileNameTxt.getText();
    String charset = fileCharsetCombo.getText();
    int threadCount = threadCountSpinner.getSelection();
    int commitedLineOnce = commitLineSpinner.getSelection();
    int totalLine = Integer.parseInt(totalLinesText.getText());
    int threadLine = totalLine / threadCount;
    int line = totalLine % threadCount;
    int startRow = 0;
    int totalProgress = 0;
    boolean isIgnoreError = importErrorControl.isIgoreOrBreak();
    if (isIgnoreError) {
        errorLogDir = PstmtDataTask.makeErrorLogDir();
    } else {
        errorLogDir = null;
    }
    if (importFileHandler == null) {
        importFileHandler = ImportFileHandlerFactory.getHandler(fileName, charset);
    }
    for (int i = 0; i < threadCount; i++) {
        // FIXME move this logic to core module
        int rowCount = threadLine;
        if (i == threadCount - 1) {
            rowCount = threadLine + line;
        }
        int commitCount = rowCount / commitedLineOnce + (rowCount % commitedLineOnce > 0 ? 1 : 0);
        int taskProgress = commitCount * PstmtDataTask.PROGRESS_COMMIT + rowCount * PstmtDataTask.PROGRESS_ROW;
        totalProgress = totalProgress + taskProgress + PstmtDataTask.PROGRESS_ROW;
        PstmtDataTask task = new PstmtDataTask(sqlTxt.getText(), database, fileNameTxt.getText(), parameterList, startRow, rowCount, commitedLineOnce, charset, firstRowAsColumnBtn.getSelection(), isIgnoreError, errorLogDir, importFileHandler);
        task.setTotalProgress(taskProgress);
        taskList.add(task);
        startRow += rowCount;
    }
    getShell().setVisible(false);
    beginTimestamp = System.currentTimeMillis();
    String jobName = Messages.executeSqlJobName;
    JobFamily jobFamily = new JobFamily();
    String serverName = database.getServer().getServerInfo().getServerName();
    String dbName = database.getName();
    jobFamily.setServerName(serverName);
    jobFamily.setDbName(dbName);
    final IProgressMonitor pmGroup = Job.getJobManager().createProgressGroup();
    pmGroup.beginTask(jobName, totalProgress);
    setStartShowResult(false);
    final CountDownLatch cdl = new CountDownLatch(taskList.size());
    final List<PstmtDataTask> threadTaskList = taskList;
    for (PstmtDataTask task : taskList) {
        TaskJobExecutor taskExec = new CommonTaskJobExec() {

            public IStatus exec(IProgressMonitor monitor) {
                IStatus status = super.exec(monitor);
                if (Status.CANCEL_STATUS == status) {
                    return status;
                }
                return Status.OK_STATUS;
            }

            public void done(IJobChangeEvent event) {
                if (event.getResult() == Status.CANCEL_STATUS) {
                    for (PstmtDataTask task : threadTaskList) {
                        if (!task.isCancel()) {
                            task.cancel();
                        }
                    }
                }
                countDownAndAwait(cdl);
                Display.getDefault().syncExec(new Runnable() {

                    public void run() {
                        pmGroup.done();
                        finish();
                    }
                });
            }
        };
        taskExec.addTask(task);
        TaskJob job = new TaskJob(jobName, taskExec);
        if (jobFamily != null) {
            job.setJobFamily(jobFamily);
        }
        job.setPriority(Job.LONG);
        job.setUser(false);
        job.setProgressGroup(pmGroup, task.getTotalProgress());
        job.schedule();
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
        }
        pmGroup.worked(PstmtDataTask.PROGRESS_ROW);
    }
}
Also used : TaskJobExecutor(com.cubrid.common.ui.spi.progress.TaskJobExecutor) IStatus(org.eclipse.core.runtime.IStatus) JobFamily(com.cubrid.common.ui.spi.progress.JobFamily) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TaskJob(com.cubrid.common.ui.spi.progress.TaskJob) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CommonTaskJobExec(com.cubrid.common.ui.spi.progress.CommonTaskJobExec)

Example 99 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by serge-rider.

the class ResultSetViewer method runDataPump.

private synchronized boolean runDataPump(@NotNull final DBSDataContainer dataContainer, @Nullable final DBDDataFilter dataFilter, final int offset, final int maxRows, final int focusRow, final boolean saveHistory, final boolean scroll, @Nullable final Runnable finalizer) {
    if (dataPumpJob != null) {
        UIUtils.showMessageBox(viewerPanel.getShell(), "Data read", "Data read is in progress - can't run another", SWT.ICON_WARNING);
        return false;
    }
    // Read data
    final DBDDataFilter useDataFilter = dataFilter != null ? dataFilter : (dataContainer == getDataContainer() ? model.getDataFilter() : null);
    Composite progressControl = viewerPanel;
    if (activePresentation.getControl() instanceof Composite) {
        progressControl = (Composite) activePresentation.getControl();
    }
    final Object presentationState = savePresentationState();
    dataPumpJob = new ResultSetJobDataRead(dataContainer, useDataFilter, this, getExecutionContext(), progressControl);
    dataPumpJob.addJobChangeListener(new JobChangeAdapter() {

        @Override
        public void aboutToRun(IJobChangeEvent event) {
            model.setUpdateInProgress(true);
            model.setStatistics(null);
            DBeaverUI.syncExec(new Runnable() {

                @Override
                public void run() {
                    filtersPanel.enableFilters(false);
                }
            });
        }

        @Override
        public void done(IJobChangeEvent event) {
            ResultSetJobDataRead job = (ResultSetJobDataRead) event.getJob();
            final Throwable error = job.getError();
            if (job.getStatistics() != null) {
                model.setStatistics(job.getStatistics());
            }
            final Control control = getControl();
            if (control.isDisposed()) {
                return;
            }
            DBeaverUI.syncExec(new Runnable() {

                @Override
                public void run() {
                    try {
                        final Control control = getControl();
                        if (control.isDisposed()) {
                            return;
                        }
                        final Shell shell = control.getShell();
                        final boolean metadataChanged = model.isMetadataChanged();
                        if (error != null) {
                            setStatus(error.getMessage(), DBPMessageType.ERROR);
                            UIUtils.showErrorDialog(shell, "Error executing query", "Query execution failed", error);
                        } else {
                            if (!metadataChanged && focusRow >= 0 && focusRow < model.getRowCount() && model.getVisibleAttributeCount() > 0) {
                                // Seems to be refresh
                                // Restore original position
                                curRow = model.getRow(focusRow);
                                restorePresentationState(presentationState);
                            }
                        }
                        activePresentation.updateValueView();
                        updatePanelsContent(false);
                        if (!scroll) {
                            // Add new history item
                            if (saveHistory && error == null) {
                                setNewState(dataContainer, dataFilter);
                            }
                            if (dataFilter != null) {
                                model.updateDataFilter(dataFilter);
                                // New data filter may have different columns visibility
                                redrawData(true, false);
                            }
                        }
                        model.setUpdateInProgress(false);
                        if (error == null) {
                            // Update status (update execution statistics)
                            updateStatusMessage();
                        }
                        updateFiltersText(error == null);
                        updateToolbar();
                        fireResultSetLoad();
                    } finally {
                        if (finalizer != null) {
                            try {
                                finalizer.run();
                            } catch (Throwable e) {
                                log.error(e);
                            }
                        }
                        dataPumpJob = null;
                    }
                }
            });
        }
    });
    dataPumpJob.setOffset(offset);
    dataPumpJob.setMaxRows(maxRows);
    dataPumpJob.schedule();
    return true;
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Example 100 with IJobChangeEvent

use of org.eclipse.core.runtime.jobs.IJobChangeEvent in project dbeaver by serge-rider.

the class ObjectListControl method addLazyObject.

private synchronized void addLazyObject(OBJECT_TYPE object, ObjectColumn column) {
    if (lazyObjects == null) {
        lazyObjects = new LinkedHashMap<>();
    }
    List<ObjectColumn> objectColumns = lazyObjects.get(object);
    if (objectColumns == null) {
        objectColumns = new ArrayList<>();
        lazyObjects.put(object, objectColumns);
    }
    if (!objectColumns.contains(column)) {
        objectColumns.add(column);
    }
    if (lazyLoadingJob == null) {
        lazyLoadingJob = new LazyLoaderJob();
        lazyLoadingJob.addJobChangeListener(new JobChangeAdapter() {

            @Override
            public void done(IJobChangeEvent event) {
                synchronized (ObjectListControl.this) {
                    if (lazyObjects == null || lazyObjects.isEmpty()) {
                        lazyLoadingJob = null;
                    } else {
                        lazyLoadingJob.schedule(LAZY_LOAD_DELAY);
                    }
                }
            }
        });
        lazyLoadingJob.schedule(LAZY_LOAD_DELAY);
    }
}
Also used : JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent)

Aggregations

IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)172 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)163 Job (org.eclipse.core.runtime.jobs.Job)46 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)45 IStatus (org.eclipse.core.runtime.IStatus)36 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)30 ArrayList (java.util.ArrayList)13 IFile (org.eclipse.core.resources.IFile)13 File (java.io.File)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)12 CoreException (org.eclipse.core.runtime.CoreException)12 DataSourceJob (org.jkiss.dbeaver.runtime.jobs.DataSourceJob)12 Status (org.eclipse.core.runtime.Status)11 IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)11 IOException (java.io.IOException)10 DBException (org.jkiss.dbeaver.DBException)10 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)10 IProject (org.eclipse.core.resources.IProject)9 DBSObject (org.jkiss.dbeaver.model.struct.DBSObject)8 Composite (org.eclipse.swt.widgets.Composite)7