Search in sources :

Example 36 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project android by JetBrains.

the class ConfigurationMatcher method selectConfigMatch.

@NotNull
private ConfigMatch selectConfigMatch(@NotNull List<ConfigMatch> matches) {
    List<String> deviceIds = myManager.getStateManager().getProjectState().getDeviceIds();
    Map<String, Integer> idRank = Maps.newHashMapWithExpectedSize(deviceIds.size());
    int rank = 0;
    for (String id : deviceIds) {
        idRank.put(id, rank++);
    }
    // API 11-13: look for a x-large device
    Comparator<ConfigMatch> comparator = null;
    IAndroidTarget projectTarget = myManager.getProjectTarget();
    if (projectTarget != null) {
        int apiLevel = projectTarget.getVersion().getFeatureLevel();
        if (apiLevel >= 11 && apiLevel < 14) {
            // TODO: Maybe check the compatible-screen tag in the manifest to figure out
            // what kind of device should be used for display.
            comparator = new TabletConfigComparator(idRank);
        }
    }
    if (comparator == null) {
        // lets look for a high density device
        comparator = new PhoneConfigComparator(idRank);
    }
    Collections.sort(matches, comparator);
    // Look at the currently active editor to see if it's a layout editor, and if so,
    // look up its configuration and if the configuration is in our match list,
    // use it. This means we "preserve" the current configuration when you open
    // new layouts.
    // TODO: This is running too late for the layout preview; the new editor has
    // already taken over so getSelectedTextEditor() returns self. Perhaps we
    // need to fish in the open editors instead.
    // We use FileEditorManagerImpl instead of FileEditorManager to get access to the lock-free version
    // (also used by DebuggerContextUtil) since the normal method only works from the dispatch thread
    // (grabbing a read lock is not enough).
    FileEditorManager editorManager = FileEditorManager.getInstance(myManager.getProject());
    if (editorManager instanceof FileEditorManagerImpl) {
        // not the case under test fixtures apparently
        Editor activeEditor = ((FileEditorManagerImpl) editorManager).getSelectedTextEditor(true);
        if (activeEditor != null) {
            FileDocumentManager documentManager = FileDocumentManager.getInstance();
            VirtualFile file = documentManager.getFile(activeEditor.getDocument());
            if (file != null && !file.equals(myFile) && file.getFileType() == StdFileTypes.XML && ResourceHelper.getFolderType(myFile) == ResourceHelper.getFolderType(file)) {
                Configuration configuration = myManager.getConfiguration(file);
                FolderConfiguration fullConfig = configuration.getFullConfig();
                for (ConfigMatch match : matches) {
                    if (fullConfig.equals(match.testConfig)) {
                        return match;
                    }
                }
            }
        }
    }
    // the list has been sorted so that the first item is the best config
    return matches.get(0);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManagerImpl(com.intellij.openapi.fileEditor.impl.FileEditorManagerImpl) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) IAndroidTarget(com.android.sdklib.IAndroidTarget) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project android by JetBrains.

the class PermissionUsageQuickFix method openFile.

public static void openFile(Project project, PsiFile file) {
    String path = file.getVirtualFile().getCanonicalPath();
    assert path != null;
    FileEditorManager manager = FileEditorManager.getInstance(project);
    VirtualFile virtFile = LocalFileSystem.getInstance().findFileByPath(path);
    assert virtFile != null;
    manager.openFile(virtFile, true, true);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager)

Example 38 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project android by JetBrains.

the class WorkBenchTest method before.

