1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Pavel Dolgov, Anton Avtamonov
19 * @version $Revision$
20 */
21package org.apache.harmony.awt;
22
23import java.awt.Component;
24import java.awt.Rectangle;
25
26import org.apache.harmony.awt.gl.MultiRectArea;
27import org.apache.harmony.awt.internal.nls.Messages;
28
29public class ClipRegion extends Rectangle {
30    private final MultiRectArea clip;
31
32    public ClipRegion(final MultiRectArea clip) {
33        this.clip = new MultiRectArea(clip);
34        setBounds(clip.getBounds());
35    }
36
37    public MultiRectArea getClip() {
38        return clip;
39    }
40
41    @Override
42    public String toString() {
43        String str = clip.toString();
44        int i = str.indexOf('[');
45        str = str.substring(i);
46        if (clip.getRectCount() == 1) {
47            str = str.substring(1, str.length() - 1);
48        }
49        return getClass().getName() + str;
50    }
51
52
53    public void convertRegion(final Component child, final Component parent) {
54        convertRegion(child, clip, parent);
55    }
56
57    public void intersect(final Rectangle rect) {
58        clip.intersect(rect);
59    }
60
61    @Override
62    public boolean isEmpty() {
63        return clip.isEmpty();
64    }
65
66    public static void convertRegion(final Component child,
67                                     final MultiRectArea region,
68                                     final Component parent) {
69        int x = 0, y = 0;
70        Component c = child;
71        //???AWT
72        /*
73        for (; c != null && c != parent; c = c.getParent()) {
74            x += c.getX();
75            y += c.getY();
76        }
77        */
78        if (c == null) {
79            // awt.51=Component expected to be a parent
80            throw new IllegalArgumentException(Messages.getString("awt.51")); //$NON-NLS-1$
81        }
82        region.translate(x, y);
83    }
84}
85