Search in sources :

Example 1 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class DragDropInteraction method moveTo.

private void moveTo(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask final int modifiers, boolean commit) {
    myScreenView = myDesignSurface.getScreenView(x, y);
    if (myScreenView == null) {
        return;
    }
    myDesignSurface.getLayeredPane().scrollRectToVisible(new Rectangle(x - NlConstants.DEFAULT_SCREEN_OFFSET_X, y - NlConstants.DEFAULT_SCREEN_OFFSET_Y, 2 * NlConstants.DEFAULT_SCREEN_OFFSET_X, 2 * NlConstants.DEFAULT_SCREEN_OFFSET_Y));
    final int ax = Coordinates.getAndroidX(myScreenView, x);
    final int ay = Coordinates.getAndroidY(myScreenView, y);
    Project project = myScreenView.getModel().getProject();
    ViewGroupHandler handler = findViewGroupHandlerAt(ax, ay);
    if (handler != myCurrentHandler) {
        if (myDragHandler != null) {
            myDragHandler.cancel();
            myDragHandler = null;
            myScreenView.getSurface().repaint();
        }
        myCurrentHandler = handler;
        if (myCurrentHandler != null) {
            assert myDragReceiver != null;
            String error = null;
            ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(project);
            for (NlComponent component : myDraggedComponents) {
                if (!myCurrentHandler.acceptsChild(myDragReceiver, component, ax, ay)) {
                    error = String.format("<%1$s> does not accept <%2$s> as a child", myDragReceiver.getTagName(), component.getTagName());
                    break;
                }
                ViewHandler viewHandler = viewHandlerManager.getHandler(component);
                if (viewHandler != null && !viewHandler.acceptsParent(myDragReceiver, component)) {
                    error = String.format("<%1$s> does not accept <%2$s> as a parent", component.getTagName(), myDragReceiver.getTagName());
                    break;
                }
            }
            if (error == null) {
                myDragHandler = myCurrentHandler.createDragHandler(new ViewEditorImpl(myScreenView), myDragReceiver, myDraggedComponents, myType);
                if (myDragHandler != null) {
                    myDragHandler.start(Coordinates.getAndroidX(myScreenView, myStartX), Coordinates.getAndroidY(myScreenView, myStartY), myStartMask);
                }
            } else {
                myCurrentHandler = null;
            }
        }
    }
    if (myDragHandler != null && myCurrentHandler != null) {
        String error = myDragHandler.update(ax, ay, modifiers);
        final List<NlComponent> added = Lists.newArrayList();
        if (commit && error == null) {
            added.addAll(myDraggedComponents);
            final NlModel model = myScreenView.getModel();
            XmlFile file = model.getFile();
            String label = myType.getDescription();
            WriteCommandAction action = new WriteCommandAction(project, label, file) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    InsertType insertType = model.determineInsertType(myType, myTransferItem, false);
                    // TODO: Run this *after* making a copy
                    myDragHandler.commit(ax, ay, modifiers, insertType);
                }
            };
            action.execute();
            model.notifyModified(NlModel.ChangeType.DND_COMMIT);
            // Select newly dropped components
            model.getSelectionModel().setSelection(added);
        }
        myScreenView.getSurface().repaint();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)

Example 2 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class BlueprintLayer method paint.

/**
   * Base buildDisplayList method. Draw the blueprint background.
   * TODO: We might want to simplify the stack of layers and not keep this one.
   *
   * @param gc The Graphics object to draw into
   */
@Override
public void paint(@NotNull Graphics2D gc) {
    myScreenView.getSize(myScreenViewSize);
    mySizeRectangle.setBounds(myScreenView.getX(), myScreenView.getY(), myScreenViewSize.width, myScreenViewSize.height);
    Rectangle2D.intersect(mySizeRectangle, gc.getClipBounds(), mySizeRectangle);
    if (mySizeRectangle.isEmpty()) {
        return;
    }
    // Draw the background
    Graphics2D g = (Graphics2D) gc.create();
    Shape prevClip = null;
    Shape screenShape = myScreenView.getScreenShape();
    if (screenShape != null) {
        prevClip = g.getClip();
        g.clip(screenShape);
    }
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(BLUEPRINT_BG_COLOR);
    g.fillRect(mySizeRectangle.x, mySizeRectangle.y, mySizeRectangle.width, mySizeRectangle.height);
    if (prevClip != null) {
        g.setClip(prevClip);
    }
    // Draw the components
    NlModel model = myScreenView.getModel();
    if (model.getComponents().size() == 0) {
        return;
    }
    NlComponent component = model.getComponents().get(0);
    component = component.getRoot();
    ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(model.getFacet());
    // Draw the components
    if (drawComponent(g, component, viewHandlerManager, false)) {
        Dimension size = myScreenView.getSize();
        DesignSurface surface = myScreenView.getSurface();
        if (size.width != 0 && size.height != 0) {
            surface.repaint(myScreenView.getX(), myScreenView.getY(), size.width, size.height);
        } else {
            surface.repaint();
        }
    }
    g.dispose();
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) NlModel(com.android.tools.idea.uibuilder.model.NlModel)

