Context Sensitive help for AbstractGraphicalEditPart(GMF) - java

the problem is simple: To add Context Sensitive Help i followed the standard steps, but once i try to link the BLOCKS to the CONTEXT IDs with SetHelp() from IWorkbenchHelpSystem. The frist argument should be either a Control(swt) or IAction.
void setHelp(Control control, String helpContextId);. How can i refer to Control from a damos.dml.Block object type ?
org.eclipselabs.damos.dml.blockTypes
FYI
I've tried and visited all the content of these sites
http://rajakannappan.blogspot.com/2009/05/context-sensitive-help-in-eclipse.html
https://help.eclipse.org/2019-03/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Fguide%2Fua_help_context.htm&cp=2_0_19_1_2
https://help.eclipse.org/luna/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fextension-points%2Forg_eclipse_ui_commands.html
The display and search methods are working correctly but I just need to set the help and not display it so that only upon calling Help (F1 or ctrl+F1) the context help is shown.
Thanks.
After trying I thought maybe this workaround would get me the same result but NADA.
private Block getBlock() {
EObject semanticElement = resolveSemanticElement();
if (semanticElement instanceof Block) {
Block block = (Block) semanticElement;
PlatformUI.getWorkbench().getHelpSystem().search(block.getType().getName());
//PlatformUI.getWorkbench().getHelpSystem().setHelp(?, Activator.HELP_VIEW); Cannot cast block directly to Control
PlatformUI.getWorkbench().getHelpSystem().displayHelp(Activator.HELP_VIEW);
return block;
} else {
return null;
}
}
#Override
protected NodeFigure createMainFigure() {
blockFigure = new BlockFigure();
// OB: java.awt.event.KeyEvent.VK_F1 is wrong, use SWT.F1
blockFigure.setFocusTraversable(true);
blockFigure.setRequestFocusEnabled(true);
blockFigure.addMouseListener(new MouseListener.Stub() {
#Override
public void mousePressed(final MouseEvent me) {
blockFigure.requestFocus();
}
});
blockFigure.addKeyListener(new KeyListener.Stub() {
#Override
public void keyReleased(KeyEvent ke) {
}
#Override
public void keyPressed(KeyEvent ke) {
if (ke.keycode == SWT.F1) {
PlatformUI.getWorkbench().getHelpSystem().search(getBlock().getType().getName());
PlatformUI.getWorkbench().getHelpSystem().displayHelp(Activator.HELP_VIEW);
}
}
});
return blockFigure;
}
Any help is appreciated!

public String getSelection() {
String blockName = "SelectBlock";
EObject element = null;
EditPart part = getDiagramEditPart();
EditPartViewer viewer = part.getViewer();
List selectedList = viewer.getSelectedEditParts();
try {
GraphicalEditPart editPart = (GraphicalEditPart) selectedList.get(0);
BlockEditPart blockPart = (BlockEditPart) editPart;
viewer.getProperty("BlockFigure");
NodeImpl node = (NodeImpl) blockPart.getModel();
element = node.getElement();
} catch (IndexOutOfBoundsException e) {
// TODO: handle exception
e.printStackTrace();
}
ISelection selection1 = viewer.getSelection();// EURêKA
if (element instanceof Block) {
Control control2 = getGraphicalViewer().getControl();
blockName = ((Block) element).getType().getName();
return blockName;
// return part.getParent().getSelected();
}
return "SelectBlock";
}
i have the possiblity to select a block type CTRL+F1 and voila the Help Definition of said Block is shown.

Related

create different InverseBindingAdapter for Short and Integer values on android:text of EditText

