-
Notifications
You must be signed in to change notification settings - Fork 325
/
PortalManager.java
143 lines (125 loc) · 4.72 KB
/
PortalManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package jmri.jmrit.logix;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Hashtable;
import javax.annotation.Nonnull;
import javax.annotation.OverridingMethodsMustInvokeSuper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Basic implementation of a PortalManager.
* <p>
* Note that this does not enforce any particular system naming convention.
* <p>
* Note this is an 'after thought' manager. Portals have been in use since 2009.
* Their use has now expanded well beyond what was expected. A Portal factory is
* needed for development to continue.
*
* Portal system names will be numbers and they will not be shown to users. The
* UI will treat Portal names as it does now as user names.
*
* <hr>
* This file is part of JMRI.
* <p>
* JMRI is free software; you can redistribute it and/or modify it under the
* terms of version 2 of the GNU General Public License as published by the Free
* Software Foundation. See the "COPYING" file for a copy of this license.
* <p>
* JMRI 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.
*
* @author Pete Cressman Copyright (C) 2014
*/
public class PortalManager implements jmri.InstanceManagerAutoDefault, PropertyChangeListener {
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
private ArrayList<Portal> _nameList = new ArrayList<>(); // stores Portal in loaded order
private Hashtable<String, Portal> _portalMap = new Hashtable<>(); // stores portal by current name
private Integer _nextIndex = 1;
public PortalManager() {
}
public int getPortalCount() {
return _nameList.size();
}
public Portal getPortal(int idx) {
return _nameList.get(idx);
}
public int getIndexOf(Portal portal) {
return _nameList.indexOf(portal);
}
public Portal getPortal(String name) {
return _portalMap.get(name);
}
public Collection<Portal> getPortalSet() {
return Collections.unmodifiableCollection(_nameList);
}
/*
* Create a new Portal with a given user name.
*
* @return null if a Portal with the same userName already exists,
* or if an empty userName was requested
*/
public Portal createNewPortal(@Nonnull String userName) {
java.util.Objects.requireNonNull(userName, "Name cannot be null");
// Check that Portal does not already exist
Portal portal;
if (userName.trim().length() > 0) {
portal = _portalMap.get(userName);
if (portal != null) {
return null;
}
} else { // must have a user name for backward compatibility
return null;
}
// Portal does not exist, create a new Portal
portal = new Portal(userName);
// save in the maps
_nameList.add(portal);
_portalMap.put(userName, portal);
_nextIndex = _nextIndex + 1;
pcs.firePropertyChange("numPortals", null, _nameList.size());
// listen for name and state changes to forward
portal.addPropertyChangeListener(this);
return portal;
}
public Portal providePortal(String name) {
if (name == null || name.trim().length() == 0) {
return null;
}
Portal portal = getPortal(name);
if (portal == null) {
portal = createNewPortal(name);
}
return portal;
}
private synchronized void deletePortal(Portal portal) {
String name = portal.getName();
_nameList.remove(portal);
_portalMap.remove(name);
pcs.firePropertyChange("numPortals", portal, _nameList.size());
}
@OverridingMethodsMustInvokeSuper
public synchronized void addPropertyChangeListener(PropertyChangeListener l) {
pcs.addPropertyChangeListener(l);
}
@OverridingMethodsMustInvokeSuper
public synchronized void removePropertyChangeListener(PropertyChangeListener l) {
pcs.removePropertyChangeListener(l);
}
public void propertyChange(PropertyChangeEvent e) {
if (!(e.getSource() instanceof Portal)) {
return;
}
Portal portal = (Portal)e.getSource();
String propertyName = e.getPropertyName();
log.debug("property = {}", propertyName);
if (propertyName.equals("portalDelete")) {
deletePortal(portal);
}
}
private final static Logger log = LoggerFactory.getLogger(PortalManager.class);
}