@Before
public void before() {
    initMocks(this);
    myContent = new JPanel();
    myContent.setPreferredSize(new Dimension(500, 400));
    mySplitter = new ThreeComponentsSplitter();
    Project project = ProjectManager.getInstance().getDefaultProject();
    myPropertiesComponent = PropertiesComponent.getInstance();
    myWorkBenchManager = WorkBenchManager.getInstance();
    myFloatingToolWindowManager = FloatingToolWindowManager.getInstance(project);
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    myModel = new SideModel<>(project);
    myLeftMinimizePanel = spy(new MinimizedPanel<>(Side.RIGHT, myModel));
    myLeftMinimizePanel.setLayout(new BoxLayout(myLeftMinimizePanel, BoxLayout.Y_AXIS));
    myRightMinimizePanel = spy(new MinimizedPanel<>(Side.RIGHT, myModel));
    myRightMinimizePanel.setLayout(new BoxLayout(myRightMinimizePanel, BoxLayout.Y_AXIS));
    WorkBench.InitParams<String> initParams = new WorkBench.InitParams<>(myModel, mySplitter, myLeftMinimizePanel, myRightMinimizePanel);
    myWorkBench = new WorkBench<>(project, "BENCH", myFileEditor, initParams);
    JRootPane rootPane = new JRootPane();
    rootPane.add(myWorkBench);
    List<ToolWindowDefinition<String>> definitions = ImmutableList.of(PalettePanelToolContent.getDefinition(), PalettePanelToolContent.getOtherDefinition(), PalettePanelToolContent.getThirdDefinition());
    myWorkBench.init(myContent, "CONTEXT", definitions);
    myToolWindow1 = myModel.getAllTools().get(0);
    myToolWindow2 = myModel.getAllTools().get(1);
    myToolWindow3 = myModel.getAllTools().get(2);
    when(fileEditorManager.getSelectedEditors()).thenReturn(new FileEditor[] { myFileEditor, myFileEditor2 });
    verify(myWorkBenchManager).register(eq(myWorkBench));
    verify(myFloatingToolWindowManager).register(eq(myFileEditor), eq(myWorkBench));
    reset(myWorkBenchManager, myFloatingToolWindowManager);
}
Also used : ThreeComponentsSplitter(com.intellij.openapi.ui.ThreeComponentsSplitter) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) Before(org.junit.Before)

Example 39 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project android by JetBrains.

the class NlModel method overrideConfigurationScreenSize.

/**
   * Changes the configuration to use a custom device with screen size defined by xDimension and yDimension.
   */
public void overrideConfigurationScreenSize(@AndroidCoordinate int xDimension, @AndroidCoordinate int yDimension) {
    Device original = myConfiguration.getDevice();
    // doesn't copy tag id
    Device.Builder deviceBuilder = new Device.Builder(original);
    if (original != null) {
        deviceBuilder.setTagId(original.getTagId());
    }
    deviceBuilder.setName("Custom");
    deviceBuilder.setId(Configuration.CUSTOM_DEVICE_ID);
    Device device = deviceBuilder.build();
    for (State state : device.getAllStates()) {
        Screen screen = state.getHardware().getScreen();
        screen.setXDimension(xDimension);
        screen.setYDimension(yDimension);
        double dpi = screen.getPixelDensity().getDpiValue();
        double width = xDimension / dpi;
        double height = yDimension / dpi;
        double diagonalLength = Math.sqrt(width * width + height * height);
        screen.setDiagonalLength(diagonalLength);
        screen.setSize(AvdScreenData.getScreenSize(diagonalLength));
        screen.setRatio(AvdScreenData.getScreenRatio(xDimension, yDimension));
        screen.setScreenRound(device.getDefaultHardware().getScreen().getScreenRound());
        screen.setChin(device.getDefaultHardware().getScreen().getChin());
    }
    // If a custom device already exists, remove it before adding the latest one
    List<Device> devices = myConfiguration.getConfigurationManager().getDevices();
    boolean customDeviceReplaced = false;
    for (int i = 0; i < devices.size(); i++) {
        if ("Custom".equals(devices.get(i).getId())) {
            devices.set(i, device);
            customDeviceReplaced = true;
            break;
        }
    }
    if (!customDeviceReplaced) {
        devices.add(device);
    }
    VirtualFile better;
    State newState;
    //Change the orientation of the device depending on the shape of the canvas
    if (xDimension > yDimension) {
        better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Landscape", null, null);
        newState = device.getState("Landscape");
    } else {
        better = ConfigurationMatcher.getBetterMatch(myConfiguration, device, "Portrait", null, null);
        newState = device.getState("Portrait");
    }
    if (better != null) {
        VirtualFile old = myConfiguration.getFile();
        assert old != null;
        Project project = mySurface.getProject();
        OpenFileDescriptor descriptor = new OpenFileDescriptor(project, better, -1);
        FileEditorManager manager = FileEditorManager.getInstance(project);
        FileEditor selectedEditor = manager.getSelectedEditor(old);
        manager.openEditor(descriptor, true);
        // Switch to the same type of editor (XML or Layout Editor) in the target file
        if (selectedEditor instanceof NlEditor) {
            manager.setSelectedEditor(better, NlEditorProvider.DESIGNER_ID);
        } else if (selectedEditor != null) {
            manager.setSelectedEditor(better, TextEditorProvider.getInstance().getEditorTypeId());
        }
        AndroidFacet facet = AndroidFacet.getInstance(myConfiguration.getModule());
        assert facet != null;
        Configuration configuration = facet.getConfigurationManager().getConfiguration(better);
        configuration.setEffectiveDevice(device, newState);
    } else {
        myConfiguration.setEffectiveDevice(device, newState);
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) FileEditor(com.intellij.openapi.fileEditor.FileEditor) Configuration(com.android.tools.idea.configurations.Configuration) Device(com.android.sdklib.devices.Device) Screen(com.android.sdklib.devices.Screen) NlEditor(com.android.tools.idea.uibuilder.editor.NlEditor) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) Project(com.intellij.openapi.project.Project) FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) State(com.android.sdklib.devices.State) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor)

