I want to edit the tree viewer element based on my context menu option. Basically i need to update the element value which is displayed. If i double click on tree viewer element i was able to update the value, but through context menu also i should be able to do.
Sample code for adding the context menu:
protected def void createContextMenu(Viewer viewer) {
val MenuManager contextMenu = new MenuManager("Menu"); // $NON-NLS-1$
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(new IMenuListener() {
public override void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
val Menu menu = contextMenu.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
}
/**
* Fill dynamic context menu
*
* #param contextMenu
*/
protected def void fillContextMenu(IMenuManager contextMenu) {
contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
contextMenu.add(new Action("Rename") {
public override void run() {
val selectedElement = (treeViewer.selection as IStructuredSelection).firstElement
}
});
}
See if this helps or let me know otherwise
package myFolder;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
public class trying {
public static void main(String[] args){
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(300,300);
TreeViewer viewer = new TreeViewer(shell);
viewer.getTree().setHeaderVisible(true);
viewer.getTree().setLinesVisible(true);
viewer.setContentProvider(new ITreeContentProvider () {
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
#Override
public Object[] getChildren(Object arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public Object[] getElements(Object arg0) {
// TODO Auto-generated method stub
return new String[]{"Hoshe","Irani"};
}
#Override
public Object getParent(Object arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean hasChildren(Object arg0) {
// TODO Auto-generated method stub
return false;
}
});
viewer.setLabelProvider(new ILabelProvider(){
#Override
public void addListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public boolean isLabelProperty(Object arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void removeListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
#Override
public Image getImage(Object arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public String getText(Object arg0) {
// TODO Auto-generated method stub
return "hoshe";
}
});
viewer.setInput("Hoshe");
Menu popupMenu = new Menu(viewer.getControl());
MenuItem newItem = new MenuItem(popupMenu, SWT.CHECK);
newItem.setText("New");
MenuItem refreshItem = new MenuItem(popupMenu, SWT.CHECK);
refreshItem.setText("Refresh");
MenuItem deleteItem = new MenuItem(popupMenu, SWT.CHECK);
deleteItem.setText("Delete");
popupMenu.addMenuListener(new MenuListener() {
#Override
public void menuShown(MenuEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void menuHidden(MenuEvent arg0) {
// TODO Auto-generated method stub
display.asyncExec(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
TreeItem [] arr = viewer.getTree().getSelection();
for(MenuItem item2 : popupMenu.getItems()){
boolean bool1 = item2.getSelection();
if(bool1){
arr[0].setText(item2.getText());
item2.setSelection(false);
}
}
}
});
}
});
viewer.getTree().setMenu(popupMenu);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Related
I am using rxjava in eclipse. I cretaed the below posted example.
the issue is,, when i run the code, I receive only
onSubscribe: 0
and I dont receive any output from onNext.
please let me know why I do not receive any output from onNext, and how to fix it
code:
public static void main(String[] args) {
Observable<String> stringObservable = Observable.just("Hello from just Operator1");
stringObservable
.subscribeOn(Schedulers.newThread())
.observeOn(Schedulers.io())
.subscribe(new Observer<Object>() {
#Override
public void onComplete() {
// TODO Auto-generated method stub
System.out.println("onComplete: OK");
}
#Override
public void onError(Throwable arg0) {
// TODO Auto-generated method stub
}
#Override
public void onNext(Object arg0) {
// TODO Auto-generated method stub
System.out.println("onNext: " + (String) arg0);
}
#Override
public void onSubscribe(Disposable arg0) {
// TODO Auto-generated method stub
System.out.println("onSubscribe: " + arg0);
}
});
}
I'm using a composite cell in a grid which has two buttons.I want to handle the click events on both the buttons seperately.Is it possible?
please explain in detail how to handle these events seperately.
I'm attaching my composite cell code with the buttons for the reference.
private CompositeCell<FilterInfo> createCompositeCell(){
HasCell<FilterInfo, String> button1= new HasCell<FilterInfo, String>() {
public Cell<String> getCell() {
return new TextCell();
}
public FieldUpdater<FilterInfo, String> getFieldUpdater() {
// TODO Auto-generated method stub
return null;
}
public String getValue(FilterInfo object) {
return "button 1";
}};
HasCell<FilterInfo, String> button2= new HasCell<FilterInfo,String>(){
public Cell<String> getCell() {
return new ButtonCell();
}
public FieldUpdater<FilterInfo, String> getFieldUpdater() {
// TODO Auto-generated method stub
return null;
}
public String getValue(FilterInfo object) {
// TODO Auto-generated method stub
return "button 2";
}
};
List<HasCell<FilterInfo, ?>> cells = new ArrayList<HasCell<FilterInfo, ?>>();
cells.add(button1);
cells.add(button2);
CompositeCell<FilterInfo> compositeCell = new CompositeCell<FilterInfo>(cells);
return compositeCell;
You handle click event in FieldUpdater of each button.
Here is minimal working example:
package com.adam.test.client;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.CompositeCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;
public class Test implements EntryPoint {
#Override
public void onModuleLoad() {
CompositeCell<FilterInfo> compositeCell = createCompositeCell();
CellList<FilterInfo> list = new CellList<FilterInfo>(compositeCell);
List<FilterInfo> values = new ArrayList<FilterInfo>();
values.add(new FilterInfo());
values.add(new FilterInfo());
values.add(new FilterInfo());
list.setRowData(values);
RootPanel.get().add(list);
}
private CompositeCell<FilterInfo> createCompositeCell() {
Column<FilterInfo, String> button1Column = new Column<FilterInfo, String>(new ButtonCell()) {
#Override
public String getValue(FilterInfo object) {
return "Button 1";
}
};
button1Column.setFieldUpdater(new FieldUpdater<FilterInfo, String>() {
#Override
public void update(int index, FilterInfo object, String value) {
Window.alert("Button 1 clicked!");
}
});
Column<FilterInfo, String> button2Column = new Column<FilterInfo, String>(new ButtonCell()) {
#Override
public String getValue(FilterInfo object) {
return "Button 2";
}
};
button2Column.setFieldUpdater(new FieldUpdater<FilterInfo, String>() {
#Override
public void update(int index, FilterInfo object, String value) {
Window.alert("Button 2 clicked!");
}
});
ArrayList<HasCell<FilterInfo, ?>> compositeColumns = new ArrayList<HasCell<FilterInfo, ?>>();
compositeColumns.add(button1Column);
compositeColumns.add(button2Column);
return new CompositeCell<FilterInfo>(compositeColumns);
}
private class FilterInfo {}
}
I would suggest you to create the custom cell and override the onBrowserEvent and render methods. Handle the click event in the onBrowserEvent method.
Refer http://www.gwtproject.org/doc/latest/DevGuideUiCustomCells.html
I want to call MainActivity class to another activity. This is my code for the MainActivity.java:
package com.blinkedup.geolocationchat;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
LocationManager locationManager;
MyLocationListener locationListener = new MyLocationListener();
Criteria criteria;
String bestProvider;
String listOfBestProviders;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
criteria = new Criteria();
textView = (TextView) findViewById(R.id.textView1);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
bestProvider = locationManager.getBestProvider(criteria, true);
Toast.makeText(getApplicationContext(), bestProvider, 3).show();
}
protected void onPause(){
super.onPause();
locationManager.removeUpdates(locationListener);
}
private class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
textView.setText("Latitude: " + location.getLatitude() +
"Longitude: " + location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
In another activity I wanted to call the lat and long of the above code but don't want to rewrite the code to the activity. I just want to call it and display the result in another activity. please help. thanks
make your variable static and define it before onCreate() method use that variable in another activity by call like this.
YourMainActivity.yourstaticvariable
I want to add a button and add click handler to it after entering the values in database.
I want that button to be on onSucess of greeting service.
help me
public static void edit1(String fnme,String lnme,String clgn,String scn){
greetingService.enter(fnme,lnme,clgn,scn, new AsyncCallback<String>()
{
public void onSuccess(String result)
{
Window.alert("successfully entered");
// TODO Auto-generated method stub
Button bt =new Button("submit");
RootPanel.get().add(bt);
bt.addClickHandler(new MyClickHandler);
}
public void onFailure(Throwable caught)
{
Window.alert("fail");
}
});
}
class MyClickHandler implements ClickHandler
{
public void onClick(ClickEvent e)
{
//create();
}
}
but this is not working.
Do you need the click handler?
Anyway this is what I think you're trying to do:
Button bt =new Button("submit");
RootPanel.get().add(bt);
bt.addClickHandler(new MyClickHandler);
public static void edit1(String fnme,String lnme,String clgn,String scn)
{
greetingService.enter(fnme,lnme,clgn,scn, new AsyncCallback<String>()
{
public void onSuccess(String result)
{
Window.alert("successfully entered");
// TODO Auto-generated method stub
create();
}
public void onFailure(Throwable caught)
{
Window.alert("fail");
}
});
}
class MyClickHandler implements ClickHandler
{
public void onClick(ClickEvent e)
{
// create();
}
}
Your MyClickHandler does nothing inside the onClick() method. It should work as long as you put code. Try this:
public static void edit1(String fnme,String lnme,String clgn,String scn){
greetingService.enter(fnme,lnme,clgn,scn, new AsyncCallback<String>() {
public void onSuccess(String result) {
Window.alert("successfully entered");
// TODO Auto-generated method stub
Button bt =new Button("submit");
RootPanel.get().add(bt);
bt.addClickHandler(new MyClickHandler() {
public void onClick(ClickEvent e) {
//DO SOMETHING HERE
}
});
}
public void onFailure(Throwable caught) {
Window.alert("fail");
}
});
}
class MyClickHandler implements ClickHandler {
public void onClick(ClickEvent e) {
/* OR DO SOMETHING HERE, BUT THAT WILL AFFECT ALL
* INSTANCES OF MyClickHandler
*/
}
}
I'm pretty new to programming but here is a code I have. I'm trying to develop an app and I want it to open the camera feature right away. This is what I'm working with. It's asking me to add more details so I figure I should type more... Any help would be appreciated. This is my only grade for a class that is taught very poorly
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder sHolder;
public Camera camera= null;{
}
public CameraSurfaceView(Context context) {
super(context);
sHolder= getHolder();
sHolder .addCallback(this);
// TODO Auto-generated constructor stub
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
Camera.Parameters params= camera.getParameters();
params.setPreviewSize(width, height);
camera.setParameters(params);
camera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try{
camera.setPreviewDisplay(sHolder);
}catch(Exception e){
}
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera= null;
}
public void capture (Camera.PictureCallback jpegHandler){
camera.takePicture(null, null, jpegHandler);
}
}