Search in sources :

Example 11 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput 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);
}
Also used : IURIEditorInput(org.eclipse.ui.IURIEditorInput) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) URI(java.net.URI) Shell(org.eclipse.swt.widgets.Shell) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) CoreException(org.eclipse.core.runtime.CoreException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IFileStore(org.eclipse.core.filesystem.IFileStore) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) IFile(org.eclipse.core.resources.IFile) File(java.io.File) FileDialog(org.eclipse.swt.widgets.FileDialog) IEditorInput(org.eclipse.ui.IEditorInput) IURIEditorInput(org.eclipse.ui.IURIEditorInput) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput)

Example 12 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput 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);
}
Also used : MessageFormat(java.text.MessageFormat) SelectionLayer(net.sourceforge.nattable.selection.SelectionLayer) FileEditorInput(org.eclipse.ui.part.FileEditorInput) PositionCoordinate(net.sourceforge.nattable.coordinate.PositionCoordinate) HorizontalViewportLayer(net.heartsome.cat.ts.ui.xliffeditor.nattable.layer.HorizontalViewportLayer) ViewportLayer(net.sourceforge.nattable.viewport.ViewportLayer) VerticalViewportLayer(net.heartsome.cat.ts.ui.xliffeditor.nattable.layer.VerticalViewportLayer) Point(org.eclipse.swt.graphics.Point) IEditorInput(org.eclipse.ui.IEditorInput) IURIEditorInput(org.eclipse.ui.IURIEditorInput)

Example 13 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput 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;
}
Also used : XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) IEditorPart(org.eclipse.ui.IEditorPart) URI(java.net.URI) Shell(org.eclipse.swt.widgets.Shell) FileEditorInput(org.eclipse.ui.part.FileEditorInput) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) File(java.io.File) FileDialog(org.eclipse.swt.widgets.FileDialog) IEditorInput(org.eclipse.ui.IEditorInput) FileStoreEditorInput(org.eclipse.ui.ide.FileStoreEditorInput) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 14 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project translationstudio8 by heartsome.

the class MultiFilesOper method getOpenedIfile.

/**
	 * 获取所选择的要合并打开的文件中已经被打开的文件
	 * @return
	 */
public List<IFile> getOpenedIfile() {
    IEditorReference[] editorRes = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
    List<IFile> isOpenedXlfList = new ArrayList<IFile>();
    IXliffEditor xlfEditor;
    try {
        for (int i = 0; i < editorRes.length; i++) {
            if (editorRes[i].getId().equals(XLIFF_EDITOR_ID)) {
                IFile iFile = ((FileEditorInput) editorRes[i].getEditorInput()).getFile();
                //合并打开的情况
                if ("xlp".equals(iFile.getFileExtension())) {
                    xlfEditor = (IXliffEditor) editorRes[i].getEditor(true);
                    isOpenedXlfList.addAll(ResourceUtils.filesToIFiles(xlfEditor.getMultiFileList()));
                } else {
                    try {
                        isOpenedXlfList.add(((FileEditorInput) editorRes[i].getEditorInput()).getFile());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    return isOpenedXlfList;
}
Also used : IEditorReference(org.eclipse.ui.IEditorReference) IFile(org.eclipse.core.resources.IFile) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ArrayList(java.util.ArrayList) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException)

Example 15 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project translationstudio8 by heartsome.

the class MultiFilesOper method hasOpenedIFile.

/**
	 * 验证当前要合并打开的文件是否有文件存在已经打开的情况,并从当前要合并打开的文件中删除已经打开的文件
	 * <div style='color:red'>这个方法一是验证 所选要打开的文件 中是否已经有被打开了的,第二是,会删除已经打开的文件,因此慎用。<br>
	 *  getOpenedIfile 方法也有验证所选择文件是否有打开的情况</div>
	 * @return
	 */
public boolean hasOpenedIFile() {
    IEditorReference[] editorRes = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getEditorReferences();
    List<IFile> isOpenedXlfList = new ArrayList<IFile>();
    IXliffEditor xlfEditor;
    boolean hasOpenedIFile = false;
    try {
        for (int i = 0; i < editorRes.length; i++) {
            if (editorRes[i].getId().equals(XLIFF_EDITOR_ID)) {
                IFile iFile = ((FileEditorInput) editorRes[i].getEditorInput()).getFile();
                //合并打开的情况
                if ("xlp".equals(iFile.getFileExtension())) {
                    System.out.println("---------------");
                    xlfEditor = (IXliffEditor) editorRes[i].getEditor(true);
                    isOpenedXlfList.addAll(ResourceUtils.filesToIFiles(xlfEditor.getMultiFileList()));
                } else {
                    try {
                        isOpenedXlfList.add(((FileEditorInput) editorRes[i].getEditorInput()).getFile());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        if (isOpenedXlfList.size() > 0) {
            for (IFile iFile : isOpenedXlfList) {
                if (selectIFiles.indexOf(iFile) >= 0) {
                    hasOpenedIFile = true;
                    selectIFiles.remove(iFile);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.error("", e);
    }
    return hasOpenedIFile;
}
Also used : IEditorReference(org.eclipse.ui.IEditorReference) IFile(org.eclipse.core.resources.IFile) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ArrayList(java.util.ArrayList) IXliffEditor(net.heartsome.cat.ts.ui.editors.IXliffEditor) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

FileEditorInput (org.eclipse.ui.part.FileEditorInput)262 IFile (org.eclipse.core.resources.IFile)187 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)99 PartInitException (org.eclipse.ui.PartInitException)96 IEditorPart (org.eclipse.ui.IEditorPart)64 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)58 IPath (org.eclipse.core.runtime.IPath)57 IEditorInput (org.eclipse.ui.IEditorInput)55 CoreException (org.eclipse.core.runtime.CoreException)51 IResource (org.eclipse.core.resources.IResource)39 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)38 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)37 StructuredSelection (org.eclipse.jface.viewers.StructuredSelection)35 SaveAsDialog (org.eclipse.ui.dialogs.SaveAsDialog)35 EObject (org.eclipse.emf.ecore.EObject)33 IWorkbenchPart (org.eclipse.ui.IWorkbenchPart)33 HashMap (java.util.HashMap)32 Resource (org.eclipse.emf.ecore.resource.Resource)32 ISelection (org.eclipse.jface.viewers.ISelection)32 URI (org.eclipse.emf.common.util.URI)31