@Override public void init(IEditorSite site, IEditorInput input) throws PartInitException { setSite(site); setInput(input); setPartName(input.getName()); // The contract of init() mentions we need to fail if we can't // understand the input. if (input instanceof FileEditorInput) { // We try to open a file that is part of the current workspace mFileEditorInput = (FileEditorInput) input; mFileName = mFileEditorInput.getName(); mProject = mFileEditorInput.getFile().getProject(); } else { throw new PartInitException( "Input is not of type FileEditorInput " + //$NON-NLS-1$ "nor FileStoreEditorInput: " + //$NON-NLS-1$ input == null ? "null" : input.toString()); // $NON-NLS-1$ } }
@Override public void elementDirtyStateChanged(final Object file, final boolean dirty) { FileEditorInput editorInputValue = editorInput.getValue(); if (editorInputValue != null && editorInputValue.equals(file)) { fileLabel.refresh(); } }
private void saveOrDiscardIfNeeded(FileEditorInput file) { TextFileDocumentProvider docProvider = file == null ? null : getTextDocumentProvider(file); if (docProvider != null && file != null && file.exists() && inputConnected) { if (docProvider.canSaveDocument(file) && ui.confirmOperation( "Changes Detected", "Manifest file '" + file.getFile().getFullPath().toOSString() + "' has been changed. Do you want to save changes or discard them?", new String[] {"Save", "Discard"}, 0) == 0) { try { docProvider.saveDocument( new NullProgressMonitor(), file, docProvider.getDocument(file), true); } catch (CoreException e) { Log.log(e); ui.errorPopup("Failed Saving File", ExceptionUtil.getMessage(e)); } } else { try { docProvider.resetDocument(file); } catch (CoreException e) { Log.log(e); } } disconnect(docProvider, file); } }
protected void performSaveAs(IProgressMonitor progressMonitor) { FileEditorInput input = (FileEditorInput) getEditorInput(); String path = input.getFile().getFullPath().toString(); ResourceSet resourceSet = getResourceSet(); URI platformURI = URI.createPlatformResourceURI(path, true); Resource oldFile = resourceSet.getResource(platformURI, true); super.performSaveAs(progressMonitor); // load and resave - input has been changed to new path by super FileEditorInput newInput = (FileEditorInput) getEditorInput(); String newPath = newInput.getFile().getFullPath().toString(); URI newPlatformURI = URI.createPlatformResourceURI(newPath, true); Resource newFile = resourceSet.createResource(newPlatformURI); // if the extension is the same, saving was already performed by super by saving // the plain text if (platformURI.fileExtension().equals(newPlatformURI.fileExtension())) { oldFile.unload(); // save code folding state, is it possible with a new name codeFoldingManager.saveCodeFoldingStateFile(getResource().getURI().toString()); } else { newFile.getContents().clear(); newFile.getContents().addAll(oldFile.getContents()); try { oldFile.unload(); if (newFile.getErrors().isEmpty()) { newFile.save(null); } } catch (Exception e) { e.printStackTrace(); } } }
@Override protected String computeLayoutFileName(IEditorInput editorInput) throws CoreException, IOException { String fileName = null; if (editorInput instanceof FileEditorInput) { FileEditorInput fileEditorInput = (FileEditorInput) editorInput; IFile ifile = fileEditorInput.getFile(); fileName = ifile.getName(); } else if (editorInput instanceof FileStoreEditorInput) { FileStoreEditorInput fileStoreInput = (FileStoreEditorInput) editorInput; IFileStore store = EFS.getStore(fileStoreInput.getURI()); File localFile = store.toLocalFile(EFS.NONE, null); // if no local file is available, obtain a cached file if (localFile == null) localFile = store.toLocalFile(EFS.CACHE, null); if (localFile == null) throw new IllegalArgumentException(); fileName = localFile.getName(); } if (fileName != null && fileName.endsWith(".xml")) { fileName = fileName.substring(0, fileName.indexOf(".xml")); } if (fileName != null) { fileName += ".layout"; } return fileName; }
public boolean matches(IEditorReference editorRef, IEditorInput input) { // first check that the file being opened is a layout file. if (input instanceof FileEditorInput) { FileEditorInput fileInput = (FileEditorInput) input; // get the IFile object and check it's in one of the layout folders. IFile iFile = fileInput.getFile(); ResourceFolder resFolder = ResourceManager.getInstance().getResourceFolder(iFile); // if it's a layout, we know check the name of the fileInput against the name of the // file being currently edited by the editor since those are independent of the config. if (resFolder != null && resFolder.getType() == ResourceFolderType.LAYOUT) { try { IEditorInput editorInput = editorRef.getEditorInput(); if (editorInput instanceof FileEditorInput) { FileEditorInput editorFileInput = (FileEditorInput) editorInput; IFile editorIFile = editorFileInput.getFile(); return editorIFile.getProject().equals(iFile.getProject()) && editorIFile.getName().equals(iFile.getName()); } } catch (PartInitException e) { // we do nothing, we'll just return false. } } } return false; }
private IProject guessProject(IStructuredSelection selection) { if (selection == null) { return null; } for (Object element : selection.toList()) { if (element instanceof IAdaptable) { IResource res = (IResource) ((IAdaptable) element).getAdapter(IResource.class); IProject project = res != null ? res.getProject() : null; // Is this an Android project? try { if (project == null || !project.hasNature(AdtConstants.NATURE_DEFAULT)) { continue; } } catch (CoreException e) { // checking the nature failed, ignore this resource continue; } return project; } else if (element instanceof Pair<?, ?>) { // Pair of Project/String @SuppressWarnings("unchecked") Pair<IProject, String> pair = (Pair<IProject, String>) element; return pair.getFirst(); } } // Try to figure out the project from the active editor IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); if (window != null) { IWorkbenchPage page = window.getActivePage(); if (page != null) { IEditorPart activeEditor = page.getActiveEditor(); if (activeEditor instanceof AndroidXmlEditor) { Object input = ((AndroidXmlEditor) activeEditor).getEditorInput(); if (input instanceof FileEditorInput) { FileEditorInput fileInput = (FileEditorInput) input; return fileInput.getFile().getProject(); } } } } IJavaProject[] projects = BaseProjectHelper.getAndroidProjects( new IProjectFilter() { public boolean accept(IProject project) { return project.isAccessible(); } }); if (projects != null && projects.length == 1) { return projects[0].getProject(); } return null; }
public static IFile getDataFileForInput(final IEditorInput input) { final IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); if (input instanceof ToscaDiagramEditorInput) { final ToscaDiagramEditorInput tdei = (ToscaDiagramEditorInput) input; return tdei.getToscaFile(); } else if (input instanceof DiagramEditorInput) { final DiagramEditorInput dei = (DiagramEditorInput) input; IPath path = new Path(dei.getUri().trimFragment().toPlatformString(true)); return recreateDataFile(path); } else if (input instanceof FileEditorInput) { final FileEditorInput fei = (FileEditorInput) input; return fei.getFile(); } else if (input instanceof IURIEditorInput) { // opened externally to Eclipse final IURIEditorInput uei = (IURIEditorInput) input; final java.net.URI uri = uei.getURI(); final String path = uri.getPath(); try { final IProject importProject = root.getProject("import"); // $NON-NLS-1$ if (!importProject.exists()) { importProject.create(null); } importProject.open(null); final InputStream is = new FileInputStream(path); final String fileName; if (path.contains("/")) { // $NON-NLS-1$ fileName = path.substring(path.lastIndexOf("/") + 1); // $NON-NLS-1$ } else { fileName = path.substring(path.lastIndexOf("\\") + 1); // $NON-NLS-1$ } IFile importFile = importProject.getFile(fileName); if (importFile.exists()) { importFile.delete(true, null); } importFile.create(is, true, null); return importProject.getFile(fileName); } catch (CoreException exception) { exception.printStackTrace(); } catch (FileNotFoundException exception) { exception.printStackTrace(); } } return null; }
/** * Test method for {@link * org.talend.dataprofiler.core.CorePlugin#itemIsOpening(org.talend.core.model.properties.Item, * boolean)}. */ @Test public void testItemIsOpeningItemBoolean() { try { CorePlugin cpMock = mock(CorePlugin.class); PowerMockito.mockStatic(CorePlugin.class); when(CorePlugin.getDefault()).thenReturn(cpMock); IWorkbench workbenchMock = mock(IWorkbench.class); when(cpMock.getWorkbench()).thenReturn(workbenchMock); IWorkbenchWindow workbenchWindowMock = mock(IWorkbenchWindow.class); when(workbenchMock.getActiveWorkbenchWindow()).thenReturn(workbenchWindowMock); IWorkbenchPage workbenchPageMock = mock(IWorkbenchPage.class); when(workbenchWindowMock.getActivePage()).thenReturn(workbenchPageMock); IEditorReference editorRefMock = mock(IEditorReference.class); IEditorReference[] editorRefMocks = new IEditorReference[] {editorRefMock}; when(workbenchPageMock.getEditorReferences()).thenReturn(editorRefMocks); FileEditorInput fileEditorInputMock = mock(FileEditorInput.class); when(editorRefMock.getEditorInput()).thenReturn(fileEditorInputMock); String path1 = "/abc1"; // $NON-NLS-1$ String path2 = "/abc2"; // $NON-NLS-1$ IFile inputFileMock = mock(IFile.class); when(fileEditorInputMock.getFile()).thenReturn(inputFileMock); IPath inputFilePathMock = mock(IPath.class); when(inputFileMock.getFullPath()).thenReturn(inputFilePathMock); when(inputFilePathMock.toString()).thenReturn(path1); Item itemMock = mock(Item.class); Property propertyMock = mock(Property.class); when(itemMock.getProperty()).thenReturn(propertyMock); Resource resourceMock = mock(Resource.class); when(propertyMock.eResource()).thenReturn(resourceMock); IPath ipathMock = mock(IPath.class); PowerMockito.mockStatic(PropertyHelper.class); when(PropertyHelper.getItemPath(propertyMock)).thenReturn(ipathMock); when(ipathMock.toString()).thenReturn(path2); CorePlugin cp = new CorePlugin(); assertFalse(cp.itemIsOpening(itemMock, false)); } catch (PartInitException e) { fail(e.getMessage()); } }
protected Property getProperty() { Property property = null; if (getEditorInput() instanceof AbstractItemEditorInput) { AbstractItemEditorInput editorInput = (AbstractItemEditorInput) getEditorInput(); if (editorInput.getItem() != null) { property = editorInput.getItem().getProperty(); } } else if (getEditorInput() instanceof FileEditorInput) { FileEditorInput editorInput = (FileEditorInput) getEditorInput(); property = PropertyHelper.getProperty(editorInput.getFile()); } return property; }
@Override public void launch(IEditorPart editor, final String mode) { Activator.logInfo("launch shortcut: editor" + editor.getTitle() + " mode " + mode); IEditorInput ei = editor.getEditorInput(); if (ei != null && ei instanceof FileEditorInput) { FileEditorInput fei = (FileEditorInput) ei; IFile file = fei.getFile(); final String fName = file.getProjectRelativePath().toOSString(); final String pName = file.getProject().getName(); launch(pName, fName, mode); } }
/* * (non-Javadoc) * * @seeorg.eclipse.ui.navigator.ILinkHelper#activateEditor(org.eclipse.ui. * IWorkbenchPage, org.eclipse.jface.viewers.IStructuredSelection) */ @Override public void activateEditor(IWorkbenchPage aPage, IStructuredSelection aSelection) { // System.out.println("activateEditor: "+isDragActive); if (aSelection == null || aSelection.isEmpty()) { return; } Object obj = aSelection.getFirstElement(); if (obj instanceof IOfsModelResource) { IOfsModelResource ofsResource = (IOfsModelResource) obj; URI uri = ofsResource.getURI(); String extension = uri.fileExtension(); if (extension != null && StringUtils.contains(PageConstants.PAGE_DESIGNER_FILE_EXTENSIONS, extension, true)) { // check if an editor is alread open for the given uri. String filename = uri.lastSegment(); String id = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(filename).getId(); for (IEditorReference editor : aPage.getEditorReferences()) { if (editor.getId().equals(id)) { try { IEditorInput eInput = editor.getEditorInput(); if (eInput instanceof FileEditorInput) { FileEditorInput fInput = (FileEditorInput) eInput; if (filename.equals(fInput.getName())) { if (isDragActive) { // an editor is already open, simply return // to avoid the activation of this editor. isDragActive = false; return; } } } } catch (PartInitException ex) { // silently ignore } } } } } // editor not found super.activateEditor(aPage, aSelection); }
/** * Initializes the combo containing all feature projects.<br> * Selects the feature project corresponding to the selected resource. */ private void initComboProject() { final Object obj = selection.getFirstElement(); // TODO: Check for projects in other languages than java if (obj instanceof JavaElement) { IProject project = ((JavaElement) obj).getJavaProject().getProject(); featureProject = CorePlugin.getFeatureProject(project); if (featureProject != null) { comboProject.setText(featureProject.getProjectName()); checkcontainer(featureProject, project); } } else if (obj instanceof IResource) { IResource resource = (IResource) obj; featureProject = CorePlugin.getFeatureProject(resource); if (featureProject != null) { comboProject.setText(featureProject.getProjectName()); checkcontainer(featureProject, resource); } } else if (featureProject == null) { IWorkbenchWindow editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); IEditorPart part = null; if (editor != null) { IWorkbenchPage page = editor.getActivePage(); if (page != null) { part = page.getActiveEditor(); if (part != null) { FileEditorInput inputFile = (FileEditorInput) part.getEditorInput(); featureProject = CorePlugin.getFeatureProject(inputFile.getFile()); if (featureProject != null) { IResource res = ResourcesPlugin.getWorkspace() .getRoot() .findMember(featureProject.getProjectName()); checkcontainer(featureProject, res); } } } } if (featureProject != null) { comboProject.setText(featureProject.getProjectName()); } } comboProjectText = comboProject.getText(); }
/** * PDE editors should call this when they are closing down. * * @param editor the pde editor to disconnect from */ public static void disconnect(PDEFormEditor editor) { IProject project = editor.getCommonProject(); if (project == null) { // getCommonProject will return null when project is deleted with editor open - bug 226788 // Solution is to use editor input if it is a FileEditorInput. IEditorInput input = editor.getEditorInput(); if (input != null && input instanceof FileEditorInput) { FileEditorInput fei = (FileEditorInput) input; IFile file = fei.getFile(); project = file.getProject(); } } if (project == null) return; if (!fOpenPDEEditors.containsKey(project)) return; ArrayList list = (ArrayList) fOpenPDEEditors.get(project); list.remove(editor); if (list.size() == 0) fOpenPDEEditors.remove(project); }
public void setInput(FileEditorInput input) { if (input != null) { m_compilationUnit = Workspace.getInstance().getCompilationUnit(input.getFile()); } else { m_compilationUnit = null; } update(); }
@Override protected FileEditorInput compute() { IFile file = getFile(); FileEditorInput currentInput = getValue(); boolean changed = currentInput == null || !currentInput.getFile().equals(file); if (changed) { saveOrDiscardIfNeeded(currentInput); if (file != null) { FileEditorInput input = new FileEditorInput(file); // Connect input to doc provider here when editor input has changed TextFileDocumentProvider provider = getTextDocumentProvider(input); if (provider != null) { connect(provider, input); } return input; } } return null; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof IFileEditorInput)) { return false; } return innerEidtorInput.equals(obj); }
protected void applyChanges() { try { m_ymlFile.save(); IEditorInput editorInput = getEditorInput(); if (!(editorInput instanceof IFileEditorInput)) return; FileEditorInput fileEditor = (FileEditorInput) editorInput; m_editor.updatePartControl(fileEditor); fileEditor.getFile().getProject().refreshLocal(IResource.DEPTH_INFINITE, null); } catch (CoreException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
private void disconnect(TextFileDocumentProvider provider, FileEditorInput input) { if (inputConnected) { provider.disconnect(input); inputConnected = false; } else { throw new IllegalStateException( "Attempting to disconnect input '" + input.getFile().getName() + "' while no inputs are connected"); } }
public boolean equals(Object o) { getXModelObject(); if (this == o) return true; if (o instanceof FileEditorInput) { if (getFile() == null) return false; FileEditorInput other = (FileEditorInput) o; IFile f1 = getFile(); IFile f2 = other.getFile(); if (f1 == null || f2 == null) return f1 == f2; if (f1.equals(f2)) return true; IPath loc1 = f1.getLocation(); IPath loc2 = f2.getLocation(); if (loc1 == null || loc2 == null) return loc1 == loc2; return loc1.equals(loc2); } if (o instanceof XModelObjectEditorInput) { return object.equals(((XModelObjectEditorInput) o).getXModelObject()); } return false; }
@Override public void doSaveAs() { IPath relativePath = null; if ((relativePath = showSaveAsDialog()) != null) { mFileEditorInput = new FileEditorInput(ResourcesPlugin.getWorkspace().getRoot().getFile(relativePath)); mFileName = mFileEditorInput.getName(); setInput(mFileEditorInput); doSave(new NullProgressMonitor()); } }
@Override public void createPartControl(Composite parent) { mMainFrame = new MainFrame(parent, SWT.NULL); ImageViewer imageViewer = mMainFrame.getImageEditorPanel().getImageViewer(); imageViewer.addUpdateListener(this); mNinePatchedImage = imageViewer.loadFile(mFileEditorInput.getPath().toOSString()); if (mNinePatchedImage.hasNinePatchExtension()) { if (!mNinePatchedImage.ensure9Patch() && showConvertMessageBox(mFileName)) { // Reload image mNinePatchedImage = imageViewer.loadFile(mFileEditorInput.getPath().toOSString()); mNinePatchedImage.convertToNinePatch(); } } else { mNinePatchedImage.convertToNinePatch(); } imageViewer.startDisplay(); parent.layout(); }
/** * Tests if the current workbench selection is a suitable * * <p>container to use. */ private void initialize() { if (selection != null && selection.isEmpty() == false && selection instanceof IStructuredSelection) { IStructuredSelection ssel = (IStructuredSelection) selection; if (ssel.size() > 1) return; Object obj = ssel.getFirstElement(); if (obj instanceof IResource) { IContainer container; if (obj instanceof IContainer) container = (IContainer) obj; else container = ((IResource) obj).getParent(); containerText.setText(container.getFullPath().toString()); } } else if (this.editor != null) { if (editor.getEditorInput() instanceof FileEditorInput) { FileEditorInput input = (FileEditorInput) editor.getEditorInput(); containerText.setText(input.getFile().getParent().getFullPath().toString()); } } // TODO: this works if you do a right click but not if you come here from a select your page // type page fileText.setText("untitled.cfm"); setFilenameFocus(); }
private void initializeResourceObjectFromFile(FileEditorInput input) { IFile inputFile = input.getFile(); com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.mopp.RezeptNature.activate( inputFile.getProject()); String path = inputFile.getFullPath().toString(); URI uri = URI.createPlatformResourceURI(path, true); ResourceSet resourceSet = getResourceSet(); com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource loadedResource = (com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource) resourceSet.getResource(uri, false); if (loadedResource == null) { try { Resource demandLoadedResource = null; // here we do not use getResource(), because 'resource' might be null, which is ok // when initializing the resource object com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource currentResource = this.resource; if (currentResource != null && !currentResource.getURI().fileExtension().equals(uri.fileExtension())) { // do not attempt to load if file extension has changed in a 'save as' operation } else { demandLoadedResource = resourceSet.getResource(uri, true); } if (demandLoadedResource instanceof com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource) { setResource( (com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.IRezeptTextResource) demandLoadedResource); } else { // the resource was not loaded by an EMFText resource, but some other EMF resource com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.ui.RezeptUIPlugin .showErrorDialog( "Invalid resource.", "The file '" + uri.lastSegment() + "' of type '" + uri.fileExtension() + "' can not be handled by the RezeptEditor."); // close this editor because it can not present the resource close(false); } } catch (Exception e) { com.github.funthomas424242.rezeptsammler.rezept.resource.rezept.ui.RezeptUIPlugin.logError( "Exception while loading resource in " + this.getClass().getSimpleName() + ".", e); } } else { setResource(loadedResource); } }
private void connect(TextFileDocumentProvider provider, FileEditorInput input) { if (!inputConnected) { try { provider.connect(input); inputConnected = true; } catch (CoreException e) { Log.log(e); } } else { throw new IllegalStateException( "Attempting to connect input '" + input.getFile().getName() + "' while previous input is still connected."); } }
private IPath showSaveAsDialog() { SaveAsDialog dialog = new SaveAsDialog(AndmoreAndroidPlugin.getDisplay().getActiveShell()); IFile dest = mProject.getFile( NinePatchedImage.getNinePatchedFileName( mFileEditorInput.getFile().getProjectRelativePath().toOSString())); dialog.setOriginalFile(dest); dialog.create(); if (dialog.open() == Window.CANCEL) { return null; } return dialog.getResult(); }
private File getProjectRootDir(FileEditorInput input) { return new File(input.getFile().getProject().getLocationURI()); }
public ImageDescriptor getImageDescriptor() { return innerEidtorInput.getImageDescriptor(); }
public boolean exists() { return innerEidtorInput.exists(); }
public IStorage getStorage() throws CoreException { return innerEidtorInput.getStorage(); }