1/*
2 * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24package com.sun.hotspot.igv.data;
25
26import java.util.*;
27
28/**
29 *
30 * @author Thomas Wuerthinger
31 */
32public class Group extends Properties.Entity implements ChangedEventProvider<Group>, Folder, FolderElement {
33
34    private final List<FolderElement> elements;
35    private final List<InputGraph> graphs;
36
37    private InputMethod method;
38    private transient ChangedEvent<Group> changedEvent;
39    private Folder parent;
40
41    public Group(Folder parent) {
42        elements = new ArrayList<>();
43        graphs = new ArrayList<>();
44        changedEvent = new ChangedEvent<>(this);
45        this.parent = parent;
46
47        // Ensure that name and type are never null
48        getProperties().setProperty("name", "");
49        getProperties().setProperty("type", "");
50    }
51
52    public void fireChangedEvent() {
53        changedEvent.fire();
54    }
55
56    public void setMethod(InputMethod method) {
57        this.method = method;
58    }
59
60    public InputMethod getMethod() {
61        return method;
62    }
63
64    @Override
65    public ChangedEvent<Group> getChangedEvent() {
66        return changedEvent;
67    }
68
69    @Override
70    public List<FolderElement> getElements() {
71        return Collections.unmodifiableList(elements);
72    }
73
74    public int getGraphsCount() {
75        return elements.size();
76    }
77
78    @Override
79    public void addElement(FolderElement element) {
80        elements.add(element);
81        if (element instanceof InputGraph) {
82            graphs.add((InputGraph) element);
83        } else {
84
85        }
86        element.setParent(this);
87        changedEvent.fire();
88    }
89
90    public Set<Integer> getAllNodes() {
91        Set<Integer> result = new HashSet<>();
92        for (FolderElement e : elements) {
93            if (e instanceof InputGraph) {
94                InputGraph g = (InputGraph) e;
95                result.addAll(g.getNodesAsSet());
96            }
97        }
98        return result;
99    }
100
101    @Override
102    public String toString() {
103        StringBuilder sb = new StringBuilder();
104        sb.append("Group ").append(getProperties()).append("\n");
105        for (FolderElement g : elements) {
106            sb.append(g.toString());
107            sb.append('\n');
108        }
109        return sb.toString();
110    }
111
112    @Override
113    public String getName() {
114        return getProperties().get("name");
115    }
116
117    public String getType() {
118        return getProperties().get("type");
119
120    }
121
122    InputGraph getPrev(InputGraph graph) {
123        InputGraph lastGraph = null;
124        for (FolderElement e : elements) {
125            if (e == graph) {
126                return lastGraph;
127            }
128            if (e instanceof InputGraph) {
129                lastGraph = (InputGraph) e;
130            }
131        }
132        return null;
133    }
134
135    InputGraph getNext(InputGraph graph) {
136        boolean found = false;
137        for (FolderElement e : elements) {
138            if (e == graph) {
139                found = true;
140            } else if (found && e instanceof InputGraph) {
141                return (InputGraph) e;
142            }
143        }
144        return null;
145    }
146
147    public InputGraph getLastGraph() {
148        InputGraph lastGraph = null;
149        for (FolderElement e : elements) {
150            if (e instanceof InputGraph) {
151                lastGraph = (InputGraph) e;
152            }
153        }
154        return lastGraph;
155    }
156
157    @Override
158    public Folder getParent() {
159         return parent;
160    }
161
162    @Override
163    public void removeElement(FolderElement element) {
164        if (elements.remove(element)) {
165            if (element instanceof InputGraph) {
166                graphs.remove((InputGraph) element);
167            }
168            changedEvent.fire();
169        }
170    }
171
172    public List<InputGraph> getGraphs() {
173        return graphs;
174    }
175
176    @Override
177    public void setParent(Folder parent) {
178        this.parent = parent;
179    }
180}
181