Search in sources :

Example 1 with NlEditor

use of com.android.tools.idea.uibuilder.editor.NlEditor in project android by JetBrains.

the class EditorFixture method getLayoutEditor.

/**
   * Returns a fixture around the layout editor, <b>if</b> the currently edited file
   * is a layout file and it is currently showing the layout editor tab or the parameter
   * requests that it be opened if necessary
   *
   * @param switchToTabIfNecessary if true, switch to the design tab if it is not already showing
   * @throws IllegalStateException if there is no selected editor or it is not a {@link NlEditor}
   */
@NotNull
public NlEditorFixture getLayoutEditor(boolean switchToTabIfNecessary) {
    if (switchToTabIfNecessary) {
        selectEditorTab(Tab.DESIGN);
    }
    return GuiQuery.getNonNull(() -> {
        FileEditor[] editors = FileEditorManager.getInstance(myFrame.getProject()).getSelectedEditors();
        checkState(editors.length > 0, "no selected editors");
        FileEditor selected = editors[0];
        checkState(selected instanceof NlEditor, "not a %s: %s", NlEditor.class.getSimpleName(), selected);
        return new NlEditorFixture(myFrame.robot(), myFrame, (NlEditor) selected);
    });
}
Also used : FileEditor(com.intellij.openapi.fileEditor.FileEditor) NlEditorFixture(com.android.tools.idea.tests.gui.framework.fixture.layout.NlEditorFixture) NlEditor(com.android.tools.idea.uibuilder.editor.NlEditor) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with NlEditor

use of com.android.tools.idea.uibuilder.editor.NlEditor 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)

Example 3 with NlEditor

use of com.android.tools.idea.uibuilder.editor.NlEditor 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)

Aggregations

NlEditor (com.android.tools.idea.uibuilder.editor.NlEditor)3 FileEditor (com.intellij.openapi.fileEditor.FileEditor)3 FileEditorManager (com.intellij.openapi.fileEditor.FileEditorManager)2 Device (com.android.sdklib.devices.Device)1 Screen (com.android.sdklib.devices.Screen)1 State (com.android.sdklib.devices.State)1 Configuration (com.android.tools.idea.configurations.Configuration)1 NlEditorFixture (com.android.tools.idea.tests.gui.framework.fixture.layout.NlEditorFixture)1 NlPreviewManager (com.android.tools.idea.uibuilder.editor.NlPreviewManager)1 Editor (com.intellij.openapi.editor.Editor)1 OpenFileDescriptor (com.intellij.openapi.fileEditor.OpenFileDescriptor)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1