Search in sources :

Example 31 with FileEditorInput

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

the class LockRepeatedSegmentHandler method lockTMSegment.

private LockTMSegment lockTMSegment(final List<IFile> iFileList, boolean isLockTM100Segment, boolean isLockTM101Segment, IProgressMonitor monitor) {
    SubProgressMonitor subMonitor = new SubProgressMonitor(monitor, iFileList.size(), SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
    subMonitor.beginTask("", 10);
    subMonitor.setTaskName(Messages.getString("translation.LockRepeatedSegmentHandler.task2"));
    XLFHandler xlfHandler = null;
    singleNattable = null;
    Display.getDefault().syncExec(new Runnable() {

        public void run() {
            IEditorReference[] editorRefer = window.getActivePage().findEditors(new FileEditorInput(iFileList.get(0)), XLIFF_EDITOR_ID, IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID);
            if (editorRefer.length > 0) {
                singleNattable = ((XLIFFEditorImplWithNatTable) editorRefer[0].getEditor(true));
            }
        }
    });
    if (singleNattable != null) {
        xlfHandler = singleNattable.getXLFHandler();
    }
    if (xlfHandler == null) {
        xlfHandler = new XLFHandler();
        for (final IFile iFile : iFileList) {
            File file = iFile.getLocation().toFile();
            try {
                Map<String, Object> resultMap = xlfHandler.openFile(file);
                if (resultMap == null || Constant.RETURNVALUE_RESULT_SUCCESSFUL != (Integer) resultMap.get(Constant.RETURNVALUE_RESULT)) {
                    // 打开文件失败。
                    Display.getDefault().syncExec(new Runnable() {

                        public void run() {
                            MessageDialog.openInformation(shell, Messages.getString("translation.LockRepeatedSegmentHandler.msgTitle"), MessageFormat.format(Messages.getString("translation.LockRepeatedSegmentHandler.msg2"), iFile.getLocation().toOSString()));
                        }
                    });
                    list.remove(iFile);
                    return null;
                }
            } catch (Exception e) {
                LOGGER.error("", e);
                e.printStackTrace();
            }
            if (!monitorWork(monitor, 1)) {
                return null;
            }
        }
    } else {
        subMonitor.worked(1);
    }
    List<String> filesPath = ResourceUtils.IFilesToOsPath(iFileList);
    LockTMSegment lts = new LockTMSegment(xlfHandler, tmMatcher, filesPath, curProject);
    lts.setLockedContextMatch(isLockTM101Segment);
    lts.setLockedFullMatch(isLockTM100Segment);
    // 查记忆库并锁定,占剩下的 9/10。
    SubProgressMonitor subSubMonitor = new SubProgressMonitor(subMonitor, 9, SubProgressMonitor.PREPEND_MAIN_LABEL_TO_SUBTASK);
    if (!lts.executeTranslation(subSubMonitor)) {
        isCancel = true;
        subSubMonitor.done();
        subMonitor.done();
        return null;
    }
    subSubMonitor.done();
    subMonitor.done();
    if (singleNattable != null) {
        Display.getDefault().syncExec(new Runnable() {

            public void run() {
                singleNattable.getTable().redraw();
            }
        });
    }
    Map<String, List<String>> needLockRowIdMap = lts.getNeedLockRowIdMap();
    if (needLockRowIdMap.size() > 0) {
        lockTU(xlfHandler, needLockRowIdMap);
    }
    return lts;
}
Also used : IFile(org.eclipse.core.resources.IFile) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) ExecutionException(org.eclipse.core.commands.ExecutionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ArrayList(java.util.ArrayList) List(java.util.List) IFile(org.eclipse.core.resources.IFile) File(java.io.File) XLFHandler(net.heartsome.cat.ts.core.file.XLFHandler)

Example 32 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput 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);
                }
            }
        });
    }
}
Also used : Entry(java.util.Map.Entry) XLIFFEditorImplWithNatTable(net.heartsome.cat.ts.ui.xliffeditor.nattable.editor.XLIFFEditorImplWithNatTable) IEditorReference(org.eclipse.ui.IEditorReference) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ArrayList(java.util.ArrayList) List(java.util.List) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IEditorInput(org.eclipse.ui.IEditorInput)

Example 33 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project Malai by arnobl.

the class ActionModelWizard method performFinish.

