getItemProperty returns not null - Vaadin bug? - containers

I need help, maybe I am blind. Here is a fragment of my code:
System.out.println("itemPropertyIDS="+item.getItemPropertyIds().toString());
System.out.println("argname="+argName);
Property<?> p = item.getItemProperty(argName);
if (p != null) {
System.out.println("p="+p.toString());
return p.getValue();
}
// Continue ...
It returns a currious null value instead of continue, even if the propertyId doesn't exists.
This is written on my console:
itemPropertyIDS=[iconName, iconResource, nodeType, nodeValue, nodeName, handler, nodeData]
argname=Lab
p=com.vaadin.data.util.IndexedContainer$IndexedContainerProperty#12967
The first row shows list of property names.
I expected getTtemProperty must return null, but not.
The item comes from IndexedContainer.
Can you help me? Any idea? Thanky You.

I tested your code and indeed - property p is not null even though property doesn't exist in IndexedContainer. Reading the comments of Vaadin ticket pasted by kris54321, it makes sense not fixing the bug as some applications may rely on that feature. Fixing the bug may break those apps.
Possible workarounds for this problem:
Check directly the propertyId-collection if property exists in the container:
if(item.getItemPropertyIds().contains(argName) {
Property<?> p = item.getItemProperty(argName);
System.out.println("p="+p.toString());
return p.getValue()
}
Change the logic to check property value
if(item.getItemProperty(argname).getValue() != null) }
//Do things
{

I was forced to make workaround using "item.getItemPropertyIds().contains(argName)". For me, the priority is to check whether the item contains the value (null possible) or not. If yes then I use the value (can be null) else another activity is done.
If they dont want to fix the bug due to application using the bug, then the documentation should be fixed, else all novices in Vaadin (as I am) will be confused.
The implementation in IndexedContainer$IndexedContainer is wrong:
#Override
public Property getItemProperty(Object id) {
return new IndexedContainerProperty(itemId, id);
}
This can never return null as the documentation says.

Related

Is there any way to tell if the TADOTable I am looking for is in the database (MS Access)?

I use C++ Builder (Delphi 10.2 and C++Builder 10.2 Update 2) and I need a method that, in case there is no particular table, creates it using TADO objects (ADODB)?
I mean TADOQuery, TADOTable, TADOConnection, etc.
How can I do this?
I tried looking at the methods of TADOConncection, of TADOTable, but none of them seem to be useful.
I also tried this route (https://docwiki.embarcadero.com/Libraries/Alexandria/en/Bde.DBTables.TTable.Exists) but there are compatibility problems.
Does this help ?
TADOConnection *YOUR_TADOCONNECTION; // your connection defined earlier in your code
TStringList *TableList = new TStringList;
bool WithSystemTables = true; // or false according to your requirements
YOUR_TADOCONNECTION->GetTableNames(TableList, WithSystemTables);
for (int i = 0 i < TableList->Count(); i++) {
String NextTableName = TableList->Strings[i];
/*.... your check for the table name being the one you want goes here .... */
}
delete TableList;

Equivalent of Platform::IBoxArray in C++/WinRT

I am currently porting an UWP application from C++/CX to C++/WinRT. I encountered a safe_cast<Platform::IBoxArray<byte>^>(data) where data is of type Windows::Foundation::IInspectable ^.
I know that the safe_cast is represented by the as<T> method, and I know there are functions for boxing (winrt::box_value) and unboxing (winrt::unbox_value) in WinRT/C++.
However, I need to know the equivalent of Platform::IBoxArray in order to perform the cast (QueryInterface). According to https://learn.microsoft.com/de-de/cpp/cppcx/platform-iboxarray-interface?view=vs-2017, IBoxArray is the C++/CX equivalent of Windows::Foundation::IReferenceArray, but there is no winrt::Windows::Foundation::IReferenceArray...
Update for nackground: What I am trying to achieve is retrieving the view transform attached by the HoloLens to every Media Foundation sample from its camera. My code is based on https://github.com/Microsoft/HoloLensForCV, and I got really everything working except for this last step. The problem is located around this piece of code:
static const GUID MF_EXTENSION_VIEW_TRANSFORM = {
0x4e251fa4, 0x830f, 0x4770, 0x85, 0x9a, 0x4b, 0x8d, 0x99, 0xaa, 0x80, 0x9b
};
// ...
// In the event handler, which receives const winrt::Windows::Media::Capture::Frames::MediaFrameReader& sender:
auto frame = sender.TryAcquireLatestFrame();
// ...
if (frame.Properties().HasKey(MF_EXTENSION_VIEW_TRANSFORM)) {
auto /* IInspectable */ userData = frame.Properties().Lookup(MF_EXTENSION_VIEW_TRANSFORM);
// Now I would have to do the following:
// auto userBytes = safe_cast<Platform::IBoxArray<Byte> ^>(userData)->Value;
//viewTransform = *reinterpret_cast<float4x4 *>(userBytes.Data);
}
I'm also working on porting some code from HoloLensForCV to C++/WinRT. I came up with the following solution for a very similar case (but not the exact same line of code you ask about):
auto user_data = source.Info().Properties().Lookup(c_MF_MT_USER_DATA); // type documented as 'array of bytes'
auto source_name = user_data.as<Windows::Foundation::IReferenceArray<std::uint8_t>>(); // Trial and error to get the right specialization of IReferenceArray
winrt::com_array<std::uint8_t> arr;
source_name.GetUInt8Array(arr);
winrt::hstring source_name_str{ reinterpret_cast<wchar_t*>(arr.data()) };
Specifically, you can replace the safe_cast with .as<Windows::Foundation::IReferenceArray<std::uint8_t> for a boxed array of bytes. Then, I suspect doing the same cast as me (except to float4x4* instead of wchar_t*) will work for you.
The /ZW flag is not required for my example above.
I can't believe that actually worked, but using information from https://learn.microsoft.com/de-de/windows/uwp/cpp-and-winrt-apis/interop-winrt-cx, I came up with the following solution:
Enable "Consume Windows Runtime Extension" via /ZW and use the following conversion:
auto abi = reinterpret_cast<Platform::Object ^>(winrt::get_abi(userData));
auto userBytes = safe_cast<Platform::IBoxArray<byte> ^>(abi)->Value;
viewTransform = *reinterpret_cast<float4x4 *>(userBytes->Data);
Unfortunately, the solution has the drawback of generating
warning C4447: 'main' signature found without threading model. Consider using 'int main(Platform::Array^ args)'.
But for now, I can live with it ...

How to check in sikuli that image is exist in screen or not and perform if else condition on that result

I am trying to automate a desktop application in sikuli. What i am doing copying data from from existing user and creating new user using some of that data. In form there are two check boxes. If that check boxes is ticked for existing user, then while creating new user i need to check the text box. for that am taking checked text box image and giving in if condition. If checked text box image is there in that page i will carry value 1 to a variable else value will be 0. according to that value am performing check uncheck function in new user creation page. But the issue what i am facing is, am not able to check if the image exist in that page or not in sikuli. Please anybody help me. my code is giving below
int bomanager=0;
int boswitchboard=0;
System.out.println("boswitchboard value before assign it to 1" + bomanager);
if (screen.exists("images/backofficeswitchboardwithtick.png") != null)
{
boswitchboard=1;
System.out.println("boswitchboard value after assign"+boswitchboard);
}
System.out.println("bomanager value before assign it to 1" + bomanager);
if(screen.exists("images/backofficemanagerwithtick.png") != null)
{
bomanager=1;
System.out.println("bomanager value after assign it to 1"+bomanager);
}
then using this value need to perform below function.
System.out.println("Before condition" + bomanager);
if (bomanager ==0){
screen.click("images/backofficemanagerwithtick.png");
}
screen.setAutoWaitTimeout(10);
System.out.println("Before condition" + boswitchboard);
if(boswitchboard==0){
System.out.println("Inside To tick Condition" + boswitchboard);
System.out.println("Ticking the SwitchBorad when itsnot already ticked");
screen.click("images/backofficeswitchboardwithtick.png");
}
I'm asssuming you're looking to use "if exists" method here
if exists(img, timeout):
click(img)
else:
....
with the method exists() I usually use:
if(exists("yourUrlImage")!=null):
(do something when exists)
else:
(do another thing when not exists)
Because that will return a "match" object.
Hope this helps

How to suppress the warning "Assignment within conditional. Did you mean == instead of =?"

With the new ASC 2.0 compiler I get warnings when I code like below:
// (_achievementsFromServer is an Array)
while(item=_achievementsFromServer.pop())
{
// do something with item here
}
The warning reads: "Assignment within conditional. Did you mean == instead of =?"
While in general I appreciate all warnings from the compiler, I'd like to suppress this one in this case because I did not mean == here. I want to pop all items in the array and do something with it until the array is empty.
while( (item=_achievementsFromServer.pop())==true )
seems to work but looks a bit confusing. Any other ideas?
This may seem better.
while(_achievementsFromServer.length > 0) {
var item:Object = _achievementsFromServer.pop();
}
Just like removeChild
var d:DisplayObjectContainer;
while(d.numChildren > 0) {
d.removeChildAt(0);
}
While I was hoping for some other way, I think #AmyBlankenship improved my own suggestion:
while((item=_achievementsFromServer.pop())!=null)
{
//....
}
It's clear and understandable what's going on, and doesn't rely on checking the length of the Array on every iteration.
Googling some more I found a compiler option -compiler.warn-assignment-within-conditional that could be set to false but then you won't be warned anywhere in your project anymore. And I'm not so confident that I never accidently type = instead of ==, so that's not a good solution I think.

Most readable way to write simple conditional check

What would be the most readable/best way to write a multiple conditional check such as shown below?
Two possibilities that I could think of (this is Java but the language really doesn't matter here):
Option 1:
boolean c1 = passwordField.getPassword().length > 0;
boolean c2 = !stationIDTextField.getText().trim().isEmpty();
boolean c3 = !userNameTextField.getText().trim().isEmpty();
if (c1 && c2 && c3) {
okButton.setEnabled(true);
}
Option 2:
if (passwordField.getPassword().length > 0 &&
!stationIDTextField.getText().trim().isEmpty() &&
!userNameTextField.getText().trim().isEmpty() {
okButton.setEnabled(true);
}
What I don't like about option 2 is that the line wraps and then indentation becomes a pain. What I don't like about option 1 is that it creates variables for nothing and requires looking at two places.
So what do you think? Any other options?
if (HasPassword() && HasStation() && HasUserName())
okButton.setEnabled(true);
bool HasPassword() {
return passwordField.getPassword().length > 0;
}
etc.
Note that option 1 does not allow for short circuiting behavior. That is, you calculate the value of all of the conditionals before evaluating the result of the first.
I would modify option 1 so that you're using variable names that actually have a meaning. That is, change the name of "c2" to be something like "stationIDIsEmpty" (and move the NOT into the conditional). That way the conditional is readable without having to glance back and forth for every variable.
So my code would probably look like:
boolean enteredPassword = passwordField.getPassword().length > 0;
boolean stationIDIsEmpty = stationIDTextField.getText().trim().isEmpty();
boolean userNameIsEmpty = userNameTextField.getText().trim().isEmpty();
if (enteredPassword && !stationIDIsEmpty && !userNameIsEmpty) {
okButton.setEnabled(true);
}
I voted for Chris Brandsma's answer.
But just wanted to mention the main issue I have with Option 1 is you are losing the benefit of &&. With option one, although I think it's more readable, you are processing comparisons when they may not be required.
Personally, I like the second way, because I find that using that way can make the predication of the conditionals clear. That is, with that method done properly, you can make the conditional comprehensible by "verablizing" it (whether or not you actually speak it is irrelevant).
That is, with your second option, it becomes clear that your conditional translates roughly as this: "If the password length is greater than zero, AND the stationIDTextField (trimmed) is NOT empty, AND the usernameTextField (trimmed) is NOT empty, then..."
I prefer the following:
if (passwordField.getPassword().length > 0
&& ! stationIDTextField.getText().trim().isEmpty()
&& ! userNameTextField.getText().trim().isEmpty())
{
okButton.setEnabled(true);
}
With this coding style I accomplish two things:
I can easily see that each extra line of the if is part of the condition because of the && (or ||) at the beggining.
I can easily see where the if statement ends because of the { at the next line.
Option1 is prime for applying the refactoring 'Replace temp with Query'. The reason being that someone can stuff in code between the variable is initialized and the check and change the behavior of the code. Or the check might be made with stale values.. an update has been made to the textfields between initialization and checking.
So my attempt at this would be
if (GetPasswordLength() > 0
&& FieldHelper.IsNotEmpty(stationIDTextField)
&& FieldHelper.IsNotEmpty(userNameTextField)
{
okButton.setEnabled(true);
}
FieldHelper is a class with public static methods (also called a Utility class / Static class in C#)