/* * This file is part of the Remote Sensor Actuator Interface (ReSAI). * * Copyright(c) Andreas Kipp, Frederic Siepmann * http://opensource.cit-ec.de/projects/resai * * This file may be licensed under the terms of of the * GNU Lesser General Public License Version 3 (the ``LGPL''), * or (at your option) any later version. * * Software distributed under the License is distributed * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either * express or implied. See the LGPL for the specific language * governing rights and limitations. * * You should have received a copy of the LGPL along with this * program. If not, go to http://www.gnu.org/licenses/lgpl.html * or write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * The development of this software was supported by the * Excellence Cluster EXC 277 Cognitive Interaction Technology. * The Excellence Cluster EXC 277 is a grant of the Deutsche * Forschungsgemeinschaft (DFG) in the context of the German * Excellence Initiative. * */ /** * Controller for Overview Gui. * * @author akipp */ package de.unibi.airobots.resaijavaclient.control; import java.awt.Dimension; import java.util.HashMap; import java.util.Observable; import java.util.Observer; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import de.unibi.airobots.resaijavaclient.communicaton.Communicator; import de.unibi.airobots.resaijavaclient.communicaton.XMPPCommunicator; import de.unibi.airobots.resaijavaclient.communicaton.XMPPPacketListener; import de.unibi.airobots.resaijavaclient.constants.Constants; import de.unibi.airobots.resaijavaclient.gui.Overview; import de.unibi.airobots.resaijavaclient.gui.panels.components.template.PanelTemplate; import de.unibi.airobots.resaijavaclient.lists.ComponentMapper; public class OverviewController implements ChangeListener, Observer { private Overview ov; private String lastComponent; private XMPPCommunicator com; private ComponentMapper cmap = new ComponentMapper(); public OverviewController(Overview overview, Communicator communicator) { ov = overview; com = (XMPPCommunicator) communicator; XMPPPacketListener pl = new XMPPPacketListener(); com.addListener(pl); pl.addObserver(this); requestComponentList(); } @Override public void stateChanged(ChangeEvent e) { if (lastComponent != null) { disconnectComponent(lastComponent); } lastComponent = ov.getTabHolder().getTitleAt( ov.getTabHolder().getSelectedIndex()); connectComponent(lastComponent); } private void requestComponentList() { HashMap propertiesToSend = new HashMap(); propertiesToSend.put(Constants.CMD, Constants.REQUEST_COMPONENT_LIST); propertiesToSend.put(Constants.TAG, Constants.COMPONENTDISPATCHER); com.sendMessage(com.getServerAccount(), propertiesToSend, "Calling ReSAI Server..."); } private void connectComponent(String component) { if (!component.equals(Constants.LOGDISPATCHER)) { HashMap propertiesToSend = new HashMap(); propertiesToSend.put(Constants.CMD, Constants.REQUEST_COMPONENT); propertiesToSend.put(Constants.COMP, component); propertiesToSend.put(Constants.TAG, Constants.COMPONENTDISPATCHER); com.sendMessage(com.getServerAccount(), propertiesToSend, "Calling ReSAI Server..."); } else { // CONNECT TO LOGDISPATCHER HashMap propertiesToSend = new HashMap(); propertiesToSend.put(Constants.TAG, Constants.LOGDISPATCHER); propertiesToSend.put(Constants.GET_LIST, Constants.TRUE); com.sendMessage(com.getServerAccount(), propertiesToSend); } } private void disconnectComponent(String component) { // System.out.println("Disconnect " + component); HashMap propertiesToSend = new HashMap(); propertiesToSend.put(Constants.CMD, Constants.DISCONNECT_COMPONENT); propertiesToSend.put(Constants.COMP, component); propertiesToSend.put(Constants.TAG, Constants.COMPONENTDISPATCHER); com.sendMessage(com.getServerAccount(), propertiesToSend, "Calling ReSAI Server..."); } @Override public void update(Observable o, Object arg) { if (o instanceof XMPPPacketListener) { XMPPPacketListener packLis = (XMPPPacketListener) o; for (String prop : packLis.getReceivedProperties().keySet()) { if (packLis.getReceivedProperties().get(prop) .equals(Constants.COMPONENT)) { try { if (cmap.getPanelname(prop) != null) { PanelTemplate panel = (PanelTemplate) Class .forName( Constants.COMPONENTPANEL_CLASSPATH + cmap.getPanelname(prop)) .newInstance(); panel.setMinimumSize(new Dimension(1024, 786)); panel.setComponent(prop); panel.setOverviewController(this); com.addObserver(panel); ov.getTabHolder().addTab(prop, panel); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } if (o instanceof Overview) { if (arg instanceof String) { String command = (String) arg; if (command.equals(Constants.DISCONNECT)) { disconnectReSAI(); } } } } public void disconnectReSAI() { HashMap propertiesToSend = new HashMap(); propertiesToSend.put(Constants.CMD, Constants.DISCONNECT); propertiesToSend.put(Constants.TAG, Constants.MAINDISPATCHER); com.sendMessage(com.getServerAccount(), propertiesToSend, "Calling ReSAI Server..."); } public void sendMessage(HashMap propertiesToSend) { com.sendMessage(com.getServerAccount(), propertiesToSend); } }