How can I obtain the version number of a custom Eclipse feature at runtime? - eclipse

I would like to display the version number of a custom Eclipse feature I am developing in the title bar of its perspective. Is there a way to obtain the version number from the runtime plugin and/or workbench?

Something like:
Platform.getBundle("my.feature.id").getHeaders().get("Bundle-Version");
should do the trick.
Note (from this thread) that it can not be used anywhere within the plugin itself:
this.getBundle() is not valid until AFTER super.start(BundleContext) has been called on your plugin.
So if you are using this.getBundle() within your constructor or within your start(BundleContext) before calling super.start() then it will return null.
If that fails, you have here a more complete "version":
public static String getPlatformVersion() {
String version = null;
try {
Dictionary dictionary =
org.eclipse.ui.internal.WorkbenchPlugin.getDefault().getBundle().getHeaders();
version = (String) dictionary.get("Bundle-Version"); //$NON-NLS-1$
} catch (NoClassDefFoundError e) {
version = getProductVersion();
}
return version;
}
public static String getProductVersion() {
String version = null;
try {
// this approach fails in "Rational Application Developer 6.0.1"
IProduct product = Platform.getProduct();
String aboutText = product.getProperty("aboutText"); //$NON-NLS-1$
String pattern = "Version: (.*)\n"; //$NON-NLS-1$
Pattern p = Pattern.compile(pattern);
Matcher m = p.matcher(aboutText);
boolean found = m.find();
if (found) {
version = m.group(1);
}
} catch (Exception e) {
}
return version;
}

I use the first option:
protected void fillStatusLine(IStatusLineManager statusLine) {
statusItem = new StatusLineContributionItem("LastModificationDate"); //$NON-NLS-1$
statusItem.setText("Ultima Actualizaci\u00f3n: "); //$NON-NLS-1$
statusLine.add(statusItem);
Dictionary<String, String> directory = Platform.getBundle("ar.com.cse.balanza.core").getHeaders();
String version = directory.get("Bundle-Version");
statusItem = new StatusLineContributionItem("CopyRight"); //$NON-NLS-1$
statusItem.setText(Messages.AppActionBar_18);
statusLine.add(statusItem);
}

As #zvikico says above, the accepted answer does not work for Features, only Plug-ins (OSGi Bundles, which Features are not). The way to get info about installed features is via org.eclipse.core.runtime.Platform.getBundleGroupProviders() as described here.

A version of what VonC provided to retrieve the primary Eclipse version number, but one that doesn't reference internal classes (which you should avoid doing):
Platform.getBundle(PlatformUI.PLUGIN_ID).getHeaders().get("Bundle-Version");

Related

Converting JAPE Rule to UIMA RUTA

Is there any example available to explain how the JAVA code written in RHS part of the JAPE rule can be converted in the UIMA RUTA? Also is there any way to get features of the annotations in RUTA?
Is your question if you can inject annotations (found by other systems) into RUTA before starting the RUTA analysis? So, if that's the question the answer is "yes, that's possible".
You can do something like this:
private static createCASAnnotation(Cas cas, MyOwnAnnotation myOwnAnnotation) {
Type annotationType = cas.getTypeSystem().getType(myOwnAnnotation.getType());
if (annotationType != null) {
AnnotationFS casAnnotation = cas.createAnnotation(annotationType, myOwnAnnotation.getTextStart(), myOwnAnnotation.getTextEnd());
// Also possible to add features / child annotations
for (MyOwnAnnotation childAnnotation : myOwnAnnotation.getChildAnnotations()) {
String featureFullName = casAnnotation.getType().getName() + ":" + childAnnotation.getName();
Feature feature = casAnnotation.getCAS().getTypeSystem().getFeatureByFullName(featureFullName);
if (feature != null && feature.getRange().isPrimitive()
&& "uima.cas.String".equalsIgnoreCase(feature.getRange().getName())) {
casAnnotation.setStringValue(feature, childAnnotation.getText());
// Other options for example "uima.cas.Integer" -> casAnnotation.setIntValue(...
}
// if not primitive you can also add Annotation type:
// AnnotationFS childCASAnnotation = createCASAnnotation(...
// casAnnotation.setFeatureValue(feature, childCASAnnotation);
}
cas.addFsToIndexes(casAnnotation);
} else {
log.error("invalid type .... or something better");
// Or throw exception
}
}
The MyOwnAnnotation is an object from your own domain/system and can be something like:
class MyAnnotation {
private final String value; // or text or fragment ...??
private final Long startIndex;
private final Long endIndex; // or use size/length
private final List<MyAnnotation> childAnnotations;
// constructor, builder pattern?, getters ....
}
Code examples are for demonstrating the concept.

