Eclipse Editor: automatically switch context when parts get active/inactive - eclipse-plugin

I am developing my own eclipse editor and need to switch between different contexts for key binding. Currently I am doing the context activation/deactivation manually upon part activation.
This page
https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fworkbench_advext_contexts.htm says:
If you are activating a more specific Context within your part (either
View or Editor) you can use the part site service locator to active
your Context. The part's IContextService will take care of activating
and deactivating the Context as your part is activated or deactivated.
It will also dispose the Context when the part is disposed.
It seems like that is just what I want. But the page did not say how. Can anyone give me a hint what a 'part site service locator' mentioned in the text is and how to use it?

I would interpret the text so that you should use the service locator of the site that corresponds to your (editor) part. In the following example, part references your editor. By obtaining the context service from the part's site, you get a child context service for that particular part in wich you can activate a specialized editor context.
IContextService contextService = part.getSite().getService( IContextService.class );
contextService.activateContext( "your.editor.context.id" );

After digging through Eclipse code, here's my answer to my own question.
First thing first, it is JUST enough to invoke
IContextService contextService = part.getSite().getService( IContextService.class );
contextService.activateContext( "your.editor.context.id" );
anywhere after init(where you get PartSite), just as #RĂ¼diger Herrmann mentioned in his answer.
AND(here's my finding) NOTHING ELSE need to be done.
Eclipse will automatically activate/deactivate the context when part is activated /deactivated, just as described in the text I refer to. In addition, when the part site is disposed, all context will be disposed.
If you are interested in how, here's more digging.
Activate/Deactivate
When we invoke getSite().getService(IContextService.class), what we get is an instance of SlaveContextService.
When we call activateContext(String contextId) on it, our request will be automatically translate to a request with a default expression ActivePartExpression.
From it's name we can easily guess that this expression will check whether a part is active and do some changes. The changes it does can be seen at ContextService.UpdateExpression.changed. Here's the code(ContextService:124-128)
if (result != EvaluationResult.FALSE) {
runExternalCode(() -> contextService.activateContext(contextId));
} else if (cached != null) {
runExternalCode(() -> contextService.deactivateContext(contextId));
}
Whenever Eclipse context changes(activate/deactivate part will trigger context change), UpdateExpression.changed will be invoked and check whether the target part is still active, then activate/deactivate the context accordingly.
Dispose
In SlaveContextService.dispose, all context registered through it will be disposed upon the service's dispose.

Related

Controlling level and focus of windows other apps with CGPrivate functions

Question
How to use these private functions on other windows? It would be nice to have this knowledge back in the wild. I am specifically trying to get CGSOrderWindow and CGSSetWindowLevel to work.
I was trying in the direction of:
temporarily register as the dock and then register the dock as the dock again immediately afterwards
or
code injection into the Dock process per this comment:
Also, the author of the above project seems determined to make all core functionality available as a framework. It seems to be implemented as code injection into the Dock process.
Reason I know this is possible
I have been doing work on trying to setLevel on window of another app, and focus window of another app if focused. I am posting this again with the info I learned because from my searching online, I know this was done in the past, its just the knowledge is not publicly out there anymore. The sourceforge pages are no longer there. So I was wondering if you could help me make this information public again.
This is the topic I read that gave me this information - http://cocoadev.com/HowtoControlOtherAppsWindows
Here you see comments like:
You cannot control an another app's windows from a user-level process, unfortunately.
SlavaKarpenko
You can, Slava, you just need to register as the Dock. It might be possible to temporarily register as the dock and then register the dock as the dock again immediately afterwards, not sure. I think the call you'd be wanting to investigate as CoreDockRegisterDockOwner in HIServices.framework.
FinlayDobbie
You could also use APE or similar to do control the windows, or (as mentioned above) register as the Dock (look for the private APIs with Universal Connection in their name). Has anyone found a polite way of getting the Dock to give up its universal connection? The only way I can find is to force quit the Dock and grab the universal connection when it's not looking (which prevents the dock reloading).
SamTaylor
There's an open source project up on sourceforge.net that looks much more like the window managers I've used on Unix boxes than Space.app (or Space.dock): http://wsmanager.sourceforge.net/
SteveCook
Verifying things work
This is what I learned, from the sources at bottom of this post, we see all these functions work with CGWindowIds, so how do I get that, this is how:
Get all windows with CGWindowListCopyWindowInfo. Then access each element from that array with CFArrayGetValueAtIndex and then get the CGWindowId with objectForKey:, kCGWindowNumber, and then integerValue.
Now if I try to focus or set level of a window that is OWNED by the app running the code, it works fantastic. For instance:
MY_TARGET_CGWINDOW_ID = 179;
rez_CGError = CGSOrderWindow(_CGSDefaultConnection, MY_TARGET_CGWINDOW_ID, kCGSOrderAbove, 0);
Will focus it, rez_CGError is 0. Even if the window is minimized, it is unminimized, without animation, and shown.
Now however, if I try this on a window of a different app we get some errors:
MY_TARGET_CGWINDOW_ID_of_other_app = 40;
rez_CGError = CGSOrderWindow(_CGSDefaultConnection, MY_TARGET_CGWINDOW_ID_of_other_app, kCGSOrderAbove, 0);
This fails and rez_CGError is 1000, which I suspect means "cid (CGSConnection) used does not have permission to modify target window". The same happens if I first do [app activateWithOptions: (NSApplicationActivateIgnoringOtherApps | NSApplicationActivateAllWindows)] before making the call above.
So I first get the cid of that owning window like this:
var rez_CGError = CGSGetWindowOwner(_CGSDefaultConnection, MY_TARGET_CGWINDOW_ID_of_other_app, &ownerCid);
This works good and I get ownerCid is set to a value. Then I do the focus command with this new connection:
rez_CGError = CGSOrderWindow(ownerCid, MY_TARGET_CGWINDOW_ID_of_other_app, kCGSOrderAbove, 0);
However this gives rez_CGError of 268435459, which I suspect means "current app does not have permission to use this ConnectionId (cid)". (Same happens if I call activateWithOptions first.
My Sources for the Private Functions
Here is the sources for some private functions I found - https://code.google.com/p/undocumented-goodness/source/browse/trunk/CoreGraphics/CGSPrivate.h
This one source here contains a function that is not in the above link - CGSGetConnectionIDForPSN - i test it and it exists - from - https://github.com/mnutt/libqxt/blob/767498816dfa1742a6f3aee787281745afec11b8/src/gui/qxtwindowsystem_mac.h#L80

Identifying objects in IBM RFT

While executing my script in RFT, my script got failed due to the slight position change of a button. (This button's position slightly changes according to the option selected for previous combo box due to the label appearing near the button)
As there are 2 positions for this button in window, one of my script fails while other passes.
Please suggest how to identify this same object in 2 different places in RFT?
If you're alright with not using pre-mapped values and instead work with objects directly in code (which I've personally found to be extremely useful... it's allowed me to do great and wondrous things with RFT :), the following ought to work fine:
private void clickObject(String uniqueIdentifier) {
// Find object
RootTestObject root = RootTestObject.getRootTestObject();
TestObject[] matchingObjs = root.find(atProperty(".id", uniqueIdentifier));
if (matchingObjs.length > 0) {
// Click the object
((GuiTestObject) matchingObjs[0]).click();
}
// Clean-up
unregister(matchingObjs);
}
Feel free to replace ".id" with whatever property is best suited for the situation... since I work primarily with a web application, the ".id" property has worked splendidly for me.
Because the method finds the object anew each time, it'll grab the object's position wherever it's at at the time the method's called. The clean-up will also prevent any weird, horrible, and otherwise unfortunate UnregisteredObjectExceptions from cropping up.
Without looking at your pages I cannot be sure, but I think the buttons are actually two different buttons. Maybe they are generated by javascript, or they are just un-hidden after the option you select in the combobox.
If they are two different buttons (record them both and look at the recognition properties) you can either replace some properties with a regular expression or check wich button is visible/exists and then click it:
if (btn_button1.exists()) {
btn_button1.click();
} else if (btn_button2.exists()) {
btn_button1.click();
}
Here's a more complete tutorial on Object Recognition.
You can increase the tolerance of Rational Performance Tester AssureScript in the properties tab or you could set the description but hide the value. You can also make a custom code that updates the object map to prepare for this change in a java IF structure

Suppress messages from embedded Windows Media PLayer

I'm working on a Windows Form Application in which I have embedded a Windows Media Player that lets you play video files. The unique thing is that I have changed the extension of the media files (for reasons which I can't get into here). For instance, "xyz.wmv" might be called "xyz.ext". They play just fine, but before they play, I get the message:
"The file you are attempting to play has an extension that does not match the file format. Playing the file may result in unexpected behaviour. Do you want the Player to try to play the file?"
You can click yes and you can even check the box to not show that message again, but I don't want that for all the obvious reasons including the fact that it confuses users. I have looked into the .settings properties but I cannot find a way to suppress this message and more importantly other messages that might come up.
The .ext (for example) extension is not known to media player, hence the warning.
What you can do to change this is modify the registry and register this extension. This is described officially here: File Name Extension Registry Settings
The most simple way to do it is to create a registry key like this:
HKEY_CURRENT_USER\Software\Microsoft\MediaPlayer\Player\Extensions\.ext
And add two key values:
Runtime (DWORD): 6
Permissions (DWORD): 15 (or 0xF in hexa)
This is shown here:
NOTE: this answer was originally in response to a bounty question and edit which was removed on how to do this via code. Parts of this are still relevant to the original question.
You can do this pretty straight forward IF you have admin rights as you need to edit the registry. Not sure how far you will get without admin rights and can test later, but here is how to do this via code (in a real implementation I would do this as part of a setup - or check if the keys exist each time which seems wasteful):
You need to add one key (showing two here for registering the extensions, you may need additional keys for auto-play or a setting on the player):
private void Form1_Load(object sender, EventArgs e) {
/*This first key is not necessary - and if you will be using common
* extensions like mp4, skip this step altogether!!
*/
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true)
.OpenSubKey("Classes", true);
key.CreateSubKey(".myExt");
key = key.OpenSubKey(".myExt", true);
key.SetValue("", "WMP11.AssocFile.myExt");
key.SetValue("Content Type", "video/x-ms-wmv");
key.SetValue("PerceivedType", "video");
/*Here is the magic key which will make the dialog go away*/
key = Registry.CurrentUser.OpenSubKey("Software", true)
.OpenSubKey("Microsoft", true)
.OpenSubKey("MediaPlayer", true)
.OpenSubKey("Player", true)
.OpenSubKey("Extensions", true);
key.CreateSubKey(".myExt");
key = key.OpenSubKey(".myExt", true);
key.SetValue("", "");
key.SetValue("Permissions", 0x20);
axWindowsMediaPlayer1.URL = #"C:\Users\Public\Documents\Wildlife.myExt";
}
Media Player creates other keys when you add through its dialog, but the only one definitely needed is: HKEY_Current_User.Software.Microsoft.MediaPlayer.Player.Extensions
If you want to see all the keys Media Player adds,
choose a crazy extension,
click always allow when prompted and then
search the registry for all the keys that get created.
The above code is tested and working for me - confirming the dialog before adding the keys and the lack of any dialog after.
This is a good generic process for programmatically adding file associations and default programs to the Windows Registry from .NET. You have to be careful about registering the extension (the first key I set above) IF the extension already exists (TEST FOR THIS). Otherwise the above code will happily overwrite your current values. All you really should need is the one added to: HKEY_Current_User.Software.Microsoft.MediaPlayer.Player.Extensions anyway. Think it through, check in advance, and test before you go crazy in the registry!!
It is also always a great idea to backup your registry before playing with it.
Final note: missed your question on how to reproduce once you have clicked always allow: just remove the entry in HKEY_Current_User.Software.Microsoft.MediaPlayer.Player.Extensions and voila!
This answer assumes you have a working knowledge of regedit.
Final note #2: Response geared to the questions in the Bounty. Other errors can be suppressed by setting telling Media Player to allow you to handle error events and then writing your custom handler. I have not done this before so cannot comment on the ease and what can/cannot be controlled through this method.
The Windows Media Player control does not raise an exception when it encounters an error such as an invalid URL. Instead, it signals an event. Your application should handle error events sent by the Player.
These can then be handled by creating / registering a MediaError event:
private void axWindowsMediaPlayer1_MediaError(object sender, AxWMPLib._WMPOCXEvents_MediaErrorEvent e) {
// Handle errors and profit!
}
Then set this as the handler in the Events property window for your control - same for other events such as ErrorEvent.

Eclipse call ViewPart saveState on View close

I have a Eclipse plugin that uses a view which extends ViewPart. ViewPart has a saveState method which requires an IMemento.
I added my code to saveState and the corresponding init method and it works. Unfortunately, saveState is only called if the entire workspace is shutting down. My view is not of such great importance that I can expect it to be opened the entire time. Hence, it would be cool if saveState would be called on view closure.
I found a view-part listener as mean to react on view closure, but what I do not get is where the IMemento comes from. Where do I get the memento object that is used on workspace closure? Or where do I have to store my own memento object to make the view part use it in the init method if the view is (re)opened?
#Override
public void saveState(IMemento memento) {
super.saveState(memento);
memento = memento.createChild(MEMENTO_GUI_STATE);
memento.putBoolean(MEMENTO_IS_FLAT, !isHierarchicalModeActive());
memento.putBoolean(MEMENTO_IS_CATEGORY_MODE_ACTIVE, comboViewer.isVisible());
}
This is my saveState - can I tell my view somehow tell to call it every time the view closes?
Take a look at this question in the Eclipse FAQ:
Storing view state is done in two commons ways, depending on whether
you want to store settings between workbench sessions or across
invocations of your view. The first of these facilities is found
directly on IViewPart. When the workbench is shut down, the method
saveState is called on all open views.
Another mechanism for persisting view state is the JFace
IDialogSettings facility. The advantage of dialog settings over the
view save/init mechanism is that you can control when settings are
persisted. The saveState method is called only if your view is open
when the workbench shuts down, so it is not useful for storing view
state when the view is closed by the user. Dialog settings, on the
other hand, can be changed and persisted whenever you want.
Go to this other question or to the Eclipse documentation itself for the settings mechanism.
Well this could be "a bit" ugly but nothing else came to my mind: store memento variable as a field variable, initialize it in your init(IViewSite site, IMemento memento) method, override dispose() and call saveState(IMemento memento) explicitely.
You can read and write your own XMLMemento from your org.eclipse.core.runtime.Plugin.getStateLocation() at any time you want. As #BelaViser mentioned, you could write your file in your IViewPart#dispose() method and read it in your view constructor.

Sharepoint 2010 WebPartZone

Is it possible to control the output of a webpartzone in sharepoint.
We would like not to have tables.
What are you doing to handle this ?
We haven't done that. But we did similar stuff to override behavior of other SP controls.
First of all you need to learn the conception of ASP .NET Control Adapter.
The main idea is that it's possible to override behavior of any control. So, in this case you can override Web Part Zone Render method and put your logic there.
The only issue is that you need to register your control adapter for specific browser and use .browser files for that. To workaround that create your own HttpModule and handle BeginRequestEvent. There you can register your adapter for all browsers, e.g.
var browser = m_Application.Context.Request.Browser;
if (browser == null)return;
if (!browser.Adapters.Contains(webPartZoneTypeName)){
browser.Adapter[webPartZoneTypeName] = strongNameOfYourAdapter;
}
Hope this will help.