samples/extensibleicon/test/org/apidesign/extensibleicon/ModifiableIcon.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 30 Oct 2014 21:30:10 +0100
changeset 409 40cabcdcd2be
permissions -rw-r--r--
Updating to NBMs from NetBeans 8.0.1 as some of them are required to run on JDK8
     1 package org.apidesign.extensibleicon;
     2 
     3 import java.awt.Component;
     4 import java.awt.Graphics;
     5 import java.io.IOException;
     6 import org.openide.util.Lookup;
     7 import org.openide.util.lookup.AbstractLookup;
     8 import org.openide.util.lookup.InstanceContent;
     9 
    10 // BEGIN: ext.modifiable.icon
    11 public final class ModifiableIcon implements ExtIcon {
    12     // AbstractLookup is helper implementation
    13     // so that people do not need to write its own
    14     // lookup from scratch
    15     private AbstractLookup lookup;
    16     // InstanceContent is interface that gives
    17     // "creator of the lookup" more rights, like
    18     // ability to modify the content
    19     private InstanceContent ic;
    20     
    21     ModifiableIcon() {
    22         ic = new InstanceContent();
    23         lookup = new AbstractLookup(ic);
    24     }
    25     
    26     public Lookup getLookup() {
    27         return lookup;
    28     }
    29     
    30     public void markModified() {
    31         if (lookup.lookup(ModifiedImpl.class) == null) {
    32             ic.add(new ModifiedImpl());
    33         }
    34     }
    35     
    36     private final class ModifiedImpl implements Modified {
    37         public void save() throws IOException {
    38             // save somehow
    39         }
    40 
    41         public void discard() throws IOException {
    42             // discard changes
    43         }
    44     }
    45 // FINISH: ext.modifiable.icon
    46 
    47     public void paintIcon(Component c, Graphics g, int x, int y) {
    48         g.fillRect(x, y, 16, 16);
    49     }
    50 
    51     public int getIconWidth() {
    52         return 16;
    53     }
    54 
    55     public int getIconHeight() {
    56         return 16;
    57     }
    58 }