drop widget beyond the AbsolutePanel gwt dnd - gwt

I'm trying to drop my draggable widgets out of the boundary panel (AbsolutePanel). In my case draggable widgets is an image. And I want to drop it, so that there will be visible only a part of the image, but when I drop it, and some parts of image beyond absolute panel, it drop automatically within absolute panel.
I tried :
dragController.setBehaviorConstrainedToBoundaryPanel(false);
and thought it means that I can drop it where ever I want, but it doesn't work.

And the working solution :)
Here is my code:
public class myEntripointClass implement EntryPOint{
AbsolutePanel droper;
public void onModuleLoad() {
Panel main = new AbsolutePanel();
droper = new AbsolutePanel();
droper.setHeight("300px");
droper.setWidth("500px");
main.add(droper);
content=new AbsolutePanel();
bt = new Button("Drag and drop it");
content.add(bt);
lb = new Label("Label drag and drop");
content.add(lb);
main.add(content);
manageDnD();
RootPanel.get().add(main);
}
private void manageDnD() {
PickupDragController dragController = new PickupDragController(
(AbsolutePanel) content, true);
dragController.makeDraggable(bt);
dragController.makeDraggable(lb);
dragController.addDragHandler(new DragHandler() {
#Override
public void onPreviewDragStart(DragStartEvent event)
throws VetoDragException {}
#Override
public void onPreviewDragEnd(DragEndEvent event) throws VetoDragException {}
#Override
public void onDragStart(DragStartEvent event) {
}
#Override
public void onDragEnd(DragEndEvent event) {
// TODO Auto-generated method stub
DragContext context = event.getContext();
int x=context.mouseX;
int y= context.mouseY;
droper.add(context.selectedWidgets.get(0),x,y);
}
});
NameDropController dropController = new NameDropController(droper);
dragController.registerDropController(dropController);
dragController.setBehaviorDragProxy(true);
}
and my DropController class is:
public class NameDropController extends AbstractDropController{
public NameDropController(Widget dropTarget) {
super(dropTarget);
}
#Override
public void onDrop(DragContext context) {
int x=getDiff(getDropTarget().getAbsoluteLeft(), context.mouseX);
int y=getDiff(getDropTarget().getAbsoluteTop(), context.mouseY);
((AbsolutePanel)getDropTarget()).add(context.selectedWidgets.get(0),x,y);
System.out.print("("+context.mouseX+"::,::"+context.mouseY+")");
}
#Override
public void onMove(DragContext context){
}
private int getDiff(int val1,int val2){
return Math.abs(val1-val2);
}
}

Related

Delete listview items in custom adapter

I'm sorry for this question, but I'm a new in Android developing. I searched in internet, but I don't found any appropriate answer. I have a following code in my custom adapter:
public class ItemsAdapter extends CursorAdapter {
public ItemsAdapter(Context context, Cursor c) {
super(context, c, false);
}
#Override
public void bindView(View view, Context arg1, Cursor cursor) {
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.title.setText(cursor.getString(cursor
.getColumnIndex("title")));
viewHolder.publishDate.setText(cursor.getString(cursor.getColumnIndex("date_time")));
viewHolder.rssNewsImage.setImageResource(R.drawable.rssnews);
}
#Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
View view = LayoutInflater.from(mContext).inflate(
R.layout.listview_rssreaderactivity_row, arg2, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) view
.findViewById(R.id.tw_title_listview_row_main);
viewHolder.publishDate = (TextView) view.findViewById(R.id.tw_pubDate_listview_row_main);
viewHolder.rssNewsImage = (ImageView) view.findViewById(R.id.imageRssView);
view.setTag(viewHolder);
return view;
}
public class ViewHolder {
TextView title;
TextView publishDate;
ImageView rssNewsImage;
}
}
I want to delete listView item in my custom adapter. Here is my code for click events:
#Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
AlertDialog.Builder itemLongClickdialog = new AlertDialog.Builder(
RssNewsActivity.this);
itemLongClickdialog.setItems(R.array.array_longclick_item,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
I can't delete given item.
How to resolve this problem? I will appreciate all answers.
Try This:
DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
ItemsAdapter dba = new ItemsAdapter (yourActivity);
dba.open();
dba.remove(_id);
Log.i("TAAG", "removed: "+_id);
dba.close();
cursor.requery();
listview.getadaper().notifyDataSetChanged();
}
});

Clear SuggestBox on blur in GWT

