Lookup Event

Alles zu NetBeans als RCP-Platform

Moderator: wegus

Antworten
meiJung
Beiträge: 5
Registriert: 05.09.2013, 13:17

Lookup Event

Beitrag von meiJung » 13.09.2013, 16:12

Servus,

habe mir zum Verständnis des Lookups mal folgende kleine Beispiel-App gebaut:
2 Module, ein Receiver-, ein Sender-Modul.
Sender: TopComponent mit 3 Buttons
Receiver: TabbedPane mit 3 Tabs und Textarea.
Verhalten: der gedrückte Button beim Sender bestimmt, welcher Tab beim Receiver aktiv sein soll.
Zusätzlich lass ich mir in der Textarea sowohl das LookupEvent als auch das ActionEvent von den Buttons ausgeben.

die Frage: die Ausgabe in der TextArea wundert mich: woher kommt das einzelne "Zwischen-LookupEvent", wenn ich zB NACH Click auf einen Button wieder auf einen Button Klicke oder hiernach auch auf einen Tab-Reiter?

das ist die Klicksequenz, und nachfolgend die Ausgabe:
Button1, Button3, Button3, HorizontalScrollPane (der Textarea im ReceiverTopComponent)
Ausgabe:

Code: Alles auswählen

LookupEvent le = { org.openide.util.LookupEvent[source=org.openide.util.lookup.SimpleProxyLookup$ProxyResult@2da6852a]};
ActionEvent aevt = { java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=jButton1,when=1379084743257,modifiers=Button1] on javax.swing.JButton[,37,13,79x25,alignmentX=0.0,alignmentY=0.5,border=com.sun.java.swing.plaf.windows.XPStyle$XPEmptyBorder@f709fd0,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=jButton1,defaultCapable=true]};
--  --  --  --  --  --  --  --  --  --  --  --  --  --
LookupEvent le = { org.openide.util.LookupEvent[source=org.openide.util.lookup.SimpleProxyLookup$ProxyResult@2da6852a]};
--  --  --  --  --  --  --  --  --  --  --  --  --  --
LookupEvent le = { org.openide.util.LookupEvent[source=org.openide.util.lookup.SimpleProxyLookup$ProxyResult@2da6852a]};
ActionEvent aevt = { java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=jButton3,when=1379084746487,modifiers=Button1] on javax.swing.JButton[,37,77,79x25,alignmentX=0.0,alignmentY=0.5,border=com.sun.java.swing.plaf.windows.XPStyle$XPEmptyBorder@f709fd0,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=jButton3,defaultCapable=true]};
--  --  --  --  --  --  --  --  --  --  --  --  --  --
LookupEvent le = { org.openide.util.LookupEvent[source=org.openide.util.lookup.SimpleProxyLookup$ProxyResult@2da6852a]};
--  --  --  --  --  --  --  --  --  --  --  --  --  --
LookupEvent le = { org.openide.util.LookupEvent[source=org.openide.util.lookup.SimpleProxyLookup$ProxyResult@2da6852a]};
ActionEvent aevt = { java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=jButton3,when=1379084749823,modifiers=Button1] on javax.swing.JButton[,37,77,79x25,alignmentX=0.0,alignmentY=0.5,border=com.sun.java.swing.plaf.windows.XPStyle$XPEmptyBorder@f709fd0,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=jButton3,defaultCapable=true]};
--  --  --  --  --  --  --  --  --  --  --  --  --  --
LookupEvent le = { org.openide.util.LookupEvent[source=org.openide.util.lookup.SimpleProxyLookup$ProxyResult@2da6852a]};
--  --  --  --  --  --  --  --  --  --  --  --  --  --
Receiver src

Code: Alles auswählen

@ConvertAsProperties(
        dtd = "-//de.bertrandt.lookups.receiver//LookupReveiver//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "LookupReveiverTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "de.bertrandt.lookups.receiver.LookupReveiverTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_LookupReveiverAction",
        preferredID = "LookupReveiverTopComponent")
