use of org.eclipse.ui.IEditorPart in project translationstudio8 by heartsome.
the class ExecuteBingTransHandler method execute.
/**
* (non-Javadoc)
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
final IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
IEditorPart editor = HandlerUtil.getActiveEditor(event);
if (!(editor instanceof IXliffEditor)) {
return null;
}
// check the google translation state: check the key availability
PrefrenceParameters ps = PrefrenceParameters.getInstance();
if (!ps.getState()) {
MessageDialog.openError(window.getShell(), Messages.getString("handler.ExecuteBingTransHandler.msgTitle"), Messages.getString("handler.ExecuteBingTransHandler.msg"));
return null;
}
String tshelp = System.getProperties().getProperty("TSHelp");
String tsstate = System.getProperties().getProperty("TSState");
if (tshelp == null || !"true".equals(tshelp) || tsstate == null || !"true".equals(tsstate)) {
LoggerFactory.getLogger(ExecuteBingTransHandler.class).error("Exception:key hs008 is lost.(Can't find the key)");
System.exit(0);
}
final IXliffEditor xliffEditor = (IXliffEditor) editor;
final int[] selectedRowIndexs = xliffEditor.getSelectedRows();
if (selectedRowIndexs.length == 0) {
return null;
}
// int currentRowIndex = selectedRowIndexs[0];
// TransUnitBean transUnitBean = xliffEditor.getRowTransUnitBean(currentRowIndex);
// if (transUnitBean == null) {
// return null;
// }
// String srcPureText = transUnitBean.getSrcText();
// String tgtLanguage = xliffEditor.getTgtColumnName();
// String srcLanguage = xliffEditor.getSrcColumnName();
// TransUnitInfo2TranslationBean tuInfo2Trans = new TransUnitInfo2TranslationBean();
// tuInfo2Trans.setSrcPureText(srcPureText);
// tuInfo2Trans.setSrcLanguage(srcLanguage);
// tuInfo2Trans.setTgtLangugage(tgtLanguage);
ISimpleMatcher matcher = new SimpleMatcherBingImpl();
// String tgtText = matcher.executeMatch(tuInfo2Trans);
// if (tgtText.equals("")) {
// return null;
// }
// AltTransBean bean = new AltTransBean(srcPureText, tgtText, srcLanguage, tgtLanguage,
// matcher.getMathcerOrigin(), matcher.getMathcerToolId());
// bean.getMatchProps().put("match-quality", "100");
// bean.getMatchProps().put("hs:matchType", matcher.getMatcherType());
// bean.setSrcContent(srcPureText);
// bean.setTgtContent(tgtText);
// List<AltTransBean> newAltTrans = new ArrayList<AltTransBean>();
// newAltTrans.add(bean);
// check if need save the AltTrans to file
// if (CommonFunction.checkEdition("U") && matcher.isSuportPreTrans()) {
// List<String> oldToolIds = new ArrayList<String>();
// oldToolIds.add(matcher.getMathcerToolId());
// xliffEditor.getXLFHandler().updateAltTrans(xliffEditor.getXLFHandler().getRowId(currentRowIndex), newAltTrans, oldToolIds);
// }
IViewPart viewPart = window.getActivePage().findView(MatchViewPart.ID);
if (viewPart != null && viewPart instanceof MatchViewPart) {
MatchViewPart matchView = (MatchViewPart) viewPart;
//matchView.refreshView(xliffEditor, selectedRowIndexs[0]);
// matchView.refreshViewByToolId(xliffEditor, newAltTrans, matcher.getMathcerToolId());
// matchView.replaceMatchs(newAltTrans);
// newAltTrans.clear();
matchView.manualExecSimpleTranslation(matcher);
}
return null;
}
use of org.eclipse.ui.IEditorPart in project translationstudio8 by heartsome.
the class PreMachineTransUitls method executeTranslation.
public static void executeTranslation(List<IFile> list, final Shell shell) {
HsMultiActiveCellEditor.commit(true);
try {
if (list.size() == 0) {
MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg1"));
return;
}
List<IFile> lstFiles = new ArrayList<IFile>();
XLFValidator.resetFlag();
for (IFile iFile : list) {
if (!XLFValidator.validateXliffFile(iFile)) {
lstFiles.add(iFile);
}
}
XLFValidator.resetFlag();
list = new ArrayList<IFile>(list);
list.removeAll(lstFiles);
if (list.size() == 0) {
return;
}
final IProject project = list.get(0).getProject();
final List<String> filesWithOsPath = ResourceUtils.IFilesToOsPath(list);
final XLFHandler xlfHandler = new XLFHandler();
Map<String, Object> resultMap = xlfHandler.openFiles(filesWithOsPath);
if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
// 打开文件失败。
MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg2"));
return;
}
Map<String, List<XliffBean>> map = xlfHandler.getXliffInfo();
final PreMachineTransParameters parameters = new PreMachineTransParameters();
PreMachineTranslationDialog dialog = new PreMachineTranslationDialog(shell, map, parameters);
if (dialog.open() == Window.OK) {
if (project == null) {
MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg3"));
return;
}
if (filesWithOsPath == null || filesWithOsPath.size() == 0) {
MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.msg4"));
return;
}
final List<IFile> lstFile = list;
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
PreMachineTranslation pt = new PreMachineTranslation(xlfHandler, filesWithOsPath, project, parameters);
try {
final List<PreMachineTranslationCounter> result = pt.executeTranslation(monitor);
Display.getDefault().syncExec(new Runnable() {
public void run() {
//PreMachineTranslationResultDialog dialog = new PreMachineTranslationResultDialog(shell, result);
//dialog.open();
}
});
project.refreshLocal(IResource.DEPTH_INFINITE, null);
result.clear();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (CoreException e) {
logger.error("", e);
e.printStackTrace();
} finally {
pt.clearResources();
}
Display.getDefault().syncExec(new Runnable() {
public void run() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
for (IFile file : lstFile) {
FileEditorInput editorInput = new FileEditorInput(file);
IEditorPart editorPart = page.findEditor(editorInput);
// 选择所有语言
XLFHandler handler = null;
if (editorPart != null && editorPart instanceof IXliffEditor) {
// xliff 文件已用 XLIFF 编辑器打开
IXliffEditor xliffEditor = (IXliffEditor) editorPart;
handler = xliffEditor.getXLFHandler();
handler.resetCache();
VTDGen vg = new VTDGen();
String path = ResourceUtils.iFileToOSPath(file);
if (vg.parseFile(path, true)) {
handler.getVnMap().put(path, vg.getNav());
xliffEditor.refresh();
}
}
}
}
});
}
};
try {
new ProgressMonitorDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()).run(true, true, runnable);
MessageDialog.openInformation(shell, Messages.getString("pretranslation.PreTransUitls.msgTitle"), Messages.getString("pretranslation.PreTransUitls.result.msg"));
} catch (InvocationTargetException e) {
logger.error("", e);
} catch (InterruptedException e) {
logger.error("", e);
}
}
} finally {
HsMultiCellEditorControl.activeSourceAndTargetCell(XLIFFEditorImplWithNatTable.getCurrent());
}
}
use of org.eclipse.ui.IEditorPart in project translationstudio8 by heartsome.
the class NextSplitPointHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
final String XLIFF_EDITOR_ID = "net.heartsome.cat.ts.ui.xliffeditor.nattable.editor";
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
final Shell shell = window.getShell();
XLIFFEditorImplWithNatTable xliffEditor = null;
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
if (activePart instanceof IEditorPart) {
if (XLIFF_EDITOR_ID.equals(activePart.getSite().getId())) {
xliffEditor = (XLIFFEditorImplWithNatTable) activePart;
List<String> splitPointList = xliffEditor.getSplitXliffPoints();
if (splitPointList.size() <= 0) {
MessageDialog.openInformation(shell, Messages.getString("all.dialog.info"), Messages.getString("NextSplitPointHandler.msg.nullSplitPoint"));
return null;
}
final XLFHandler xlfHander = xliffEditor.getXLFHandler();
// 先对 splitPointList 进行排序
Collections.sort(splitPointList, new Comparator<String>() {
public int compare(String rowId1, String rowId2) {
int rowIndex1 = xlfHander.getRowIndex(rowId1);
int rowIndex2 = xlfHander.getRowIndex(rowId2);
return rowIndex1 > rowIndex2 ? 1 : -1;
}
});
List<String> selectionRowIdList = xliffEditor.getSelectedRowIds();
if (selectionRowIdList != null && selectionRowIdList.size() > 0) {
curSelectionRowId = selectionRowIdList.get(0);
}
// 开始定位,定位之前让 nattable 恢复默认布局
xliffEditor.resetOrder();
if (curSelectionRowId == null) {
curSelectionRowId = splitPointList.get(0);
} else {
int curSelectionRowIndex = xlfHander.getRowIndex(curSelectionRowId);
for (String curRowId : splitPointList) {
int pointRowIndex = xlfHander.getRowIndex(curRowId);
if (pointRowIndex > curSelectionRowIndex) {
curSelectionRowId = curRowId;
xliffEditor.jumpToRow(curSelectionRowId);
break;
}
}
}
}
}
return null;
}
use of org.eclipse.ui.IEditorPart in project translationstudio8 by heartsome.
the class SplitXliffHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
final String navegatorID = "net.heartsome.cat.common.ui.navigator.view";
final String XLIFF_EDITOR_ID = "net.heartsome.cat.ts.ui.xliffeditor.nattable.editor";
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
final Shell shell = window.getShell();
IFile selectFile = null;
XLIFFEditorImplWithNatTable xliffEditor = null;
List<Integer> splitXlfPointsIndex = new LinkedList<Integer>();
List<String> splitXlfPointsRowId = new LinkedList<String>();
IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
// 如果是导航视图,那么就获取导航视图中选中的文件
if (activePart instanceof IViewPart) {
if (navegatorID.equals(activePart.getSite().getId())) {
IViewPart viewPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(navegatorID);
ISelection currentSelection = (StructuredSelection) viewPart.getSite().getSelectionProvider().getSelection();
if (currentSelection != null && !currentSelection.isEmpty() && currentSelection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection) currentSelection;
Object object = structuredSelection.getFirstElement();
if (object instanceof IFile) {
selectFile = (IFile) object;
String fileExtension = selectFile.getFileExtension();
// 如果后缀名不是xlf,那么就退出操作
if (fileExtension == null || !CommonFunction.validXlfExtension(fileExtension)) {
MessageDialog.openInformation(shell, Messages.getString("handler.SplitXliffHandler.msgTitle"), Messages.getString("handler.SplitXliffHandler.msg1"));
return null;
}
FileEditorInput fileInput = new FileEditorInput(selectFile);
IEditorReference[] editorRefer = window.getActivePage().findEditors(fileInput, XLIFF_EDITOR_ID, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
IEditorPart editorPart = null;
if (editorRefer.length >= 1) {
editorPart = editorRefer[0].getEditor(true);
xliffEditor = (XLIFFEditorImplWithNatTable) editorPart;
if (window.getActivePage().getActiveEditor() != editorPart) {
window.getActivePage().activate(editorPart);
}
} else {
try {
xliffEditor = (XLIFFEditorImplWithNatTable) window.getActivePage().openEditor(fileInput, XLIFF_EDITOR_ID, true, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
} catch (PartInitException e) {
LOGGER.error("", e);
e.printStackTrace();
}
}
} else {
MessageDialog.openInformation(shell, Messages.getString("handler.SplitXliffHandler.msgTitle"), Messages.getString("handler.SplitXliffHandler.msg1"));
return null;
}
}
}
} else if (activePart instanceof IEditorPart) {
if (XLIFF_EDITOR_ID.equals(activePart.getSite().getId())) {
xliffEditor = (XLIFFEditorImplWithNatTable) activePart;
selectFile = ((FileEditorInput) xliffEditor.getEditorInput()).getFile();
}
}
// 根据每个tu节点的rowId获取其具体的位置,才好进行排序
Map<Integer, String> pointIndexRowIdMap = new HashMap<Integer, String>();
for (String rowId : xliffEditor.getSplitXliffPoints()) {
// 获取指定tu节点所处其他结点的序列号
int tuPostion = xliffEditor.getXLFHandler().getTUPositionByRowId(rowId);
if (tuPostion >= 1) {
splitXlfPointsIndex.add(tuPostion);
pointIndexRowIdMap.put(tuPostion, rowId);
}
}
if (splitXlfPointsIndex.size() <= 0) {
MessageDialog.openInformation(shell, Messages.getString("handler.SplitXliffHandler.msgTitle"), Messages.getString("handler.SplitXliffHandler.msg2"));
return null;
}
// 对切割点集合进行排序
for (int i = 0; i < splitXlfPointsIndex.size(); i++) {
int point1 = splitXlfPointsIndex.get(i);
for (int j = i + 1; j < splitXlfPointsIndex.size(); j++) {
int point2 = splitXlfPointsIndex.get(j);
if (point1 > point2) {
splitXlfPointsIndex.set(i, point2);
splitXlfPointsIndex.set(j, point1);
point1 = point2;
}
}
}
// 向存储rowId的list存放数据,这样的话,所存储的rowId就是经过排序了的。
for (int i = 0; i < splitXlfPointsIndex.size(); i++) {
splitXlfPointsRowId.add(pointIndexRowIdMap.get(splitXlfPointsIndex.get(i)));
}
SplitOrMergeXlfModel model = new SplitOrMergeXlfModel();
model.setSplitFile(selectFile);
model.setSplitXlfPointsIndex(splitXlfPointsIndex);
model.setSplitXlfPointsRowId(splitXlfPointsRowId);
model.setXliffEditor(xliffEditor);
model.setShell(shell);
SplitXliffWizard wizard = new SplitXliffWizard(model);
final TSWizardDialog dialog = new NattableWizardDialog(shell, wizard);
dialog.open();
return null;
}
use of org.eclipse.ui.IEditorPart in project translationstudio8 by heartsome.
the class ImportProjectWizardPage2 method getAllOpenedIFile.
/**
* 获取所有已经被打开的文件
* @return
*/
private Map<IFile, IEditorPart> getAllOpenedIFile() {
Map<IFile, IEditorPart> openedIfileMap = new HashMap<IFile, IEditorPart>();
IEditorReference[] referenceArray = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
for (IEditorReference reference : referenceArray) {
IEditorPart editor = reference.getEditor(true);
// 如果这是一个 nattable 编辑器
if (XLIFF_EDITOR_ID.equals(editor.getSite().getId())) {
IXliffEditor xlfEditor = (IXliffEditor) editor;
if (xlfEditor.isMultiFile()) {
for (File file : xlfEditor.getMultiFileList()) {
openedIfileMap.put(ResourceUtils.fileToIFile(file.getAbsolutePath()), editor);
}
} else {
openedIfileMap.put(((FileEditorInput) editor.getEditorInput()).getFile(), editor);
}
} else {
// 其他情况,直接将文件丢进去就行了
openedIfileMap.put(((FileEditorInput) editor.getEditorInput()).getFile(), editor);
}
}
return openedIfileMap;
}
Aggregations