1 6 7 12 13 package org.exoplatform.portlets.weather.component; 14 15 import java.util.List ; 16 17 import javax.faces.context.FacesContext; 18 import javax.portlet.ActionResponse; 19 import javax.portlet.PortletMode; 20 import org.exoplatform.faces.core.component.UIPortlet; 21 import org.exoplatform.faces.core.event.ExoActionEvent; 22 import org.exoplatform.faces.core.event.ExoActionListener; 23 import org.exoplatform.portlets.weather.WeatherData; 24 import org.exoplatform.portlets.weather.WeatherUtil; 25 26 27 public class UIWeatherEditPortlet extends UIPortlet { 28 29 private WeatherUtil weatherUtil_; 30 private WeatherData weatherData_; 31 private boolean storeUserPref_; 32 boolean editOK_; 33 34 private UIWeatherTitle uiWeatherTitle; 35 private UIWeatherForm uiWeatherForm; 36 private UISelectStationForm uiSelectStationForm; 37 38 public UIWeatherEditPortlet() { 39 weatherData_ = new WeatherData(); 40 weatherUtil_ = WeatherUtil.getInstance(this); 41 storeUserPref_ = false; 42 43 setId("UIWeatherEditPortlet") ; 44 setRendererType("ChildrenRenderer"); 45 46 List children = getChildren(); 47 48 uiWeatherTitle = new UIWeatherTitle(); 49 uiWeatherTitle.setId("UIWeatherEditPortletTitle"); 50 uiWeatherTitle.setRendered(true); 51 52 uiWeatherForm = new UIWeatherForm(); 53 uiWeatherForm.setRendered(true); 54 uiSelectStationForm = new UISelectStationForm(); 55 uiSelectStationForm.setRendered(false); 56 57 process(); 58 children.add(uiWeatherTitle); 59 children.add(uiWeatherForm); 60 children.add(uiSelectStationForm); 61 62 uiWeatherForm.addActionListener(new SearchStationListener(). 65 setActionToListen("WeatherFormSearch")) ; 66 uiSelectStationForm.addActionListener(new SelectStationListener(). 67 setActionToListen("SelectStationFormOk")) ; 68 } 69 70 private void process() { 71 String code; 72 73 if (!weatherUtil_.isServiceInitialized()) { 74 uiWeatherTitle.setTitle("GlobalWeather Service can not be initialized"); 75 uiWeatherTitle.setRendered(true); 76 uiWeatherForm.setRendered(false); 77 uiSelectStationForm.setRendered(false); 78 } 79 else { 80 try { 81 weatherUtil_.checkRequest(weatherData_); 82 } 83 catch (Exception ex) { 84 ex.printStackTrace(); 85 weatherUtil_.setServiceAvailable(false); 86 } 87 if (!weatherUtil_.isServiceAvailable()) { 88 uiWeatherTitle.setTitle("Service is not available"); 89 uiWeatherTitle.setRendered(true); 90 uiWeatherForm.setRendered(false); 91 uiSelectStationForm.setRendered(false); 92 } 93 else { 94 if (weatherData_.isStationFound()) { 95 if (weatherData_.getNbStations() == 1) { 96 uiWeatherTitle.setTitle("Weather Portlet Preferences ("+ 97 weatherData_.getStationsList()[0].getName()+")"); 98 if (storeUserPref_) { 99 try { 100 weatherData_.getPreferences().setValue("code",weatherData_.getDisplayedStationCode().toUpperCase()); 101 weatherData_.getPreferences().store(); 102 editOK_ = true; 103 } 104 catch (Exception ex) { 105 storeUserPref_ = false; 106 ex.printStackTrace(); 107 uiWeatherTitle.setTitle("Weather Portlet Preferences : Erreur durant la sauvegarde"); 108 } 109 if (editOK_) { 110 uiWeatherForm.setSearchStationCode(weatherData_.getPreferredStationCode()); 111 uiWeatherForm.setSearchStationName(""); 112 ActionResponse response = (ActionResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); 114 try { 115 response.setPortletMode(PortletMode.VIEW); 116 } catch (Exception e) { 117 e.printStackTrace(); 118 } 119 } 120 } 121 else { 122 uiWeatherForm.setSearchStationCode(weatherData_.getPreferredStationCode()); 123 uiWeatherForm.setSearchStationName(""); 124 } 125 uiWeatherTitle.setRendered(true); 126 uiWeatherForm.setRendered(true); 127 uiSelectStationForm.setRendered(false); 128 } else if (weatherData_.getNbStations() > 1) { 129 uiWeatherTitle.setTitle("S�lection d'une station"); 130 uiWeatherTitle.setRendered(true); 131 uiWeatherForm.setRendered(false); 132 uiSelectStationForm.setStationsList(weatherData_.getStationsList()); 133 uiSelectStationForm.setRendered(true); 134 } 135 } 136 else { 137 uiWeatherTitle.setTitle("Weather Portlet Preferences : Station inconnue !"); 138 storeUserPref_ = false; 139 uiWeatherTitle.setRendered(true); 140 uiWeatherForm.setRendered(true); 141 uiSelectStationForm.setRendered(false); 142 } 143 } 144 } 145 } 146 147 public class SearchStationListener extends ExoActionListener { 148 public void execute(ExoActionEvent event) throws Exception { 149 editOK_ = false; 150 storeUserPref_ = true; 151 weatherData_.setSearchStationCode(uiWeatherForm.getSearchStationCode()); 152 weatherData_.setSearchStationName(uiWeatherForm.getSearchStationName()); 153 weatherData_.setDisplayedStationCode(""); 154 process(); 155 } 156 } 157 158 public class SelectStationListener extends ExoActionListener { 159 public void execute(ExoActionEvent event) throws Exception { 160 editOK_ = false; 161 storeUserPref_ = true; 162 weatherData_.setSearchStationCode(""); 163 weatherData_.setSearchStationName(""); 164 weatherData_.setDisplayedStationCode(uiSelectStationForm.getSelectStationCode()); 165 process(); 166 } 167 } 168 } 169 170 | Popular Tags |