Example usage for com.intellij.openapi.fileEditor OpenFileDescriptor OpenFileDescriptor

List of usage examples for com.intellij.openapi.fileEditor OpenFileDescriptor OpenFileDescriptor

Introduction

In this page you can find the example usage for com.intellij.openapi.fileEditor OpenFileDescriptor OpenFileDescriptor.

Prototype

public OpenFileDescriptor(@NotNull Project project, @NotNull VirtualFile file, int offset) 

Source Link

Usage

From source file:com.android.tools.idea.actions.AndroidShowNavigationEditor.java

License:Apache License

public void showNavigationEditor(@Nullable Project project, @Nullable Module module, final String dir,
        final String file) {
    if (project == null) {
        return;//  w w w  .j  ava  2  s . co  m
    }
    if (module == null) {
        return;
    }
    VirtualFile baseDir = project.getBaseDir();
    if (baseDir == null) { // this happens when we have the 'default' project; can't launch nav editor from here
        return;
    }
    VirtualFile navFile = NavigationEditorUtils.getNavigationFile(baseDir, module.getName(), dir, file);
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, navFile, 0);
    FileEditorManager manager = FileEditorManager.getInstance(project);
    manager.openEditor(descriptor, true);
    manager.setSelectedEditor(navFile, NavigationEditorProvider.ID);
}

From source file:com.android.tools.idea.actions.OverrideResourceAction.java

License:Apache License

private static void forkResourceFile(@NotNull Project project, @NotNull final ResourceFolderType folderType,
        @NotNull final VirtualFile file, @Nullable final XmlFile xmlFile, @Nullable String myNewFolder,
        @Nullable Configuration configuration, boolean open) {
    final FolderConfiguration folderConfig;
    if (myNewFolder == null) {
        // Open a file chooser to select the configuration to be created
        VirtualFile parentFolder = file.getParent();
        assert parentFolder != null;
        VirtualFile res = parentFolder.getParent();
        folderConfig = selectFolderConfig(project, res, folderType);
    } else {//from ww w  .ja va  2 s . c o m
        folderConfig = FolderConfiguration.getConfigForFolder(myNewFolder);
    }
    if (folderConfig == null) {
        return;
    }

    final Computable<Pair<String, VirtualFile>> computable = new Computable<Pair<String, VirtualFile>>() {
        @Override
        public Pair<String, VirtualFile> compute() {
            String folderName = folderConfig.getFolderName(folderType);
            try {
                VirtualFile parentFolder = file.getParent();
                assert parentFolder != null;
                VirtualFile res = parentFolder.getParent();
                VirtualFile newParentFolder = res.findChild(folderName);
                if (newParentFolder == null) {
                    newParentFolder = res.createChildDirectory(this, folderName);
                    if (newParentFolder == null) {
                        String message = String.format("Could not create folder %1$s in %2$s", folderName,
                                res.getPath());
                        return Pair.of(message, null);
                    }
                }

                final VirtualFile existing = newParentFolder.findChild(file.getName());
                if (existing != null && existing.exists()) {
                    String message = String.format("File 'res/%1$s/%2$s' already exists!", folderName,
                            file.getName());
                    return Pair.of(message, null);
                }

                // Attempt to get the document from the PSI file rather than the file on disk: get edited contents too
                String text;
                if (xmlFile != null) {
                    text = xmlFile.getText();
                } else {
                    text = StreamUtil.readText(file.getInputStream(), "UTF-8");
                }
                VirtualFile newFile = newParentFolder.createChildData(this, file.getName());
                VfsUtil.saveText(newFile, text);
                return Pair.of(null, newFile);
            } catch (IOException e2) {
                String message = String.format("Failed to create File 'res/%1$s/%2$s' : %3$s", folderName,
                        file.getName(), e2.getMessage());
                return Pair.of(message, null);
            }
        }
    };

    WriteCommandAction<Pair<String, VirtualFile>> action = new WriteCommandAction<Pair<String, VirtualFile>>(
            project, "Add Resource") {
        @Override
        protected void run(@NotNull Result<Pair<String, VirtualFile>> result) throws Throwable {
            result.setResult(computable.compute());
        }
    };
    Pair<String, VirtualFile> result = action.execute().getResultObject();

    String error = result.getFirst();
    VirtualFile newFile = result.getSecond();
    if (error != null) {
        Messages.showErrorDialog(project, error, "Create Resource");
    } else {
        // First create a compatible configuration based on the current configuration
        if (configuration != null) {
            ConfigurationManager configurationManager = configuration.getConfigurationManager();
            configurationManager.createSimilar(newFile, file);
        }

        if (open) {
            OpenFileDescriptor descriptor = new OpenFileDescriptor(project, newFile, -1);
            FileEditorManager.getInstance(project).openEditor(descriptor, true);
        }
    }
}

From source file:com.android.tools.idea.actions.PsiClassNavigation.java

License:Apache License

@Override
public void navigate(boolean requestFocus) {
    OpenFileDescriptor fileDescriptor = myOffset >= 0
            ? new OpenFileDescriptor(myPsiFile.getProject(), myPsiFile.getVirtualFile(), myOffset)
            : new OpenFileDescriptor(myPsiFile.getProject(), myPsiFile.getVirtualFile(), myLine, 0);
    fileDescriptor.navigate(requestFocus);
}

From source file:com.android.tools.idea.actions.PsiFileAndLineNavigation.java

License:Apache License

@Override
public void navigate(boolean requestFocus) {
    OpenFileDescriptor fileDescriptor = new OpenFileDescriptor(myPsiFile.getProject(),
            myPsiFile.getVirtualFile(), myLineNumber);
    fileDescriptor.navigate(requestFocus);
}

