android-binding: onclick depend on variable - android-databinding

I need when isFavorite is true then when click then call onClickSubscribe() else call onClickUnsubscribe().
In my fragment:
#Override
public void onClickSubscribe() {
}
In my xml:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="handler"
type="SubscribeBrandDialogFragment" />
</data>
<TextView
android:id="#+id/subscribeTextView"
android:layout_width="185dp"
android:layout_height="40dp"
android:onClick="#{handler.isFavorite ? handler.onClickSubscribe() : handler.onClickUnsubscribe()}"
android:text="#{handler.isFavorite ? #string/unsubscribe : #string/subscribe}"/>
</layout>
But I get error:
e: [kapt] An exception occurred: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:onClick' with parameter type void on android.widget.TextView.
file:res\layout\subscribe_brand_dialog.xml
loc:100:31 - 100:108
****\ data binding error ****
at android.databinding.tool.processing.Scope.assertNoError(Scope.java:112)

Cannot find the setter for attribute 'android:onClick' with parameter
type void on android.widget.TextView
What does this say? Exactly this is problem!
If you don't write ()-> in your onClick then you have to give your View type as first parameter in method.
Solution
Change onClick line to below.
android:onClick="#{handler.isFavorite ? handler::onClickSubscribe : handler::onClickUnsubscribe}"
And set TextView as first parameter of onClickSubscribe & onClickUnsubscribe like.
void onClickSubscribe (TextView tv) {
...
}
& same for onClickUnsubscribe
All done!
I like shortcuts, so here is Tip
Make a common method with Boolean.
void onSubscribe(boolean isSubscribe){
if(isSubscribe){}
else {}
}
and call it like
android:onClick="#{() -> handler.onSubscribe(handler.isFavorite)}"
So your two methods are reduced to 1.
If you need to pass any other value just change parameter of onSubscribe. and pass from xml.

you can use this :
android:onClick="#{()->handler.isFavorite ? handler.onClickSubscribe() : handler.onClickUnsubscribe()}"
do no forget ()-> at first

Related

how to hide property page from some resource type

my application project contains a different type of resources. for some IFolder not all folders in the project I need to set property page on right click properties.
my problem is that this page is shown for all folders even the property tester returns false.
plugin.xml
<extension
point="org.eclipse.ui.propertyPages">
<page
class="com.my.ui.properties.PropertiesPage"
id="com.my.ui.properties.Properties"
name="page">
<enabledWhen>
<adapt
type="org.eclipse.core.resources.IFolder">
<test
property="com.my.ui.propertyTester.supperFolder">
</test>
</adapt></enabledWhen>
</page>
<extension
point="org.eclipse.core.expressions.propertyTesters">
<propertyTester
class="com.my.ui.properties.FolderTester"
id="com.my.ui.propertyTester"
namespace="com.my.ui.propertyTester"
properties="supperFolder"
type="org.eclipse.core.resources.IResource">
</propertyTester>
public class FolderTester extends PropertyTester {
#Override
public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
if ("supperFolder".equals(property) && receiver instanceof IFolder) {
IFolder folder = (IFolder) receiver;
if (IWResourceManager.getInstance().isIWResource(folder)) {
..
..
return true;
}
}
}
return false;
}
So what am I missing?
after hours of debugging, i finally found the mistake, I fill like I need to share it, in case someone has the same problem...
so..
in property tester extension point, tester "id" and "namespace" had the same value and that's was creating some sort of conflict.

Spring AOP get annotation value

I am using spring AOP, how can I get values from annotation,
Here is my annotation:
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.METHOD)
#Inherited
#Documented
public #interface ExecutionMethodProfiler
{
String value() default "defaultValue";;
}
here is my XML:
<aop:aspectj-autoproxy/>
<aop:config>
<aop:aspect ref="methodProfiler">
<aop:pointcut id="serviceMethod"
expression="(execution(* com.old..*(..)) or execution(* com.test..*(..))) and #annotation(com.test.profiler.ExecutionMethodProfiler)" />
<aop:around pointcut-ref="serviceMethod" method="profile"/>
</aop:aspect>
</aop:config>
And this is my serviceMethod:
public void profile(ProceedingJoinPoint jointPoint) throws Throwable {}
as of now I can get the values by using this code:
MethodSignature signature = (MethodSignature) jointPoint.getSignature();
System.out.println(signature.getMethod().getAnnotation(ExecutionMethodProfiler.class).value());
I don't like it, is there a better way?
First change your advice to take your annotation as an additional argument:
public void profile(
ProceedingJoinPoint jointPoint,
ExecutionMethodProfiler methodProfiler
) throws Throwable {}
Then bind the annotation to that argument in your pointcut:
<aop:around
pointcut="(execution(* com.old..*(..)) or execution(* com.test..*(..))) and #annotation(methodProfiler)"
method="profile"
arg-names="methodProfiler"
/>
I did not actually test it because I am a bit busy now, but this is basically how it works.

How to access the map returned by IParameterValues::getParameterValues()?

