OSDN Git Service

android-2.1_r1 snapshot
[android-x86/sdk.git] / eclipse / plugins / com.android.ide.eclipse.adt / src / com / android / ide / eclipse / adt / internal / editors / layout / parts / LayoutFigure.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php
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
17 package com.android.ide.eclipse.adt.internal.editors.layout.parts;
18
19 import com.android.ide.eclipse.adt.internal.editors.layout.parts.UiElementsEditPartFactory.IOutlineProvider;
20 import com.android.ide.eclipse.adt.internal.editors.layout.parts.UiLayoutEditPart.HighlightInfo;
21
22 import org.eclipse.draw2d.ColorConstants;
23 import org.eclipse.draw2d.Figure;
24 import org.eclipse.draw2d.Graphics;
25 import org.eclipse.draw2d.geometry.Rectangle;
26 import org.eclipse.swt.SWT;
27
28 /**
29  * The figure used to draw the feedback on a layout.
30  * <p/>
31  * By default the figure is transparent and empty.
32  * The base {@link ElementFigure} knows how to draw the selection border.
33  * This figure knows how to draw the drop feedback.
34  *
35  * @since GLE1
36  */
37 class LayoutFigure extends ElementFigure {
38
39     private HighlightInfo mHighlightInfo;
40
41     public LayoutFigure(IOutlineProvider provider) {
42         super(provider);
43     }
44
45     public void setHighlighInfo(HighlightInfo highlightInfo) {
46         mHighlightInfo = highlightInfo;
47         repaint();
48     }
49
50     /**
51      * Paints the "border" for this figure.
52      * <p/>
53      * The parent {@link Figure#paint(Graphics)} calls {@link #paintFigure(Graphics)} then
54      * {@link #paintClientArea(Graphics)} then {@link #paintBorder(Graphics)}. Here we thus
55      * draw the actual highlight border but also the highlight anchor lines and points so that
56      * we can make sure they are all drawn on top of the border.
57      * <p/>
58      * Note: This method doesn't really need to restore its graphic state. The parent
59      * Figure will do it for us.
60      * <p/>
61      *
62      * @param graphics The Graphics object used for painting
63      */
64     @Override
65     protected void paintBorder(Graphics graphics) {
66         super.paintBorder(graphics);
67
68         if (mHighlightInfo == null) {
69             return;
70         }
71
72         // Draw the border. We want other highlighting to be drawn on top of the border.
73         if (mHighlightInfo.drawDropBorder) {
74             graphics.setLineWidth(3);
75             graphics.setLineStyle(SWT.LINE_SOLID);
76             graphics.setForegroundColor(ColorConstants.green);
77             graphics.drawRectangle(getInnerBounds().getCopy().shrink(1, 1));
78         }
79
80         Rectangle bounds = getBounds();
81         int bx = bounds.x;
82         int by = bounds.y;
83         int w = bounds.width;
84         int h = bounds.height;
85
86         // Draw frames of target child parts, if any
87         if (mHighlightInfo.childParts != null) {
88             graphics.setLineWidth(2);
89             graphics.setLineStyle(SWT.LINE_DOT);
90             graphics.setForegroundColor(ColorConstants.lightBlue);
91             for (UiElementEditPart part : mHighlightInfo.childParts) {
92                 if (part != null) {
93                     graphics.drawRectangle(part.getBounds().getCopy().translate(bx, by));
94                 }
95             }
96         }
97
98         // Draw the target line, if any
99         if (mHighlightInfo.linePoints != null) {
100             int x1 = mHighlightInfo.linePoints[0].x;
101             int y1 = mHighlightInfo.linePoints[0].y;
102             int x2 = mHighlightInfo.linePoints[1].x;
103             int y2 = mHighlightInfo.linePoints[1].y;
104
105             // if the line is right to the edge, draw it one pixel more inside so that the
106             // full 2-pixel width be visible.
107             if (x1 <= 0) x1++;
108             if (x2 <= 0) x2++;
109             if (y1 <= 0) y1++;
110             if (y2 <= 0) y2++;
111
112             if (x1 >= w - 1) x1--;
113             if (x2 >= w - 1) x2--;
114             if (y1 >= h - 1) y1--;
115             if (y2 >= h - 1) y2--;
116
117             x1 += bx;
118             x2 += bx;
119             y1 += by;
120             y2 += by;
121
122             graphics.setLineWidth(2);
123             graphics.setLineStyle(SWT.LINE_DASH);
124             graphics.setLineCap(SWT.CAP_ROUND);
125             graphics.setForegroundColor(ColorConstants.orange);
126             graphics.drawLine(x1, y1, x2, y2);
127         }
128
129         // Draw the anchor point, if any
130         if (mHighlightInfo.anchorPoint != null) {
131             int x = mHighlightInfo.anchorPoint.x;
132             int y = mHighlightInfo.anchorPoint.y;
133
134             // If the point is right on the edge, draw it one pixel inside so that it
135             // matches the highlight line. It makes it slightly more visible that way.
136             if (x <= 0) x++;
137             if (y <= 0) y++;
138             if (x >= w - 1) x--;
139             if (y >= h - 1) y--;
140             x += bx;
141             y += by;
142
143             graphics.setLineWidth(2);
144             graphics.setLineStyle(SWT.LINE_SOLID);
145             graphics.setLineCap(SWT.CAP_ROUND);
146             graphics.setForegroundColor(ColorConstants.orange);
147             graphics.drawLine(x-5, y-5, x+5, y+5);
148             graphics.drawLine(x-5, y+5, x+5, y-5);
149             // 7 * cos(45) == 5 so we use 8 for the circle radius (it looks slightly better than 7)
150             graphics.setLineWidth(1);
151             graphics.drawOval(x-8, y-8, 16, 16);
152         }
153     }
154 }