use of org.eclipse.ui.IEditorInput in project translationstudio8 by heartsome.
the class ResourceLinkHelper method activateEditor.
/* (non-Javadoc)
* @see org.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui.IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection)
*/
public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) {
if (aSelection == null || aSelection.isEmpty())
return;
if (aSelection.getFirstElement() instanceof IFile) {
IEditorInput fileInput = new FileEditorInput((IFile) aSelection.getFirstElement());
IEditorPart editor = null;
if ((editor = aPage.findEditor(fileInput)) != null)
aPage.bringToTop(editor);
}
}
use of org.eclipse.ui.IEditorInput in project translationstudio8 by heartsome.
the class DeleteResourceAndCloseEditorAction method closeRelatedEditors.
/**
* 删除文件时,如果文件在编辑器中已打开,则关闭此文件。 ;
*/
private void closeRelatedEditors() {
IWorkbenchPage page = null;
for (IWorkbenchWindow window : PlatformUI.getWorkbench().getWorkbenchWindows()) {
if (window != null) {
page = window.getActivePage();
break;
}
}
if (page == null) {
return;
}
IEditorReference[] editorReferences = page.getEditorReferences();
final List<IResource> selectionResource = getSelectedResources();
final List<IEditorReference> lstCloseEditor = new ArrayList<IEditorReference>();
final VTDGen vg = new VTDGen();
final AutoPilot ap = new AutoPilot();
final List<IEditorInput> inputList = new ArrayList<IEditorInput>();
for (final IEditorReference reference : editorReferences) {
Display.getDefault().asyncExec(new Runnable() {
public void run() {
IFile file = ((FileEditorInput) reference.getEditor(true).getEditorInput()).getFile();
if ("xlp".equals(file.getFileExtension())) {
if (vg.parseFile(file.getLocation().toOSString(), true)) {
VTDNav vn = vg.getNav();
ap.bind(vn);
try {
ap.selectXPath("/mergerFiles/mergerFile/@filePath");
int index = -1;
merge: while ((index = ap.evalXPath()) != -1) {
String fileLC = vn.toString(index + 1);
if (fileLC != null && !"".equals(fileLC)) {
for (IResource resource : selectionResource) {
if (resource instanceof IProject || resource instanceof IFolder) {
if (fileLC.startsWith(resource.getLocation().toOSString() + File.separator)) {
lstCloseEditor.add(reference);
inputList.add(reference.getEditorInput());
break merge;
}
} else if (resource instanceof IFile) {
if (resource.getLocation().toOSString().equals(fileLC)) {
lstCloseEditor.add(reference);
inputList.add(reference.getEditorInput());
break merge;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
try {
for (IResource resource : selectionResource) {
if (resource instanceof IProject) {
if (resource.getLocation().toOSString().equals(file.getProject().getLocation().toOSString())) {
lstCloseEditor.add(reference);
inputList.add(reference.getEditorInput());
break;
}
} else if (resource instanceof IFolder) {
if (file.getLocation().toOSString().startsWith(resource.getLocation().toOSString() + File.separator)) {
lstCloseEditor.add(reference);
inputList.add(reference.getEditorInput());
break;
}
} else if (resource instanceof IFile) {
if (resource.getLocation().toOSString().equals(file.getLocation().toOSString())) {
lstCloseEditor.add(reference);
inputList.add(reference.getEditorInput());
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
final IWorkbenchPage page2 = page;
Display.getDefault().asyncExec(new Runnable() {
public void run() {
IEditorReference[] arrEditorReference = new IEditorReference[lstCloseEditor.size()];
page2.closeEditors(lstCloseEditor.toArray(arrEditorReference), false);
// 删除文件时,刷新访问记录 robert 2012-11-20
for (IEditorInput input : inputList) {
System.out.println("input = " + input);
CommonFunction.refreshHistoryWhenDelete(input);
}
}
});
}
use of org.eclipse.ui.IEditorInput in project translationstudio8 by heartsome.
the class XLIFFEditorImplWithNatTable method performSaveAs.
/**
* 执行另存为
* @param progressMonitor
* 进度条;
*/
private void performSaveAs(IProgressMonitor progressMonitor) {
Shell shell = getSite().getShell();
final IEditorInput input = getEditorInput();
// 新的EditorInput
final IEditorInput newInput;
// 原始的file
final File oldFile;
// if (input instanceof IURIEditorInput && !(input instanceof IFileEditorInput)) { // 外部文件
FileDialog dialog = new FileDialog(shell, SWT.SAVE);
URI uri = ((IURIEditorInput) input).getURI();
IPath oldPath = URIUtil.toPath(uri);
if (oldPath != null) {
dialog.setFileName(oldPath.lastSegment());
// 设置所在文件夹
dialog.setFilterPath(oldPath.removeLastSegments(1).toOSString());
oldFile = oldPath.toFile();
} else {
oldFile = new File(uri);
}
// 得到保存路径
String newPath = dialog.open();
if (newPath == null) {
if (progressMonitor != null)
progressMonitor.setCanceled(true);
return;
}
// 检查文件是否存在,如果存在则确认是否覆盖
final File localFile = new File(newPath);
if (localFile.exists()) {
String msg = MessageFormat.format(Messages.getString("editor.XLIFFEditorImplWithNatTable.msg1"), newPath);
MessageDialog overwriteDialog = new MessageDialog(shell, Messages.getString("editor.XLIFFEditorImplWithNatTable.overwriteDialog"), null, msg, MessageDialog.WARNING, new String[] { IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL }, 1);
if (overwriteDialog.open() != MessageDialog.OK) {
if (progressMonitor != null) {
progressMonitor.setCanceled(true);
return;
}
}
}
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// 得到新文件
IFile file = root.getFileForLocation(URIUtil.toPath(localFile.toURI()));
if (file != null) {
// 是“WorkSpace”内的文件
newInput = new FileEditorInput(file);
} else {
// 不是“WorkSpace”内的文件
try {
IFileStore fileStore = EFS.getStore(localFile.toURI());
newInput = new FileStoreEditorInput(fileStore);
} catch (CoreException ex) {
// EditorsPlugin.log(ex.getStatus());
LOGGER.error("", ex);
String title = Messages.getString("editor.XLIFFEditorImplWithNatTable.msgTitle1");
String msg = MessageFormat.format(Messages.getString("editor.XLIFFEditorImplWithNatTable.msg2"), ex.getMessage());
MessageDialog.openError(shell, title, msg);
return;
}
}
// } else {
// SaveAsDialog dialog = new SaveAsDialog(shell);
// // 源文件
// IFile original = (input instanceof IFileEditorInput) ? ((IFileEditorInput) input).getFile() : null;
// if (original != null) {
// dialog.setOriginalFile(original); // 添加源文件信息
// oldFile = original.getLocation().toFile();
// if ((!oldFile.exists() || !oldFile.canRead()) && original != null) {
// String message = MessageFormat.format(
// "The original file ''{0}'' has been deleted or is not accessible.", original.getName());
// dialog.setErrorMessage(null);
// dialog.setMessage(message, IMessageProvider.WARNING);
// }
// } else {
// oldFile = null;
// }
// dialog.create();
//
// if (dialog.open() == MessageDialog.CANCEL) { // 打开“另存为”对话框,用户点击了“取消”按钮
// if (progressMonitor != null)
// progressMonitor.setCanceled(true);
// return;
// }
//
// IPath filePath = dialog.getResult(); // 获得用户选择的路径
// if (filePath == null) { // 检查路径
// if (progressMonitor != null)
// progressMonitor.setCanceled(true);
// return;
// }
//
// IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
// IFile file = root.getFile(filePath);
// newInput = new FileEditorInput(file);
// }
saveAs(newInput, oldFile, progressMonitor);
}
use of org.eclipse.ui.IEditorInput in project translationstudio8 by heartsome.
the class XLIFFEditorImplWithNatTable method updateStatusLine.
/**
* 在状态栏上显示被编辑文件的信息。
*/
public void updateStatusLine() {
if (table == null || table.isDisposed()) {
return;
}
SelectionLayer selectionLayer = bodyLayer.getSelectionLayer();
ViewportLayer viewportLayer = bodyLayer.getViewportLayer();
PositionCoordinate cellPosition = selectionLayer.getLastSelectedCellPosition();
if (cellPosition == null) {
return;
}
// int order = LayerUtil.convertRowPosition(selectionLayer, cellPosition.rowPosition, viewportLayer);
// Bug #2317:选中文本段后排序,不会刷新状态栏中的序号
int[] selectedRowPostions = selectionLayer.getFullySelectedRowPositions();
if (selectedRowPostions.length <= 0) {
return;
}
// 刷新选中行的术语,使其排序后保持高亮显示
// if (!isHorizontalLayout()) {
// int rowPosition = selectedRowPostions[0];
// rowPosition *= VerticalNatTableConfig.ROW_SPAN;
// cellPosition.set(rowPosition, cellPosition.getColumnPosition());
// } else {
// cellPosition.set(selectedRowPostions[0], cellPosition.getColumnPosition());
// }
// if (!FindReplaceDialog.isOpen) {
// CellRegion cellRegion = new CellRegion(cellPosition, new Region(0, selectionLayer.getWidth()));
// ActiveCellRegion.setActiveCellRegion(cellRegion);
// }
int order = LayerUtil.convertRowPosition(selectionLayer, selectedRowPostions[0], viewportLayer);
order += viewportLayer.getOriginRowPosition() + 1;
// 垂直布局时order需要进行两行递增的处理
if (!isHorizontalLayout) {
order = (int) Math.ceil(order / 2.0);
}
MessageFormat messageFormat = null;
if (order > 0) {
/* 一个Xliff文件,可能有多个File节点,这里使用File结点的original属性 */
/* 当前文件:{0} | 顺序号:{1} | 可见文本段数:{2} | 文本段总数:{3} | 当前用户名" */
messageFormat = new MessageFormat(Messages.getString("editor.XLIFFEditorImplWithNatTable.messageFormat1"));
} else {
messageFormat = new MessageFormat(Messages.getString("editor.XLIFFEditorImplWithNatTable.messageFormat2"));
}
String fileName = "";
// 添加 Project Name
IEditorInput editorInput = getEditorInput();
String filePath = "";
if (isMultiFile()) {
if (getSelectedRowIds().size() > 0) {
filePath = RowIdUtil.getFileNameByRowId(getSelectedRowIds().get(0));
fileName = ResourceUtils.toWorkspacePath(filePath);
}
} else {
fileName = getEditorInput().getName();
if (editorInput instanceof FileEditorInput) {
FileEditorInput fileEditorInput = (FileEditorInput) editorInput;
filePath = fileEditorInput.getFile().getLocation().toOSString();
fileName = fileEditorInput.getFile().getFullPath().toOSString();
}
}
String systemUser = Activator.getDefault().getPreferenceStore().getString(IPreferenceConstants.SYSTEM_USER);
int editableTuSum = handler.countEditableTransUnit();
int tuSum = handler.countTransUnit();
// int translatedSum1 = handler
// .getNodeCount(filePath,
// "/xliff/file/body/trans-unit[@approved = 'yes' and target/@state != 'translated' and target/@state != 'signed-off']");
// int translatedSum2 = handler.getNodeCount(filePath,
// "/xliff/file/body/trans-unit/target[@state = 'translated' or @state = 'signed-off']");
// int approveSum1 = handler.getNodeCount(filePath,
// "/xliff/file/body/trans-unit[not(@approved='yes') and target/@state='signed-off']");
// int approveSum2 = handler.getNodeCount(filePath, "/xliff/file/body/trans-unit[@approved = 'yes']");
int translatedSum = handler.getTranslatedCount();
int approveedSum = handler.getApprovedCount();
int approveP = (int) Math.floor(approveedSum / (double) tuSum * 100.00);
int translatedP = (int) Math.floor(translatedSum / (double) tuSum * 100.00);
translationItem.setProgressValue(translatedP);
approveItem.setProgressValue(approveP);
// 将信息显示在状态栏
String message = messageFormat.format(new String[] { fileName, String.valueOf(order), String.valueOf(editableTuSum), String.valueOf(tuSum), systemUser });
statusLineManager.setMessage(statusLineImage, message);
}
use of org.eclipse.ui.IEditorInput in project translationstudio8 by heartsome.
the class ExportAsHtmlHandler method execute.
public Object execute(ExecutionEvent event) throws ExecutionException {
String elementName = event.getParameter("elementName");
IEditorPart activeEditor = HandlerUtil.getActiveEditor(event);
Shell shell = activeEditor.getEditorSite().getShell();
if (activeEditor == null || !(activeEditor instanceof XLIFFEditorImplWithNatTable)) {
return null;
}
XLIFFEditorImplWithNatTable xliffEditor = (XLIFFEditorImplWithNatTable) activeEditor;
if (xliffEditor.isMultiFile()) {
MessageDialog.openInformation(shell, "", "当前编辑器打开了多个文件,无法执行该操作。");
}
IEditorInput input = xliffEditor.getEditorInput();
URI uri = null;
if (input instanceof FileEditorInput) {
uri = ((FileEditorInput) input).getURI();
} else if (input instanceof FileStoreEditorInput) {
uri = ((FileStoreEditorInput) input).getURI();
} else {
return null;
}
File xliff = new File(uri);
FileDialog fd = new FileDialog(shell, SWT.SAVE);
String[] names = { "HTML Files [*.html]", "All Files [*.*]" };
//$NON-NLS-1$ //$NON-NLS-2$
String[] extensions = { "*.html", "*.*" };
fd.setFilterExtensions(extensions);
fd.setFilterNames(names);
//$NON-NLS-1$
fd.setFileName(xliff.getName() + ".html");
String out = fd.open();
if (out == null) {
return null;
}
XLFHandler handler = xliffEditor.getXLFHandler();
boolean result = handler.saveAsHtml(xliff.getAbsolutePath(), out, elementName);
if (result) {
IWorkbenchPage page = xliffEditor.getEditorSite().getPage();
OpenEditorUtil.OpenFileWithSystemEditor(page, out);
} else {
MessageDialog.openInformation(shell, "", "文件 “" + out + "” 保存失败!请重试。");
}
return null;
}
Aggregations