GXT3/GWT - How to remove cells(HasCell) from compositeCell dynamically? - gwt

I have a list of cells which i'll send it as an argument to create a composite cell. In the below code i'm creating the cells and adding it to the list.
List<HasCell<SensorTreeModel, ?>> cells= new ArrayList<HasCell<SensorTreeModel,?>>();
com.sencha.project.client.ImageCell editSwitchIcon = new com.sencha.project.client.ImageCell(ButtonType.EDITSWITCH);
com.sencha.project.client.ImageCell editIcon = new com.sencha.project.client.ImageCell(ButtonType.EDIT);
com.sencha.project.client.ImageCell switchRoleIcon = new com.sencha.project.client.ImageCell(ButtonType.SWITCH);
com.sencha.project.client.ImageCell breakIcon = new com.sencha.project.client.ImageCell(ButtonType.BREAK);
com.sencha.project.client.ImageCell deleteIcon = new com.sencha.project.client.ImageCell(ButtonType.DELETE);
cells.add(editSwitchIcon);
cells.add(editIcon);
cells.add(switchRoleIcon);
cells.add(breakIcon);
cells.add(deleteIcon);
In the below code. I'm adding the list to the composite cell.(ActionCell)
actionsCol.setCell(new ActionCell(cells));
Now what if I want to delete the cells dynamically in the columnConfig(actionCell) depending on the object which is going into the column?

Related

How to add rows to TableView without having any data model

I'm fairly new to javafx, so please bear with me if my question is unclear. I've a TableView which need to be populated using an ObservableList.
The code below populates my "data" with the arraylists generated out of the Map, which in turn is used to add rows to my table.
TableView<ArrayList> table = new TableView<>();
ObservableList<ArrayList> data = FXCollections.observableArrayList();
for(int i=0;i<listSelectedVerticesIds.size();i++){
ArrayList<String> tempString = new ArrayList<>();
for(Map.Entry<String,String> temp2 : mapVertex.get(listSelectedVerticesIds.get(i)).entrySet()){
tempString.add(temp2.getValue());
}
data.add(tempString);
}
table.setItems(data);
However, I do not see the table populated with the list in "data". I'm guessing this is because there is no data binding (using setCellValueFactory). However, as you can see I dont have a data model class. All of my data comes from the Map as strings which I would like to populate in my tableview.
Please suggest.
Here is a simple way to do it that works great. You don't need a data structure to populate a table. You only see that because that's what most examples show. It is an extremely common use case to populate a table from a database or a file. I don't understand why this is so hard to find examples for. Well, hope this helps.
private TableView<ObservableList<StringProperty>> table = new TableView<>();
private ArrayList<String> myList = new ArrayList<>();
private void updateTableRow() {
for (int row = 0; row < numberOfRows; row++) {
ObservableList<StringProperty> data = FXCollections.observableArrayList();
for (int column = 0; column < numberOfColumns; column++) {
data.add(column, new SimpleStringProperty(myList.get(row + (column * numberOfRows))));
}
table.getItems().add(data);
}
}

Updating column in tableviwer

