blob: 55efbaa778b0e287a47e0211795bd8bf790e0aa1 [file] [log] [blame]
Richard S. Hallbad49dc2007-07-13 16:08:06 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.example.servicebased.triangle;
20
21import java.awt.BasicStroke;
22import java.awt.Color;
23import java.awt.GradientPaint;
24import java.awt.Graphics2D;
25import java.awt.Point;
Richard S. Hall57376bf2012-03-06 19:19:04 +000026import java.awt.geom.GeneralPath;
27import java.util.Dictionary;
Richard S. Hallbad49dc2007-07-13 16:08:06 +000028import java.util.Hashtable;
29import javax.swing.ImageIcon;
30import org.apache.felix.example.servicebased.host.service.SimpleShape;
Richard S. Hall57376bf2012-03-06 19:19:04 +000031import org.osgi.framework.BundleActivator;
32import org.osgi.framework.BundleContext;
Richard S. Hallbad49dc2007-07-13 16:08:06 +000033
34/**
35 * This class implements a simple bundle activator for the triangle
36 * <tt>SimpleShape</tt> service. This activator simply creates an instance
37 * of the triangle service object and registers it with the service registry
38 * along with the service properties indicating the service's name and icon.
39**/
40public class Activator implements BundleActivator
41{
42 private BundleContext m_context = null;
43
44 /**
45 * Implements the <tt>BundleActivator.start()</tt> method, which
46 * registers the triangle <tt>SimpleShape</tt> service.
47 * @param context The context for the bundle.
48 **/
49 public void start(BundleContext context)
50 {
51 m_context = context;
Richard S. Hall57376bf2012-03-06 19:19:04 +000052 Dictionary<String, Object> dict = new Hashtable<String, Object>();
Richard S. Hallbad49dc2007-07-13 16:08:06 +000053 dict.put(SimpleShape.NAME_PROPERTY, "Triangle");
54 dict.put(SimpleShape.ICON_PROPERTY,
55 new ImageIcon(this.getClass().getResource("triangle.png")));
56 m_context.registerService(
57 SimpleShape.class.getName(), new Triangle(), dict);
58 }
59
60 /**
61 * Implements the <tt>BundleActivator.start()</tt> method, which
62 * does nothing.
63 * @param context The context for the bundle.
64 **/
65 public void stop(BundleContext context)
66 {
67 }
68
69 /**
70 * This inner class implements the triangle <tt>SimpleShape</tt> service.
71 * It simply provides a <tt>draw()</tt> that paints a triangle.
72 **/
73 public class Triangle implements SimpleShape
74 {
75 /**
76 * Implements the <tt>SimpleShape.draw()</tt> method for painting
77 * the shape.
78 * @param g2 The graphics object used for painting.
79 * @param p The position to paint the triangle.
80 **/
81 public void draw(Graphics2D g2, Point p)
82 {
83 int x = p.x - 25;
84 int y = p.y - 25;
85 GradientPaint gradient = new GradientPaint(
86 x, y, Color.GREEN, x + 50, y, Color.WHITE);
87 g2.setPaint(gradient);
88 int[] xcoords = { x + 25, x, x + 50 };
89 int[] ycoords = { y, y + 50, y + 50 };
90 GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD, xcoords.length);
91 polygon.moveTo(x + 25, y);
92 for (int i = 0; i < xcoords.length; i++)
93 {
94 polygon.lineTo(xcoords[i], ycoords[i]);
95 }
96 polygon.closePath();
97 g2.fill(polygon);
98 BasicStroke wideStroke = new BasicStroke(2.0f);
99 g2.setColor(Color.black);
100 g2.setStroke(wideStroke);
101 g2.draw(polygon);
102 }
103 }
104}