-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mcculls
committed
May 18, 2010
1 parent
04adaec
commit cbc134d
Showing
217 changed files
with
11,387 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0"?> | ||
<project name="chapter05" default="dist"> | ||
<property name="chapter" value="chapter05"/> | ||
<import file="../build.xml"/> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0"?> | ||
<project name="paint.example" default="dist"> | ||
|
||
<property name="version" value="5.0"/> | ||
<dirname property="example.dir" file="${ant.file.paint.example}"/> | ||
<import file="../build.xml"/> | ||
|
||
<target name="shape"> | ||
<ant dir="${example.dir}/org.foo.shape" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="circle"> | ||
<ant dir="${example.dir}/org.foo.shape.circle" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="circle.resource-de"> | ||
<ant dir="${example.dir}/org.foo.shape.circle.resource-de" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="triangle"> | ||
<ant dir="${example.dir}/org.foo.shape.triangle" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="triangle.resource-de"> | ||
<ant dir="${example.dir}/org.foo.shape.triangle.resource-de" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="square"> | ||
<ant dir="${example.dir}/org.foo.shape.square" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="square.resource-de"> | ||
<ant dir="${example.dir}/org.foo.shape.square.resource-de" inheritAll="false"/> | ||
</target> | ||
|
||
<target name="paint"> | ||
<ant dir="${example.dir}/org.foo.paint" inheritAll="false"/> | ||
</target> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#------------------------------------------------- | ||
title=Paint Example - Paint Frame | ||
#------------------------------------------------- | ||
|
||
module=org.foo.paint | ||
custom=true | ||
|
||
Bundle-Activator: ${module}.Activator | ||
|
||
Private-Package: \ | ||
${module} | ||
|
||
Import-Package: \ | ||
org.foo.shape;version="[5.0,6.0)", \ | ||
org.osgi.framework;version="[1.3,2.0)", \ | ||
org.osgi.util.tracker;version="[1.3,2.0)", \ | ||
javax.swing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0"?> | ||
<project name="paint" default="dist"> | ||
|
||
<property file="build.properties"/> | ||
<import file="../build.xml"/> | ||
|
||
<target name="compile" depends="shape,common.compile"/> | ||
|
||
<target name="local.dist" depends="common.local.dist,launcher,shell"> | ||
<copy file="${launcher.jar}" todir="${example.dir}"/> | ||
<copy todir="${dist}"> | ||
<path refid="shell.tty"/> | ||
</copy> | ||
</target> | ||
|
||
<target name="local.clean" depends="common.local.clean"> | ||
<delete file="${example.dir}/launcher.jar"/> | ||
</target> | ||
|
||
</project> |
100 changes: 100 additions & 0 deletions
100
chapter05/paint-example/org.foo.paint/src/org/foo/paint/Activator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.foo.paint; | ||
|
||
import java.awt.event.WindowAdapter; | ||
import java.awt.event.WindowEvent; | ||
import javax.swing.JFrame; | ||
import javax.swing.SwingUtilities; | ||
import org.osgi.framework.*; | ||
|
||
/** | ||
* The activator of the host application bundle. The activator creates the main | ||
* application <tt>JFrame</tt> and starts tracking <tt>SimpleShape</tt> | ||
* services. All activity is performed on the Swing event thread to avoid | ||
* synchronization and repainting issues. Closing the application window will | ||
* result in <tt>Bundle.stop()</tt> being called on the system bundle, which | ||
* will cause the framework to shutdown and the JVM to exit. | ||
**/ | ||
public class Activator implements BundleActivator, Runnable { | ||
private BundleContext m_context = null; | ||
private PaintFrame m_frame = null; | ||
private ShapeTracker m_shapetracker = null; | ||
|
||
/** | ||
* Displays the applications window and starts service tracking; everything is | ||
* done on the Swing event thread to avoid synchronization and repainting | ||
* issues. | ||
* | ||
* @param context The context of the bundle. | ||
**/ | ||
public void start(BundleContext context) { | ||
m_context = context; | ||
if (SwingUtilities.isEventDispatchThread()) { | ||
run(); | ||
} else { | ||
try { | ||
javax.swing.SwingUtilities.invokeAndWait(this); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Stops service tracking and disposes of the application window. | ||
* | ||
* @param context The context of the bundle. | ||
**/ | ||
public void stop(BundleContext context) { | ||
m_shapetracker.close(); | ||
final PaintFrame frame = m_frame; | ||
javax.swing.SwingUtilities.invokeLater(new Runnable() { | ||
public void run() { | ||
frame.setVisible(false); | ||
frame.dispose(); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* This method actually performs the creation of the application window. It is | ||
* intended to be called by the Swing event thread and should not be called | ||
* directly. | ||
**/ | ||
public void run() { | ||
m_frame = new PaintFrame(); | ||
|
||
m_frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); | ||
m_frame.addWindowListener(new WindowAdapter() { | ||
public void windowClosing(WindowEvent evt) { | ||
try { | ||
m_context.getBundle(0).stop(); | ||
} catch (BundleException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
}); | ||
|
||
m_frame.setVisible(true); | ||
|
||
m_shapetracker = new ShapeTracker(m_context, m_frame); | ||
m_shapetracker.open(); | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
chapter05/paint-example/org.foo.paint/src/org/foo/paint/DefaultShape.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.foo.paint; | ||
|
||
import java.awt.*; | ||
import javax.swing.ImageIcon; | ||
import org.foo.shape.SimpleShape; | ||
import org.osgi.framework.BundleContext; | ||
import org.osgi.framework.ServiceReference; | ||
|
||
/** | ||
* This class is used as a proxy to defer object creation from shape provider | ||
* bundles and also as a placeholder shape when previously used shapes are no | ||
* longer available. These two purposes are actually orthogonal, but were | ||
* combined into a single class to reduce the number of classes in the | ||
* application. The proxy-related functionality is introduced as a way to lazily | ||
* create shape objects in an effort to improve performance; this level of | ||
* indirection could be removed if eager creation of objects is not a concern. | ||
* Since this application uses the service-based extension appraoch, lazy shape | ||
* creation will only come into effect if service providers register service | ||
* factories instead of directly registering <tt>SimpleShape</tt> or if they use | ||
* a technology like Declarative Services or iPOJO to register services. Since | ||
* the example providers register services instances directly there is no | ||
* laziness in the example, but the proxy approach is still used to demonstrate | ||
* how to make laziness possible and to keep it similar to the extender-based | ||
* approach. | ||
**/ | ||
class DefaultShape implements SimpleShape { | ||
private SimpleShape m_shape; | ||
private ImageIcon m_icon; | ||
private BundleContext m_context; | ||
private ServiceReference m_ref; | ||
|
||
/** | ||
* This constructs a placeholder shape that draws a default icon. It is used | ||
* when a previously drawn shape is no longer available. | ||
**/ | ||
public DefaultShape() { | ||
// Do nothing. | ||
} | ||
|
||
/** | ||
* This constructs a proxy shape that lazily gets the shape service. | ||
* | ||
* @param context The bundle context to use for retrieving the shape service. | ||
* @param ref The service reference of the service. | ||
**/ | ||
public DefaultShape(BundleContext context, ServiceReference ref) { | ||
m_context = context; | ||
m_ref = ref; | ||
} | ||
|
||
/** | ||
* This method tells the proxy to dispose of its service object; this is | ||
* called when the underlying service goes away. | ||
**/ | ||
public void dispose() { | ||
if (m_shape != null) { | ||
m_context.ungetService(m_ref); | ||
m_context = null; | ||
m_ref = null; | ||
m_shape = null; | ||
} | ||
} | ||
|
||
/** | ||
* Implements the <tt>SimpleShape</tt> interface method. When acting as a | ||
* proxy, this method gets the shape service and then uses it to draw the | ||
* shape. When acting as a placeholder shape, this method draws the default | ||
* icon. | ||
* | ||
* @param g2 The graphics object used for painting. | ||
* @param p The position to paint the triangle. | ||
**/ | ||
public void draw(Graphics2D g2, Point p) { | ||
// If this is a proxy shape, instantiate the shape class | ||
// and use it to draw the shape. | ||
if (m_context != null) { | ||
try { | ||
if (m_shape == null) { | ||
// Get the shape service. | ||
m_shape = (SimpleShape) m_context.getService(m_ref); | ||
} | ||
// Draw the shape. | ||
m_shape.draw(g2, p); | ||
// If everything was successful, then simply return. | ||
return; | ||
} catch (Exception ex) { | ||
// This generally should not happen, but if it does then | ||
// we can just fall through and paint the default icon. | ||
} | ||
} | ||
|
||
// If the proxied shape could not be drawn for any reason or if | ||
// this shape is simply a placeholder, then draw the default icon. | ||
if (m_icon == null) { | ||
try { | ||
m_icon = new ImageIcon(this.getClass().getResource("underc.png")); | ||
} catch (Exception ex) { | ||
ex.printStackTrace(); | ||
g2.setColor(Color.red); | ||
g2.fillRect(0, 0, 60, 60); | ||
return; | ||
} | ||
} | ||
g2.drawImage(m_icon.getImage(), 0, 0, null); | ||
} | ||
} |
Oops, something went wrong.