Example 40 with FileEditorManager

use of com.intellij.openapi.fileEditor.FileEditorManager in project android by JetBrains.

the class GenerateLayoutTestSkeletonAction method getSurface.

@Nullable
private static DesignSurface getSurface(@NotNull Project project) {
    FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
    FileEditor[] editors = fileEditorManager.getSelectedEditors();
    for (FileEditor fileEditor : editors) {
        if (fileEditor instanceof NlEditor) {
            return ((NlEditor) fileEditor).getComponent().getSurface();
        }
    }
    Editor editor = fileEditorManager.getSelectedTextEditor();
    if (editor == null) {
        return null;
    }
    NlPreviewManager previewManager = NlPreviewManager.getInstance(project);
    if (previewManager.isWindowVisible()) {
        return previewManager.getPreviewForm().getSurface();
    }
    PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
    if (file == null) {
        return null;
    }
    for (FileEditor fileEditor : fileEditorManager.getEditors(file.getVirtualFile())) {
        if (fileEditor instanceof NlEditor) {
            return ((NlEditor) fileEditor).getComponent().getSurface();
        }
    }
    return null;
}
Also used : FileEditorManager(com.intellij.openapi.fileEditor.FileEditorManager) FileEditor(com.intellij.openapi.fileEditor.FileEditor) NlPreviewManager(com.android.tools.idea.uibuilder.editor.NlPreviewManager) NlEditor(com.android.tools.idea.uibuilder.editor.NlEditor) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) FileEditor(com.intellij.openapi.fileEditor.FileEditor) NlEditor(com.android.tools.idea.uibuilder.editor.NlEditor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)67 VirtualFile (com.intellij.openapi.vfs.VirtualFile)36 FileEditor (com.intellij.openapi.fileEditor.FileEditor)29 Editor (com.intellij.openapi.editor.Editor)22 Project (com.intellij.openapi.project.Project)22 TextEditor (com.intellij.openapi.fileEditor.TextEditor)12 Document (com.intellij.openapi.editor.Document)10 PsiFile (com.intellij.psi.PsiFile)10 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)9 Nullable (org.jetbrains.annotations.Nullable)8 TextRange (com.intellij.openapi.util.TextRange)6 NotNull (org.jetbrains.annotations.NotNull)6 FileEditorManagerListener (com.intellij.openapi.fileEditor.FileEditorManagerListener)5 PsiElement (com.intellij.psi.PsiElement)5 StructureViewComponent (com.intellij.ide.structureView.newStructureView.StructureViewComponent)4 EditorColorsScheme (com.intellij.openapi.editor.colors.EditorColorsScheme)4 GuiTask (org.fest.swing.edt.GuiTask)4 HighlightManager (com.intellij.codeInsight.highlighting.HighlightManager)3 ResourceBundleAsVirtualFile (com.intellij.lang.properties.editor.ResourceBundleAsVirtualFile)3 PsiClass (com.intellij.psi.PsiClass)3