Saturday Dec 08, 2012

Savable in Widget Lookup on Move Action

Possible from 7.3 onwards, since Widget now implements Lookup.Provider for the first time:
import java.awt.Point;
import java.io.IOException;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.MoveProvider;
import org.netbeans.api.visual.widget.LabelWidget;
import org.netbeans.api.visual.widget.Scene;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.spi.actions.AbstractSavable;
import org.openide.util.Lookup;
import org.openide.util.lookup.AbstractLookup;
import org.openide.util.lookup.InstanceContent;
import org.openide.windows.TopComponent;

public class MyWidget extends LabelWidget {
    
    private MySavable mySavable;
    private Lookup lookup;
    private TopComponent tc;
    private InstanceContent ic;
    
    public MyWidget(Scene scene, String label, TopComponent tc) {
        super(scene, label);
        this.tc = tc;
        ic = new InstanceContent();
        getActions().addAction(ActionFactory.createMoveAction(null, new MoveStrategyProvider()));
    }
    
    @Override
    public Lookup getLookup() {
        if (lookup == null) {
            lookup = new AbstractLookup(ic);
        }
        return lookup;
    }
    
    private class MoveStrategyProvider implements MoveProvider {
        @Override
        public void movementStarted(Widget widget) {
        }
        @Override
        public void movementFinished(Widget widget) {
            modify();
        }
        @Override
        public Point getOriginalLocation(Widget widget) {
            return ActionFactory.createDefaultMoveProvider().getOriginalLocation(widget);
        }
        @Override
        public void setNewLocation(Widget widget, Point point) {
            ActionFactory.createDefaultMoveProvider().setNewLocation(widget, point);
        }
    }
    
    private void modify() {
        if (getLookup().lookup(MySavable.class) == null) {
            ic.add(mySavable = new MySavable());
        }
    }
    
    private class MySavable extends AbstractSavable {
        public MySavable() {
            register();
        }
        TopComponent tc() {
            return tc;
        }
        @Override
        protected String findDisplayName() {
            return getLabel();
        }
        @Override
        protected void handleSave() throws IOException {
            ic.remove(mySavable);
            unregister();
        }
        @Override
        public boolean equals(Object obj) {
            if (obj instanceof MySavable) {
                MySavable m = (MySavable) obj;
                return tc() == m.tc();
            }
            return false;
        }
        @Override
        public int hashCode() {
            return tc().hashCode();
        }
    }

}

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« December 2012 »
SunMonTueWedThuFriSat
      
1
15
23
24
25
29
     
Today