This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

setPreserveAspectRatio

General questions regarding the development with JVx.

setPreserveAspectRatio

Postby lucdep » Thu Nov 15, 2018 1:47 pm

Hi,

I'm trying to set the aspect ratio of ImageViewer with this code:

editWorkpicsWrkpImage.setDataRow(rdbWorkpics);
editWorkpicsWrkpImage.setColumnName("WRKP_IMAGE");
editWorkpicsWrkpImage.setBackground(new UIColor(0xb8cce4));
((IImageViewer<UIImageViewer>)editWorkpicsWrkpImage.getCurrentCellEditor()).setPreserveAspectRatio(true);

but Eclipse always give me a type safety warning: Unchecked cast from ICellEditor to IImageViewer<UIImageViewer>

Can't find where my error comes from.
lucdep
 
Posts: 14
Joined: Wed Oct 10, 2018 12:01 pm

Re: setPreserveAspectRatio

Postby rzenz » Thu Nov 15, 2018 2:25 pm

The error message in this case says that the cast cannot be verified at runtime. An example:

We have a List which holds custom objects.

Code: Select all
List<CustomObject> list = new ArrayList<>();
list.add(new CustomObject());
list.add(new CustomObject());
list.add(new CustomObject());


Now, the List interface itself is rather typesafe, but assuming that we have a method which returns Object and our list:


Code: Select all
public Object getList()
{
    List<CustomObject> list = new ArrayList<>();
    list.add(new CustomObject());
    list.add(new CustomObject());
    list.add(new CustomObject());
    return list;
}


We'de need to cast the list to the appropriate type when calling that function:

Code: Select all
List<CustomObject> castedList = (List<CustomObject>)getList();

CustomObject item = castedList.get(1);


However, information about generic types is only available in source-code form. The Java generics system is a compile-time only system, at runtime there is no information on the type of a generic variable. Keeping that in mind, the actual code during runtime would look like this:

Code: Select all
List castedList = (List)getList();

CustomObject item = (CustomObject)castedList.get(1);


As we can see, we only get a non-specific List anymore. Now in our case that is not a problem, but it is possible that during runtime an item of a different class has ended up in the list, so the content of our list is this:

Code: Select all
[0]: CustomObject
[1]: String
[2]: CustomObject
[3]: CustomObject


So our code from above, which is compiling fine and also looks sound:

Code: Select all
List<CustomObject> castedList = (List<CustomObject>)getList();

CustomObject item = castedList.get(1);


Is actually throwing a ClassCastException during runtime. And that is what that error message is trying to tell you, that you can cast this object to another object with a generic type, but in no way can this cast be verified during compiletime or during runtime (and so should be avoided, if possible).
User avatar
rzenz
 
Posts: 36
Joined: Mon Dec 12, 2016 1:40 pm
Location: Vienna, Austria

Re: setPreserveAspectRatio

Postby johnit » Thu Nov 15, 2018 3:36 pm

johnit
 
Posts: 29
Joined: Fri Nov 16, 2012 5:58 pm

Re: setPreserveAspectRatio

Postby lucdep » Thu Nov 15, 2018 3:49 pm

Is my formulation wrong? Should I formulate it in another way? There must certainly be a safe way to use the Method setPreserveAspectRatio.
lucdep
 
Posts: 14
Joined: Wed Oct 10, 2018 12:01 pm

Re: setPreserveAspectRatio

Postby Development@SIB » Thu Nov 15, 2018 4:08 pm

Simply use a field:

Syntax: [ Download ] [ Hide ]
IImageViewer<UIImageViewer> viewer = editor.getCurrentCellEditor();
viewer.setPreserveAspectRatio(true);
User avatar
Development@SIB
 
Posts: 321
Joined: Mon Sep 28, 2009 1:54 pm

Re: setPreserveAspectRatio

Postby rzenz » Thu Nov 15, 2018 4:10 pm

Yes, you could set a UIImageView with the appropriate property on the model.

Code: Select all

private static final UIImageViewer IMAGEVIEWER_WITH_ASPECT_RATIO;

static
{
    IMAGEVIEWER_WITH_ASPECT_RATIO = new UIImageViewer();
    IMAGEVIEWER_WITH_ASPECT_RATIO.setPreserveAspectRatio(true);
}

// Some time later.
rdbWorkpics
    .getRowDefinition()
    .getColumnDefinition("WRKP_IMAGE")
    .getDataType()
    .setCellEditor(IMAGEVIEWER_WITH_ASPECT_RATIO);
User avatar
rzenz
 
Posts: 36
Joined: Mon Dec 12, 2016 1:40 pm
Location: Vienna, Austria

Re: setPreserveAspectRatio

Postby lucdep » Mon Nov 19, 2018 4:02 pm

OK, thanks it works like this!
lucdep
 
Posts: 14
Joined: Wed Oct 10, 2018 12:01 pm


Return to Development