I have a SuggestionBox in GWT. Is there a way to clear it when it blurs (unless the user made a selection, in which case an action should happen)?
Add a BlurHandler:
suggestionBox.getValueBox().addBlurHandler(new BlurHandler() {
#Override
public void onBlur(BlurEvent event) {
// your code goes here
}
});
Try this one using ValueChangeHandler:
Note: ValueChange event has same behavior as Blue event but it is fired only if value is changed in SuggestBox.
class MyMultiWordSuggestOracle extends MultiWordSuggestOracle {
private Set<String> values = new HashSet<String>();
#Override
public void add(String value) {
super.add(value);
values.add(value);
}
#Override
public void clear(){
super.clear();
values.clear();
}
public boolean contains(String value) {
return values.contains(value);
}
}
You code:
final MyMultiWordSuggestOracle oracle = new MyMultiWordSuggestOracle();
oracle.add("A");
oracle.add("AB");
oracle.add("BCD");
oracle.add("BCDE");
final SuggestBox suggestionBox = new SuggestBox(oracle);
suggestionBox.addValueChangeHandler(new ValueChangeHandler<String>() {
#Override
public void onValueChange(ValueChangeEvent<String> event) {
if (!oracle.contains(event.getValue())) {
suggestionBox.setValue("");
}
}
});

how to delete item from listview and refresh that

I need some help in my homework here...
I want to do a listview with itens from editText.. but I dont know how to delete and edit the item, I tryed to use ContextMenu. I will post my code, and if you think that i have to change anything, tell me.. I'm begginer.
private Button bt;
private ListView lv;
private EditText et;
private ArrayList<String> Arraylt = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
bt = (Button)findViewById(R.id.bt);
lv = (ListView)findViewById(R.id.lv);
et = (EditText)findViewById(R.id.et);
lv.setOnCreateContextMenuListener(this);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view,
int posicao, long id) {
Object o = lv.getItemAtPosition(posicao);
et.setText(String.valueOf(o));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addadicionar();
}
});
}
public void addadicionar() {
Arraylt.add(et.getText().toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arraylt));
et.setText("");
}
hey this is my note pad code it will help you .. have a look
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
ArrayAdapter <String> adapter=new ArrayAdapter<String>(menu.this,android.R.layout.simple_list_item_1,data);
Cursor cursor2=mydb1.rawQuery("SELECT * FROM notes;", null);
data.remove(info);
lv.setAdapter(adapter);
cursor2.moveToPosition(info.position);
int id= cursor2.getInt(cursor2.getColumnIndex("id"));
mydb1.delete("notes", "id=?", new String[] {Integer.toString(id)});
list(view); // this will refresh my list by calling my display list method here
return true;
default:
return super.onContextItemSelected(item);
}
try to remove the item from listItems(List), then call adapter.notifyDataSetChanged();
try this code :
// note : you need to create xml file in res/menu folder which conatain deleter and editer item
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.listView1)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
}
}
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.Deletar:
// edit stuff here
return true;
case R.id.editar:
ArrayAdapter <String> adapter=new ArrayAdapter<String>(menu.this,android.R.layout.simple_list_item_1,data);
data.remove(info);
lv.setAdapter(adapter);
list(view); // this will refresh my list by calling my display //list method here
return true;
default:
return super.onContextItemSelected(item);
}
Yes, I win!.. now, I can edit and delete the item from listview...
private Button bt;
private ListView lv;
private EditText et;
private ArrayList<String> Arraylt = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_l);
bt = (Button)findViewById(R.id.bt);
lv = (ListView)findViewById(R.id.lv);
et = (EditText)findViewById(R.id.et);
lv.setOnCreateContextMenuListener(this);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> adapter, View view, int posicao, long id) {
Object o = lv.getItemAtPosition(posicao);
et.setText(String.valueOf(o));
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addadicionar();
}
});
}
public void addadicionar() {
Arraylt.add(et.getText().toString());
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Arraylt));
et.setText("");
}
sorry for the bad indent..

Refresh Listview in android