I declared a command and a commandParameter for this command. I specified the "values" of this commandParameter as a class implemented by myself. The implementation of this class is below,
public class ParameterValues implements IParameterValues {
#Override
public Map<String, Double> getParameterValues() {
// TODO Auto-generated method stub
Map<String, Double> values = new HashMap<String, Double>(2);
values.put("testParam", 1.1239);
values.put("AnotherTest", 4.1239);
return values;
}
}
The implementation of the handler of this command is blow,
public class testHandler extends AbstractHandler implements IHandler {
private static String PARAMETER_ID = "my.parameter1";
#Override
public Object execute(ExecutionEvent event) throws ExecutionException {
String value = event.getParameter(PARAMETER_ID);
MessageDialog.openInformation(HandlerUtil.getActiveShell(event),
"Test", "Parameter ID: " + PARAMETER_ID + "\nValue: " + value);
return null;
}
}
Now, I contribute the command to a menu,
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="my.edit"
label="Edit">
<command
commandId="myCommand.test"
label="Test1">
<parameter
name="my.parameter1"
value="testParam">
</parameter>
</command>
Since I specified a "values" class for the commandParater, I expect when the menu is clicked, this code line "String value = event.getParameter(PARAMETER_ID);" in the handler class returns 1.1239 instead of "testParam".
But, I still see that code line returns "testParam".
What's the problem? How could I access the map returned by getParameterValues()?
By the way, following menu declaration still works even I don't define "ppp" in the map.
<menuContribution
locationURI="menu:org.eclipse.ui.main.menu">
<menu
id="my.edit"
label="Edit">
<command
commandId="myCommand.test"
label="Test1">
<parameter
name="my.parameter1"
value="ppp">
</parameter>
</command>
Thanks!
From the javadoc, org.eclipse.core.commands.IParameterValues.getParameterValues() "Returns a map keyed by externalized names for parameter values. These names should be human-readable, and are generally for display to the user in a user interface of some sort. The values should be actual values that will be interpreted by the handler for the command."
This interface is used to provide translatable names for command parameters. So that "Show View (Package Explorer)" can be displayed in the Keys preference page instead of "Show View (org.eclipse.jdt.ui.PackageExplorer)"
But org.eclipse.ui.menus definitions must contain the parameter value as opposed to a translatable name.
You can define the parameter type and AbstractParameterValueConverter in the command definition if you want to process the string parameters and turn them into various objects.

Add scriptable property to Cocoa application derived from NSObject <NSApplicationDelegate>?

Can someone point me to an example of this working. I just want to set a property value via AppleScript. I have gone through all of the scriptable examples, which are setup differently.
<?xml version="1.0" encoding="UTF-8"?>
<dictionary title="">
<suite name="Circle View Scripting" code="bccS" description="Commands and classes for Circle View Scripting">
<class name="application" code="capp" description="" >
<cocoa class="NSApplication"/>
<property name="circletext" code="crtx" type="text" description="The text that gets spun into a circle">
<cocoa key="circleText"/>
</property>
<property name="myint" code="crmy" type="integer" description="The text that gets spun into a circle">
<cocoa key="myInt"/>
</property>
</class>
</suite>
the header file:
// header
#interface MyDelegate : NSObject <NSApplicationDelegate>
{
WebScriptObject *scriptObject;
WebView *webView;
NSWindow *window;
NSInteger myInt;
}
// implementation
- (BOOL)application:(NSApplication*)sender delegateHandlesKey:(NSString*)key
{
return key isEqualToString:#"myInt"] || [key isEqualToString:#"circleText"];;
}
-(NSInteger)myInt
{
NSInteger myInteger = 42;
return myInteger;
}
-(void)setMyInt:(NSInteger*)newVal
{
// do nothing right now
NSLog(#"SETTER CALLED");
}
// Applescript attempt to set property "myInt"
tell application "BrowserConfigClient"
set myint to 7
properties
end tell
Ultimately, the delegateHandlesKey method is called, I am able to return a value for the property, but the setter is never called. Thanks in advance...
Your method statement has an error...
-(void)setMyInt:(NSInteger*)newVal
There should not be a "*" as NSInteger is not a "pointer" variable. I see in the comments of your question that Ken Thomases has already told you this so make sure to fix it.
So if this is not your problem then look at your sdef file. I can see you did not close the dictionary tag. You need this as the last line of that file.
</dictionary>
I also have this as the second line in my sdef files...
<!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd">

NHibernate stored procedure problem

I'm having a hard time trying to get my stored procedure works with NHibernate. The data returned from the SP does not correspond to any database table.
This is my mapping file:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="DomainModel" namespace="DomainModel.Entities">
<sql-query name="DoSomething">
<return class="SomeClass">
<return-property name="ID" column="ID"/>
</return>
exec [dbo].[sp_doSomething]
</sql-query>
</hibernate-mapping>
Here is my domain class:
namespace DomainModel.Entities
{
public class SomeClass
{
public SomeClass()
{
}
public virtual Guid ID
{
get;
set;
}
}
}
When I run the code, it fails with
Exception Details: NHibernate.HibernateException: Errors in named queries: {DoSomething}
at line 80
Line 78: config.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "NHibernate.config"));
Line 79:
Line 80: g_sessionFactory = config.BuildSessionFactory();
When I debug into NHibernate code, it seems that SomeClass is not added to the persister dictionary because there isn't a class mapping (only sql-query) defined in hbm.xml. And later on in CheckNamedQueries function, it is not able to find the persistor for SomeClass.
I've checked all the obvious things (e.g. make hbm as an embedded resource) and my code isn't too much different from other samples I found on the web, but somehow I just can't get it working. Any idea how I can resolve this issue?
Well, where is your class mapping for SomeClass?
You still need to map it. Read http://nhibernate.info/doc/nh/en/index.html#querysql-load.
Look at using a class mapping with a subselect block. I found this in the Java documentation but maybe it will work for .Net too.
http://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html (scroll down to section 5.1.3)