/**
 * Do the work after everything is specified.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public boolean performFinish() {
    try {
        // Remember the file.
        // 
        final IFile modelFile = getModelFile();
        // Do the work within an operation.
        // 
        WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {

            @Override
            protected void execute(IProgressMonitor progressMonitor) {
                try {
                    // Create a resource set
                    // 
                    ResourceSet resourceSet = new ResourceSetImpl();
                    // Get the URI of the model file.
                    // 
                    URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true);
                    // Create a resource for this file.
                    // 
                    Resource resource = resourceSet.createResource(fileURI);
                    // Add the initial model object to the contents.
                    // 
                    EObject rootObject = createInitialModel();
                    if (rootObject != null) {
                        resource.getContents().add(rootObject);
                    }
                    // Save the contents of the resource to the file system.
                    // 
                    Map<Object, Object> options = new HashMap<Object, Object>();
                    options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
                    resource.save(options);
                } catch (Exception exception) {
                    ActionEditorPlugin.INSTANCE.log(exception);
                } finally {
                    progressMonitor.done();
                }
            }
        };
        getContainer().run(false, false, operation);
        // Select the new file resource in the current view.
        // 
        IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
        IWorkbenchPage page = workbenchWindow.getActivePage();
        final IWorkbenchPart activePart = page.getActivePart();
        if (activePart instanceof ISetSelectionTarget) {
            final ISelection targetSelection = new StructuredSelection(modelFile);
            getShell().getDisplay().asyncExec(new Runnable() {

                public void run() {
                    ((ISetSelectionTarget) activePart).selectReveal(targetSelection);
                }
            });
        }
        // 
        try {
            page.openEditor(new FileEditorInput(modelFile), workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
        } catch (PartInitException exception) {
            MessageDialog.openError(workbenchWindow.getShell(), ActionEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
            return false;
        }
        return true;
    } catch (Exception exception) {
        ActionEditorPlugin.INSTANCE.log(exception);
        return false;
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) HashMap(java.util.HashMap) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) IResource(org.eclipse.core.resources.IResource) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) PartInitException(org.eclipse.ui.PartInitException) MissingResourceException(java.util.MissingResourceException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) EObject(org.eclipse.emf.ecore.EObject) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ISelection(org.eclipse.jface.viewers.ISelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ISetSelectionTarget(org.eclipse.ui.part.ISetSelectionTarget) EObject(org.eclipse.emf.ecore.EObject) PartInitException(org.eclipse.ui.PartInitException)

Example 34 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project Malai by arnobl.

the class EventModelWizard method performFinish.

/**
 * Do the work after everything is specified.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public boolean performFinish() {
    try {
        // Remember the file.
        // 
        final IFile modelFile = getModelFile();
        // Do the work within an operation.
        // 
        WorkspaceModifyOperation operation = new WorkspaceModifyOperation() {

            @Override
            protected void execute(IProgressMonitor progressMonitor) {
                try {
                    // Create a resource set
                    // 
                    ResourceSet resourceSet = new ResourceSetImpl();
                    // Get the URI of the model file.
                    // 
                    URI fileURI = URI.createPlatformResourceURI(modelFile.getFullPath().toString(), true);
                    // Create a resource for this file.
                    // 
                    Resource resource = resourceSet.createResource(fileURI);
                    // Add the initial model object to the contents.
                    // 
                    EObject rootObject = createInitialModel();
                    if (rootObject != null) {
                        resource.getContents().add(rootObject);
                    }
                    // Save the contents of the resource to the file system.
                    // 
                    Map<Object, Object> options = new HashMap<Object, Object>();
                    options.put(XMLResource.OPTION_ENCODING, initialObjectCreationPage.getEncoding());
                    resource.save(options);
                } catch (Exception exception) {
                    MalaiEventEditorPlugin.INSTANCE.log(exception);
                } finally {
                    progressMonitor.done();
                }
            }
        };
        getContainer().run(false, false, operation);
        // Select the new file resource in the current view.
        // 
        IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
        IWorkbenchPage page = workbenchWindow.getActivePage();
        final IWorkbenchPart activePart = page.getActivePart();
        if (activePart instanceof ISetSelectionTarget) {
            final ISelection targetSelection = new StructuredSelection(modelFile);
            getShell().getDisplay().asyncExec(new Runnable() {

                public void run() {
                    ((ISetSelectionTarget) activePart).selectReveal(targetSelection);
                }
            });
        }
        // 
        try {
            page.openEditor(new FileEditorInput(modelFile), workbench.getEditorRegistry().getDefaultEditor(modelFile.getFullPath().toString()).getId());
        } catch (PartInitException exception) {
            MessageDialog.openError(workbenchWindow.getShell(), MalaiEventEditorPlugin.INSTANCE.getString("_UI_OpenEditorError_label"), exception.getMessage());
            return false;
        }
        return true;
    } catch (Exception exception) {
        MalaiEventEditorPlugin.INSTANCE.log(exception);
        return false;
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) IFile(org.eclipse.core.resources.IFile) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) HashMap(java.util.HashMap) WorkspaceModifyOperation(org.eclipse.ui.actions.WorkspaceModifyOperation) Resource(org.eclipse.emf.ecore.resource.Resource) XMLResource(org.eclipse.emf.ecore.xmi.XMLResource) IResource(org.eclipse.core.resources.IResource) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) PartInitException(org.eclipse.ui.PartInitException) MissingResourceException(java.util.MissingResourceException) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IWorkbenchPart(org.eclipse.ui.IWorkbenchPart) EObject(org.eclipse.emf.ecore.EObject) FileEditorInput(org.eclipse.ui.part.FileEditorInput) ISelection(org.eclipse.jface.viewers.ISelection) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) ISetSelectionTarget(org.eclipse.ui.part.ISetSelectionTarget) EObject(org.eclipse.emf.ecore.EObject) PartInitException(org.eclipse.ui.PartInitException)

Example 35 with FileEditorInput

use of org.eclipse.ui.part.FileEditorInput in project Malai by arnobl.

the class InteractionEditor method doSaveAs.

/**
 * This also changes the editor's input.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void doSaveAs() {
    SaveAsDialog saveAsDialog = new SaveAsDialog(getSite().getShell());
    saveAsDialog.open();
    IPath path = saveAsDialog.getResult();
    if (path != null) {
        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
        if (file != null) {
            doSaveAs(URI.createPlatformResourceURI(file.getFullPath().toString(), true), new FileEditorInput(file));
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) SaveAsDialog(org.eclipse.ui.dialogs.SaveAsDialog) FileEditorInput(org.eclipse.ui.part.FileEditorInput)

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