Simon Hunt | ed804d5 | 2016-03-30 09:51:40 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.impl.topo.model; |
| 18 | |
| 19 | import org.slf4j.Logger; |
| 20 | import org.slf4j.LoggerFactory; |
| 21 | |
| 22 | /** |
| 23 | * Base class for modeling the Topology View layout. |
| 24 | * <p> |
| 25 | * Note that an instance of this class will be created for each |
| 26 | * {@link org.onosproject.ui.impl.UiWebSocket} connection, and will contain |
| 27 | * the state of how the topology is laid out for the logged-in user. |
| 28 | */ |
| 29 | public class UiTopoLayout { |
| 30 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 31 | |
| 32 | private final String username; |
| 33 | private final UiSharedTopologyModel sharedModel; |
| 34 | |
| 35 | private boolean registered = false; |
| 36 | |
| 37 | /** |
| 38 | * Creates a new topology layout. |
| 39 | */ |
| 40 | public UiTopoLayout(String username) { |
| 41 | this.username = username; |
| 42 | this.sharedModel = UiSharedTopologyModel.instance(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Initializes the layout; registering with the shared model. |
| 47 | */ |
| 48 | public void init() { |
| 49 | if (!registered) { |
| 50 | sharedModel.register(this); |
| 51 | registered = true; |
| 52 | } else { |
| 53 | log.warn("already registered"); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Destroys the layout; unregistering from the shared model. |
| 59 | */ |
| 60 | public void destroy() { |
| 61 | if (!registered) { |
| 62 | sharedModel.unregister(this); |
| 63 | } else { |
| 64 | log.warn("already unregistered"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String toString() { |
| 70 | return String.format("{UiTopoLayout for user <%s>}", username); |
| 71 | } |
| 72 | } |