From 61a84575510a2cdde57ce7b2139c52458903df52 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 1 Nov 2010 14:20:55 +0000 Subject: [PATCH] SONAR-1643 add example of widget properties in plugin archetype + use enumeration for the type of widget property --- archetypes/sonar-basic-plugin/README.txt | 3 + .../plugins/sample/SampleDashboardWidget.java | 23 +++++-- .../sample_dashboard_widget.html.erb | 19 +++++- .../src/main/java/SampleDashboardWidget.java | 24 +++++-- .../sample_dashboard_widget.html.erb | 19 +++++- .../java/org/sonar/api/web/Description.java | 6 -- .../org/sonar/api/web/WidgetProperties.java | 8 --- .../org/sonar/api/web/WidgetProperty.java | 2 +- .../org/sonar/api/web/WidgetPropertyType.java | 24 +++++++ .../app/controllers/dashboard_controller.rb | 4 +- .../WEB-INF/app/helpers/dashboards_helper.rb | 66 ------------------- .../app/helpers/widget_properties_helper.rb | 26 ++------ .../main/webapp/WEB-INF/app/models/widget.rb | 3 - .../WEB-INF/app/models/widget_property.rb | 6 +- .../src/main/webapp/stylesheets/dashboard.css | 5 +- .../org/sonar/server/ui/ViewProxyTest.java | 2 +- 16 files changed, 115 insertions(+), 125 deletions(-) create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java delete mode 100644 sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboards_helper.rb diff --git a/archetypes/sonar-basic-plugin/README.txt b/archetypes/sonar-basic-plugin/README.txt index 6f0d0d9c557..a4dca475c74 100644 --- a/archetypes/sonar-basic-plugin/README.txt +++ b/archetypes/sonar-basic-plugin/README.txt @@ -3,3 +3,6 @@ To update the archetype project : 1. update sources of the initial project (/project directory) 2. /project$ mvn install -DsonarTargetVersion= 3. copy the content of /project/target/generated-sources/archetype/src/main/resources to src/main/resources. Be careful with .svn files ! + +To execute the archetype : +mvn archetype:generate -B -DarchetypeGroupId=org.codehaus.sonar.archetypes -DarchetypeArtifactId=sonar-basic-plugin-archetype -DarchetypeVersion= -DgroupId=com.mycompany.sonar -DartifactId=sonar-basic-sample-plugin -Dversion=0.1-SNAPSHOT \ No newline at end of file diff --git a/archetypes/sonar-basic-plugin/project/src/main/java/org/sonar/plugins/sample/SampleDashboardWidget.java b/archetypes/sonar-basic-plugin/project/src/main/java/org/sonar/plugins/sample/SampleDashboardWidget.java index 1ce3680f5b6..0bc7ba1cc55 100644 --- a/archetypes/sonar-basic-plugin/project/src/main/java/org/sonar/plugins/sample/SampleDashboardWidget.java +++ b/archetypes/sonar-basic-plugin/project/src/main/java/org/sonar/plugins/sample/SampleDashboardWidget.java @@ -1,12 +1,27 @@ package org.sonar.plugins.sample; -import org.sonar.api.web.AbstractRubyTemplate; -import org.sonar.api.web.NavigationSection; -import org.sonar.api.web.RubyRailsWidget; -import org.sonar.api.web.UserRole; +import org.sonar.api.web.*; @NavigationSection(NavigationSection.RESOURCE) @UserRole(UserRole.USER) +@Description("Show how to use Ruby Widget API") +@WidgetProperties({ + @WidgetProperty(key="param1", + description="This is a mandatory parameter", + optional=false + ), + @WidgetProperty(key="max", + description="max threshold", + type=WidgetPropertyType.INTEGER, + defaultValue="80" + ), + @WidgetProperty(key="param2", + description="This is an optional parameter" + ), + @WidgetProperty(key="floatprop", + description="test description" + ) +}) public class SampleDashboardWidget extends AbstractRubyTemplate implements RubyRailsWidget { public String getId() { diff --git a/archetypes/sonar-basic-plugin/project/src/main/resources/sample_dashboard_widget.html.erb b/archetypes/sonar-basic-plugin/project/src/main/resources/sample_dashboard_widget.html.erb index 34b80a937e7..d52038ffe25 100644 --- a/archetypes/sonar-basic-plugin/project/src/main/resources/sample_dashboard_widget.html.erb +++ b/archetypes/sonar-basic-plugin/project/src/main/resources/sample_dashboard_widget.html.erb @@ -20,4 +20,21 @@ Sample of Jfree Eastwood chart :
<%= gchart('cht=bhs&chco=FF0000,00FF00,0000FF&chs=200x125&chd=s:FOE,THE,Bar&chxt=x,y&chxl=1:|Dec|Nov|Oct|0:||20K||60K||100K|') -%>

- \ No newline at end of file +

+ Widget properties: + + + + + + + + + + + + + +
max:<%= widget_properties['max'] -%>
param1:<%= widget_properties['param1'] -%>
param2:<%= widget_properties['param2'] -%>
+

+ diff --git a/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/java/SampleDashboardWidget.java b/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/java/SampleDashboardWidget.java index e545be14157..05789347804 100644 --- a/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/java/SampleDashboardWidget.java +++ b/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/java/SampleDashboardWidget.java @@ -3,13 +3,28 @@ #set( $symbol_escape = '\' ) package ${package}; -import org.sonar.api.web.AbstractRubyTemplate; -import org.sonar.api.web.NavigationSection; -import org.sonar.api.web.RubyRailsWidget; -import org.sonar.api.web.UserRole; +import org.sonar.api.web.*; @NavigationSection(NavigationSection.RESOURCE) @UserRole(UserRole.USER) +@Description("Show how to use Ruby Widget API") +@WidgetProperties({ + @WidgetProperty(key="param1", + description="This is a mandatory parameter", + optional=false + ), + @WidgetProperty(key="max", + description="max threshold", + type=WidgetPropertyType.INTEGER, + defaultValue="80" + ), + @WidgetProperty(key="param2", + description="This is an optional parameter" + ), + @WidgetProperty(key="floatprop", + description="test description" + ) +}) public class SampleDashboardWidget extends AbstractRubyTemplate implements RubyRailsWidget { public String getId() { @@ -17,7 +32,6 @@ public class SampleDashboardWidget extends AbstractRubyTemplate implements RubyR } public String getTitle() { - // not used for the moment by widgets. return "Sample"; } diff --git a/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/resources/sample_dashboard_widget.html.erb b/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/resources/sample_dashboard_widget.html.erb index 34b80a937e7..d52038ffe25 100644 --- a/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/resources/sample_dashboard_widget.html.erb +++ b/archetypes/sonar-basic-plugin/src/main/resources/archetype-resources/src/main/resources/sample_dashboard_widget.html.erb @@ -20,4 +20,21 @@ Sample of Jfree Eastwood chart :
<%= gchart('cht=bhs&chco=FF0000,00FF00,0000FF&chs=200x125&chd=s:FOE,THE,Bar&chxt=x,y&chxl=1:|Dec|Nov|Oct|0:||20K||60K||100K|') -%>

- \ No newline at end of file +

+ Widget properties: + + + + + + + + + + + + + +
max:<%= widget_properties['max'] -%>
param1:<%= widget_properties['param1'] -%>
param2:<%= widget_properties['param2'] -%>
+

+ diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/Description.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/Description.java index 34d44a6f341..17baa1a731d 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/Description.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/Description.java @@ -24,12 +24,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -/** - * Created by IntelliJ IDEA. - * User: dreik - * Date: 02.08.2010 - * Time: 13:00:45 - */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) public @interface Description { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperties.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperties.java index 86d6d7c7706..b0d21e2aa22 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperties.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperties.java @@ -19,17 +19,9 @@ */ package org.sonar.api.web; -import org.sonar.api.Property; - import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -/** - * Created by IntelliJ IDEA. - * User: dreik - * Date: 09.08.2010 - * Time: 10:40:09 - */ @Retention(RetentionPolicy.RUNTIME) public @interface WidgetProperties { WidgetProperty[] value() default {}; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java index 87e3743b8dd..37d2b1205df 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetProperty.java @@ -30,7 +30,7 @@ public @interface WidgetProperty { String key(); - String type() default "STRING"; + WidgetPropertyType type() default WidgetPropertyType.STRING; String defaultValue() default ""; diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java new file mode 100644 index 00000000000..6481cd552c3 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java @@ -0,0 +1,24 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.api.web; + +public enum WidgetPropertyType { + INTEGER, BOOLEAN, FLOAT, STRING +} diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb index 958ac6eb0bf..b554f01d01a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb @@ -108,11 +108,11 @@ class DashboardController < ApplicationController errors_by_property_key={} definition.getProperties().each do |property_def| value=params[property_def.key()] || property_def.defaultValue() - value='false' if value.empty? && property_def.type()==WidgetProperty::TYPE_BOOLEAN + value='false' if value.empty? && property_def.type.name()==WidgetProperty::TYPE_BOOLEAN errors=WidgetProperty.validate_definition(property_def, value) if errors.empty? - widget.set_property(property_def.key(), value, property_def.type()) + widget.set_property(property_def.key(), value, property_def.type.name()) else widget.unset_property(property_def.key()) errors_by_property_key[property_def.key()]=errors diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboards_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboards_helper.rb deleted file mode 100644 index 4893c32db0d..00000000000 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboards_helper.rb +++ /dev/null @@ -1,66 +0,0 @@ -# -# Sonar, entreprise quality control tool. -# Copyright (C) 2009 SonarSource SA -# mailto:contact AT sonarsource DOT com -# -# Sonar is free software; you can redistribute it and/or -# modify it under the terms of the GNU Lesser General Public -# License as published by the Free Software Foundation; either -# version 3 of the License, or (at your option) any later version. -# -# Sonar is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public -# License along with Sonar; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 -# -module DashboardsHelper - include ActionView::Helpers::UrlHelper - include WidgetPropertiesHelper - - def item_by_metric_id(items, metric_id) - return nil if items.nil? - items.each do |item| - return item if (item.metric.id==metric_id and item.rules_category_id.nil?) - end - nil - end - - def active_widgets_ids_formatted(column) - active_widget_ids=[] - @dashboard.widgets.find(:all, :conditions => {:column_index => column}, :order => :order_index).each do |widget| - widget_view=nil - found_index=-1 - @widgets.each_with_index {|item, index| - if item.getId()==widget.widget_key - found_index=index - end - } - if found_index>-1 - active_widget_ids=active_widget_ids << (widget.widget_key+"_"+found_index.to_s()) - end - end - return "\'"+active_widget_ids.join("\',\'")+"\'" - end - - def item_by_metric_name_and_categ_id(items, metric_name, rules_category_id) - return nil if items.nil? - items.each do |item| - return item if (item.metric.name==metric_name and - item.rules_category_id == rules_category_id and - item.rule_id.nil?) - end - nil - end - - def formatted_value(measure, default='') - measure ? measure.formatted_value : default - end - - def measure(metric_key) - @snapshot.measure(metric_key) - end -end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb index 9b353e4f638..bded9a5f141 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/widget_properties_helper.rb @@ -20,36 +20,18 @@ module WidgetPropertiesHelper - def valid_property_value?(type, value, parameter="") - if type==WidgetProperty::TYPE_INTEGER - value.to_i.to_s == value - - elsif type==WidgetProperty::TYPE_FLOAT - true if Float(value) rescue false - - elsif type==WidgetProperty::TYPE_BOOLEAN - value=="1" || value=="0" - - elsif type==WidgetProperty::TYPE_STRING - true - - else - false - end - end - def property_value_field(definition, value) val=value || definition.defaultValue() - if definition.type()==WidgetProperty::TYPE_INTEGER + if definition.type.name()==WidgetProperty::TYPE_INTEGER text_field_tag definition.key(), val, :size => 10 - elsif definition.type()==WidgetProperty::TYPE_FLOAT + elsif definition.type.name()==WidgetProperty::TYPE_FLOAT text_field_tag definition.key(), val, :size => 10 - elsif definition.type()==WidgetProperty::TYPE_BOOLEAN + elsif definition.type.name()==WidgetProperty::TYPE_BOOLEAN check_box_tag definition.key(), "true", val=='true' - elsif definition.type()==WidgetProperty::TYPE_STRING + elsif definition.type.name()==WidgetProperty::TYPE_STRING text_field_tag definition.key(), val, :size => 10 else diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/widget.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/widget.rb index 4cbc5280f80..4546bc3ad62 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/widget.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/widget.rb @@ -27,9 +27,6 @@ class Widget < ActiveRecord::Base validates_presence_of :widget_key validates_length_of :widget_key, :within => 1..256 - #--------------------------------------------------------------------- - # WIDGET PROPERTIES - #--------------------------------------------------------------------- def property(key) properties().each do |p| return p if (p.key==key) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb index 3a0706c886b..df755df8221 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/widget_property.rb @@ -66,15 +66,15 @@ class WidgetProperty < ActiveRecord::Base if value.empty? errors<<"Missing value" unless definition.optional() else - errors<<"Please type an integer (example: 123)" if definition.type()==TYPE_INTEGER && value.to_i.to_s!=value - if definition.type()==TYPE_FLOAT + errors<<"Please type an integer (example: 123)" if definition.type.name()==TYPE_INTEGER && value.to_i.to_s!=value + if definition.type.name()==TYPE_FLOAT begin Float(value) rescue errors<<"Please type a number (example: 123.45)" end end - errors<<"Please check value" if definition.type()==TYPE_BOOLEAN && !(value=="true" || value=="false") + errors<<"Please check value" if definition.type.name()==TYPE_BOOLEAN && !(value=="true" || value=="false") end errors end diff --git a/sonar-server/src/main/webapp/stylesheets/dashboard.css b/sonar-server/src/main/webapp/stylesheets/dashboard.css index a34478cdaa6..58ec7523270 100644 --- a/sonar-server/src/main/webapp/stylesheets/dashboard.css +++ b/sonar-server/src/main/webapp/stylesheets/dashboard.css @@ -55,11 +55,12 @@ } #dashboard .widget_def { - border-right: 2px solid #FFD324; + border-right: 1px dashed #FFD324; padding: 5px; display: inline-block; - width: 180px; + width: 160px; white-space: normal; + vertical-align: top; } /*OPERATIONS*/ diff --git a/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java b/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java index 52eb5197aa7..c93558678e0 100644 --- a/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java +++ b/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java @@ -154,7 +154,7 @@ class FakeView implements View { @WidgetProperties({ @WidgetProperty(key="foo", optional = false), - @WidgetProperty(key="bar", defaultValue = "30", type = "INTEGER") + @WidgetProperty(key="bar", defaultValue = "30", type = WidgetPropertyType.INTEGER) }) class EditableWidget implements Widget { -- 2.39.5