1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/extensibleicon/test/org/apidesign/extensibleicon/ModifiableIcon.java Sat Jun 14 10:09:05 2008 +0200
1.3 @@ -0,0 +1,58 @@
1.4 +package org.apidesign.extensibleicon;
1.5 +
1.6 +import java.awt.Component;
1.7 +import java.awt.Graphics;
1.8 +import java.io.IOException;
1.9 +import org.openide.util.Lookup;
1.10 +import org.openide.util.lookup.AbstractLookup;
1.11 +import org.openide.util.lookup.InstanceContent;
1.12 +
1.13 +// BEGIN: ext.modifiable.icon
1.14 +public final class ModifiableIcon implements ExtIcon {
1.15 + // AbstractLookup is helper implementation
1.16 + // so that people do not need to write its own
1.17 + // lookup from scratch
1.18 + private AbstractLookup lookup;
1.19 + // InstanceContent is interface that gives
1.20 + // "creator of the lookup" more rights, like
1.21 + // ability to modify the content
1.22 + private InstanceContent ic;
1.23 +
1.24 + ModifiableIcon() {
1.25 + ic = new InstanceContent();
1.26 + lookup = new AbstractLookup(ic);
1.27 + }
1.28 +
1.29 + public Lookup getLookup() {
1.30 + return lookup;
1.31 + }
1.32 +
1.33 + public void markModified() {
1.34 + if (lookup.lookup(ModifiedImpl.class) == null) {
1.35 + ic.add(new ModifiedImpl());
1.36 + }
1.37 + }
1.38 +
1.39 + private final class ModifiedImpl implements Modified {
1.40 + public void save() throws IOException {
1.41 + // save somehow
1.42 + }
1.43 +
1.44 + public void discard() throws IOException {
1.45 + // discard changes
1.46 + }
1.47 + }
1.48 +// FINISH: ext.modifiable.icon
1.49 +
1.50 + public void paintIcon(Component c, Graphics g, int x, int y) {
1.51 + g.fillRect(x, y, 16, 16);
1.52 + }
1.53 +
1.54 + public int getIconWidth() {
1.55 + return 16;
1.56 + }
1.57 +
1.58 + public int getIconHeight() {
1.59 + return 16;
1.60 + }
1.61 +}