Search in sources :

Example 1 with DnDTransferItem

use of com.android.tools.idea.uibuilder.model.DnDTransferItem in project android by JetBrains.

the class NlPalettePanelTest method testCopy.

public void testCopy() throws Exception {
    myPanel.requestFocus();
    DataContext context = mock(DataContext.class);
    CopyProvider provider = (CopyProvider) myPanel.getData(PlatformDataKeys.COPY_PROVIDER.getName());
    assertThat(provider).isNotNull();
    assertThat(provider.isCopyVisible(context)).isTrue();
    assertThat(provider.isCopyEnabled(context)).isTrue();
    provider.performCopy(context);
    ArgumentCaptor<Transferable> captor = ArgumentCaptor.forClass(Transferable.class);
    verify(myCopyPasteManager).setContents(captor.capture());
    Transferable transferable = captor.getValue();
    assertThat(transferable).isNotNull();
    assertThat(transferable.isDataFlavorSupported(ItemTransferable.DESIGNER_FLAVOR)).isTrue();
    Object item = transferable.getTransferData(ItemTransferable.DESIGNER_FLAVOR);
    assertThat(item).isInstanceOf(DnDTransferItem.class);
    DnDTransferItem dndItem = (DnDTransferItem) item;
    assertThat(dndItem.getComponents().size()).isEqualTo(1);
    DnDTransferComponent component = dndItem.getComponents().get(0);
    assertThat(component.getRepresentation()).startsWith(("<TextView"));
}
Also used : DnDTransferComponent(com.android.tools.idea.uibuilder.model.DnDTransferComponent) DataContext(com.intellij.openapi.actionSystem.DataContext) CopyProvider(com.intellij.ide.CopyProvider) Transferable(java.awt.datatransfer.Transferable) ItemTransferable(com.android.tools.idea.uibuilder.model.ItemTransferable) DnDTransferItem(com.android.tools.idea.uibuilder.model.DnDTransferItem)

Example 2 with DnDTransferItem

use of com.android.tools.idea.uibuilder.model.DnDTransferItem in project android by JetBrains.

the class ItemTransferHandler method createTransferable.

@Override
@Nullable
protected Transferable createTransferable(@NotNull JComponent component) {
    Palette.Item item = myItemSupplier.get();
    if (item == null) {
        return null;
    }
    ScreenView screenView = myDesignSurface.getCurrentScreenView();
    if (screenView == null) {
        return null;
    }
    @AndroidCoordinate Dimension size;
    BufferedImage image = myIconFactory.renderDragImage(item, screenView);
    if (image != null) {
        size = new Dimension(image.getWidth(), image.getHeight());
        double scale = myDesignSurface.getScale();
        image = ImageUtils.scale(image, scale);
    } else {
        Icon icon = item.getIcon();
        //noinspection UndesirableClassUsage
        image = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = (Graphics2D) image.getGraphics();
        icon.paintIcon(component, g2, 0, 0);
        g2.dispose();
        double scale = myDesignSurface.getScale();
        size = new Dimension((int) (image.getWidth() / scale), (int) (image.getHeight() / scale));
    }
    setDragImage(image);
    setDragImageOffset(new Point(-image.getWidth() / 2, -image.getHeight() / 2));
    DnDTransferComponent dndComponent = new DnDTransferComponent(item.getTagName(), item.getXml(), size.width, size.height);
    return new ItemTransferable(new DnDTransferItem(dndComponent));
}
Also used : ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) ItemTransferable(com.android.tools.idea.uibuilder.model.ItemTransferable) BufferedImage(java.awt.image.BufferedImage) AndroidCoordinate(com.android.tools.idea.uibuilder.model.AndroidCoordinate) DnDTransferComponent(com.android.tools.idea.uibuilder.model.DnDTransferComponent) DnDTransferItem(com.android.tools.idea.uibuilder.model.DnDTransferItem) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

DnDTransferComponent (com.android.tools.idea.uibuilder.model.DnDTransferComponent)2 DnDTransferItem (com.android.tools.idea.uibuilder.model.DnDTransferItem)2 ItemTransferable (com.android.tools.idea.uibuilder.model.ItemTransferable)2 AndroidCoordinate (com.android.tools.idea.uibuilder.model.AndroidCoordinate)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 CopyProvider (com.intellij.ide.CopyProvider)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 Transferable (java.awt.datatransfer.Transferable)1 BufferedImage (java.awt.image.BufferedImage)1 Nullable (org.jetbrains.annotations.Nullable)1