001/** 002 * 003 */ 004package plugins.tutorial.event; 005 006import icy.gui.frame.IcyFrameAdapter; 007import icy.gui.frame.IcyFrameEvent; 008import icy.gui.frame.TitledFrame; 009import icy.gui.main.ActiveSequenceListener; 010import icy.gui.main.ActiveViewerListener; 011import icy.gui.viewer.Viewer; 012import icy.gui.viewer.ViewerEvent; 013import icy.main.Icy; 014import icy.plugin.abstract_.PluginActionable; 015import icy.sequence.Sequence; 016import icy.sequence.SequenceEvent; 017 018import javax.swing.JScrollPane; 019import javax.swing.JTextArea; 020import javax.swing.ScrollPaneConstants; 021 022/** 023 * @author Stephane 024 */ 025public class ActiveSequenceListenerTutorial extends PluginActionable 026{ 027 @Override 028 public void run() 029 { 030 // build a simple titled frame 031 final TitledFrame frame = new TitledFrame("Active Sequence/Viewer Event logger", true, true); 032 033 // we need a text area to put out event informations 034 final JTextArea area = new JTextArea(); 035 area.setWrapStyleWord(true); 036 area.setEditable(false); 037 038 // better to have a scroll pane as many events can occurs 039 final JScrollPane scrollPane = new JScrollPane(area); 040 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 041 scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 042 043 // build the focused sequence event listener 044 final ActiveSequenceListener focusedSequenceListener = new ActiveSequenceListener() 045 { 046 @Override 047 public void sequenceActivated(Sequence sequence) 048 { 049 area.append("Sequence " + sequence + " activated\n"); 050 area.setCaretPosition(area.getDocument().getLength()); 051 } 052 053 @Override 054 public void sequenceDeactivated(Sequence sequence) 055 { 056 area.append("Sequence " + sequence + " deactivated\n"); 057 area.setCaretPosition(area.getDocument().getLength()); 058 } 059 060 @Override 061 public void activeSequenceChanged(SequenceEvent event) 062 { 063 area.append("Active sequence changed : [" + event.getType() + ", " + event.getSourceType() + "]\n"); 064 area.setCaretPosition(area.getDocument().getLength()); 065 }; 066 }; 067 068 // build the focused sequence event listener 069 final ActiveViewerListener focusedViewerListener = new ActiveViewerListener() 070 { 071 @Override 072 public void viewerActivated(Viewer viewer) 073 { 074 area.append("Viewer " + viewer + " activated\n"); 075 area.setCaretPosition(area.getDocument().getLength()); 076 } 077 078 @Override 079 public void viewerDeactivated(Viewer viewer) 080 { 081 area.append("Viewer " + viewer + " deactivated\n"); 082 area.setCaretPosition(area.getDocument().getLength()); 083 } 084 085 @Override 086 public void activeViewerChanged(ViewerEvent event) 087 { 088 area.append("Active viewer changed : [" + event.getType() + ", " + event.getDim() + "]\n"); 089 area.setCaretPosition(area.getDocument().getLength()); 090 } 091 }; 092 093 // add the focused sequence event listener 094 Icy.getMainInterface().addActiveSequenceListener(focusedSequenceListener); 095 // add the focused viewer event listener 096 Icy.getMainInterface().addActiveViewerListener(focusedViewerListener); 097 098 frame.addFrameListener(new IcyFrameAdapter() 099 { 100 @Override 101 public void icyFrameClosed(IcyFrameEvent e) 102 { 103 // remove the focused sequence listener to not maintain any reference on plugin 104 // instance 105 Icy.getMainInterface().removeActiveSequenceListener(focusedSequenceListener); 106 // remove the focused viewer listener to not maintain any reference on plugin 107 // instance 108 Icy.getMainInterface().removeActiveViewerListener(focusedViewerListener); 109 } 110 }); 111 112 // add the scroll pane to the frame 113 frame.getMainPanel().add(scrollPane); 114 // set default size 115 frame.setSize(700, 300); 116 // add the frame to the interface 117 addIcyFrame(frame); 118 // center frame 119 frame.center(); 120 // make it visible 121 frame.setVisible(true); 122 // get focus 123 frame.requestFocus(); 124 } 125 126}