001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.web;
021    
022    import com.google.common.collect.ArrayListMultimap;
023    import com.google.common.collect.ListMultimap;
024    import com.google.common.collect.Maps;
025    
026    import java.util.Collection;
027    import java.util.List;
028    import java.util.Map;
029    
030    /**
031     * Definition of a dashboard.
032     * <p/>
033     * Its name and description can be retrieved using the i18n mechanism, using the keys "dashboard.&lt;id&gt;.name" and
034     * "dashboard.&lt;id&gt;.description".
035     *
036     * @since 2.13
037     */
038    public final class Dashboard {
039    
040      private String description;
041      private DashboardLayout layout = DashboardLayout.TWO_COLUMNS;
042      private ListMultimap<Integer, Widget> widgetsByColumn = ArrayListMultimap.create();
043    
044      private Dashboard() {
045      }
046    
047      /**
048       * Creates a new {@link Dashboard}.
049       */
050      public static Dashboard create() {
051        return new Dashboard();
052      }
053    
054      /**
055       * Add a widget with the given parameters, and return the newly created {@link Widget} object if one wants to add parameters to it.
056       *
057       * @param widgetId id of an existing widget
058       * @param columnId column starts with 1. The widget is ignored if the column id does not match the layout.
059       */
060      public Widget addWidget(String widgetId, int columnId) {
061        if (columnId < 1) {
062          throw new IllegalArgumentException("Widget column starts with 1");
063        }
064    
065        Widget widget = new Widget(widgetId);
066        widgetsByColumn.put(columnId, widget);
067        return widget;
068      }
069    
070      public Collection<Widget> getWidgets() {
071        return widgetsByColumn.values();
072      }
073    
074      public List<Widget> getWidgetsOfColumn(int columnId) {
075        return widgetsByColumn.get(columnId);
076      }
077    
078      /**
079       * Returns the description of the dashboard.
080       *
081       * @return the description
082       */
083      public String getDescription() {
084        return description;
085      }
086    
087      /**
088       * Sets the description of the dashboard.
089       * <p/>
090       * Note: you should use the i18n mechanism for the description.
091       *
092       * @param description the description to set
093       */
094      public Dashboard setDescription(String description) {
095        this.description = description;
096        return this;
097      }
098    
099      /**
100       * Returns the layout. Default value is the 2 columns mode with width 50%/50%.
101       *
102       * @return the layout
103       */
104      public DashboardLayout getLayout() {
105        return layout;
106      }
107    
108      public Dashboard setLayout(DashboardLayout dl) {
109        if (dl == null) {
110          throw new IllegalArgumentException("The layout can not be null");
111        }
112        this.layout = dl;
113        return this;
114      }
115    
116    
117      /**
118       * Note that this class is an inner class to avoid confusion with the extension point org.sonar.api.web.Widget.
119       */
120      public static final class Widget {
121        private String id;
122        private Map<String, String> properties;
123    
124        Widget(String id) {
125          this.id = id;
126          this.properties = Maps.newHashMap();
127        }
128    
129        public Widget setProperty(String key, String value) {
130          properties.put(key, value);
131          return this;
132        }
133    
134        /**
135         * Returns the properties of this widget.
136         *
137         * @return the properties
138         */
139        public Map<String, String> getProperties() {
140          return properties;
141        }
142    
143        public String getProperty(String key) {
144          return properties.get(key);
145        }
146    
147        /**
148         * Returns the identifier of this widget.
149         *
150         * @return the id
151         */
152        public String getId() {
153          return id;
154        }
155      }
156    
157    }