I have retrieved data from database and i am able to show it in Table of the Table Viewer but my task is on click of a row the data should be appear in another form from where i can edit it and on click of update the view should be updated with the new values.I am using viewer.addSelectionChangedListener to retrieve the selected row but i am not getting how to update the table .Please suggest me few ideas
I have written the below code in the constructor of my class so whenever object is created Table is generated and l1 is list of data which i am passing to another UI
input[i] = new MyModel(persons[i].getDateOfRegistration(), persons[i].getFirstName(),
persons[i].getMiddleName(), persons[i].getLastName(), persons[i].getGender(),
persons[i].getDob(), persons[i].getContactNumber(), persons[i].getMaritalStatus(),
persons[i].getAddress(), persons[i].getCountry(), persons[i].getBloodGroup(),
persons[i].getInsuranceDetails().getPolicyHolderName(),
persons[i].getInsuranceDetails().getPolicyNumber(),
persons[i].getInsuranceDetails().getSubscriberName(),
persons[i].getInsuranceDetails().getRelationshipToPatient());
viewer.setInput(input);
table.setHeaderVisible(true);
table.setLinesVisible(true);
GridData gridData = new GridData();
gridData.verticalAlignment = GridData.FILL;
gridData.horizontalSpan = 2;
gridData.grabExcessHorizontalSpace = true;
gridData.grabExcessVerticalSpace = true;
gridData.horizontalAlignment = GridData.FILL;
viewer.getControl().setLayoutData(gridData);
viewer.addSelectionChangedListener(new ISelectionChangedListener() {
public void selectionChanged(SelectionChangedEvent event) {
IStructuredSelection selection = (IStructuredSelection) event.getSelection();
StringBuffer sb = new StringBuffer("Selection - ");
int j = 0;
String[] s;
for (Iterator iterator = selection.iterator(); iterator.hasNext();) {
sb.append(iterator.next() + ", ");
System.out.println("Testing::" + sb);
}
System.out.println(sb);
String result[] = new String[18];
List l1 = new ArrayList(100);
String[] parts = sb.toString().split("=");
for (int i = 0; i < parts.length; i++) {
System.out.println("s" + parts[i]);
String[] s1 = parts[i].split(",");
l1.add(s1[0]);
}
SWTPatientRegistrationUpdatePageEventView swtPatientRegistrationUpdatePageEventView = new SWTPatientRegistrationUpdatePageEventView();
swtPatientRegistrationUpdatePageEventView.openParentUpdateShell(l1);
// viewer.refresh();
//flag=false;
//refreshingData(list);
}
});
Make sure we will follow table viewer MVC architecture all the time. Steps we can do to resolve your issue isas follow:
Create proper datastructure(Model Class) to hold the data. and use the same in array which you set in viewer.setInput()
When you click/double click on table row, fetch the data and save it in datastructure created(with new object).
Pass that data structure to your form dialog.
when you are done with update in form. Fill the updated data in new Model object again.
And after form close, pass that model object back to parent.
updated the corresponding array element with object received from Form Dialog and just refresh the tableviewer.
Please let me know if you need some code module. Thanks

How do I add a linked document to a linked list using the Java API for OrientDB?

I have an OType.LINKLIST field children in a class.
I can use the following command to update it:
update <parent_rid> add children = <child_rid>
But I don't know how to do this without using SQL, which is my goal.
code
ODocument doc=new ODocument("ClassA");
ODocument parentDoc=db.load(new ORecordId(rid));
How do I add doc to parentDoc's field children without using SQL?
create class Doc
create class ParentDoc
create property ParentDoc.children LINKLIST
insert into Doc set name = 'doc1' #12:0
insert into Doc set name = 'doc2' #12:1
insert into ParentDoc set name = 'pd', children = [#12:0] #13:0
update #13:0 add children = #12:1
For what I understood you want a piece of code that replaces the last four commands using Java Document API.
try (ODatabaseDocument db = new ODatabaseDocumentTx("remote:localhost/DB")) {
db.open("admin", "admin");
ODocument doc1 = new ODocument("Doc");
doc1.field("name", "doc1");
doc1.save();
List<OIdentifiable> linklist = new ArrayList();
linklist.add(doc1);
ODocument parent = new ODocument("ParentDoc");
parent.field("children", linklist, OType.LINKLIST);
parent.save();
// ...
ODocument doc2 = new ODocument("Doc");
doc2.field("name", "doc2");
doc2.save();
List children = parent.field("children");
children.add(doc2);
parent.field("children", children);
parent.save();
}

Using Drag-Sort ListView with SQLite DB

I'm trying to create a simple to-do-list app with an SQLite DB and a Drag-Sort ListView.
Right now I am binding data from an SQLite database cursor into a ListView using a SimpleCursorAdapter and adding items with an EditText view as explained in this tutorial.
I have moved everything into one activity and I have swapped the plain ListView with a Drag-Sort ListView. My issue is connecting the DB and Drag-Sort ListView together. The DLSV demos use a predefined array to fill fill the DSLV.
Snippet form DSLV:
DragSortListView lv = (DragSortListView) getListView();
lv.setDropListener(onDrop);
lv.setRemoveListener(onRemove);
array = getResources().getStringArray(R.array.jazz_artist_names);
list = new ArrayList<String>(Arrays.asList(array));
adapter = new ArrayAdapter<String>(this, R.layout.list_item1, R.id.text1, list);
setListAdapter(adapter);
Snippet from my existing code:
mDbHelper = new NotesDbAdapter(this);
mDbHelper.open();
...
mDbHelper.createNote(noteName, "");
fillData();
private void fillData() {
// Get all of the notes from the database and create the item list
Cursor c = mDbHelper.fetchAllNotes();
startManagingCursor(c);
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
setListAdapter(notes);
}
Thank you and sorry if this doesn't make all that much sense.
Since you cannot reorder the items in a Cursor, you have to create a mapping between Cursor positions and ListView positions. This is done for you by the DragSortCursorAdapter class and subclasses so that the drag and drop reordering is maintained visually. If you need the new orders persisted (like to your database), then you must implement that logic yourself. The method getCursorPositions() will help you with this.
I have made use of Drag sort list view. Its amazing! but instead of using the dragsortcursoradapter i created my own trick. here it is.
What i have done is that whenever i swapped any item, i passed the new swapped list in an array to the database, deleted the table and recreated the new updated table. here is my code
here is the code snippet from my database handler
public void onUpdateToDoTable(ArrayList<Task> taskList) {
SQLiteDatabase db = this.getWritableDatabase();
db.execSQL("DROP TABLE IF EXISTS " + TABLE_TASKTODO);
String CREATE_TASK_TODO_TABLE = "CREATE TABLE IF NOT EXISTS "
+ TABLE_TASKTODO + "(" + SEQ_ID + " INTEGER PRIMARY KEY,"
+ TASK_NAME + " TEXT )";
db.execSQL(CREATE_TASK_TODO_TABLE);
for (int i = 0; i < taskList.size(); i++) {
ContentValues values = new ContentValues();
values.put(TASK_NAME, taskList.get(i).getTask());
db.insert(TABLE_TASKTODO, null, values);
}
db.close();
}
then on using drop listener
i called the above method..
private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
#Override
public void drop(int from, int to) {
Task item = adapter.getItem(from);
adapter.notifyDataSetChanged();
adapter.remove(item);
adapter.insert(item, to);
db.onUpdateToDoTable(list);
Log.d("LIST", db.getAllToDoTasks().toString());
}
};
db is my object of database handler. whole source code is available at dragdroplistview. amazing example. this was a life saver for me. Ciao!
There is a github project available at https://github.com/jmmcreynolds/dslv-db-demo
This demo includes a working example of how to setup a DragSortListView that will save the changes that you make to the list (position, add/delete) to a database.
I have used it just now. Its perfect demo project available for using DSLV with SQLiteDB.
Thanks to github user jmmcreynolds.

