View Javadoc
1   /*
2   * Copyright 2012-2025 Christophe Friederich
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  package org.devacfr.maven.skins.reflow.model;
17  
18  import com.google.common.collect.Lists;
19  import java.util.List;
20  import java.util.Objects;
21  import javax.annotation.Nonnull;
22  import org.apache.maven.doxia.site.Image;
23  import org.devacfr.maven.skins.reflow.ISkinConfig;
24  
25  /**
26   * @author Christophe Friederich
27   * @since 2.0
28   */
29  public class MenuItem {
30  
31    /** */
32    private final String name;
33  
34    /** */
35    private final String href;
36  
37    /** */
38    private final Image image;
39  
40    /** */
41    private final String target;
42  
43    /** **/
44    private final String inherit;
45  
46    /** */
47    private final boolean active;
48  
49    /** */
50    private final List<MenuItem> menuItems = Lists.newArrayList();
51  
52    /**
53     * Initialize with {@link org.apache.maven.doxia.site.MenuItem}.
54     *
55     * @param config
56     *          a config (can <b>not</b> be {@code null}).
57     * @param item
58     *          item menu used to.
59     */
60    public MenuItem(@Nonnull final ISkinConfig config, final org.apache.maven.doxia.site.MenuItem item) {
61      Objects.requireNonNull(item);;
62      this.href = config.relativeLink(item.getHref());
63      this.image = item.getImage();
64      this.name = item.getName();
65      this.target = item.getTarget();
66      this.active = config.isActiveLink(this.href);
67      this.inherit = null;
68      recurciveAddItem(config, this.menuItems, item.getItems());
69    }
70  
71    /**
72     * recurcive
73     */
74    private void recurciveAddItem(final ISkinConfig config,
75      final List<MenuItem> menuItems,
76      final List<org.apache.maven.doxia.site.MenuItem> origMenuItems) {
77      if (origMenuItems == null) {
78        return;
79      }
80      for (final org.apache.maven.doxia.site.MenuItem menuItem : origMenuItems) {
81        menuItems.add(new MenuItem(config, menuItem));
82      }
83    }
84  
85    /**
86     * @return the name
87     */
88    public String getName() {
89      return name;
90    }
91  
92    /**
93     * @return the menuItems
94     */
95    public List<MenuItem> getMenuItems() {
96      return menuItems;
97    }
98  
99    /**
100    * @return the inherit
101    */
102   public String getInherit() {
103     return inherit;
104   }
105 
106   /**
107    * @return the active
108    */
109   public boolean isActive() {
110     boolean active = this.active;
111     if (active) {
112       return active;
113     }
114     for (final MenuItem menuItem : menuItems) {
115       active = menuItem.isActive();
116       if (active) {
117         break;
118       }
119     }
120     return active;
121   }
122 
123   /**
124    * @return the href
125    */
126   public String getHref() {
127     return href;
128   }
129 
130   /**
131    * @return the image
132    */
133   public Image getImage() {
134     return image;
135   }
136 
137   /**
138    * @return the target
139    */
140   public String getTarget() {
141     return target;
142   }
143 
144 }