# HG changeset patch # User Jaroslav Tulach # Date 1213429827 -7200 # Node ID 32b39aaa68f02c490f8930022dab95497aa0d426 Adding friend access example diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/nbproject/genfiles.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/nbproject/genfiles.properties Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,8 @@ +build.xml.data.CRC32=b1c774b2 +build.xml.script.CRC32=fa64c0b4 +build.xml.stylesheet.CRC32=d5b6853a +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=b1c774b2 +nbproject/build-impl.xml.script.CRC32=d7cec00a +nbproject/build-impl.xml.stylesheet.CRC32=3a518818 diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/nbproject/project.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/nbproject/project.properties Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,53 @@ +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/friendpackage.jar +dist.javadoc.dir=${dist.dir}/javadoc +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.source=1.5 +javac.target=1.5 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${libs.junit.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding= +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +meta.inf.dir=${src.dir}/META-INF +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project +# (you may also define separate properties like run-sys-prop.name=value instead of -Dname=value +# or test-sys-prop.name=value to set system properties for unit tests): +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +src.dir=src +test.src.dir=test diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/nbproject/project.xml Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,16 @@ + + + org.netbeans.modules.java.j2seproject + + + friendpackage + 1.6.5 + + + + + + + + + diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/src/apipkg/AccessorImpl.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/src/apipkg/AccessorImpl.java Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,32 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package apipkg; + +import implpkg.Accessor; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** The bridge between api and impl package. + * + * @author Jaroslav Tulach + */ +final class AccessorImpl extends Accessor { + protected Item newItem() { + return new Item(); + } + + protected void addChangeListener(Item item, ChangeListener l) { + item.addChangeListener(l); + } +} diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/src/apipkg/Item.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/src/apipkg/Item.java Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,59 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package apipkg; + +import implpkg.Accessor; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; + +/** Class in API that everyone can use. + * + * @author Jaroslav Tulach + */ +public final class Item { + private int value; + private ChangeListener listener; + + static { + Accessor.DEFAULT = new AccessorImpl(); + } + + /** Contructor for friends */ + Item() { + } + + /** Anyone can value of the item. At least if it can get a reference to it. + */ + public void setValue(int x) { + value = x; + ChangeListener l = listener; + if (l != null) { + l.stateChanged(new ChangeEvent(this)); + } + } + + /** Anyone can get the value of the item. + */ + public int getValue() { + return value; + } + + /** Only the impl package can listen. + */ + void addChangeListener(ChangeListener l) { + assert listener == null; + listener = l; + } + +} diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/src/implpkg/Accessor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/src/implpkg/Accessor.java Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,38 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package implpkg; + +import apipkg.Item; +import javax.swing.event.ChangeListener; + +/** + * + * @author Jaroslav Tulach + */ +public abstract class Accessor { + public static Accessor DEFAULT; + static { + try { + Class.forName(Item.class.getName(), true, Item.class.getClassLoader()); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + + public Accessor() { + } + + protected abstract Item newItem(); + protected abstract void addChangeListener(Item item, ChangeListener l); +} diff -r 000000000000 -r 32b39aaa68f0 samples/friendpackage/test/implpkg/AccessorTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/friendpackage/test/implpkg/AccessorTest.java Sat Jun 14 09:50:27 2008 +0200 @@ -0,0 +1,59 @@ +/* + * Sun Public License Notice + * + * The contents of this file are subject to the Sun Public License + * Version 1.0 (the "License"). You may not use this file except in + * compliance with the License. A copy of the License is available at + * http://www.sun.com/ + * + * The Original Code is NetBeans. The Initial Developer of the Original + * Code is Sun Microsystems, Inc. Portions Copyright 1999-2006 Sun + * Microsystems, Inc. All Rights Reserved. + */ + +package implpkg; + +import javax.swing.event.ChangeEvent; +import junit.framework.TestCase; +import junit.framework.*; +import apipkg.Item; +import javax.swing.event.ChangeListener; + +/** + * + * @author Jaroslav Tulach + */ +public class AccessorTest extends TestCase +implements ChangeListener { + + private int cnt; + + public AccessorTest(String testName) { + super(testName); + } + + protected void setUp() throws Exception { + } + + protected void tearDown() throws Exception { + } + + public void testGetTheItemAttachListenerChangeValue() { + Item item = Accessor.DEFAULT.newItem(); + assertNotNull("Some item is really created", item); + + Accessor.DEFAULT.addChangeListener(item, this); + + item.setValue(10); + assertEquals("Value is 10", 10, item.getValue()); + cnt = 0; + item.setValue(7); + assertEquals("Now it is 7", 7, item.getValue()); + + assertEquals("There was one change", 1, cnt); + } + + public void stateChanged(ChangeEvent e) { + cnt++; + } +}