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  import io.github.tonywasher.joceanus.oceanus.event.OceanusEventRegistrar.OceanusEventProvider;
22  
23  import java.util.ArrayList;
24  import java.util.EnumMap;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.concurrent.atomic.AtomicInteger;
29  
30  /**
31   * Viewer Data Manager.
32   */
33  public class MetisViewerManager
34          implements OceanusEventProvider<MetisViewerEvent> {
35      /**
36       * The Event Manager.
37       */
38      private final OceanusEventManager<MetisViewerEvent> theEventManager;
39  
40      /**
41       * The Root List.
42       */
43      private final List<MetisViewerEntry> theRootList;
44  
45      /**
46       * The Standard Entry Map.
47       */
48      private final Map<MetisViewerStandardEntry, MetisViewerEntry> theStdEntries;
49  
50      /**
51       * The Next entryId.
52       */
53      private static final AtomicInteger NEXT_ENTRY_ID = new AtomicInteger(1);
54  
55      /**
56       * The Focused Entry.
57       */
58      private MetisViewerEntry theFocused;
59  
60      /**
61       * Constructor.
62       */
63      public MetisViewerManager() {
64          /* Create the event manager */
65          theEventManager = new OceanusEventManager<>();
66  
67          /* Create the root list */
68          theRootList = new ArrayList<>();
69  
70          /* Create the standard entry map */
71          theStdEntries = new EnumMap<>(MetisViewerStandardEntry.class);
72          createStandardEntries();
73      }
74  
75      @Override
76      public OceanusEventRegistrar<MetisViewerEvent> getEventRegistrar() {
77          return theEventManager.getEventRegistrar();
78      }
79  
80      /**
81       * Get root iterator.
82       *
83       * @return the iterator
84       */
85      public Iterator<MetisViewerEntry> rootIterator() {
86          return theRootList.iterator();
87      }
88  
89      /**
90       * Get focused entry.
91       *
92       * @return the focused entry
93       */
94      protected MetisViewerEntry getFocus() {
95          return theFocused;
96      }
97  
98      /**
99       * Set focused entry.
100      *
101      * @param pEntry the entry to focus on
102      */
103     protected void setFocus(final MetisViewerEntry pEntry) {
104         /* If this is a change in focus */
105         if (!pEntry.equals(theFocused)) {
106             /* Record and report the change */
107             theFocused = pEntry;
108             fireEvent(MetisViewerEvent.FOCUS, pEntry);
109         }
110     }
111 
112     /**
113      * Fire event.
114      *
115      * @param pEventId the eventId
116      * @param pValue   the relevant value
117      */
118     protected void fireEvent(final MetisViewerEvent pEventId,
119                              final Object pValue) {
120         theEventManager.fireEvent(pEventId, pValue);
121     }
122 
123     /**
124      * Get NextId.
125      *
126      * @return the next id
127      */
128     protected int getNextId() {
129         return NEXT_ENTRY_ID.getAndIncrement();
130     }
131 
132     /**
133      * Create a new root entry.
134      *
135      * @param pName the name of the new entry
136      * @return the new entry
137      */
138     public MetisViewerEntry newEntry(final String pName) {
139         /* Create the entry and add to root List */
140         final MetisViewerEntry myEntry = new MetisViewerEntry(this, null, pName);
141         theRootList.add(myEntry);
142         fireEvent(MetisViewerEvent.ENTRY, myEntry);
143         return myEntry;
144     }
145 
146     /**
147      * Create a new entry under parent.
148      *
149      * @param pParent the parent entry
150      * @param pName   the name of the new entry
151      * @return the new entry
152      */
153     public MetisViewerEntry newEntry(final MetisViewerEntry pParent,
154                                      final String pName) {
155         /* Create the entry under the parent */
156         final MetisViewerEntry myEntry = new MetisViewerEntry(this, pParent, pName);
157         fireEvent(MetisViewerEvent.ENTRY, myEntry);
158         return myEntry;
159     }
160 
161     /**
162      * Create standard entries.
163      */
164     private void createStandardEntries() {
165         /* Loop through the standard entries */
166         for (MetisViewerStandardEntry myId : MetisViewerStandardEntry.values()) {
167             /* Create invisible root entry and add to the map */
168             final MetisViewerEntry myEntry = newEntry(myId.toString());
169             myEntry.setVisible(false);
170             theStdEntries.put(myId, myEntry);
171         }
172     }
173 
174     /**
175      * Obtain standard entry.
176      *
177      * @param pEntry the standard entry id
178      * @return the viewer entry
179      */
180     public MetisViewerEntry getStandardEntry(final MetisViewerStandardEntry pEntry) {
181         final MetisViewerEntry myEntry = theStdEntries.get(pEntry);
182         myEntry.setVisible(true);
183         return myEntry;
184     }
185 }