use of net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable in project translationstudio8 by heartsome.
the class SplitXliff method splitTheXliff.
/**
* 切割Xliff文件 注意:源文本没有解析,因为xlfhandler是从xlfeditor取过来的,因此不用再解析一次。
*/
public boolean splitTheXliff(IProgressMonitor monitor) {
if (monitor == null) {
monitor = new NullProgressMonitor();
}
monitor.beginTask(Messages.getString("splitAndMergeXliff.SplitXliff.task1"), splitPointsIndex.size());
String xlfPath = splitFile.getLocation().toOSString();
if (xlfPath != null) {
File f = new File(xlfPath);
// 如果文件不存在,提示并退出操作
if (!f.exists()) {
f = null;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
MessageDialog.openInformation(shell, Messages.getString("splitAndMergeXliff.SplitXliff.msgTitle1"), MessageFormat.format(Messages.getString("splitAndMergeXliff.SplitXliff.msg1"), new Object[] { splitFile.getFullPath().toOSString() }));
}
});
return false;
} else {
Map<String, String> newInfo = null;
File src = new File(xlfPath);
String fileName = null;
String splitXliffName = null;
try {
splitXliffName = src.getName();
// 源文件的文件名
fileName = new String(splitXliffName.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e1) {
LOGGER.error("", e1);
e1.printStackTrace();
}
// 判断分割后生成的子文件是否重复,如果重复就进行提示
String copyFiles = "";
LinkedList<String> newSplitedFilesName = model.getNewSplitedFilesName();
for (int i = 0; i < newSplitedFilesName.size(); i++) {
final String newXlfPath = model.getSplitXlfsContainer().getLocation().append(newSplitedFilesName.get(i)).toOSString();
File newXlfFile = new File(newXlfPath);
if (newXlfFile.exists()) {
copyFiles += getFullPath(newXlfPath) + "\n";
repeateFileList.add(ResourceUtils.fileToIFile(newXlfPath));
}
}
final String copyFilesTip = copyFiles.substring(0, copyFiles.length());
// 如果新生成的文件已经存在,那么提示是否覆盖
if (copyFiles.length() > 0) {
try {
Display.getDefault().syncExec(new Runnable() {
public void run() {
boolean response = MessageDialog.openConfirm(shell, Messages.getString("splitAndMergeXliff.SplitXliff.msgTitle2"), MessageFormat.format(Messages.getString("splitAndMergeXliff.SplitXliff.msg2"), new Object[] { copyFilesTip }));
if (!response) {
needCover = true;
}
}
});
} catch (Exception e) {
LOGGER.error("", e);
e.printStackTrace();
}
if (needCover) {
return false;
}
}
// 先删除重复的文件,再关闭已经打开的重复文件的编辑器
Display.getDefault().syncExec(new Runnable() {
public void run() {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
try {
IEditorReference[] refrences = window.getActivePage().getEditorReferences();
for (IEditorReference refrence : refrences) {
if (refrence.getEditor(true).getEditorSite().getId().equals(NATABLE_ID)) {
XLIFFEditorImplWithNatTable nattable = (XLIFFEditorImplWithNatTable) refrence.getEditor(true);
if (nattable.isMultiFile()) {
for (File file : nattable.getMultiFileList()) {
if (repeateFileList.indexOf(ResourceUtils.fileToIFile(file.getAbsolutePath())) >= 0) {
window.getActivePage().closeEditor(nattable, true);
break;
}
}
} else {
if (repeateFileList.indexOf(((FileEditorInput) nattable.getEditorInput()).getFile()) >= 0) {
window.getActivePage().closeEditor(nattable, true);
}
}
}
}
for (IFile iFile : repeateFileList) {
ResourceUtil.getFile(iFile).delete(true, null);
}
model.getSplitFile().getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
} catch (Exception e) {
LOGGER.error("", e);
}
}
});
// 得到当前的时间, 开始进行循环切割
long splitTime = System.currentTimeMillis();
for (int i = CONSTANT_ONE, pointsSize = (splitPointsIndex.size() + 1); i <= pointsSize; i++) {
newInfo = getNewSplitInfo(fileName, oldInfo, i, pointsSize, splitTime);
monitor.subTask(Messages.getString("splitAndMergeXliff.SplitXliff.task2") + newInfo.get("name"));
// 创建新的XLIFF的文件路径
final String newXlfPath = model.getSplitXlfsContainer().getLocation().append(getSplitFileName(splitXliffName, oldInfo, i)).toOSString();
String xliffNodeHeader = xlfhandler.getNodeHeader(xlfPath, "xliff", "/xliff");
createNewSplitXliff(newXlfPath, xliffNodeHeader);
// 打开这个新创建的xliff文件,将xliff与file,header等节点写入进去
Map<String, Object> newResultMap = splitHandler.openFile(newXlfPath);
if (newResultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) newResultMap.get(Constant.RETURNVALUE_RESULT)) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
MessageDialog.openError(shell, Messages.getString("splitAndMergeXliff.SplitXliff.msgTitle3"), Messages.getString("splitAndMergeXliff.SplitXliff.msg3"));
}
});
return false;
}
if (fileInfo == null) {
fileInfo = xlfhandler.getFileInfo(xlfPath);
}
// 获取当前分割段的首末rowId
String startRowId = xlfhandler.getNextRowId(xlfPath, i == CONSTANT_ONE ? "start" : splitPointsRowId.get(i - 2));
String endRowId = (i == pointsSize) ? xlfhandler.getNextRowId(xlfPath, "end") : splitPointsRowId.get(// 因为这里的I是从1开始的,故要减1
i - CONSTANT_ONE);
// 获取分割段的file,与body第一子节点的序列
Map<String, Integer> startNodeIdxMap = xlfhandler.getSplitNodeIdx(xlfPath, i == CONSTANT_ONE ? "start" : splitPointsRowId.get(i - 2));
Map<String, Integer> endNodeIdxMap = (i == pointsSize) ? xlfhandler.getSplitNodeIdx(xlfPath, "end") : xlfhandler.getSplitNodeIdx(xlfPath, splitPointsRowId.get(i - CONSTANT_ONE));
// 获取当前起始rowId所在的file节点的序列号
int startFileNodeIdx = startNodeIdxMap.get("fileNodeIdx");
int endFileNodeIdx = endNodeIdxMap.get("fileNodeIdx");
int startBodyChildIdx = startNodeIdxMap.get("bodyChildNodeIdx");
// 开始循环每一个file节点,进行获取相关数据
// 这是新生成的xliff文件中的file的序列号
int n = 1;
for (int fileIdx = startFileNodeIdx; fileIdx <= endFileNodeIdx; fileIdx++) {
// 开始将数据存入新切割的xliff文件中,先存放file节点的头
String fileNodeHeader = xlfhandler.getNodeHeader(xlfPath, "file", "/xliff/file[" + fileIdx + "]");
splitHandler.addDataToXlf(newXlfPath, fileNodeHeader, "/xliff");
String headerFrag = xlfhandler.getNodeFrag(xlfPath, "/xliff/file[" + fileIdx + "]/header");
splitHandler.addDataToXlf(newXlfPath, headerFrag, "/xliff/file[" + n + "]");
// 向新生成xliff文件添加body元素
String bodyNodeHeader = xlfhandler.getNodeHeader(xlfPath, "body", "/xliff/file[" + fileIdx + "]/body");
splitHandler.addDataToXlf(newXlfPath, bodyNodeHeader, "/xliff/file[" + n + "]");
boolean isLastOfFile = false;
if (i == pointsSize && fileIdx == endFileNodeIdx) {
isLastOfFile = true;
}
// UNDO 分割这里还要好发测试一下,针对不同情况。
String tuData = xlfhandler.getSplitTuData(xlfPath, fileIdx, n == 1 ? startBodyChildIdx : 1, n == 1 ? startRowId : null, endRowId, n == 1, isLastOfFile);
splitHandler.addDataToXlf(newXlfPath, tuData, "/xliff/file[" + n + "]/body");
n++;
}
if (monitor.isCanceled()) {
throw new OperationCanceledException(Messages.getString("splitAndMergeXliff.SplitXliff.msg3"));
}
monitor.worked(1);
// 添加新的切割信息
splitHandler.addNewInfoToSplitXlf(newXlfPath, newInfo);
}
monitor.done();
}
}
return true;
}
use of net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable 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 net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable 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 net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable in project translationstudio8 by heartsome.
the class LockRepeatedSegmentHandler method lockTU.
/**
* 概据内部匹配结果,锁定文本段。
* @param xlfHandler
* @param rowIdMap
*/
private void lockTU(final XLFHandler xlfHandler, Map<String, List<String>> rowIdMap) {
Iterator<Entry<String, List<String>>> it = rowIdMap.entrySet().iterator();
while (it.hasNext()) {
isLocked = false;
final Entry<String, List<String>> rowIdsEntry = it.next();
final String fileLC = rowIdsEntry.getKey();
// 查看该文件是否打开,若打开,则获editor的handler,若未打开,则直接使用当前handler
final IEditorInput input = new FileEditorInput(ResourceUtils.fileToIFile(fileLC));
final IEditorReference[] editorRefes = window.getActivePage().getEditorReferences();
Display.getDefault().syncExec(new Runnable() {
public void run() {
for (int i = 0; i < editorRefes.length; i++) {
if (XLIFF_EDITOR_ID.equals(editorRefes[i].getId())) {
// 先判断打开单个文件的情况
XLIFFEditorImplWithNatTable nattable = (XLIFFEditorImplWithNatTable) (editorRefes[i].getEditor(true));
if (!nattable.isMultiFile()) {
if (nattable.getEditorInput().equals(input)) {
nattable.getXLFHandler().lockTransUnits(rowIdsEntry.getValue(), true);
isLocked = true;
nattable.getTable().redraw();
}
} else {
// 这是合并打开的情况
if (nattable.getMultiFileList().indexOf(new File(fileLC)) >= 0) {
nattable.getXLFHandler().lockTransUnits(rowIdsEntry.getValue(), true);
isLocked = true;
nattable.getTable().redraw();
}
;
}
}
}
// 如果未被锁定(当前文件没有打开),就调用当前XLFHandler去锁定所有文本段
if (!isLocked) {
xlfHandler.lockTransUnits(rowIdsEntry.getValue(), true);
}
}
});
}
}
use of net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable in project translationstudio8 by heartsome.
the class PluginConfigManage method executePlugin.
/**
* 运行自定义的插件
* @param bean
* ;
*/
@SuppressWarnings("unchecked")
public void executePlugin(PluginConfigBean bean) {
String commandLine = bean.getCommandLine();
if (commandLine == null || "".equals(commandLine)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg1"));
return;
}
try {
// 当输出(进程)为当前文档时
if (bean.getOutput().equals(PluginConstants.SEGMENT)) {
// 先检查是否有已经打开的文件,若没有,退出插件执行
XLIFFEditorImplWithNatTable nattable = XLIFFEditorImplWithNatTable.getCurrent();
if (nattable == null) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg2"));
return;
}
XLFHandler handler = nattable.getXLFHandler();
List<String> selectRowIds = nattable.getSelectedRowIds();
if (selectRowIds.size() <= 0) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg3"));
return;
}
if (selectRowIds.size() > 1) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg4"));
}
String rowId = selectRowIds.get(0);
sendSegment(bean, handler, rowId);
// 执行后返回的文件
String returnContent = runPlugin(bean);
// 如果返回为交换文件,则更新当前文本段
if (bean.getInput().equals(PluginConstants.EXCHANGEFILE)) {
handler.updateAndSave(rowId, "", returnContent);
// 更新完后,要刷新界面。此处未做,滞留。
}
}
// 当输出(进程)为当前文档时
if (bean.getOutput().equals(PluginConstants.DOCUMENT)) {
XLFHandler handler;
IFile selectIFile = null;
// 先检查是否有已经打开的文件,若没有,退出插件执行
XLIFFEditorImplWithNatTable nattable = XLIFFEditorImplWithNatTable.getCurrent();
if (nattable == null) {
// 如果当前没有打开的文件,那么获取左边导航框中选中的文件
ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection();
if (selection == null || selection.isEmpty() || !(selection instanceof StructuredSelection)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg5"));
return;
}
IStructuredSelection structuredSelection = (IStructuredSelection) selection;
Iterator<Object> selectIt = structuredSelection.iterator();
if (selectIt.hasNext()) {
Object object = selectIt.next();
if (object instanceof IFile) {
selectIFile = (IFile) object;
String fileExtension = selectIFile.getFileExtension();
if (!CommonFunction.validXlfExtension(fileExtension)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg5"));
return;
}
}
}
handler = new XLFHandler();
// 打开该xliff文件
Map<String, Object> resultMap = handler.openFile(selectIFile.getLocation().toOSString());
if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
MessageDialog.openInformation(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg5"));
return;
}
} else {
handler = nattable.getXLFHandler();
selectIFile = ResourceUtil.getFile(nattable.getEditorInput());
// IEditorInput fileInput = nattable.getEditorInput();
}
sendDocument(bean, selectIFile);
if (bean.getInput().equals(PluginConstants.DOCUMENT)) {
// 重新解析该文件
Map<String, Object> resultMap = handler.openFile(selectIFile.getLocation().toOSString());
if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
MessageDialog.openError(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg6"));
return;
}
}
}
// 当输出(进程)为空时
if (bean.getOutput().equals(PluginConstants.NONE)) {
runPlugin(bean);
}
} catch (Exception e) {
LOGGER.error("", e);
MessageDialog.openError(shell, Messages.getString("plugin.PluginConfigManage.msgTitle"), Messages.getString("plugin.PluginConfigManage.msg7"));
}
}
Aggregations