Mercurial > hg > release > thermostat-2.0
changeset 2554:45c6d9e60653
Add Reference Selection Listener
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-January/021924.html
reviewed-by: jerboaa
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Tue, 10 Jan 2017 11:00:14 +0100 |
parents | 59c314511010 |
children | e7e190503d38 |
files | client/swing/src/main/java/com/redhat/thermostat/client/swing/ReferenceSelectionChangeListener.java client/swing/src/main/java/com/redhat/thermostat/client/swing/ReferenceSelectionChangedEvent.java client/swing/src/main/java/com/redhat/thermostat/client/swing/ReferenceSelectionService.java client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImpl.java client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/ReferenceSelectionServiceImpl.java client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/ReferenceSelectionServiceImplTest.java |
diffstat | 6 files changed, 384 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/ReferenceSelectionChangeListener.java Tue Jan 10 11:00:14 2017 +0100 @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing; + +import java.util.EventListener; + +/** + * Listener interface for implementing ReferenceSelectionChange handlers. + */ +public interface ReferenceSelectionChangeListener extends EventListener { + void referenceChanged(ReferenceSelectionChangedEvent event); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/ReferenceSelectionChangedEvent.java Tue Jan 10 11:00:14 2017 +0100 @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing; + +import com.redhat.thermostat.storage.core.Ref; + +import java.util.EventObject; + +/** + */ +public class ReferenceSelectionChangedEvent extends EventObject { + + private Ref oldRef; + private Ref newRef; + + public ReferenceSelectionChangedEvent(Object source, Ref oldRef, Ref newRef) { + super(source); + this.oldRef = oldRef; + this.newRef = newRef; + } + + @Override + public ReferenceSelectionService getSource() { + return (ReferenceSelectionService) super.getSource(); + } + + public Ref getSelected() { + return newRef; + } + + public Ref getOld() { + return oldRef; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/ReferenceSelectionService.java Tue Jan 10 11:00:14 2017 +0100 @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing; + +import com.redhat.thermostat.annotations.Service; +import com.redhat.thermostat.storage.core.Ref; + +/** + * A service that exposes the currently selected {@link com.redhat.thermostat.storage.core.Ref}. + */ +@Service +public interface ReferenceSelectionService { + Ref getReference(); + void addReferenceSelectionChangeListener(ReferenceSelectionChangeListener listener); + void removeReferenceSelectionChangeListener(ReferenceSelectionChangeListener listener); +}
--- a/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImpl.java Wed Jan 04 16:54:27 2017 -0500 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/MainWindowControllerImpl.java Tue Jan 10 11:00:14 2017 +0100 @@ -48,6 +48,8 @@ import java.util.logging.Level; import java.util.logging.Logger; +import com.redhat.thermostat.client.swing.ReferenceSelectionService; +import com.redhat.thermostat.client.swing.internal.vmlist.ReferenceSelectionServiceImpl; import org.osgi.framework.BundleContext; import org.osgi.framework.InvalidSyntaxException; @@ -156,6 +158,8 @@ private VMMonitorController vmMonitor; + private ReferenceSelectionServiceImpl referenceSelectionService; + private MenuRegistry menuRegistry; private ActionListener<ThermostatExtensionRegistry.Action> menuListener = new ActionListener<ThermostatExtensionRegistry.Action>() @@ -297,7 +301,9 @@ networkMonitor = services.get(NetworkMonitor.class); hostMonitor = services.get(HostMonitor.class); - + + referenceSelectionService = createReferenceService(context); + initView(); vmInfoControllerProvider = new VmInformationControllerProvider(); @@ -358,7 +364,13 @@ filter.addVMs(host, vms); } } - + + ReferenceSelectionServiceImpl createReferenceService(BundleContext context) { + ReferenceSelectionServiceImpl referenceSelectionService = new ReferenceSelectionServiceImpl(); + context.registerService(ReferenceSelectionService.class, referenceSelectionService, null); + return referenceSelectionService; + } + private void initView() { view.setCommonPaths(paths); view.setWindowTitle(appInfo.getName()); @@ -518,6 +530,9 @@ } void updateView(Ref ref) { + + referenceSelectionService.setReference(ref); + if (ref == null) { VersionAndInfoController controller = createSummaryController(); view.setContent(controller);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/main/java/com/redhat/thermostat/client/swing/internal/vmlist/ReferenceSelectionServiceImpl.java Tue Jan 10 11:00:14 2017 +0100 @@ -0,0 +1,100 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing.internal.vmlist; + +import com.redhat.thermostat.client.swing.ReferenceSelectionChangeListener; +import com.redhat.thermostat.client.swing.ReferenceSelectionChangedEvent; +import com.redhat.thermostat.client.swing.ReferenceSelectionService; +import com.redhat.thermostat.storage.core.Ref; + +import javax.swing.event.EventListenerList; + +/** + */ +public class ReferenceSelectionServiceImpl implements ReferenceSelectionService { + + protected EventListenerList listenerList; + private static final String __LOCK__ = new String("ReferenceSelectionServiceImpl__LOCK__"); + + private Ref reference; + + public ReferenceSelectionServiceImpl() { + listenerList = new EventListenerList(); + } + + public void setReference(Ref reference) { + Ref oldRef = null; + Ref newRef = reference; + synchronized (__LOCK__) { + oldRef = this.reference; + this.reference = reference; + } + + fireSelectionChangedEvent(oldRef, newRef); + } + + public Ref getReference() { + Ref _ref = null; + synchronized (__LOCK__) { + _ref = reference; + } + return _ref; + } + + private void fireSelectionChangedEvent(Ref oldReference, Ref newReference) { + Object[] listeners = listenerList.getListenerList(); + + ReferenceSelectionChangedEvent event = + new ReferenceSelectionChangedEvent(this, oldReference, newReference); + + for (int i = listeners.length - 2; i >= 0; i -= 2) { + if (listeners[i] == ReferenceSelectionChangeListener.class) { + ((ReferenceSelectionChangeListener) listeners[i + 1]).referenceChanged(event); + } + } + } + + @Override + public void addReferenceSelectionChangeListener(ReferenceSelectionChangeListener listener) { + listenerList.add(ReferenceSelectionChangeListener.class, listener); + } + + @Override + public void removeReferenceSelectionChangeListener(ReferenceSelectionChangeListener listener) { + listenerList.remove(ReferenceSelectionChangeListener.class, listener); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/client/swing/src/test/java/com/redhat/thermostat/client/swing/internal/vmlist/ReferenceSelectionServiceImplTest.java Tue Jan 10 11:00:14 2017 +0100 @@ -0,0 +1,103 @@ +/* + * Copyright 2012-2016 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.client.swing.internal.vmlist; + +import com.redhat.thermostat.client.swing.ReferenceSelectionChangeListener; +import com.redhat.thermostat.client.swing.ReferenceSelectionChangedEvent; +import com.redhat.thermostat.client.swing.ReferenceSelectionService; +import com.redhat.thermostat.storage.core.Ref; +import org.junit.Test; +import org.mockito.Mockito; + +import static org.junit.Assert.*; + +/** + */ +public class ReferenceSelectionServiceImplTest { + + private static final int SELECTED = 1; + private static final int OLD = 0; + + @Test + public void testSetReferenceFiresEvent() throws Exception { + + Ref oldRef = Mockito.mock(Ref.class); + Ref newRef = Mockito.mock(Ref.class); + + final Ref[] references = new Ref[2]; + + ReferenceSelectionServiceImpl service = new ReferenceSelectionServiceImpl(); + service.addReferenceSelectionChangeListener(new ReferenceSelectionChangeListener() { + @Override + public void referenceChanged(ReferenceSelectionChangedEvent event) { + references[OLD] = event.getOld(); + references[SELECTED] = event.getSelected(); + } + }); + + service.setReference(oldRef); + assertNull(references[OLD]); + assertEquals(oldRef, references[SELECTED]); + + service.setReference(newRef); + assertEquals(oldRef, references[OLD]); + assertEquals(newRef, references[SELECTED]); + } + + @Test + public void testRemoveReferenceSelectionChangeListener() throws Exception { + + Ref newRef = Mockito.mock(Ref.class); + + final boolean[] result = new boolean[1]; + result[0] = true; + + ReferenceSelectionServiceImpl service = new ReferenceSelectionServiceImpl(); + ReferenceSelectionChangeListener listener = new ReferenceSelectionChangeListener() { + @Override + public void referenceChanged(ReferenceSelectionChangedEvent event) { + result[0] = false; + } + }; + + service.addReferenceSelectionChangeListener(listener); + service.removeReferenceSelectionChangeListener(listener); + + service.setReference(newRef); + assertTrue(result[0]); + } +} \ No newline at end of file