I create these methods for custom data binding
#BindingAdapter("android:text")
public static void setShortText(TextView view, short value) {
view.setText(String.valueOf(value));
}
#InverseBindingAdapter(attribute = "android:text")
public static Short getShortText(TextView view) {
try {
return Short.parseShort(view.getText().toString());
} catch (NumberFormatException e) {
return null;
}
}
#BindingAdapter("android:text")
public static void setIntText(TextView view, int value) {
view.setText(String.valueOf(value));
}
#InverseBindingAdapter(attribute = "android:text")
public static Integer getIntText(TextView view) {
try {
return Integer.parseInt(view.getText().toString());
} catch (NumberFormatException e) {
return null;
}
}
and bind my Short and Integer variables to views. when I build my project, the auto generated java uses the first three methods and it's have no usage from the fourth one (getIntText), and the problem is here. it use getShortText for Integer fields and cause compile time casting error.
I'm surprised, data binder properly detect it should use setIntText for set Integer values to views but it can't recognize that for inverseBinding.
how can I fix the problem?
please note that I prefer not use from below technique because I want to have control on binding when data in null
android:text="#={`` + obj.field}
I find a solution, but really not satisfy myself.
I change getIntText return value from Integer to int and problem is solved.
if someone find other better solution please let me know.
#InverseBindingAdapter(attribute = "android:text")
public static int getIntText(TextView view) {
try {
return Integer.parseInt(view.getText().toString());
} catch (NumberFormatException e) {
return -1;
}
}
For this solution to work correctly, it is necessary to make it possible to receive a null value, and check if the new value of TextView is different from the previous value.
If I contract, the Binding methods will be executed forever.
Here is my solution:
#BindingAdapter("android:text")
fun setBindShortText(view: TextView, value: Short?) {
value?.let {
if (!it.toString().contentEquals(view.text))
view.text = it.toString()
}
}
#InverseBindingAdapter(attribute = "android:text")
fun getBindShortText(view: TextView): Short? {
return try {
view.text.toString().toShort()
} catch (e: java.lang.NumberFormatException) {
null
}
}
#BindingAdapter("android:text")
fun setBindIntText(view: TextView, value: Int?) {
value?.let {
if (!it.toString().contentEquals(view.text))
view.text = it.toString()
}
}
#InverseBindingAdapter(attribute = "android:text")
fun getBindIntText(view: TextView): Int? {
return try {
view.text.toString().toInt()
} catch (e: NumberFormatException) {
null
}
}

Java - Access to a local variable from anonymous class