Xamarin.Android How to Get Google Play Store app version number using Dcsoup Nuget Plugin?

I am trying to get the latest version number of my store app in order to notify user for updates if they are using an older version.
This is my code so far but its obviously just retrieving the div containing the text "Version Number". How do I get the actual version number (in this case 1.1) referring to the attached screenshot of the DOM tree?
public static string GetAndroidStoreAppVersion()
{
string androidStoreAppVersion = null;
try
{
using (var client = new HttpClient())
{
var doc = client.GetAsync("https://play.google.com/store/apps/details?id=" + AppInfo.PackageName + "&hl=en_CA").Result.Parse();
var versionElement = doc.Select("div:containsOwn(Current Version)");
androidStoreAppVersion = versionElement.Text;
}
}
catch (Exception ex)
{
// do something
Console.WriteLine(ex.Message);
}
return androidStoreAppVersion;
}
According to the parser doc,the containsOwm selector selects elements that directly contain the specified text.
As a result, your code
var versionElement = doc.Select("div:containsOwn(Current Version)");
will surely return "Current Version". The real element you would like to get is the child of the child of the sibling of "Current Version" element. So you would have to get that element using the selector.
So you can get the version number in this way:
var versionElement = doc.Select("div:containsOwn(Current Version)");
Element headElement = versionElement[0];
Elements siblingsOfHead = headElement.SiblingElements;
Element contentElement = siblingsOfHead.First;
Elements childrenOfContentElement = contentElement.Children;
Element childOfContentElement = childrenOfContentElement.First;
Elements childrenOfChildren = childOfContentElement.Children;
Element childOfChild = childrenOfChildren.First;
androidStoreAppVersion = childOfChild.Text;

how to read problems explorer view in eclipse programatically

is there any way to read the eclipse problem view programatically in eclipse plugin.
I want to fetch data from the following screen-
Yes: Ask the workbench for all Markers of type IMarker.PROBLEM. The documentation contains a code snippet for this:
IMarker[] problems = null;
int depth = IResource.DEPTH_INFINITE;
try {
problems = resource.findMarkers(IMarker.PROBLEM, true, depth);
} catch (CoreException e) {
// something went wrong
}
To get the workspace root, use ResourcesPlugin.getWorkspace().getRoot();
The file MarkerTypesModel.java contains this code:
private String getWellKnownLabel(String type) {
if (type.equals(IMarker.PROBLEM)) {
return "Problem";//$NON-NLS-1$
}
if (type.equals(IMarker.TASK)) {
return "Task";//$NON-NLS-1$
}
if (type.equals("org.eclipse.jdt.core.problem")) { //$NON-NLS-1$
return "Java Problem";//$NON-NLS-1$
}
return type;
}
As you can see, it compares the type with a fixed string to produce Java Problem (and the NON_NLS-Comments are wrong, too).

How to programmatically find a .java file in an Eclipse plugin from full classname?