From source file:com.android.tools.idea.configurations.ConfigurationAction.java

License:Apache License

protected void pickedBetterMatch(@NotNull VirtualFile file) {
    // Switch files, and leave this configuration alone
    Module module = myRenderContext.getModule();
    assert module != null;
    Project project = module.getProject();
    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, -1);
    FileEditorManager.getInstance(project).openEditor(descriptor, true);
}

From source file:com.android.tools.idea.configurations.RenderOptionsMenuBuilder.java

License:Apache License

@NotNull
public RenderOptionsMenuBuilder addPreferXmlOption() {
    myGroup.addAction(new CheckboxAction("Prefer XML Editor") {
        @Override// w  ww.j  a va 2s . c  om
        public boolean isSelected(AnActionEvent e) {
            return mySettings.getGlobalState().isPreferXmlEditor();
        }

        @Override
        public void setSelected(AnActionEvent e, boolean state) {
            mySettings.getGlobalState().setPreferXmlEditor(state);

            // Switch to XML editor
            Module module = myContext.getModule();
            VirtualFile file = myContext.getVirtualFile();
            if (module != null && file != null) {
                Project project = module.getProject();
                FileEditorManager manager = FileEditorManager.getInstance(project);
                if (state) {
                    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, file, 0);
                    manager.openEditor(descriptor, true);
                } else {
                    FileEditorManager.getInstance(project).setSelectedEditor(file,
                            AndroidDesignerEditorProvider.ANDROID_DESIGNER_ID);
                }
            }
        }
    }).setAsSecondary(true);

    return this;
}

From source file:com.android.tools.idea.experimental.actions.PermissionUsageQuickFix.java

License:Apache License

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file,
        @Nullable /*is null when called from inspection*/ Editor editor, @NotNull PsiElement startElement,
        @NotNull PsiElement endElement) {

    //The Editor can be null at any time.
    //Open the editor manually
    openFile(project, file);//from   www.  ja  va  2  s .  c om
    OpenFileDescriptor fileDesc = new OpenFileDescriptor(project, file.getVirtualFile(),
            startElement.getTextOffset());
    editor = FileEditorManager.getInstance(project).openTextEditor(fileDesc, true);
    assert editor != null;

    int startOffset = startElement.getTextOffset();
    int endOffset = endElement.getTextOffset() + endElement.getTextLength();
    editor.getSelectionModel().setSelection(startOffset, endOffset);

    PermissionUsageQuickFixRunnable job = new PermissionUsageQuickFixRunnable(project, editor);
    execRunnable(job, "Permission Fix");
}

From source file:com.android.tools.idea.gradle.editor.action.GradleEntityNavigateAction.java

License:Apache License

@Override
protected void doActionPerformed(@NotNull GradleEditorEntity entity, AnActionEvent event) {
    if (entity instanceof GradleEntityDefinitionValueLocationAware) {
        GradleEditorSourceBinding location = ((GradleEntityDefinitionValueLocationAware) entity)
                .getDefinitionValueLocation();
        if (location != null) {
            RangeMarker marker = location.getRangeMarker();
            if (marker.isValid()) {
                OpenFileDescriptor descriptor = new OpenFileDescriptor(location.getProject(),
                        location.getFile(), marker.getStartOffset());
                if (descriptor.canNavigate()) {
                    descriptor.navigate(true);
                }//from   ww  w .j  a  va2 s.  c o m
            }
        }
    }
}

From source file:com.android.tools.idea.gradle.project.sync.setup.module.common.DependencySetupErrors.java

License:Apache License

public void reportErrors() {
    reportModulesNotFoundIssues(getMissingModules());

    for (String dependent : getMissingNames()) {
        String msg = String.format("Module '%1$s' depends on modules that do not have a name.", dependent);
        mySyncMessages.report(new SyncMessage(MISSING_DEPENDENCIES, ERROR, msg));
    }/*w  w w  .  j a  v  a 2  s  . c  o  m*/

    for (String dependent : getDependentsOnLibrariesWithoutBinaryPath()) {
        String msg = String.format("Module '%1$s' depends on libraries that do not have a 'binary' path.",
                dependent);
        mySyncMessages.report(new SyncMessage(MISSING_DEPENDENCIES, ERROR, msg));
    }

    for (DependencySetupErrors.InvalidModuleDependency dependency : myInvalidModuleDependencies) {
        String msg = String.format("Ignoring dependency of module '%1$s' on module '%2$s'. %3$s",
                dependency.dependent, dependency.dependency.getName(), dependency.causeDescription);
        VirtualFile buildFile = getGradleBuildFile(dependency.dependency);
        assert buildFile != null;
        OpenFileDescriptor navigatable = new OpenFileDescriptor(dependency.dependency.getProject(), buildFile,
                0);
        mySyncMessages.report(new SyncMessage(MISSING_DEPENDENCIES, WARNING, navigatable, msg));
    }

    reportModulesNotFoundIssues(getMissingModulesWithBackupLibraries());
    clear();
}

From source file:com.android.tools.idea.rendering.GutterIconRenderer.java

License:Apache License

@Nullable
@Override//from   w w w .  j  a v  a 2  s.c o m
public AnAction getClickAction() {
    return new AnAction() {
        @Override
        public void actionPerformed(AnActionEvent e) {
            final Editor editor = PlatformDataKeys.EDITOR.getData(e.getDataContext());
            if (editor != null) {
                Project project = editor.getProject();
                VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(myFile);
                if (project != null && virtualFile != null) {
                    OpenFileDescriptor descriptor = new OpenFileDescriptor(project, virtualFile, -1);
                    FileEditorManager.getInstance(project).openEditor(descriptor, true);
                }
            }
        }
    };
}