Example 3 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class PaletteTestCase method checkComponent.

private void checkComponent(@NotNull NlComponent component, @NotNull String expectedTitle, @NotNull Icon expectedIcon) {
    ViewHandlerManager handlerManager = ViewHandlerManager.get(getProject());
    ViewHandler handler = handlerManager.getHandlerOrDefault(component);
    String title = handler.getTitle(component);
    String attrs = handler.getTitleAttributes(component);
    if (!StringUtil.isEmpty(attrs)) {
        title += " " + attrs;
    }
    assertEquals(component.getTagName() + ".Component.Title", expectedTitle, title);
    assertEquals(component.getTagName() + ".Component.Icon", expectedIcon, handler.getIcon(component));
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) TextViewHandler(com.android.tools.idea.uibuilder.handlers.TextViewHandler) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler)

Example 4 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class NlModel method handleDeletion.

private void handleDeletion(@NotNull Collection<NlComponent> components) {
    // Segment the deleted components into lists of siblings
    Map<NlComponent, List<NlComponent>> siblingLists = groupSiblings(components);
    ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(myFacet);
    // Notify parent components about children getting deleted
    for (Map.Entry<NlComponent, List<NlComponent>> entry : siblingLists.entrySet()) {
        NlComponent parent = entry.getKey();
        if (parent == null) {
            continue;
        }
        List<NlComponent> children = entry.getValue();
        boolean finished = false;
        ViewHandler handler = viewHandlerManager.getHandler(parent);
        if (handler instanceof ViewGroupHandler) {
            finished = ((ViewGroupHandler) handler).deleteChildren(parent, children);
        }
        if (!finished) {
            for (NlComponent component : children) {
                NlComponent p = component.getParent();
                if (p != null) {
                    p.removeChild(component);
                }
                component.getTag().delete();
            }
        }
    }
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) List(java.util.List) PropertiesMap(com.android.util.PropertiesMap)

Example 5 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class StyleFilter method getWidgetStyleNames.

@NotNull
private static List<String> getWidgetStyleNames(@NotNull Project project, @NotNull ResourceResolver resolver, @NotNull String tagName) {
    ViewHandlerManager manager = ViewHandlerManager.get(project);
    ViewHandler handler = manager.getHandler(tagName);
    if (handler == null) {
        return Collections.emptyList();
    }
    List<String> possibleNames = handler.getBaseStyles(tagName);
    ResourceValueMap frameworkStyles = resolver.getFrameworkResources().get(ResourceType.STYLE);
    ResourceValueMap projectStyles = resolver.getProjectResources().get(ResourceType.STYLE);
    // This is usually a small list
    List<String> names = new ArrayList<>(4);
    for (String styleName : possibleNames) {
        if (frameworkStyles.containsKey(styleName)) {
            names.add(SdkConstants.PREFIX_ANDROID + styleName);
        }
        if (projectStyles.containsKey(styleName)) {
            names.add(styleName);
        }
    }
    return names;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)9 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)3 ViewEditorImpl (com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)3 NotNull (org.jetbrains.annotations.NotNull)3 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Project (com.intellij.openapi.project.Project)2 XmlFile (com.intellij.psi.xml.XmlFile)2 List (java.util.List)2 Nullable (org.jetbrains.annotations.Nullable)2 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 com.android.tools.idea.uibuilder.api (com.android.tools.idea.uibuilder.api)1 InsertType (com.android.tools.idea.uibuilder.api.InsertType)1 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)1 NlConstants (com.android.tools.idea.uibuilder.graphics.NlConstants)1 NlGraphics (com.android.tools.idea.uibuilder.graphics.NlGraphics)1 TextViewHandler (com.android.tools.idea.uibuilder.handlers.TextViewHandler)1 com.android.tools.idea.uibuilder.model (com.android.tools.idea.uibuilder.model)1 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1