The problem in found = true; line
public boolean containsBlock(String nodeName, ASTNode node)
{
boolean found = false;
if(nodeName.equals("if"))
{
node.accept(new ASTVisitor()
{
public boolean visit(IfStatement s)
{
found = true;
return false;
}
});
}
return found;
}
I know that I will be able to access this if I make found a class global variable, but I don't want to do this. Maybe there is another way? I just need to let the other code know that something was found
UPD:
Is that code better and will return the right
public boolean containsBlock(Text nodeName, ASTNode node)
{
final AtomicBoolean flag = new AtomicBoolean(false);
Thread thread = new Thread(new Runnable()
{
#Override
public void run()
{
if(nodeName.matches("if"))
{
node.accept(new ASTVisitor()
{
public boolean visit(IfStatement s)
{
flag.set(true);
return false;
}
});
}
else
throw new RuntimeException("Unknown NodeName: " + nodeName);
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag.get();
}
When you pass a ASTVisitor instance to the ASTNode instance (using the accept method), it doesn't execute your visit method immediately. Therefore, there's no point to attempt to change a local variable of your containsBlock method from within the visit method.
visit will most likely be executed after your containsBlock method has returned, at which point your local found variable will no longer be in the call stack.

handle toolbar actions on eclipse plugin

i'm writing a plugin for eclipse. my problem is: i wrote a class MyEditor extends MultiPageEditorPart to edit my files, and a class MyContributor extends MultiPageEditorActionBarContributor to add actions to the toolbar.
so far i can see the buttons on the toolbar added by MyContributore.contributeToolbar() but they are always deactivated, even when i select some editparts in my editor.
i can get it working with "normal" editors (i.e. extending EditorPart), but i don't know why it doesn't work for multi page editor.
besides the usual implemented methods, here are the init and createPage overridden method I wrote in MyEditor (named XALDesignerMultiPage, in the snippet below), as required by the comment:
#Override
public void init(IEditorSite site, IEditorInput input) throws PartInitException
{
super.init(site, input);
this.setPartName(input.getName());
XALDesignerMultiPage.site = this.getSite();
//this.model = new Program();
try {
this.model = XALInput.parseXALFile((FileEditorInput)input);
} catch (Exception e) {
// ... do something
}
}
...
#Override
protected void createPages() {
try
{
for (Automaton currAut : this.model.getAutomata())
{
createGraphicalEditor(currAut);
}
}
catch (Exception e)
{
// ... do something
}
// ... other stuff
}
...
private void createGraphicalEditor(Automaton currAut)
{
try
{
IEditorPart editor = new XALDesigner(); // XALDesigner is an instance of single page editor
int index = this.getPageCount();
addPage(index, editor, new AutomatonInput(((FileEditorInput)getEditorInput()).getFile(), currAut)); // AutomatonInput wraps the single page input
String autName = AutomatonInput.defaultName;
if (currAut != null)
{
autName = currAut.getName();
}
setPageText(index, autName);
}
catch (PartInitException e)
{
// ... do something
}
catch (Throwable t)
{
// ... do something
}
}
thanks in advance

Java Sax Parser - stop processing in EndElementListener

I'm using android's SAX parser and I wish to stop processing after I have read N elements. Some of the feeds are quite large and can take a while to churn through. How can I stop parsing if certain conditions are met in the EndElementListener for a certain element? Here is my current listener
chanItem.setEndElementListener(new EndElementListener() {
public void end() {
_items.add(_item);
if (++_currentItem == _maxElements) {
//BREAK OUT HERE
}
}
});
I've tried throwing an exception within end() but EndElementListener doesn't allow for throwing any exceptions.
Guidance would be much appreciated.
Define a new unchecked exception called "SaxBreakOutException":
class SaxBreakOutException extends RuntimeException {
}
Throw this in your listener:
chanItem.setEndElementListener(new EndElementListener() {
public void end() {
_items.add(_item);
if (++_currentItem == _maxElements) {
throw new SaxBreakOutException();
}
}
});
And catch it in the code that calls XmLReader.parse():
reader.setContentHandler(handler);
try {
reader.parse(new InputSource(new StringReader(xml)));
} catch (SaxBreakOutException allDone) {
}
Alternatively, switch to Android's XmlPullParser which doesn't require exceptions to break out early.
The Answer of "Jesse Wilson" is good but in my case I had to preserve de values that returns my parse() method.
List<Article> ArticleNews = parser.parse();
So i added a boolean value;
int countArts;
boolean stopParse;
when i had to stop my parser() method, i consume all the listeners with
if(stopParse)return;
an example:
public List<Article> parse(){
final List<Article> myArticles= new ArrayList<Article>();
final RootElement root = new RootElement("articles");
Element nota = root.getChild("article");
nota.setStartElementListener(new StartElementListener(){
#Override
public void start(Attributes attributes) {
Log.i("----------------------------------------", "START Article!\n");
}
});
nota.setEndElementListener(new EndElementListener(){
public void end() {
if(countArts>9){ //only 10 articles!.
stopParse=true;
}
countArts++;
Log.i("----------------------------------------", "END Article!\n");
}
});
nota.getChild(ID).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
if(stopParse)return;
if(Utilities.isNumeric(body)) {
idA = body;
}
}
});
nota.getChild(CATEGORY).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
if(stopParse)return;
categoriaA = body;
}
});
nota.getChild(TITLE).setEndTextElementListener(new EndTextElementListener(){
public void end(String body) {
if(stopParse)return;
tituloA = body;
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8, root.getContentHandler());
} catch (Exception e) {
Log.e(" *** Exception Xml.parse() ", e.getMessag
}
return myArticles;
}

Can I run one SwingWorker within another?

I need to run two SwingWorkers. One of them can only run after the other is done. Can I run them like this?
class TestWorker {
private FirstWorker worker1;
private SecondWorker worker2;
public TestWorker() {
worker1 = new FirstWorker() {
#Override
protected void done() {
try {
result1 = get();
} catch (Exception) {
// exception handling
}
worker2 = new SecondWorker() {
#Override
protected void done() {
try {
result2 = get();
} catch (Exception) {
// exception handling
}
}
}
worker2.execute();
}
}
worker1.execute();
}
}
And how should I cancel them? Like this?
private cancel() {
if (worker2 != null) work2.cancel();
if (worker1 != null) work1.cancel();
}
Thanks a lot!
You can do it that way and it will work. However, unless there are other operations in your outer done that you're not showing, you would probably be better off with something that did both operations in doInBackground and returned an array of the results.

Categories

Resources