handle toolbar actions on eclipse plugin - java

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

Related

Context Sensitive help for AbstractGraphicalEditPart(GMF)

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.

Null Pointeer Exception in a Custom Cordova Plugin

Context : I'm creating a custom Cordova plugin that uses a scanner native functions such as scan, print, display scanned document (the scanner have a screen on it just like a tablet).
The code by itself is working fine (the scanner is running on an Android OS), things gets tricky when i want to "convert" the code to a Cordova Plugin (i have already done that for another project and it worked perfectly).
Problem : In order to use the method that starts the scan i have to make sure that the said method in ScanService.java (= it's an interface that has an Abstract Class Stub extends android.os.Binder implements my.package.name.ScanService ) is binded and can be used via an intent like this :
Context ctx = this.cordova.getActivity().getApplicationContext();
public boolean BindServiceContext(Context ctx)
{
Intent myintent = new Intent("name.of.package.ScanService");
myintent.setPackage("name.of.package");
ctx.startService(myintent);
return ctx.bindService(myintent, ScanServConn, ctx.BIND_AUTO_CREATE);
}
The ScanServConn variable :
public ScanService mService;
public ServiceConnection ScanServConn = new ServiceConnection()
{
#Override
public void onServiceConnected(ComponentName name, IBinder service)
{
mService = ScanService.Stub.asInterface(service);
try
{
mService.initService(mScanCK);
mService.registerSystemCallBack(mSysCK);
mService.mountScannerDevice();
}
catch (RemoteException e)
{
e.printStackTrace();
}
}
#Override
public void onServiceDisconnected(ComponentName name)
{
try
{
if (mService != null)
{
mService.unmountScannerDevice();
mService.unregisterSystemCallBack(mSysCK);
mService.unregisterCallBack(mScanCK);
mService.uninitService();
}
}
catch (RemoteException e)
{
e.printStackTrace();
}
mService = null;
mSysCK = null;
mScanCK = null;
}
};
At this point, the BindingServiceContext method returns false.
private void StartScan(String strScanProfilePath) //
{
try
{
int iRet = mService.getScannerDeviceStatus();
if ( iRet == Defination.PAPER_STATUS_ON_TRAY)
{
int ret = mService.setScanProfile(strScanProfilePath);
if (ret == Defination.ERROR_SUCCESS)
{
// Start scanning.
mService.scanStart();
}
}
}
catch (RemoteException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The precise error i have when adding the plugin into a cordova project and testing it :
Attempt to invoke interface method 'int my.package.name.ScanService.getScannerDeviceStatus()' on a null object reference.
In my plugin.xml i have added this line, so in the AndroidManifest.xml of the project my service is declared :
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<service android:name="my.package.name.ScanService" />
</config-file>

Refactoring duplication with two different return types in java

Say I have these lines of code in all of my controllers:
public View ControllerClass() {
// ...
// some code in controller
SomeClass someClass;
try {
someClass = Util.getParam(
context.getEncryptedParam(), soemthignElse.getSomething());
} catch (SomeException ex) {
log.error(ex);
return viewBuilderFactory.view1.view();
} catch (AnotherException ex) {
return viewBuilderFactory.view2.view();
} catch (etc ...) {}
// use someClass
// ...
return viewBuilderFactory.view3.view();
}
In this case I'd have two different return types (void and view) if I want to move the duplication to its own method. What'd be a good approach here?
Your code is best restructured as follows:
public View ControllerClass() {
ViewBuilderFactoryView viewBuilderFactoryView;
try {
SomeClass someClass = Util.getParam(
context.getEncryptedParam(), soemthignElse.getSomething());
// use someClass
// ...
viewBuilderFactoryView = viewBuilderFactory.view3;
} catch (SomeException ex) {
log.error(ex);
viewBuilderFactoryView = viewBuilderFactory.view1;
} catch (AnotherException ex) {
viewBuilderFactoryView = viewBuilderFactory.view2;
} catch (etc ...) {}
return viewBuilderFactoryView.view();
}
In other words, if you successfully obtain a SomeClass, go ahead and use it, and afterwards return some View. If you do not successfully obtain a SomeClass, then just return some View.

My button can't open my class

Basically, the problem is I created an interface with Java Scene Builder. And from FXML button I wanted to open my class.
#FXML
public void pressButton(ActionEvent event) throws Exception {
Platform.runLater(() -> {
try{
new SerialChart().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
});
}
#FXML
public void pressButton2(ActionEvent event) throws Exception {
Platform.runLater(() -> {
try {
new Main().start(new Stage());
} catch (Exception e) {
e.printStackTrace();
}
});
}
And my Main can be open, but my SerialChart can't be opened. It says "The constructor SerialChart() is undefined". So here is the problem I think
public SerialChart(String title) {
super(title);
I think this is the problem why I can't open. Please help me... I can show you the whole code if you need.
Like Jim Garrisson said, your constructor that you call takes no arguments, but your defined one does (String title). This means you need to pass in a String argument (Even a blank one like "" will work) when you call it in the Button function.
new SerialChart("Some Title").start(new Stage()); //should be your call in the Button function.
so this is the answer I figured out SO HAPPY
public void pressButton(ActionEvent event) throws Exception {
Platform.runLater(() -> {
try{
SerialChart serialChartDemo = new SerialChart("Clean Energy Data Real time graph");
serialChartDemo.pack();
RefineryUtilities.centerFrameOnScreen(serialChartDemo);
serialChartDemo.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
});
}

J2ME media player doesn't play

public class Midlet extends MIDlet implements CommandListener{
Player p;
public void startApp() {
Display.getDisplay(this).setCurrent(new SongsList(this));
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
notifyDestroyed();
}
public void commandAction(Command cmnd, Displayable dsplbl) {
if (cmnd.getLabel().equals("Exit"))
{
destroyApp(true);
}
else
{
try {
//InputStream is = getClass().getResourceAsStream("/res/getlucky.mpeg");
//p = Manager.createPlayer(is, "audio/mpeg");
p = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
p.realize();
p.start();
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
}
}
}
this is the songslist class :
public class SongsList extends List{
public SongsList(Midlet midlet)
{
super("Songs", List.IMPLICIT);
append("get lucky", null);
addCommand(new Command("Exit", Command.EXIT, 0));
addCommand(new Command("Select", Command.OK, 0));
setCommandListener(midlet);
}
}
tried use via file stored in project (its under src/res):
inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
as well as from HTTP:
//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
Nothing works, what am I doing wrong?
EDIT:
I've tried to delete my application and just copy paste it to a new project and it worked for some reason.. now I encounter new problems:
1) I try to play a song - this is the link http://puu.sh/6n9jC.mp3
its not playing so I guess there's a limited file size for what can be played can someone tell me what is this limit ?
2) Im trying to record the audio with RecordPlayer but its always null
public AudioAnalyzer()
{
try {
thread = new Thread(this);
recordFinished = false;
//inputStream = getClass().getResourceAsStream("res/getlucky.mpg");
//audioPlayer = Manager.createPlayer(inputStream, "audio/mpg");
audioPlayer = Manager.createPlayer("http://puu.sh/35YTG.mp3");
//audioPlayer = Manager.createPlayer("http://puu.sh/6n9jC.mp3");
audioPlayer.realize();
System.out.println(System.getProperty("supports.audio.capture"));
recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
recordOutput = new ByteArrayOutputStream();
recordControl.setRecordStream(recordOutput);
recordControl.startRecord();
audioPlayer.start();
//thread.start();
} catch (MediaException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
I even tried to print if the system is supporting audio capture and the result were true but I get NullPointException at this line :
recordOutput = new ByteArrayOutputStream();
although I tried to get the recordcontrol from the player it is still null :
recordControl = (RecordControl)audioPlayer.getControl("RecordControl");
I think I read that it'll always give NullPointerException unless you run it on a real device and not an emulator is that true ? can someone verify it ? and if so what can I do if I don't own a device currently any other way to use recordcontrol feature in emulator (assuming recordcontrol isn't working on emulators).
File size is 8MB (maybe play on your phone), try to this code
public void initMedia(final String aFileUrl) {
if (m_player == null) {
try {
m_player = Manager.createPlayer(aFileUrl);
m_player.addPlayerListener(this);
m_player.realize();
m_player.prefetch();
m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");
} catch (IOException ex) {
} catch (Exception ex) {
} catch (OutOfMemoryError e) {
}
}
}
In your code, i guess you miss "m_player.prefetch()", try this. And print your Exception message...
This code in general for file, resourcce, http...
public void initMedia(final String aProtocol, final String aMediaSource) {
if (m_player == null) {
try {
if (aMediaSource.indexOf("file://") == 0) {
InputStream iRecordStream = Connector.openInputStream(aMediaSource);
m_player = Manager.createPlayer(iRecordStream, "audio/amr");
} else {
m_player = Manager.createPlayer(aProtocol);
}
m_player.addPlayerListener(this);
m_player.realize();
boolean isPrefetch = true;
try {
m_player.prefetch();
} catch (Exception ex) {
isPrefetch = false;
}
// trick to pass prefetch error
if (!isPrefetch) {
if (m_player != null) {
m_player.close();
m_player = null;
}
if (aMediaSource.indexOf("file://") == 0) {
InputStream iRecordStream = Connector.openInputStream(aMediaSource);
m_player = Manager.createPlayer(iRecordStream, "audio/amr");
} else {
m_player = Manager.createPlayer(aProtocol);
}
m_player.addPlayerListener(this);
m_player.realize();
m_player.prefetch();
}
m_volumeControl = (VolumeControl) m_player.getControl("VolumeControl");
} catch (IOException ex) {
} catch (Exception ex) {
} catch (OutOfMemoryError e) {
}
}
}
In general when it comes to J2ME development, you should always test your app on multiple real devices.
Emulators can't be trusted.
Also, J2ME is very fragmented, and various devices have various bugs and behaves differently with the same code. This will affect any app on many areas. One area being audio playback.
For example, some devices requires that you use the realize() and prefetch() methods, while other devices will crash if you use prefetch(). The only possible solution (if you wish to support as many devices as possible) is to use multiple try/catch blocks.
See this link for a detailed explanation and other tips'n'tricks on audio playback with MIDP2.0
http://indiegamemusic.com/help.php?id=1

Categories

Resources