@Messages({
    "CTL_LookupReveiverAction=LookupReveiver",
    "CTL_LookupReveiverTopComponent=LookupReveiver Window",
    "HINT_LookupReveiverTopComponent=This is a LookupReveiver window"
})
public final class LookupReceiverTopComponent extends TopComponent 
    implements LookupListener{
    
    // sowas wie die "Mailbox" : Lookup.Result vom Typ ActionEvent
    private Lookup.Result<ActionEvent> result = null;

    public LookupReceiverTopComponent() {
        initComponents();
        setName(Bundle.CTL_LookupReveiverTopComponent());
        setToolTipText(Bundle.HINT_LookupReveiverTopComponent());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(LookupReceiverTopComponent.class, "LookupReceiverTopComponent.jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(LookupReceiverTopComponent.class, "LookupReceiverTopComponent.jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 395, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(org.openide.util.NbBundle.getMessage(LookupReceiverTopComponent.class, "LookupReceiverTopComponent.jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        jScrollPane1.setToolTipText(org.openide.util.NbBundle.getMessage(LookupReceiverTopComponent.class, "LookupReceiverTopComponent.jScrollPane1.toolTipText")); // NOI18N
        jScrollPane1.setFocusable(false);
        jScrollPane1.setRequestFocusEnabled(false);
        jScrollPane1.setWheelScrollingEnabled(false);

        jTextArea1.setColumns(20);
        jTextArea1.setRows(5);
        jTextArea1.setText(org.openide.util.NbBundle.getMessage(LookupReceiverTopComponent.class, "LookupReceiverTopComponent.jTextArea1.text")); // NOI18N
        jScrollPane1.setViewportView(jTextArea1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jTabbedPane1)
            .addComponent(jScrollPane1)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 253, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>                        

    // Variables declaration - do not modify                     
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextArea jTextArea1;
    // End of variables declaration                   
    @Override
    public void componentOpened() {
        // wir interessieren uns (nur)
        // für ActionEvent-Objekte aus dem globalen Lookup
        result = Utilities.actionsGlobalContext().lookupResult(ActionEvent.class);
        // geöffnete Component -> reagiere auf LookupChanges
        result.addLookupListener(this);
    }

    @Override
    public void componentClosed() {
        // geschlossene Component -> reagiere nicht auf LookupChanges
        result.removeLookupListener(this);
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }

    @Override
    public void resultChanged(LookupEvent le) {
        // sobald sich im Lookup was getan hat:
        // schreibe zum Textarea das LookupEvent le + das ActionEvent aevt dazu
        
        StringBuilder sb = new StringBuilder();        
        // das LookupEvent
        sb.append("LookupEvent le = { ");
        sb.append(le.toString());
        sb.append("};\n");        
        ActionEvent aevt;
        javax.swing.JButton button; Object source; String str;
        
        Collection<? extends ActionEvent> allEvents = result.allInstances();
        if (!allEvents.isEmpty()) {
            for (Iterator i = allEvents.iterator(); i.hasNext();) {
                aevt = (ActionEvent) i.next(); 
                // ActionEvent(s?) aus dem Lookup
                sb.append("ActionEvent aevt = { ");
                sb.append(aevt.toString());
                sb.append("};\n");
                // zusätzlich den zum Button gehörigen Tab aktivieren
                source = aevt.getSource();
                button = (JButton) aevt.getSource();
                if (source instanceof javax.swing.JButton) {
                    button = (javax.swing.JButton) source;
                    // der Tab-Index wird aus dem Button-Text extrahiert -
                    // ist natürlich ein dirty-hack - tut aber hier zum Demozwecken
                    int idx = button.getText().length()-1;
                    str = "" + button.getText().charAt( idx );
                    idx = Integer.parseInt( str );
                    jTabbedPane1.setSelectedIndex(idx-1);
                }
            }
        }
        sb.append("--  --  --  --  --  --  --  --  --  --  --  --  --  --\n");
        jTextArea1.append(sb.toString());
        
    }
}
Sender src

Code: Alles auswählen

@ConvertAsProperties(
        dtd = "-//de.bertrandt.test.lookups.sender//LookupSender//EN",
        autostore = false)
@TopComponent.Description(
        preferredID = "LookupSenderTopComponent",
        //iconBase="SET/PATH/TO/ICON/HERE", 
        persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "de.bertrandt.test.lookups.sender.LookupSenderTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
        displayName = "#CTL_LookupSenderAction",
        preferredID = "LookupSenderTopComponent")
@Messages({
    "CTL_LookupSenderAction=LookupSender",
    "CTL_LookupSenderTopComponent=LookupSender Window",
    "HINT_LookupSenderTopComponent=This is a LookupSender window"
})
public final class LookupSenderTopComponent extends TopComponent {

    private InstanceContent content = new InstanceContent();
    
    public LookupSenderTopComponent() {
        initComponents();
        setName(Bundle.CTL_LookupSenderTopComponent());
        setToolTipText(Bundle.HINT_LookupSenderTopComponent());
        associateLookup(new AbstractLookup(content));

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jButton1, org.openide.util.NbBundle.getMessage(LookupSenderTopComponent.class, "LookupSenderTopComponent.jButton1.text")); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton2, org.openide.util.NbBundle.getMessage(LookupSenderTopComponent.class, "LookupSenderTopComponent.jButton2.text")); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jButton3, org.openide.util.NbBundle.getMessage(LookupSenderTopComponent.class, "LookupSenderTopComponent.jButton3.text")); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, org.openide.util.NbBundle.getMessage(LookupSenderTopComponent.class, "LookupSenderTopComponent.jLabel1.text")); // NOI18N
        jLabel1.setToolTipText(org.openide.util.NbBundle.getMessage(LookupSenderTopComponent.class, "LookupSenderTopComponent.jLabel1.toolTipText")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton3)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1))
                    .addComponent(jButton1))
                .addContainerGap(266, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton3)
                .addContainerGap(198, Short.MAX_VALUE))
        );
    }// </editor-fold>                        

    private void passEventIntoInstanceContentThatIsAssociatedWithLookup(ActionEvent evt){
        content.set (Arrays.asList (Object.class), null);
        content.add(evt);
    }
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jLabel1.setText("2");
        passEventIntoInstanceContentThatIsAssociatedWithLookup(evt);
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jLabel1.setText("3");
        passEventIntoInstanceContentThatIsAssociatedWithLookup(evt);
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jLabel1.setText("1");
        passEventIntoInstanceContentThatIsAssociatedWithLookup(evt);
    }                                        

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    // End of variables declaration                   
    @Override
    public void componentOpened() {
        // TODO add custom code on component opening
    }

    @Override
    public void componentClosed() {
        // TODO add custom code on component closing
    }

    void writeProperties(java.util.Properties p) {
        // better to version settings since initial version as advocated at
        // http://wiki.apidesign.org/wiki/PropertyFiles
        p.setProperty("version", "1.0");
        // TODO store your settings
    }

    void readProperties(java.util.Properties p) {
        String version = p.getProperty("version");
        // TODO read your settings according to their version
    }
}

ebaumann
Beiträge: 288
Registriert: 22.01.2009, 08:53
Wohnort: Würzburg
Kontaktdaten:

Re: Lookup Event

Beitrag von ebaumann » 16.09.2013, 07:42

Habe den Code nur überflogen. Einfacher wäre er zu lesen, wären nur die relevanten Abschnitte gepostet, d.h. Stellen an denen der Content vom Sender modifiziert wird (associateLookup() ist auch relevant) sowie Code-Stellen, wo der Receiver sich beim Lookup anmeldet und auf Lookup-Änderungen reagiert. Statt dessen das ganze Projekt als ZIP anhängen, sodass es sich nachvollziehen (ausführen, debuggen) lässt.

Meine Vermutung: Mit Utilities.actionsGlobalContext() erhält der Receiver das globale Lookup, das immer enthält, was im Lookup der aktuell selektierten TC ist - jene die den Focus hat bzw. eines ihrer Children. Sobald der Focus woanders ist wird der Lookup-Inhalt der Sender-TC aus dem globalen Lookup "geleert", darauf deutet auch die Ausgabe des Events hin: Keine Action im Lookup-Content.

Antworten