blob: f1391e382ad61d6f3c8daaa854eb94123a7d3d31 [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.circle;
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.Ellipse2D;
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 circle
36 * <tt>SimpleShape</tt> service. This activator simply creates an instance
37 * of the circle 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 circle <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, "Circle");
54 dict.put(SimpleShape.ICON_PROPERTY,
55 new ImageIcon(this.getClass().getResource("circle.png")));
56 m_context.registerService(
57 SimpleShape.class.getName(), new Circle(), 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 circle <tt>SimpleShape</tt> service.
71 * It simply provides a <tt>draw()</tt> that paints a circle.
72 **/
73 public class Circle 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.RED, x + 50, y, Color.WHITE);
87 g2.setPaint(gradient);
88 g2.fill(new Ellipse2D.Double(x, y, 50, 50));
89 BasicStroke wideStroke = new BasicStroke(2.0f);
90 g2.setColor(Color.black);
91 g2.setStroke(wideStroke);
92 g2.draw(new Ellipse2D.Double(x, y, 50, 50));
93 }
94 }
95}