001    package org.hackystat.projectbrowser.page.projectportfolio.configurationpanel;
002    
003    import org.apache.wicket.AttributeModifier;
004    import org.apache.wicket.ajax.AjaxRequestTarget;
005    import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
006    import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
007    import org.apache.wicket.markup.html.basic.Label;
008    import org.apache.wicket.markup.html.basic.MultiLineLabel;
009    import org.apache.wicket.markup.html.form.Button;
010    import org.apache.wicket.markup.html.form.DropDownChoice;
011    import org.apache.wicket.markup.html.form.Form;
012    import org.apache.wicket.markup.html.list.ListItem;
013    import org.apache.wicket.markup.html.list.ListView;
014    import org.apache.wicket.markup.html.panel.Panel;
015    import org.apache.wicket.model.Model;
016    import org.apache.wicket.model.PropertyModel;
017    import org.hackystat.projectbrowser.page.popupwindow.PopupWindowPanel;
018    import org.hackystat.projectbrowser.page.projectportfolio.ProjectPortfolioPage;
019    import org.hackystat.projectbrowser.page.projectportfolio.detailspanel.
020            PortfolioMeasureConfiguration;
021    import org.hackystat.projectbrowser.page.projectportfolio.detailspanel.ProjectPortfolioDataModel;
022    
023    /**
024     * Form in Project Portfolio configuration panel to input data.
025     * 
026     * @author Shaoxuan Zhang
027     * 
028     */
029    public class ProjectPortfolioConfigurationForm extends Form {
030      /** Support serialization. */
031      private static final long serialVersionUID = 447730202032199770L;
032      
033      /** The associtated ProjectPortfolioDataModel. */
034      private ProjectPortfolioDataModel dataModel;
035      /** The colors to choose from. */
036      /*
037      private static final String[] colors = { "ffff00", // yellow
038          "ff6600", // orange
039          //"ff8080", // pink
040          "ff00ff", // magenta
041          "ff0000", // red
042          "6600ff", //purple
043          //"808080", // gray
044          "00ffff", // cyan
045          "00ff00", // green
046          "0066ff", 
047          //"0000ff", // blue
048          //"000000", // black
049      };
050      */
051      /** The introductions. */
052      private static final String introductions = 
053        "Enabled : Only enabled measure will be shown in detail panel\n\n"
054        + "Colorable : Colorable measure's chart and value will be colored according to the following"
055        + " setting. Noncolorable measure's chart and value will be black.\n\n"
056        + "Is Higher Better: If higher value means better."
057        + "Higher Threshold : The threshold of high value. \n\n"
058        + "Lower Threshold : The threshold of low value.\n\n";
059      /** The word style. */
060      //private static final String STYLE_KEY = "style";
061      /** THe preceding of HTTP background color style setting. */
062      //private static final String BACKGROUND_COLOR_PRECEDING = "background-color:#";
063      
064      /**
065       * @param id the wicket component id.
066       * @param d the data model that will be configure here.
067       */
068      public ProjectPortfolioConfigurationForm(String id, ProjectPortfolioDataModel d) {
069        super(id);
070        
071        this.dataModel = d;
072        /*
073        //General settings
074        add(new TextField("timePhrase", new PropertyModel(dataModel, "timePhrase")));
075        add(new DropDownChoice("granularity", 
076                               new PropertyModel(dataModel, "telemetryGranularity"), 
077                               dataModel.getGranularities()));
078        add(new CheckBox("includeCurrentWeek", new PropertyModel(dataModel, "includeCurrentWeek")));
079        */
080        
081        //Color settings.
082        /*
083        final Select goodColorSelect = new Select("goodColorSelect", new PropertyModel(dataModel,
084        "goodColor"));
085        goodColorSelect.add(new ColorSelectOptionList("goodColorOptionList", Arrays.asList(colors)));
086        goodColorSelect.add(new AttributeModifier(STYLE_KEY, true, new Model(
087            BACKGROUND_COLOR_PRECEDING + dataModel.getGoodColor())));
088        goodColorSelect.setOutputMarkupId(true);
089        goodColorSelect.add(new AjaxColorSelectChangeBackgroundColorBehavior(goodColorSelect));
090        add(goodColorSelect);
091    
092        final Select sosoColorSelect = new Select("sosoColorSelect", new PropertyModel(dataModel,
093        "sosoColor"));
094        sosoColorSelect.add(new ColorSelectOptionList("sosoColorOptionList", Arrays.asList(colors)));
095        sosoColorSelect.add(new AttributeModifier(STYLE_KEY, true, new Model(
096            BACKGROUND_COLOR_PRECEDING + dataModel.getSosoColor())));
097        sosoColorSelect.setOutputMarkupId(true);
098        sosoColorSelect.add(new AjaxColorSelectChangeBackgroundColorBehavior(goodColorSelect));
099        add(sosoColorSelect);
100    
101        final Select badColorSelect = new Select("badColorSelect", new PropertyModel(dataModel,
102        "badColor"));
103        badColorSelect.add(new ColorSelectOptionList("badColorOptionList", Arrays.asList(colors)));
104        badColorSelect.add(new AttributeModifier(STYLE_KEY, true, new Model(
105            BACKGROUND_COLOR_PRECEDING + dataModel.getBadColor())));
106        badColorSelect.setOutputMarkupId(true);
107        badColorSelect.add(new AjaxColorSelectChangeBackgroundColorBehavior(goodColorSelect));
108        add(badColorSelect);
109        */
110        
111        //measure specified settings.
112        
113        final ListView measureList = new ListView("measureList", dataModel.getMeasures()) {
114          /** Support serialization. */
115          public static final long serialVersionUID = 1L;
116    
117          @Override
118          protected void populateItem(final ListItem item) {
119            item.setOutputMarkupId(true);
120            final PortfolioMeasureConfiguration measure = 
121              (PortfolioMeasureConfiguration) item.getModelObject();
122            
123            String cellClass;
124            if (item.getIndex() % 2 == 0) {
125              cellClass = "even";
126            }
127            else {
128              cellClass = "odd";
129            }
130            item.add(new AttributeModifier("class", true, new Model(cellClass)));
131    
132            item.add(new Label("measureNameLabel", measure.getDisplayName()));
133    
134            // Add measure's configuration panel
135            final Panel measurePanel = measure.getConfigurationPanel("measurePanel");
136            measurePanel.setVisible(measure.hasClassifier() && measure.isEnabled());
137            measurePanel.setOutputMarkupId(true);
138            item.add(measurePanel);
139            
140            item.add(new AjaxCheckBox("enableCheckBox", new PropertyModel(measure, "enabled")) {
141              /** Support serialization. */
142              public static final long serialVersionUID = 1L;
143    
144              @Override
145              protected void onUpdate(AjaxRequestTarget target) {
146                //measurePanel.setVisible(measure.isEnabled());
147                target.addComponent(getForm());
148              }
149            });
150            final DropDownChoice colorMethodMenu = new DropDownChoice("colorMethod", 
151                new Model(measure.getClassiferName()),
152                ProjectPortfolioDataModel.availableClassifiers) {
153              /** Support serialization. */
154              private static final long serialVersionUID = 5465487314644465276L;
155              @Override
156              public boolean isVisible() {
157                return measure.isEnabled();
158              }
159            };
160            colorMethodMenu.add(new AjaxFormComponentUpdatingBehavior("onchange") {
161              /** Support serialization. */
162              private static final long serialVersionUID = -6447496738809283902L;
163    
164              @Override
165              protected void onUpdate(AjaxRequestTarget target) {
166                String colorMethod = colorMethodMenu.getModelObjectAsString();
167                measure.setStreamClassifier(colorMethod);
168                target.addComponent(getFormComponent().getForm());
169              }
170              
171            });
172            item.add(colorMethodMenu);
173            
174            // Add parameter List
175    
176            item.add(new TelemetryParameterPanel("telemetryParameters", measure));
177          }
178        };
179        add(measureList);
180        PopupWindowPanel parameterPopup = new PopupWindowPanel("instructionPopup",
181            "Configuration instruction", "Configuration Instructions");
182        parameterPopup.getModalWindow().setContent(
183            new MultiLineLabel(parameterPopup.getModalWindow().getContentId(), introductions));
184        add(parameterPopup);
185    
186        Button okButton = new Button("submit") {
187          /** Support serialization. */
188          public static final long serialVersionUID = 1L;
189          @Override
190          public void onSubmit() {
191            dataModel.saveUserConfiguration();
192            ((ProjectPortfolioPage)this.getPage()).setConfigurationPanelVisible(false);
193          }
194        };
195        add(okButton);
196        this.setDefaultButton(okButton);
197        
198        Button resetButton = new Button("reset") {
199          /** Support serialization. */
200          public static final long serialVersionUID = 1L;
201          @Override
202          public void onSubmit() {
203            dataModel.resetUserConfiguration();
204          }
205        };
206        add(resetButton);
207      }
208    
209      /**
210       * Set the panel to be invisible after form submitted.
211       */
212      /*
213      @Override
214      public void onSubmit() {
215        dataModel.saveUserConfiguration();
216        ((ProjectPortfolioPage)this.getPage()).setConfigurationPanelVisible(false);
217      }
218      */
219    
220      /**
221       * The list of select options for the color select field.
222       * 
223       * @author Shaoxuan Zhang
224       */
225      /*
226      public static class ColorSelectOptionList extends ListView {
227        public ColorSelectOptionList(final String id, final List<?> list) {
228          super(id, list);
229        }
230        private static final long serialVersionUID = -3842069135751729472L;
231        @Override
232        protected void populateItem(final ListItem item) {
233          item.setRenderBodyOnly(true);
234          final String colorHex = item.getModelObjectAsString();
235          final SelectOption colorSelectOption = new SelectOption("option", new Model(
236              (Serializable) item.getModelObject())) {
237            private static final long serialVersionUID = 4298345446185051771L;
238    
239            protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag) {
240              replaceComponentTagBody(markupStream, openTag, "");
241            }
242          };
243          colorSelectOption.add(new SimpleAttributeModifier(STYLE_KEY, BACKGROUND_COLOR_PRECEDING
244              + colorHex));
245          item.add(colorSelectOption);
246        }
247      }
248      */
249    
250      /**
251       * Change the component's background color to the color value within the component.
252       * 
253       * @author Shaoxuan Zhang
254       */
255      /*
256      public static class AjaxColorSelectChangeBackgroundColorBehavior extends OnChangeAjaxBehavior {
257        private static final long serialVersionUID = -5326547000439295241L;
258        Select select;
259        public AjaxColorSelectChangeBackgroundColorBehavior(Select select) {
260          this.select = select;
261        }
262        @Override
263        protected void onUpdate(AjaxRequestTarget target) {
264          select.add(new AttributeModifier(STYLE_KEY, true, new Model(BACKGROUND_COLOR_PRECEDING
265              + select.getModelObjectAsString())));
266          target.addComponent(select);
267        }
268      }
269      */
270    
271    }