Jun 6, 2012 - Getting PrimeFaces mix of Selects, Objects and Converters to work

Comments

If you are having problems with the AJAX Select components from PrimeFaces when selecting Java objects, inside a DataTable or not, this solution might work for you.

If you have something like the following:
<p:selectOneMenu value="#{deviceBean.currentLocation}"
converter="locationConverter">

<f:selectItem itemLabel="#{deviceBean.currentLocation.name}"
itemValue="#{deviceBean.currentLocation}" />
<f:selectItems
value="#{deviceBean.locations}"
var="location"
itemLabel="#{location.place}"
itemValue="#{location}" />
<f:ajax event="change" execute="@this" render="@this" />

</p:selectOneMenu>

Your managed bean is something like my DeviceBean:
@ManagedBean
@SessionScoped
public class DeviceBean implements Serializable {

@EJB
private Backend backend;

private List locations;
private Location currentLocation;

public DeviceBean() {
locations = backend.getLocations();
}

public List getCurrentLocation() {
return currentLocation;
}

public Location getCurrentLocation() {
return currentLocation;
}
}

Your converter is something like:
@FacesConverter(forClass=Location.class,value="locationConverter")
public class LocationConverter implements Converter {

@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Location location = new Location()
// some operations to set the parameters of Location based on the String
return location;
}

@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
Location location = (Location) value;
String string;
// some operations to save the parameters of Location into a String
return string;
}
}

And the object you want to change is something like my Location:
public class Location {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}


And your Select doesn't work, e.g. it doesn't respond to the accept button nor gives any error or logging, then probably it's just discarding the new element you just selected (in my case, of type Location).

It discards it because the object returned by the converter's getAsObject method may not match any of the objects allowed in the locations list from the DeviceBean. If this is the cause, then it means Location doesn't have proper hashCode and equals methods, which are required by Java to check if two objects of the same type have different contents or not, among other things. Because the selectOneMenu requires an object that is on the list, as defined in value="#{deviceBean.currentLocation}", it just fails to do anything.

So, don't forget, get hashCode and equals to the data structures you need to check for equality, explicitily or not, like this (as generated by my NetBeans):
public class Location {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public boolean equals(Object obj) {
if(obj == null)
return false;
if(getClass() != obj.getClass())
return false;
final LocationTest other = (LocationTest)obj;
if(!Objects.equals(this.name, other.name))
return false;
return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.name);
return hash;
}
}

May 18, 2012 - Google Earth Plug-in and the ATL 10.00

Comments


While playing with the Google Earth Plug-in to integrate in a WebBrowser control of a WPF application I'm currently developing, I started getting this strange background text instead of the map/earth: ATL 10.00.
ATL 10.00
I needed to do two things to get my page working. one simple thing.


Update:  It seems my previous method could still fail in some random times and even throw beautiful IE script errors, so here's what's really going on:

The WebBrowser control will by default, for compatibility reasons, render pages as in Internet Explorer 7 standards. The Google Earth plug-in seems to currently have some compatibility issues with IE7, therefore problems can arise. In this case, the ATL 10.00 stays above the map, blanking it.

The Fix
Use the most recent version of Internet Explorer. In order to do this, you must declare a meta tag in your HTML page like the following (this enables IE9 in WebBrowser when it loads the page):
<meta http-equiv="X-UA-Compatible" content="IE=9"/>


First, I used a workaround which consists in delaying the creation of the Google Earth plug-in, thus avoiding a race condition as mentioned in issue 701 from the earth-api-samples:


function Init() {
if (google.earth.isSupported()) {
// timeout is required or google earth plugin may get into a race condition and die
setTimeout("google.earth.createInstance('map3d', initCB, failureCB); alert('done');", 500);
}
}

/* other functions like initCV and failureCB */

google.setOnLoadCallback(Init);
google.load("earth", "1");


Second, I made sure the #map_canvas is above everything, especially during loading.


My application initially hides the WebBrowser. If I displayed the WebBrowser before the loading timeout, I could see the map alright... but if I waited longer than the loading timeout and then displayed the map, I just got the ATL 10.00.


So I thought, maybe the map is always being rendered but the ATL 10.00 layer is above it. I set #map_canvas the style z-index: -1 and it worked properly!


#map_canvas {
z-index: -1;
/* etc */
}


My JS scripts are simple and small and I cannot reproduce ATL 10.00 in native Internet Explorer (in my case IE9) because, I suppose, the map is always visible with positive dimensions (instead of what happens in my WPF application).

Useful links:

May 14, 2012 - Aligning contents of PrimeFaces' PanelGrid

Comments

If you are trying to align the contents of a panelGrid and nothing seems to work (styles that you define eventually don't appear in the rendered page) then this might help you.


This post covers a very specific case of alignment not working, and it depends on the following:

  • You are using the columns property in your panelGrid;
  • You are trying to define rows and columns manually, with <p:row> and <p:column>, because you probably want to use colspan or rowspan

Well, you probably checked the ShowCase of PrimeFaces to learn about PanelGrid. However, it is not clear in their example (neither in their Documentation) that when manually defining rows and columns, the total number of columns in the panelGrid should not be defined, i.e, the columns property should not exist. Their example is correct but doesn't make it clear that it should be exactly like that.

In conclusion, you cannot use the panelGrid like the following:
<p:panelGrid columns="2">
<p:row>
<p:column>
<h:outputText value="Hello"></h:outputText>
</p:column>
<p:column>
<h:outputText value="World"></h:outputText>
</p:column>
</p:row>
</p:panelGrid>

Useful links: