001/* 002 * Copyright 2010, 2011 Institut Pasteur. 003 * 004 * This file is part of ICY. 005 * 006 * ICY is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * ICY is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with ICY. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package plugins.tutorial.event; 020 021import icy.gui.frame.IcyFrameAdapter; 022import icy.gui.frame.IcyFrameEvent; 023import icy.gui.frame.TitledFrame; 024import icy.gui.main.GlobalOverlayListener; 025import icy.gui.main.GlobalPluginListener; 026import icy.gui.main.GlobalROIListener; 027import icy.gui.main.GlobalSequenceListener; 028import icy.gui.main.GlobalViewerListener; 029import icy.gui.viewer.Viewer; 030import icy.main.Icy; 031import icy.painter.Overlay; 032import icy.plugin.abstract_.Plugin; 033import icy.plugin.abstract_.PluginActionable; 034import icy.roi.ROI; 035import icy.sequence.Sequence; 036 037import javax.swing.JScrollPane; 038import javax.swing.JTextArea; 039import javax.swing.ScrollPaneConstants; 040 041/** 042 * This class demonstrate how use the MainListener to track global event 043 * on Viewer, Sequence, ROI and Painter objects 044 * 045 * @author Stephane 046 * @author Fab 047 */ 048public class MainListenerTutorial extends PluginActionable 049{ 050 @Override 051 public void run() 052 { 053 // build a simple titled frame 054 final TitledFrame frame = new TitledFrame("Main Event logger", true, true); 055 056 // we need a text area to put out event informations 057 final JTextArea area = new JTextArea(); 058 area.setWrapStyleWord(true); 059 area.setEditable(false); 060 061 // better to have a scroll pane as many events can occurs 062 final JScrollPane scrollPane = new JScrollPane(area); 063 scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); 064 scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); 065 066 // build the global event listeners 067 final GlobalSequenceListener globalSequenceListener = new GlobalSequenceListener() 068 { 069 @Override 070 public void sequenceOpened(Sequence sequence) 071 { 072 area.append("Sequence " + sequence + " opened\n"); 073 area.setCaretPosition(area.getDocument().getLength()); 074 } 075 076 @Override 077 public void sequenceClosed(Sequence sequence) 078 { 079 area.append("Sequence " + sequence + " closed\n"); 080 area.setCaretPosition(area.getDocument().getLength()); 081 } 082 }; 083 084 final GlobalViewerListener globalViewerListener = new GlobalViewerListener() 085 { 086 @Override 087 public void viewerOpened(Viewer viewer) 088 { 089 area.append("Viewer " + viewer + " opened\n"); 090 area.setCaretPosition(area.getDocument().getLength()); 091 } 092 093 @Override 094 public void viewerClosed(Viewer viewer) 095 { 096 area.append("Viewer " + viewer + " closed\n"); 097 area.setCaretPosition(area.getDocument().getLength()); 098 } 099 }; 100 101 final GlobalOverlayListener globalOverlayListener = new GlobalOverlayListener() 102 { 103 @Override 104 public void overlayRemoved(Overlay overlay) 105 { 106 area.append("Overlay " + overlay + " removed\n"); 107 area.setCaretPosition(area.getDocument().getLength()); 108 } 109 110 @Override 111 public void overlayAdded(Overlay overlay) 112 { 113 area.append("Overlay " + overlay + " added\n"); 114 area.setCaretPosition(area.getDocument().getLength()); 115 } 116 }; 117 118 final GlobalROIListener globalROIListener = new GlobalROIListener() 119 { 120 @Override 121 public void roiRemoved(ROI roi) 122 { 123 area.append("ROI " + roi + " removed\n"); 124 area.setCaretPosition(area.getDocument().getLength()); 125 } 126 127 @Override 128 public void roiAdded(ROI roi) 129 { 130 area.append("ROI " + roi + " added\n"); 131 area.setCaretPosition(area.getDocument().getLength()); 132 } 133 }; 134 135 final GlobalPluginListener globalPluginListener = new GlobalPluginListener() 136 { 137 @Override 138 public void pluginStarted(Plugin plugin) 139 { 140 area.append("Plugin " + plugin + " started\n"); 141 area.setCaretPosition(area.getDocument().getLength()); 142 } 143 144 @Override 145 public void pluginEnded(Plugin plugin) 146 { 147 area.append("Plugin " + plugin + " ended\n"); 148 area.setCaretPosition(area.getDocument().getLength()); 149 } 150 }; 151 152 // add the global event listeners 153 Icy.getMainInterface().addGlobalSequenceListener(globalSequenceListener); 154 Icy.getMainInterface().addGlobalViewerListener(globalViewerListener); 155 Icy.getMainInterface().addGlobalROIListener(globalROIListener); 156 Icy.getMainInterface().addGlobalOverlayListener(globalOverlayListener); 157 Icy.getMainInterface().addGlobalPluginListener(globalPluginListener); 158 159 frame.addFrameListener(new IcyFrameAdapter() 160 { 161 @Override 162 public void icyFrameClosed(IcyFrameEvent e) 163 { 164 // remove the listeners so there is no more reference on plugin instance 165 Icy.getMainInterface().removeGlobalSequenceListener(globalSequenceListener); 166 Icy.getMainInterface().removeGlobalViewerListener(globalViewerListener); 167 Icy.getMainInterface().removeGlobalROIListener(globalROIListener); 168 Icy.getMainInterface().removeGlobalOverlayListener(globalOverlayListener); 169 Icy.getMainInterface().removeGlobalPluginListener(globalPluginListener); 170 } 171 }); 172 173 // add the scroll pane to the frame 174 frame.getMainPanel().add(scrollPane); 175 // set default size 176 frame.setSize(700, 300); 177 // add the frame to the interface 178 addIcyFrame(frame); 179 // center frame 180 frame.center(); 181 // make it visible 182 frame.setVisible(true); 183 // get focus 184 frame.requestFocus(); 185 } 186}