Inside an Eclipse plugin, I'd like to open a file in editor.
I know the full package and class name
How can I determine the path of the .java file from this?
Take a look at IJavaProject.findType( name ) method. Once you have an IType, you can use getPath or getResource methods to locate the file. This method searches across a project and everything visible from that project.
To search the whole workspace, iterate through all the Java projects in the workspace, calling the findType method on each in turn.
You also need to know the source folder.
IProject prj = ResourcePlugin.getWorkspace().getRoot().getProject("project-name");
IFile theFile = prj.getFile(sourceFolder + packageName.replace('.','/') + className + ".java");
Generally you specify the file for an editor with an IFile. You can also ask an IFile for variants of the file's path.
I know this is a bit old but I had the same need and I had a look at how eclipse does it for stack trace elements (they have a hyperlink on them). The code is in org.eclipse.jdt.internal.debug.ui.console.JavaStackTraceHyperlink (the link is "lazy" so the editor to open is resolved only when you click on it).
What it does is it first searches for the type in the context of the launched application, then for in the whole workspace (method startSourceSearch) :
IType result = OpenTypeAction.findTypeInWorkspace(typeName, false);
And then opens the associated editor (method processSearchResult, source is the type retrieved above) :
protected void processSearchResult(Object source, String typeName, int lineNumber) {
IDebugModelPresentation presentation = JDIDebugUIPlugin.getDefault().getModelPresentation();
IEditorInput editorInput = presentation.getEditorInput(source);
if (editorInput != null) {
String editorId = presentation.getEditorId(editorInput, source);
if (editorId != null) {
try {
IEditorPart editorPart = JDIDebugUIPlugin.getActivePage().openEditor(editorInput, editorId);
if (editorPart instanceof ITextEditor && lineNumber >= 0) {
ITextEditor textEditor = (ITextEditor)editorPart;
IDocumentProvider provider = textEditor.getDocumentProvider();
provider.connect(editorInput);
IDocument document = provider.getDocument(editorInput);
try {
IRegion line = document.getLineInformation(lineNumber);
textEditor.selectAndReveal(line.getOffset(), line.getLength());
} catch (BadLocationException e) {
MessageDialog.openInformation(JDIDebugUIPlugin.getActiveWorkbenchShell(), ConsoleMessages.JavaStackTraceHyperlink_0, NLS.bind("{0}{1}{2}", new String[] {(lineNumber+1)+"", ConsoleMessages.JavaStackTraceHyperlink_1, typeName})); //$NON-NLS-2$ //$NON-NLS-1$
}
provider.disconnect(editorInput);
}
} catch (CoreException e) {
JDIDebugUIPlugin.statusDialog(e.getStatus());
}
}
}
}
Code has copyright from eclipse. Hopfully I'm allowed to reproduced it if this is mentionned.

Does exists Eclipse plugin for log files?

I have check very long logs after each start of Tomcat (from Eclipse).
Does exist a plugin or editor that I can use inside Eclipse? At least it must have colored for errors, debug and info messages.
Thanks.
I would recommend using Log4j:
http://logging.apache.org/log4j/1.2/
http://en.wikipedia.org/wiki/Log4j
It works great with eclipse and has the color scheme built in. It's highly customizable, takes some time to figure out but it's worth it.
The code to do this is rather simple, aside from syntax coloring. Just start a plugin project, add dependancies for org.eclipse.ui.console and do something like this:
public void log() {
BufferedReader br = new BufferedReader(new FileReader("path of log file"));
String line = null;
while (br.nextLine ) {
line = br.readLine();
if (line == null) {
Thread.sleep(1000);
}
else {
MessageConsole console = findConsole("tomcat log");
MessageConsoleStream stream = console.newMessageStream();
stream.println(message);
}
}
}
private MessageConsole findConsole(String name) {
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
IConsole[] existing = conMan.getConsoles();
for (IConsole element : existing)
if (name.equals(element.getName()))
return (MessageConsole) element;
// no console found, so create a new one
MessageConsole myConsole = new MessageConsole(name, null);
conMan.addConsoles(new IConsole[] { myConsole });
return myConsole;
}