Eclipse Rcp - Is there a recommended way to get the parent view or editor that calls an event in the handler? - event-handling

I am extending a AbstractHandler for an event that can be called from different views. What is the recommended way for the handler to find the view or editor that called the event?
HandlerUtil.getActivePart(event) is not returning the view that spawned the event because the active view changed before the event reached the handler. (This is because we have code in a non-UI thread that periodically gathers info and updates a different view which causes the different view to become active.)
This code appears to work, but is it recommended? Could that key or implementation change since it's not really in the api?
public class MyHandler extends AbstractHandler
{
public Object execute(ExecutionEvent event) throws ExecutionException
{
org.eclipse.core.expressions.IEvaluationContext context = (org.eclipse.core.expressions.IEvaluationContext) event.getApplicationContext();
String myParentContext = context.getVariable("parentContext").toString();
This returned
PartImpl (correct.view.id) Context
One idea might be to use naming conventions for the Handlers so they are not re-used across different views. However, that doesn't cover the case where a view has secondary ids. We need to know the secondary id to find the view and refresh the object after taking actions on it.

Greg answered the question in the comment above. 
The use case is:
A user takes an action on a selection in View1
It is a long running job so the handler launches a Job which updates status for that job in View2
Job updates View2 frequently using the following to get access to the view
showView("View2",null,IWorkbenchPage.VIEW_VISIBLE) 
Problem:  User takes another action on a new selection in View1, but the handler's getActivePart(event) returns View2 instead of View1
   
Greg answered the question with:
Your background thread should not be changing the active part
Solution
View1's handler now does the following
IWorkBenchPart part = HandlerUtil.getActivePart(event)    
// saved to refresh View1's selection when long running Job is done
    showView("View2",null,IWorkbenchPage.VIEW_VISIBLE)   
//so user doesn't have to manually show view themselves. It does change the active view to View2 
                                                                      
The scheduled Job now does the following
findView("View2")   
Gets access to the view in the background to make status updates
When the user takes another action on View1, the handler's getActiveParent(event) returns View1 since taking that action marked View1 as active
And findView is not corrupting the active view from the background job, so it all works now.  Thanks Gregg

Related

Auto Adjust UITextView and UITextField on appearance on keyboard [duplicate]

This question already has answers here:
How can I make a UITextField move up when the keyboard is present - on starting to edit?
(98 answers)
Closed 9 years ago.
I have a project which Im working on this time and I would like to adjust the UITextView and UITextField above the keyboard when it appears, because when the keyboard appears the TextView and TextField and hidden behind it.
Here are my screen shots,
Without Keyboard:
And with the keyboard,
This is a fairly common problem, there are all sorts of solutions to it. I put one together and made it part of my EnkiUtils package which you can download from https://github.com/pcezanne/EnkiUtils
Short version: You'll want to watch for keyboard events and call the Enki keyboardWasShown method, passing it the current view (and cell if you are in a table).
Long Version: Here's the text from my blog (http://www.notthepainter.com/expose-the-uitextfield-when-keyboard-is-shown/)
Notice that it is a class method, not an instance method. I keep a class called EnkiUtilities around to hold useful things. To call it, we first set up our observer:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
And in the keyboardWasShown method we call our utilities method:
- (void)keyboardWasShown:(NSNotification*)aNotification
{
[EnkiUtilities keyboardWasShown:aNotification view:self scrollView:myTableView activeField:activeField activeCell:activeCell];
}
So what is activeField and activeCell? Lets look at our textViewDidBeginEditing method to see how those are set
-(void)textViewDidBeginEditing:(UITextView *)sender
{
activeField = sender;
if ([sender isEqual:descriptionTextView]) {
activeCell = descriptionCell;
if (shouldClearDescription) {
[descriptionTextView initWithLPLStyle:#""];
shouldClearDescription = false;
}
}else if ([sender isEqual:hintTextView]) {
activeCell = hintCell;
if (shouldClearHint) {
[hintTextView initWithLPLStyle:#""];
shouldClearHint = false;
}
} else {
activeCell = nil;
}
}
This method is called when a UITextField begins editing. I set the activeField to the sender and then I set the activeCell to the cell that corresponds to the field.
The only bit remaining is to restore the view when the keyboard disappears.
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
myTableView.contentInset = contentInsets;
myTableView.scrollIndicatorInsets = contentInsets;
}
When you are working without a UITableView, just put the text fields into a UIScrollView and pass that, not the UITableView, to keyboardWasShown.

How to call An Animation Function from another class in ios? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have code for slide animation written in first class
-(void)send
{
right=[[ViewProfileRightPanel alloc]initWithNibName:#"ViewProfileRightPanel" bundle:Nil];
right.view.frame=CGRectMake(0, 0, 260, 548);
[self.view showOrigamiTransitionWith:right.view
NumberOfFolds:[#"1" intValue]
Duration:[#"0.58" floatValue]
Direction:currDirectionRightPanel
completion:^(BOOL finished) { NSLog(#"4");
}];
}
This code is called when I press button in first class and it will open the right panel. Animation to close the right panel is,
-(void)close
{
[self.view hideOrigamiTransitionWith:right.view
NumberOfFolds:[#"1" intValue]
Duration:[#"0.58" floatValue]
Direction:currDirectionRightPanel
completion:^(BOOL finished) {
}];
}
Now I want to perform the close animation on press of button in second class instead of pressing button from first class.
Question: How can I access the function of first class from second class?
I would suggest that you define protocol (delegate) in your firstClass which has the function (void)close and then include that protocol as a property in your second class and access the function from the second class via this property.

Fill array only once in iphone application [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am filling array in view did load method but the problem is that each time view is loaded it adds again and again same content i want that if first time view is loaded then it should add contents in array not in the second time repeatedly.
Here is my code:
for (int i=0;i<5;i++) {
Coffee*obj=[appDelegate.coffeeArray objectAtIndex:i];
NSLog(#"This is working %d",i);
[appDelegate.arrayOne addObject:obj];
}
for (int a=5;a<8;a++) {
Coffee*obj=[appDelegate.coffeeArray objectAtIndex:a];
[appDelegate.arrayTwo addObject:obj];
}
two array i am filling from another array but it repeats values if we move from one view to another and again come back
First check that here it have any value then remove that all data from array like bellow..
if ([appDelegate.arrayOne count]>0) {
[appDelegate.arrayOne removeAllObjects];
}
   if ([appDelegate.arrayTwo count]>0) {
         [appDelegate.arrayTwo removeAllObjects];
   }
for (int i=0;i<5;i++) {
Coffee*obj=[appDelegate.coffeeArray objectAtIndex:i];
NSLog(#"This is working %d",i);
[appDelegate.arrayOne addObject:obj];
}
for (int a=5;a<8;a++) {
Coffee*obj=[appDelegate.coffeeArray objectAtIndex:a];
[appDelegate.arrayTwo addObject:obj];
}
and the add in that array..
Assuming you are using NSMutableArray use removeAllObjects method avilable for Array before adding objects to it. Checkout Documentation

Remove existing annotation and add new annotation into the mapview

I am loading a mapview with a single annotation. Just like the "map app" in iphone. I have a search bar, which shows the address of the pin location, shown in map. Now I decide to change the location address. I type a new address location in the search bar. Then my mapview has to remove the existing annotation and add the new annotation. Right now, I am adding the new annotation, but I am unable to remove the existing annotation. How can I remove that existing annotation?
First, get the list of annotations on the map view, and then remove the annotations that are not the current user location (i.e. in the MKUserLocation class).
NSArray *annotations = [mapView annotations];
for (id annotation in annotations) {
if (annotation isKindOfClass:[MKUserLocation class]) {
continue;
}
[mapView removeAnnotation:annotation];
}
Then you can add your new annotation as normal.
If you're using custom annotations another way could be this:
for (int i =0; i < [mapView.annotations count]; i++) {
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyAnnotationClass class]]) {                      
         [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
       }
    }
In this way remove just the annotations you have added.

UIImagePickerController Camera Bug??? (Looks like it is "Sent to Back")

I am creating a window-based application (I am using window-based because I like how the universal apps are organized), but I am having trouble with the UIImagePickerControllerSourceTypeCamera. I want to have two views:
the parent view: a menu screen
the sub view: the screen where you can choose the photo from a gallery or where you can use the camera to take a photo
The main issue is, when I push the button to take a picture with the camera, it works and I can see the camera controls, and I can even take a picture with no problem. However, whatever objects that are in the parent view are covering the "viewfinding" screen. (i.e. if I am pointing my camera to a flower, I can see the flower on the screen, but there are buttons and imageviews from the parent view overlayed on it. I hope that makes sense).
I am adding the ImagePickerController as a modal view. You can see the code below. When I choose photo from album, it seems to be working okay, but when I try to take a picture with the camera, it is like it is sent to the back of everything.
-(IBAction)setImage:(id)sender{
  UIImagePickerController* picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
 if((UIButton *) sender == ChoosePhoto) {
     picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
 } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
 }
  [self presentModalViewController:picker animated:YES];
}
Maybe I am just missing something really obvious. Any help would greatly be appreciated. Thanks!
It sounds like you are putting all your "subviews" into the UIWindow rather than into the root view controller's view. Don't do that, add them to the root view controller's view.