com.viaoa.hub.HubCombined Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of oa Show documentation
Show all versions of oa Show documentation
Object Automation library
The newest version!
/* Copyright 1999-2015 Vince Via [email protected]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.viaoa.hub;
import java.util.ArrayList;
import java.util.logging.Logger;
/**
* Combines multiple hubs into one.
*/
public class HubCombined {
private static Logger LOG = Logger.getLogger(HubCombined.class.getName());
private static final long serialVersionUID = 1L;
protected Hub hubMaster;
protected ArrayList> alHub;
protected ArrayList> alHubListener;
public HubCombined(Hub hubMaster, Hub ... hubs) {
this.hubMaster = hubMaster;
if (hubs == null) return;
for (Hub h : hubs) {
add(h);
}
}
public void close() {
int i = 0;
for (Hub h : alHub) {
h.removeHubListener( alHubListener.get(i++));
}
alHub.clear();
alHubListener.clear();
}
public void add(Hub hub) {
if (alHub == null) alHub = new ArrayList>();
alHub.add(hub);
HubListener hl = new HubListenerAdapter() {
@Override
public void afterAdd(HubEvent e) {
hubMaster.add(e.getObject());
}
@Override
public void afterInsert(HubEvent e) {
afterAdd(e);
}
@Override
public void afterRemove(HubEvent e) {
T obj = e.getObject();
for (Hub h :alHub) {
if (h.contains(obj)) return;
}
hubMaster.remove(obj);
}
@Override
public void beforeRemoveAll(HubEvent e) {
Hub h = e.getHub();
for (Object obj : h) {
boolean b = false;
for (Hub hx :alHub) {
if (hx != h && hx.contains(obj)) {
b = true;
break;
}
}
if (!b) hubMaster.remove(obj);
}
}
@Override
public void onNewList(HubEvent e) {
beforeRemoveAll(e);
for (Object obj : e.getHub()) {
hubMaster.add((T) obj);
}
}
};
hub.addHubListener(hl);
if (alHubListener == null) alHubListener = new ArrayList>();
alHubListener.add(hl);
for (T obj : hub) {
hubMaster.add(obj);
}
}
public void refresh() {
hubMaster.clear();
for (Hub h :alHub) {
for (T obj : h) {
hubMaster.add(obj);
}
}
}
}