I have one requirement in WICKET framework.
I would like to enable and disable link based on if else condition. Anybody can give suggestion to how to archive it?
Here is the sample code:
Link<OrderAssetAncillaryListEntry> getDesc = new Link<OrderAssetAncillaryListEntry>(
"descLink", new Model(oale)) {
#Override
public void onClick() {
final OrderAssetAncillaryListEntry oale = this.getModelObject();
String[] scrids = {oale.getScrid()};
try {
byte[] content = getReportBytes(scrids);
IResourceStream resourceStream = new ByteArrayResourceStream(
content, "application/vnd.ms-excel");
getRequestCycle().setRequestTarget(
new ResourceStreamRequestTarget(resourceStream) {
#Override
public String getFileName() {
return oale.getShowCode() + "_desc.xls";
}
});
} catch (Exception e) {
LOGGER.error("Unable to fetch Description Report file", e);
}
}
};
// add label
getDesc.add(new Label("descLinkLabel", "Description"));
return getDesc;
#isEnabled () is called many times per request. It is better to override #onConfigure () and use setEnabled () in it.
I suggest that you override Link#isEnabled() and evaluate your condition there.
You have different options to do so:
//Business logic turning someCondition true or false
final boolean someCondition = verifyBusiness();
Link<String> testLink = new Link<String>("test", Model.of("someProperty")){
#Override
public boolean isEnabled() {//Option 1
if (someCondition){
return true;
} else {
return false;
}
}
#Override
public void onClick() {
//business logic
}
};
//OR use something as below
testLink.setEnabled(someCondition ? true : false);//Option 2
private boolean verifyBusiness(){
return true; //Whatever you want to return based upon your logic
}
Related
I have the following code which is executed asynchronously. I would like to make it synchronous in order to follow some logical flow but I cannot work out how.
You will see that scanning is set to true to indicate that the method is still working, at the beginning - I then initiate a findPrinters(...) command - this contains a DiscoveryHandler which runs asynchronously - foundPrinter() is called each time an item is discovered. discoveryFinished() is when the discovery process is successfully completed, and discoveryError(...) is called whenever an error occurs.
I rely on something being set in my DiscoveryHandler before I would like to return from this method. Hence why I have while (scanning) underneath it. But this feels like a hack to me, and not the correct way of doing things. I cannot get wait() and notify() working. Can someone tell me what the correct way to do this is please?
private boolean findPrinter(final Context ctx) {
try {
scanning = true;
BluetoothDiscoverer.findPrinters(ctx, new DiscoveryHandler() {
public void foundPrinter(DiscoveredPrinter device) {
if (device instanceof DiscoveredPrinterBluetooth) {
DiscoveredPrinterBluetooth btDevice = (DiscoveredPrinterBluetooth) device;
if (btDevice.friendlyName.startsWith("XXXX")) {
try {
connection = new BluetoothConnection(btDevice.address);
connection.open();
if (connection.isConnected()) {
address = btDevice.address;
}
} catch (Exception ex) {
}
}
}
}
public void discoveryFinished() {
scanning = false;
}
public void discoveryError(String arg0) {
scanning = false;
}
});
} catch (Exception ex) {
}
while (scanning) {}
return false;
}
You could do this with CountDownLatch, which might be the lightest synchronization primitive in java.util.concurrent:
private boolean findPrinter(final Context ctx) {
final CountDownLatch latch = new CountDownLatch(1);
final boolean[] result = {false};
...
BluetoothDiscoverer.findPrinters(ctx, new DiscoveryHandler() {
...
public void discoveryFinished() {
result[0] = true;
latch.countDown();
}
public void discoveryError(String arg0) {
result[0] = false;
latch.countDown();
}
...
}
// before final return
// wait for 10 seconds for the response
latch.await(10, TimeUnit.SECONDS);
//return the result, it will return false when there is timeout
return result[0];
}
There are a bunch of ways you can do this and wait()/notify() is probably not the best since you probably want to return something from your async method. As such I suggest using something like a BlockingQueue. Here is a simplified example of how you can do this:
private boolean findPrinter(final Context ctx) {
final BlockingQueue<?> asyncResult = new SynchronousQueue<?>();
try {
BluetoothDiscoverer.findPrinters(ctx, new DiscoveryHandler() {
public void foundPrinter(DiscoveredPrinter device) {
if (device instanceof DiscoveredPrinterBluetooth) {
DiscoveredPrinterBluetooth btDevice = (DiscoveredPrinterBluetooth) device;
if (btDevice.friendlyName.startsWith("XXXX")) {
try {
connection = new BluetoothConnection(btDevice.address);
connection.open();
if (connection.isConnected()) {
address = btDevice.address;
}
} catch (Exception ex) {
}
}
}
}
public void discoveryFinished() {
asyncResult.put(true);
}
public void discoveryError(String arg0) {
asyncResult.put(arg0);
}
});
} catch (Exception ex) {
}
Object result = asyncResult.take();
if (result instanceof Boolean) {
return (Boolean) result;
} else if (result instanceof String) {
logError((String) result);
}
return false;
}
One problem with using SynchronousQueue here though is that if discoveryFinished()/discoveryError() is called more than once, then the thread executing the code asynchronously will block forever since the SynchronousQueue assumes there will be exactly one take() per every put() and will block if a put() is made without a corresponding take() or vice versa. So if in your case those methods can be called more than once you would probably use a different kind of BlockingQueue instead (see documentation).
So atm my code looks like this:
FetchUpdates() {
if (checkupdatesComplete) {
FetchUpdatedsneeded();
}else{
//wait until checkupdates == true, then re execute FetchUpdates();
}
Pretty much i'm stuck on the else statement... Anyone know the method for what i put in "//"?
This is the only way i know how to call the FetchUpdates method after checking for needed updates.
static File f = new File("C:\\Users\\Directory");
static File f1 = new File("C:\\Users\\File");
public void checkforUpdates() {
if (f1.exists) {
checkVersion();
}else{
F1Exists =false;
}
FetchUpdates();
}
Public void FetchUpdates() {
if (!F1Exists) {
try (InputStream in = URI.create("C:\\DownloadUrlHere").toURL().openStream()) {
Files.copy(in, Paths.get("C:\\DirectoryHere"));
}catch(Exception ex){}
}
while(!checkupdates){
FetchUpdatedsneeded();
if (checkupdates) {
execute FetchUpdates();
break;
}
}
Instead of checking for updates when checkupdates is set to true, just fetch updates when FetchUpdates is called.
I'm attempting to create something like a read-only console window using apache wicket.
Essentially the users submits a form to kick off a server-side operation. Then can then track the job output on the page.
I'm currently displaying the output as shown below:
public class ConsoleExample extends WebPage {
protected boolean refreshing;
private String output = "";
public void setOutput(String newOutput) {
synchronized (this) {
output = newOutput;
}
}
public void appendOutput(String added) {
synchronized (this) {
this.output = output+added;
}
}
public ConsoleExample() {
Form<ConsoleExample> form = new Form<ConsoleExample>("mainform");
add(form);
final TextArea<String> outputArea = new TextArea<String>("output",
new PropertyModel<String>(this, "output"));
outputArea.setOutputMarkupId(true);
// A timer event to add the outputArea to the target, triggering the refresh
outputArea.add(new AbstractAjaxTimerBehavior(Duration.ONE_SECOND){
private static final long serialVersionUID = 1L;
#Override
protected void onTimer(AjaxRequestTarget target) {
synchronized (this) {
if(refreshing ){
target.focusComponent(null);
target.addComponent(getComponent());
}
}
}
});
add(outputArea);
form.add(new AjaxSubmitLink("run") {
private static final long serialVersionUID = 1L;
#Override
public void onSubmit(final AjaxRequestTarget target, Form<?> form) {
setOutput("");
new Thread(new Runnable() {
#Override
public void run() {
try {
refreshing = true;
ProcessBuilder pb = new ProcessBuilder(Collections.singletonList("execute"));
pb.redirectErrorStream(true);
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(pb.start().getInputStream()));
while ((line = br.readLine()) != null) {
appendOutput("\n" + line);
}
} catch (IOException e) {
//...
} finally {
//...
refreshing = false;
}
}
}).start();
}
});
}
The problem with this solution is that each time the AjaxTimerBehaviorRuns the refresh resets the text area properties, namely, cursor position and scroll position.
Therefore as the output increases the user can't track the output as the textarea jumps back to start every second.
Is there a better way to achieve this?
A posible easy to implement approach is to add a hidden TextField that you update throw AJAX with a AjaxTimerBehavior and then calls a JavaScript function (using AjaxRequestTarget.appendJavaScript()) that synchronizes the value of the hidden TextField with your <textarea>.
Partial Solution
Following on from jordeu's suggestion of using the appendJavaScript() function I am just adding a bit of javascript to append the new text:
protected void onTimer(AjaxRequestTarget target) {
synchronized (this) {
if(refreshing ){
if(update != null){
target.appendJavascript("var obj=document.getElementById(\"output\");var txt=document.createTextNode(\""+update+"\");obj.appendChild(txt)");
update = null;
}
}
}
}
Where the update field is any new text since the last update.
This solves the scroll issue but any user selection is still reset.
Also it doesn't seem like a "nice" solution to me.
Any further suggestions of how to improve are welcome.
I'm having problem when taking a picture using VideoControl.getSnapshot() method. It always throw the exception: getSnapshot not Supported. I'm using JRE 5.0.0 with Eclipse and BlackBerry® Java® SDK 5.0 Plugin.
What I do first is to list the encoding supported by Blackberry SmartPhone selected (bold 9700) with the command System.getProperty("video.snapshot.encodings") and select one encoding from the list and pass it as the getSnapshot argument.
I've tested on several Blackberry and the same exception is thrown.
Part of the code:
mPlayer = Manager.createPlayer("capture://video?encoding=video/3gpp");
mPlayer.realize();
mPlayer = Manager.createPlayer("capture://video?encoding=video/3gpp");
mPlayer.start();
videoControl = (VideoControl)mPlayer.getControl("VideoControl");
Field cameraView = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
Thread.sleep(1000);
UiApplication.getUiApplication().pushScreen(new TempScreen(cameraView));
byte[] snapShot = videoControl.getSnapshot("encoding=jpeg&width=480&height=360&quality=superfine");
Bitmap image = Bitmap.createBitmapFromBytes(snapShot, 0, snapShot.length, 1);
UiApplication.getUiApplication().pushScreen(new TempScreen(image));
}catch (MediaException e){
UiApplication.getUiApplication().pushScreen(new TempScreen("Exception: " + e.getMessage())); }
catch (IOException e){
UiApplication.getUiApplication().pushScreen(new TempScreen("IO Exception: " + e.getMessage()));
}
catch (InterruptedException e){UiApplication.getUiApplication().pushScreen(new TempScreen("Interrupted Exception: "+ e.getMessage()));}
Not sure is my answer is actual after more than a half of year, but may be it will be useful.
You may try to use Thread.sleep(1000); before getSnapshot() call.
The problem may be related with that fact: "viewfinder must actually be visible on the screen prior to calling getSnapShot()."
So if you call getSnapshot immediately after UiApplication.getUiApplication().pushScreen(new TempScreen(cameraView));
the camera isn't prepared for the next shot.
Also are you really sure that getSnapshot() API is supported exactly on your device? Some manufacturers may not support it, despite the API defines this method. Did you run System.getProperty("video.snapshot.encodings") exactly on the same device where you test getSnapshot()?
Player _p;
VideoControl _vc ;
RecordControl _rc ;
String PATH;
FileConnection fileconn;
Object canvas= new Object();
public static boolean SdcardAvailabulity() {
String root = null;
Enumeration e = FileSystemRegistry.listRoots();
while (e.hasMoreElements()) {
root = (String) e.nextElement();
if( root.equalsIgnoreCase("sdcard/") ) {
return true;
}else if( root.equalsIgnoreCase("store/") ) {
return false;
}
}
class MySDListener implements FileSystemListener {
public void rootChanged(int state, String rootName) {
if( state == ROOT_ADDED ) {
if( rootName.equalsIgnoreCase("sdcard/") ) {
}
} else if( state == ROOT_REMOVED ) {
}
}
}
return true;
}
protected boolean invokeAction(int action){
boolean handled = super.invokeAction(action);
if(SdcardAvailabulity()){
PATH = System.getProperty("fileconn.dir.memorycard.videos")+"Video_"+System.currentTimeMillis()+".3gpp";//here "str" having the current Date and Time;
} else {
PATH = System.getProperty("fileconn.dir.videos")+"Video_"+System.currentTimeMillis()+".3gpp";
}
if(!handled){
if(action == ACTION_INVOKE){
try{
if(_p!=null)
_p.close();
}catch(Exception e){
}
}
}
return handled;
}
public MyScreen(){
setTitle("Video recording demo");
ButtonField AddPhoto = new ButtonField("push",ButtonField.FOCUSABLE | ButtonField.FIELD_HCENTER | ButtonField.FIELD_VCENTER | DrawStyle.HCENTER | ButtonField.NEVER_DIRTY | Field.USE_ALL_WIDTH);
FieldChangeListener PhotoListener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField Button = (ButtonField) field;
if (Button.getLabel().equals("push")){
}
}
};
AddPhoto.setChangeListener(PhotoListener);
add(AddPhoto);
}
}
So, I'm working on a plugin at work and I've run into a situation where I could use a ContentProposalAdapter to my benefit. Basically, a person will start typing in someone's name and then a list of names matching the current query will be returned in a type-ahead manner (a la Google). So, I created a class IContentProposalProvider which, upon calling it's getProposals() method fires off a thread which handles getting the proposals in the background. The problem I am having is that I run into a race condition, where the processing for getting the proposals via HTTP happens and I try to get the proposals before they have actually been retrieved.
Now, I'm trying not to run into an issue of Thread hell, and that isn't getting me very far anyway. So, here is what I've done so far. Does anyone have any suggestions as to what I can do?
public class ProfilesProposalProvider implements IContentProposalProvider, PropertyChangeListener {
private IContentProposal[] props;
#Override
public IContentProposal[] getProposals(String arg0, int arg1) {
Display display = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay();
RunProfilesJobThread t1 = new RunProfilesJobThread(arg0, display);
t1.run();
return props;
}
#Override
public void propertyChange(PropertyChangeEvent arg0) {
if (arg0.getSource() instanceof RunProfilesJobThread){
RunProfilesJobThread thread = (RunProfilesJobThread)arg0.getSource();
props = thread.getProps();
}
}
}
public class RunProfilesJobThread extends Thread {
private ProfileProposal[] props;
private Display display;
private String query;
public RunProfilesJobThread(String query, Display display){
this.query = query;
}
#Override
public void run() {
if (!(query.equals(""))){
GetProfilesJob job = new GetProfilesJob("profiles", query);
job.schedule();
try {
job.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
GetProfilesJobInfoThread thread = new GetProfilesJobInfoThread(job.getResults());
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
props = thread.getProps();
}
}
public ProfileProposal[] getProps(){
return props;
}
}
public class GetProfilesJobInfoThread extends Thread {
private ArrayList<String> names;
private ProfileProposal[] props;
public GetProfilesJobInfoThread(ArrayList<String> names){
this.names = names;
}
#Override
public void run() {
if (names != null){
props = new ProfileProposal[names.size()];
for (int i = 0; i < props.length - 1; i++){
ProfileProposal temp = new ProfileProposal(names.get(i), names.get(i));
props[i] = temp;
}
}
}
public ProfileProposal[] getProps(){
return props;
}
}
Ok, i'll try it...
I haven't tried to run it, but it should work more or less. At least it's a good start. If you have any questions, feel free to ask.
public class ProfilesProposalProvider implements IContentProposalProvider {
private List<IContentProposal> proposals;
private String proposalQuery;
private Thread retrievalThread;
public void setProposals( List<IContentProposal> proposals, String query ) {
synchronized( this ) {
this.proposals = proposals;
this.proposalQuery = query;
}
}
public IContentProposal[] getProposals( String contents, int position ) {
// Synchronize incoming thread and retrieval thread, so that the proposal list
// is not replaced while we're processing it.
synchronized( this ) {
/**
* Get proposals if query is longer than one char, or if the current list of proposals does with a different
* prefix than the new query, and only if the current retrieval thread is finished.
*/
if ( retrievalThread == null && contents.length() > 1 && ( proposals == null || !contents.startsWith( proposalQuery ) ) ) {
getProposals( contents );
}
/**
* Select valid proposals from retrieved list.
*/
if ( proposals != null ) {
List<IContentProposal> validProposals = new ArrayList<IContentProposal>();
for ( IContentProposal prop : proposals ) {
if(prop == null) {
continue;
}
String propVal = prop.getContent();
if ( isProposalValid( propVal, contents )) {
validProposals.add( prop );
}
}
return validProposals.toArray( new IContentProposal[ validProposals.size() ] );
}
}
return new IContentProposal[0];
}
protected void getProposals( final String query ) {
retrievalThread = new Thread() {
#Override
public void run() {
GetProfilesJob job = new GetProfilesJob("profiles", query);
job.schedule();
try {
job.join();
ArrayList<String> names = job.getResults();
if (names != null){
List<IContentProposal> props = new ArrayList<IContentProposal>();
for ( String name : names ) {
props.add( new ProfileProposal( name, name ) );
}
setProposals( props, query );
}
} catch (InterruptedException e) {
e.printStackTrace();
}
retrievalThread = null;
}
};
retrievalThread.start();
}
protected boolean isProposalValid( String proposalValue, String contents ) {
return ( proposalValue.length() >= contents.length() && proposalValue.substring(0, contents.length()).equalsIgnoreCase(contents));
}
}