Showing more code to understand the use of AbstractLookup
authorJaroslav Tulach <jtulach@netbeans.org>
Sat, 14 Jun 2008 10:09:05 +0200
changeset 2569d7d928a6b52
parent 255 6ef1415912c1
child 257 95ff2df63db0
Showing more code to understand the use of AbstractLookup
samples/extensibleicon/nbproject/build-impl.xml
samples/extensibleicon/test/org/apidesign/extensibleicon/ModifiableIcon.java
samples/extensibleicon/test/org/apidesign/extensibleicon/ModifiedTest.java
     1.1 --- a/samples/extensibleicon/nbproject/build-impl.xml	Sat Jun 14 10:08:59 2008 +0200
     1.2 +++ b/samples/extensibleicon/nbproject/build-impl.xml	Sat Jun 14 10:09:05 2008 +0200
     1.3 @@ -19,7 +19,7 @@
     1.4    - cleanup
     1.5  
     1.6          -->
     1.7 -<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject2="http://www.netbeans.org/ns/j2se-project/2" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="extensibleicon-impl">
     1.8 +<project xmlns:j2seproject1="http://www.netbeans.org/ns/j2se-project/1" xmlns:j2seproject3="http://www.netbeans.org/ns/j2se-project/3" xmlns:jaxrpc="http://www.netbeans.org/ns/j2se-project/jax-rpc" basedir=".." default="default" name="extensibleicon-impl">
     1.9      <target depends="test,jar,javadoc" description="Build and test whole project." name="default"/>
    1.10      <!-- 
    1.11                  ======================
    1.12 @@ -235,7 +235,9 @@
    1.13              <attribute default="${build.classes.dir}" name="dir"/>
    1.14              <sequential>
    1.15                  <nbjpdareload>
    1.16 -                    <fileset dir="@{dir}" includes="${fix.includes}*.class"/>
    1.17 +                    <fileset dir="@{dir}" includes="${fix.classes}">
    1.18 +                        <include name="${fix.includes}*.class"/>
    1.19 +                    </fileset>
    1.20                  </nbjpdareload>
    1.21              </sequential>
    1.22          </macrodef>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/samples/extensibleicon/test/org/apidesign/extensibleicon/ModifiableIcon.java	Sat Jun 14 10:09:05 2008 +0200
     2.3 @@ -0,0 +1,58 @@
     2.4 +package org.apidesign.extensibleicon;
     2.5 +
     2.6 +import java.awt.Component;
     2.7 +import java.awt.Graphics;
     2.8 +import java.io.IOException;
     2.9 +import org.openide.util.Lookup;
    2.10 +import org.openide.util.lookup.AbstractLookup;
    2.11 +import org.openide.util.lookup.InstanceContent;
    2.12 +
    2.13 +// BEGIN: ext.modifiable.icon
    2.14 +public final class ModifiableIcon implements ExtIcon {
    2.15 +    // AbstractLookup is helper implementation
    2.16 +    // so that people do not need to write its own
    2.17 +    // lookup from scratch
    2.18 +    private AbstractLookup lookup;
    2.19 +    // InstanceContent is interface that gives
    2.20 +    // "creator of the lookup" more rights, like
    2.21 +    // ability to modify the content
    2.22 +    private InstanceContent ic;
    2.23 +    
    2.24 +    ModifiableIcon() {
    2.25 +        ic = new InstanceContent();
    2.26 +        lookup = new AbstractLookup(ic);
    2.27 +    }
    2.28 +    
    2.29 +    public Lookup getLookup() {
    2.30 +        return lookup;
    2.31 +    }
    2.32 +    
    2.33 +    public void markModified() {
    2.34 +        if (lookup.lookup(ModifiedImpl.class) == null) {
    2.35 +            ic.add(new ModifiedImpl());
    2.36 +        }
    2.37 +    }
    2.38 +    
    2.39 +    private final class ModifiedImpl implements Modified {
    2.40 +        public void save() throws IOException {
    2.41 +            // save somehow
    2.42 +        }
    2.43 +
    2.44 +        public void discard() throws IOException {
    2.45 +            // discard changes
    2.46 +        }
    2.47 +    }
    2.48 +// FINISH: ext.modifiable.icon
    2.49 +
    2.50 +    public void paintIcon(Component c, Graphics g, int x, int y) {
    2.51 +        g.fillRect(x, y, 16, 16);
    2.52 +    }
    2.53 +
    2.54 +    public int getIconWidth() {
    2.55 +        return 16;
    2.56 +    }
    2.57 +
    2.58 +    public int getIconHeight() {
    2.59 +        return 16;
    2.60 +    }
    2.61 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/extensibleicon/test/org/apidesign/extensibleicon/ModifiedTest.java	Sat Jun 14 10:09:05 2008 +0200
     3.3 @@ -0,0 +1,28 @@
     3.4 +package org.apidesign.extensibleicon;
     3.5 +
     3.6 +import org.junit.After;
     3.7 +import org.junit.Before;
     3.8 +import org.junit.Test;
     3.9 +import static org.junit.Assert.*;
    3.10 +
    3.11 +public class ModifiedTest {
    3.12 +    private ModifiableIcon icon;
    3.13 +    
    3.14 +    public ModifiedTest() {
    3.15 +    }
    3.16 +
    3.17 +    @Before
    3.18 +    public void setUp() {
    3.19 +        icon = new ModifiableIcon();
    3.20 +    }
    3.21 +
    3.22 +    @After
    3.23 +    public void tearDown() {
    3.24 +    }
    3.25 +
    3.26 +    @Test public void testEnabledDisabledState() {
    3.27 +        assertNull("Not yet modified", icon.getLookup().lookup(Modified.class));
    3.28 +        icon.markModified();
    3.29 +        assertNotNull("Modified", icon.getLookup().lookup(Modified.class));
    3.30 +    }
    3.31 +}
    3.32 \ No newline at end of file