replaced <code></code> with more concise {@code}
[idea/community.git] / plugins / ui-designer / src / com / intellij / uiDesigner / designSurface / PassiveDecorationLayer.java
1 /*
2  * Copyright 2000-2009 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.uiDesigner.designSurface;
17
18 import com.intellij.uiDesigner.FormEditingUtil;
19 import com.intellij.uiDesigner.componentTree.ComponentTree;
20 import com.intellij.uiDesigner.propertyInspector.DesignerToolWindowManager;
21 import com.intellij.uiDesigner.radComponents.RadButtonGroup;
22 import com.intellij.uiDesigner.radComponents.RadComponent;
23 import com.intellij.uiDesigner.radComponents.RadRootContainer;
24 import com.intellij.util.containers.HashSet;
25 import icons.UIDesignerIcons;
26 import org.jetbrains.annotations.NotNull;
27
28 import javax.swing.*;
29 import java.awt.*;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.Set;
33
34 /**
35  * Decoration layer is over COMPONENT_LAYER (layer where all components are located).
36  * It contains all necessary decorators. Decorators are:
37  * - special borders to show component bounds and cell bounds inside grids
38  * - special component which marks selected rectangle
39  *
40  * @author Anton Katilin
41  * @author Vladimir Kondratyev
42  */
43 class PassiveDecorationLayer extends JComponent{
44   @NotNull private final GuiEditor myEditor;
45
46   public PassiveDecorationLayer(@NotNull final GuiEditor editor) {
47     myEditor = editor;
48   }
49
50   /**
51    * Paints all necessary decoration for the specified {@code component}
52    */
53   protected final void paintPassiveDecoration(final RadComponent component, final Graphics g){
54     // Paint component bounds and grid markers
55     Painter.paintComponentDecoration(myEditor, component, g);
56
57     final Set<RadButtonGroup> paintedGroups = new HashSet<>();
58     final RadRootContainer rootContainer = myEditor.getRootContainer();
59     final ComponentTree componentTree = DesignerToolWindowManager.getInstance(myEditor).getComponentTree();
60     final Collection<RadButtonGroup> selectedGroups = componentTree != null
61                                                       ? componentTree.getSelectedElements(RadButtonGroup.class)
62                                                       : Collections.emptyList();
63
64     // Paint selection and dragger
65     FormEditingUtil.iterate(
66       component,
67       new FormEditingUtil.ComponentVisitor<RadComponent>() {
68         public boolean visit(final RadComponent component) {
69           final Point point = SwingUtilities.convertPoint(
70             component.getDelegee(),
71             0,
72             0,
73             rootContainer.getDelegee()
74           );
75           RadButtonGroup group = (RadButtonGroup)FormEditingUtil.findGroupForComponent(rootContainer, component);
76           if (group != null && !paintedGroups.contains(group) && (component.isSelected() || selectedGroups.contains(group))) {
77             paintedGroups.add(group);
78             Painter.paintButtonGroupLines(rootContainer, group, g);
79           }
80           g.translate(point.x, point.y);
81           try{
82             if (myEditor.isShowComponentTags() && FormEditingUtil.isComponentSwitchedInView(component)) {
83               Painter.paintComponentTag(component, g);
84             }
85             Painter.paintSelectionDecoration(component, g,myEditor.getGlassLayer().isFocusOwner());
86             // Over selection we have to paint dragger
87             if (component.hasDragger()){
88               final Icon icon = getDragIcon();
89               icon.paintIcon(PassiveDecorationLayer.this, g, - icon.getIconWidth(), - icon.getIconHeight());
90             }
91           }finally{
92             g.translate(-point.x, -point.y);
93           }
94           return true;
95         }
96       }
97     );
98   }
99
100   private static Icon getDragIcon() {
101     return UIDesignerIcons.Drag;
102   }
103
104   public void paint(final Graphics g){
105     // Passive decoration
106     final RadRootContainer root = myEditor.getRootContainer();
107     for(int i = root.getComponentCount() - 1; i >= 0; i--){
108       final RadComponent component = root.getComponent(i);
109       paintPassiveDecoration(component, g);
110     }
111
112     // Paint active decorators
113     paintChildren(g);
114   }
115 }