GWT Sorting a cell table, probably just something i didn't saw

I've been struggling for the last couple of hour trying to sort a GWT CellTable.
It's really a stupid problem because it's been done here
http://gwt.google.com/samples/Showcase/Showcase.html#!CwCellTable
But I do not understand what I'm missing in the exemple ...
Here is my code I use to create the column:
Column<RemoteCommand, String> nbProducts = new Column<RemoteCommand, String>(
new TextCell()) {
#Override
public String getValue(RemoteCommand object) {
return object.getNumberProduct();
}
};
nbProducts.setSortable(true);
sortHandler.setComparator(nbProducts, new Comparator<RemoteCommand>() {
public int compare(RemoteCommand o1, RemoteCommand o2) {
cellTable.redraw();
return o1.getCommandSize().compareTo(o2.getCommandSize());
// System.out.println(Integer.parseInt(o1.getCommandSize() ) - Integer.parseInt(o2.getCommandSize()));
// return Integer.parseInt(o1.getCommandSize() ) - Integer.parseInt(o2.getCommandSize());
}
});
And here is the declaration of the table itself:
// Add a selection model so we can select cells.
final SelectionModel<RemoteCommand> selectionModel = new MultiSelectionModel<RemoteCommand>(
RemoteCommand.KEY_PROVIDER);
cellTable.setSelectionModel(selectionModel,
DefaultSelectionEventManager.<RemoteCommand> createCheckboxManager());
// Attach a column sort handler to the ListDataProvider to sort the list.
ListHandler<RemoteCommand> sortHandler = new ListHandler<RemoteCommand>(values);
cellTable.addColumnSortHandler(sortHandler);
// Initialize the columns.
initTableColumns(selectionModel, sortHandler);
cellTable.setRowData(values);
help is requierd :)
i guess You've already found the solution, but just to keep it here:
First, create your dataProvider with some known List.
Than feed sortHandler with same List;
and use the list to update data.
Celltable should be set as dataDisplay of the dataProvider:
List myDataList = new ArrayList();
ListDataProvider dataProvider = new ListDataProvider(KEY_PROVIDER);
dataProvider.setList(myDataList);
ListHandler sortHandler = new ListHandler(dataProvider.getList);
//tie provider and table
dataProvider.addDataDisplay(cellTable);
//when you need to update dataprovider
//first do some myDataList cleanup to remove old values
myListData.addAll(values);
//update data displays
dataProvider.refresh();
Consider, you have to always use one and the same List object.