View Javadoc
1   /*
2    * Metis: Java Data Framework
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.metis.viewer;
18  
19  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventManager;
20  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar;
21  
22  import java.util.ArrayList;
23  import java.util.EnumMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  import java.util.concurrent.atomic.AtomicInteger;
28  
29  /**
30   * Viewer Data Manager.
31   */
32  public class MetisViewerManagerImpl
33          implements MetisViewerManager {
34      /**
35       * The Event Manager.
36       */
37      private final OceanusEventManager<MetisViewerEvent> theEventManager;
38  
39      /**
40       * The Root List.
41       */
42      private final List<MetisViewerEntry> theRootList;
43  
44      /**
45       * The Standard Entry Map.
46       */
47      private final Map<MetisViewerStandardEntry, MetisViewerEntry> theStdEntries;
48  
49      /**
50       * The Next entryId.
51       */
52      private static final AtomicInteger NEXT_ENTRY_ID = new AtomicInteger(1);
53  
54      /**
55       * The Focused Entry.
56       */
57      private MetisViewerEntry theFocused;
58  
59      /**
60       * Constructor.
61       */
62      public MetisViewerManagerImpl() {
63          /* Create the event manager */
64          theEventManager = new OceanusEventManager<>();
65  
66          /* Create the root list */
67          theRootList = new ArrayList<>();
68  
69          /* Create the standard entry map */
70          theStdEntries = new EnumMap<>(MetisViewerStandardEntry.class);
71          createStandardEntries();
72      }
73  
74      @Override
75      public OceanusEventRegistrar<MetisViewerEvent> getEventRegistrar() {
76          return theEventManager.getEventRegistrar();
77      }
78  
79      @Override
80      public Iterator<MetisViewerEntry> rootIterator() {
81          return theRootList.iterator();
82      }
83  
84      @Override
85      public MetisViewerEntry getFocus() {
86          return theFocused;
87      }
88  
89      @Override
90      public void setFocus(final MetisViewerEntry pEntry) {
91          /* If this is a change in focus */
92          if (!pEntry.equals(theFocused)) {
93              /* Record and report the change */
94              theFocused = pEntry;
95              fireEvent(MetisViewerEvent.FOCUS, pEntry);
96          }
97      }
98  
99      @Override
100     public void fireEvent(final MetisViewerEvent pEventId,
101                           final Object pValue) {
102         theEventManager.fireEvent(pEventId, pValue);
103     }
104 
105     @Override
106     public int getNextId() {
107         return NEXT_ENTRY_ID.getAndIncrement();
108     }
109 
110     @Override
111     public MetisViewerEntry newEntry(final String pName) {
112         /* Create the entry and add to root List */
113         final MetisViewerEntry myEntry = new MetisViewerEntryImpl(this, null, pName);
114         theRootList.add(myEntry);
115         fireEvent(MetisViewerEvent.ENTRY, myEntry);
116         return myEntry;
117     }
118 
119     @Override
120     public MetisViewerEntry newEntry(final MetisViewerEntry pParent,
121                                      final String pName) {
122         /* Create the entry under the parent */
123         final MetisViewerEntry myEntry = new MetisViewerEntryImpl(this, pParent, pName);
124         fireEvent(MetisViewerEvent.ENTRY, myEntry);
125         return myEntry;
126     }
127 
128     /**
129      * Create standard entries.
130      */
131     private void createStandardEntries() {
132         /* Loop through the standard entries */
133         for (MetisViewerStandardEntry myId : MetisViewerStandardEntry.values()) {
134             /* Create invisible root entry and add to the map */
135             final MetisViewerEntry myEntry = newEntry(myId.toString());
136             myEntry.setVisible(false);
137             theStdEntries.put(myId, myEntry);
138         }
139     }
140 
141     @Override
142     public MetisViewerEntry getStandardEntry(final MetisViewerStandardEntry pEntry) {
143         final MetisViewerEntry myEntry = theStdEntries.get(pEntry);
144         myEntry.setVisible(true);
145         return myEntry;
146     }
147 }