I"m tring it refresh my listview when my sqllite database is change (when delete or update query).
the querys itself works just fine, but it doesn't update the listview layout, only when i"m exiting the acitvity and renter it the liseview is changing.
I tried the methodes:
notifyDataSetChanged()
requery()
the code of the activiy is:
public class ShowListActivity extends ListActivity {
private ItemsDataSource itemsDataSource;
private String source[] = new String[] {MySQLiteHelper.KEY_NAME, MySQLiteHelper.KEY_QUANTITY, MySQLiteHelper.KEY_CHECKED};
private int dest[] = new int[] {R.id.itemTitle, R.id.itemQuantity, R.id.itemCheck};
public void goBackMethod(View view) {
Intent intent = new Intent(this, MainScreen.class);
startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_show_list);
} catch (Exception e) {
e.getMessage();
}
ApplicationController applicationController = (ApplicationController)getApplicationContext();
itemsDataSource = applicationController.itemsDataSource;
final Cursor mCursor = itemsDataSource.getAllItems();
startManagingCursor(mCursor);
CustomCursorAdapter adapter = new CustomCursorAdapter(this, mCursor);
adapter.notifyDataSetChanged();
setListAdapter(adapter);
ListView listView = getListView();
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
selectAction(id);
}
});
}
private void selectAction(final long position) {
Builder builder = new AlertDialog.Builder(this);
builder.setTitle("בחר פעולה");
builder
.setMessage("בחר בפעולה שברצונך לבצע:");
builder.setPositiveButton("עדכן פריט קניה",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//do update
}
});
builder.setNeutralButton("מחק פריט קניה",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
itemsDataSource.deleteItem(position);
Toast toast = Toast.makeText(getBaseContext(), "הפריט הנבחר נמחק בהצלחה", Toast.LENGTH_SHORT);
toast.show();
}
});
builder.setNegativeButton("חזור לרשימת הקניות",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
the code of the customadapter is:
public class CustomCursorAdapter extends CursorAdapter implements Adapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
public CustomCursorAdapter(Context context, Cursor c) {
super(context, c);
mInflater=LayoutInflater.from(context);
mContext=context;
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView itemTitle= (TextView)view.findViewById(R.id.itemTitle);
itemTitle.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)));
TextView itemQuantity = (TextView)view.findViewById(R.id.itemQuantity);
itemQuantity.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY)));
CheckBox itemCheck = (CheckBox) view.findViewById(R.id.itemCheck);
itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED))==1);
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view=mInflater.inflate(R.layout.listview_item_row, parent, false);
TextView itemTitle= (TextView)view.findViewById(R.id.itemTitle);
itemTitle.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_NAME)));
TextView itemQuantity = (TextView)view.findViewById(R.id.itemQuantity);
itemQuantity.setText(cursor.getString(cursor.getColumnIndex(MySQLiteHelper.KEY_QUANTITY)));
CheckBox itemCheck = (CheckBox) view.findViewById(R.id.itemCheck);
itemCheck.setChecked(cursor.getInt(cursor.getColumnIndex(MySQLiteHelper.KEY_CHECKED))==1);
return view;
}
}
in your code you have not added your adapter class. I am sure that you will be adding the data in list view from an array list.
So in the time of updating the list view by adding or removing a data to the list view, first either add or remove the data from your array list and the call as follows
listView.invalidateViews();
then call your set Adapter method

Drag and Drop in GWT 2.4

I have a custom widget that is actually an image, and i would like to be able to drag it inside an AbsolutePanel and get its coordinates every time. I would like to use the new DND API from GWT 2.4, but i'm having a hard time to implement it. Can someone propose the basic steps i must take?
The new DnD API introduced with GWT 2.4 doesn't currently support the AbsolutePanel (see the implementations of the HasAllDragAndDropHandlers interface). Looking at the implementation of FocusPanel it shouldn't be too hard to enable it for other panels.
Here's a quick proof of concept on how to solve your problem:
public void onModuleLoad() {
DragImage image = new DragImage();
image.setUrl(Resources.INSTANCE.someImage().getSafeUri());
final DropAbsolutePanel target = new DropAbsolutePanel();
target.getElement().getStyle().setBorderWidth(1.0, Unit.PX);
target.getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
target.getElement().getStyle().setBorderColor("black");
target.setSize("200px", "200px");
// show drag over effect
target.addDragOverHandler(new DragOverHandler() {
#Override
public void onDragOver(DragOverEvent event) {
target.getElement().getStyle().setBackgroundColor("#ffa");
}
});
// clear drag over effect
target.addDragLeaveHandler(new DragLeaveHandler() {
#Override
public void onDragLeave(DragLeaveEvent event) {
target.getElement().getStyle().clearBackgroundColor();
}
});
// enable as drop target
target.addDropHandler(new DropHandler() {
#Override
public void onDrop(DropEvent event) {
event.preventDefault();
// not sure if the calculation is right, didn't test it really
int x = (event.getNativeEvent().getClientX() - target.getAbsoluteLeft()) + Window.getScrollLeft();
int y = (event.getNativeEvent().getClientY() - target.getAbsoluteTop()) + Window.getScrollTop();
target.getElement().getStyle().clearBackgroundColor();
Window.alert("x: " + x + ", y:" + y);
// add image with same URL as the dropped one to absolute panel at given coordinates
target.add(new Image(event.getData("text")), x, y);
}
});
RootPanel.get().add(image);
RootPanel.get().add(target);
}
And here the custom panel
public class DropAbsolutePanel extends AbsolutePanel implements HasDropHandlers, HasDragOverHandlers,
HasDragLeaveHandlers {
#Override
public HandlerRegistration addDropHandler(DropHandler handler) {
return addBitlessDomHandler(handler, DropEvent.getType());
}
#Override
public HandlerRegistration addDragOverHandler(DragOverHandler handler) {
return addBitlessDomHandler(handler, DragOverEvent.getType());
}
#Override
public HandlerRegistration addDragLeaveHandler(DragLeaveHandler handler) {
return addBitlessDomHandler(handler, DragLeaveEvent.getType());
}
}
and image class:
public class DragImage extends Image {
public DragImage() {
super();
initDnD();
}
private void initDnD() {
// enables dragging if browser supports html5
getElement().setDraggable(Element.DRAGGABLE_TRUE);
addDragStartHandler(new DragStartHandler() {
#Override
public void onDragStart(DragStartEvent event) {
// attach image URL to drag data
event.setData("text", getUrl());
}
});
}
}