PDA

View Full Version : TreePanel DND (Drag And Drop) to Self as Source



andi@mra.co.id
3 Feb 2010, 3:29 AM
Dear bro and sis,
i have one root lets say "Root"
i make one TreePanel as drag source and self drop target, and context menu to add header tree and add leaf tree, when add 2 header ("Header 1" and "Header 2") and 1 one leaf("Leaf 2-1") at "Header 2" , and i move "Header 2" to be a child of "Header 1" , and child leaf "Leaf 2-1" is not show.

this is snippet code.

Thank you sir/madam,



public class DimensionView extends GeneralView {

private TreePanel<MGLDimensionVO> m_dimensionTP = null;
private MGLDimensionVO m_dimensionChoosed = null;
private TreeStore<MGLDimensionVO> m_treeStoreData = null;
private RpcProxy<List<MGLDimensionVO>> m_proxyTree = null;
private TreeLoader<MGLDimensionVO> m_treeLoader = null;
private Long m_primaryKeyAdded = new Long(0);
private final ContentPanel m_dimensionCP = new ContentPanel();
private final Button m_dimensionRefreshBtn = new Button("Refresh", Pictures.ICONS.refresh());
private final ContentPanel m_dimensionFrmCP = new ContentPanel();
private final Button m_dimensionSaveChangesBtn = new Button("Save Changes", Pictures.ICONS.save_changes());
private final TextField<String> m_codeNameTxtField = new TextField<String>();
private final TextField<String> m_companyNameTxtField = new TextField<String>();
private final CheckBox m_isLeafCB = new CheckBox();
private ComboBox<BeanModel> m_currencyCB = null;
private final TextArea m_addressTxtArea = new TextArea();
private final TextField<String> m_emailTxtField = new TextField<String>();
private final LabelField m_parentCodeNameLblField = new LabelField();
private final UtilityServiceAsync m_utilityService = (UtilityServiceAsync) Registry.get(Constanta.UTILITY_SERVICE);
private RpcProxy<List<MCurrencyVO>> m_proxyCurrency = null;

public DimensionView() {
setScrollMode(Scroll.AUTO);
setLayout(new RowLayout());

TableLayout l_tblLayout = new TableLayout(5);
l_tblLayout.setCellPadding(2);
l_tblLayout.setCellSpacing(4);
l_tblLayout.setBorder(0);
m_dimensionFrmCP.setLayout(l_tblLayout);
m_dimensionFrmCP.setHeight(400);
}

private void createRpcAndLoader() {
this.m_proxyTree = new RpcProxy<List<MGLDimensionVO>>() {

@Override
protected void load(
Object loadConfig,
AsyncCallback<List<MGLDimensionVO>> callback) {

MGLDimensionVO l_dimensionVO = (MGLDimensionVO) loadConfig;
m_dimensionService.getDimensionTree(l_dimensionVO, callback);
}
};

this.m_proxyCurrency = new RpcProxy<List<MCurrencyVO>>() {

@Override
protected void load(Object loadConfig, AsyncCallback<List<MCurrencyVO>> callback) {
m_utilityService.getAllCurrency(callback);
}
};

this.m_treeLoader = new BaseTreeLoader<MGLDimensionVO>(m_proxyTree) {

@Override
public boolean hasChildren(MGLDimensionVO p_parent) {
if (p_parent != null && p_parent.getIsLeaf() != null)
return !p_parent.getIsLeaf();

return false;
}
};
}

private void createDimensionTreePanel() {
m_treeStoreData = new TreeStore<MGLDimensionVO>(m_treeLoader);
m_treeStoreData.setKeyProvider(new ModelKeyProvider<MGLDimensionVO>() {

@Override
public String getKey(MGLDimensionVO model) {
return model.getCodeName();
}
});

this.m_dimensionTP = new TreePanel<MGLDimensionVO>(m_treeStoreData) {

@Override
protected boolean hasChildren(MGLDimensionVO model) {
if (model.getIsLeaf())
return false;
return true;
}
};

this.m_dimensionTP.setHeight(Constanta.CONTENT_PANEL_HEIGHT - 10);
this.m_dimensionTP.setDisplayProperty("codeName");
this.m_dimensionTP.getStyle().setLeafIcon(Pictures.ICONS.dimension_leaf());
this.m_dimensionTP.getStyle().setNodeCloseIcon(Pictures.ICONS.treeClose());
this.m_dimensionTP.getStyle().setNodeOpenIcon(Pictures.ICONS.treeOpen());
this.m_dimensionTP.setCaching(true);
this.m_dimensionTP.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
this.m_dimensionTP.addListener(Events.OnDoubleClick, new Listener<TreePanelEvent<MGLDimensionVO>>() {

@Override
public void handleEvent(TreePanelEvent<MGLDimensionVO> be) {
MGLDimensionVO l_tree = be.getItem();
m_dimensionChoosed = new MGLDimensionVO();
m_dimensionChoosed.setPrimaryKey(l_tree.getPrimaryKey());
m_dimensionChoosed.setCodeName(l_tree.getCodeName());
m_dimensionChoosed.setCompanyName(l_tree.getCompanyName());
m_dimensionChoosed.setCompleteName(l_tree.getCodeName() + " " + l_tree.getCompanyName());
m_dimensionChoosed.setParentIdFk(l_tree.getParentIdFk());
m_dimensionChoosed.setIsLeaf(l_tree.getIsLeaf());
m_dimensionChoosed.setEmail(l_tree.getEmail());
m_dimensionChoosed.setAddress(l_tree.getAddress());
m_dimensionChoosed.setCurrencyCode(l_tree.getCurrencyCode());
m_dimensionChoosed.setParentCodeName(l_tree.getParentCodeName());

m_codeNameTxtField.setValue(m_dimensionChoosed.getCodeName());
m_isLeafCB.setValue(m_dimensionChoosed.getIsLeaf());
m_companyNameTxtField.setValue(m_dimensionChoosed.getCompanyName());
m_emailTxtField.setValue(m_dimensionChoosed.getEmail());
m_addressTxtArea.setValue(m_dimensionChoosed.getAddress());
m_parentCodeNameLblField.setValue(m_dimensionChoosed.getParentCodeName());

for (BeanModel bm : m_currencyCB.getStore().getModels())
if (bm.get("currencyCode").toString().equals(m_dimensionChoosed.getCurrencyCode()))
m_currencyCB.setValue(bm);

}
});


TreePanelDragSource l_dragSource = new TreePanelDragSource(m_dimensionTP);
l_dragSource.setGroup("DimensionTreePanel");
l_dragSource.addDNDListener(new DNDListener() {

@Override
public void dragStart(DNDEvent e) {
MGLDimensionVO l_dimSource = m_dimensionTP.getSelectionModel().getSelectedItem();
Window.alert("COde Name " + l_dimSource.getCodeName());
super.dragStart(e);
}
});

TreePanelDropTarget l_dropTarget = new TreePanelDropTarget(m_dimensionTP);
l_dropTarget.setFeedback(DND.Feedback.APPEND);
l_dropTarget.setGroup("DimensionTreePanel");
l_dropTarget.setAllowDropOnLeaf(false);
l_dropTarget.setAllowSelfAsSource(true);
l_dropTarget.addDNDListener(new DNDListener() {

@Override
public void dragDrop(DNDEvent e) {
TreePanel<MGLDimensionVO> l_treeSource = (TreePanel<MGLDimensionVO>) e.getDropTarget().getComponent();
MGLDimensionVO l_dimensionSource = l_treeSource.getSelectionModel().getSelectedItem();
Window.alert("Code srouce " + l_dimensionSource.getCodeName());


TreePanel<MGLDimensionVO> l_treeTarget = (TreePanel<MGLDimensionVO>) e.getDragSource().getComponent();
MGLDimensionVO l_dimensionTarget = l_treeTarget.getSelectionModel().getSelectedItem();

Window.alert("Target Code " + l_dimensionTarget.getCodeName());
//ada anaknya gak ?
Window.alert("Target Coutn " + l_dimensionTarget.getChildCount());
super.dragDrop(e);
}
});

m_dimensionCP.setHeight(Constanta.CONTENT_PANEL_HEIGHT);
m_dimensionCP.setWidth(Constanta.CONTENT_PANEL_WIDTH);

m_dimensionCP.setBodyBorder(true);
m_dimensionCP.setLayout(new FitLayout());

ToolBar l_toolBar = new ToolBar();
l_toolBar.add(m_dimensionRefreshBtn);
l_toolBar.add(new SeparatorToolItem());
l_toolBar.add(m_dimensionSaveChangesBtn);
m_dimensionCP.setTopComponent(l_toolBar);

m_dimensionCP.add(m_dimensionTP);
RowData l_rowData = new RowData();
l_rowData.setMargins(new Margins(5));
this.add(m_dimensionCP, l_rowData);
}

public MGLDimensionVO getDimensionChoosed() {
return m_dimensionChoosed;
}

private void createContextMenu() {
Menu l_dimensionMenu = new Menu();
MenuItem l_menuChildLeaf = new MenuItem();
MenuItem l_menuChildHeader = new MenuItem();
MenuItem l_menuChildDelete = new MenuItem();

l_menuChildHeader.setText("Add Dimension Header");
l_menuChildHeader.setIcon(Pictures.ICONS.add());
l_menuChildHeader.addSelectionListener(new SelectionListener<MenuEvent>() {

@Override
public void componentSelected(MenuEvent ce) {
MGLDimensionVO l_parent = m_dimensionTP.getSelectionModel().getSelectedItem();

if (l_parent != null && l_parent.getPrimaryKey() != null && l_parent.getIsLeaf()) {
Window.alert("Cannot add to child please add to header");
return;
}

m_primaryKeyAdded--;
MGLDimensionVO l_child = new MGLDimensionVO();
l_child.setPrimaryKey(m_primaryKeyAdded);
l_child.setChildren(new ArrayList<ModelData>());
l_child.setIsLeaf(Boolean.FALSE);
l_child.setCodeName("Added " + m_primaryKeyAdded);
l_child.setParentIdFk(l_parent.getPrimaryKey());
l_child.setParent(l_parent);
l_child.setParentCodeName(l_parent.getCodeName());
l_parent.getChildren().add(l_child);
m_dimensionTP.getStore().add(l_parent, l_child, true);
}
});

l_menuChildLeaf.setText("Add Dimension Leaf");
l_menuChildLeaf.setIcon(Pictures.ICONS.add());
l_menuChildLeaf.addSelectionListener(new SelectionListener<MenuEvent>() {

@Override
public void componentSelected(MenuEvent ce) {
MGLDimensionVO l_parent = m_dimensionTP.getSelectionModel().getSelectedItem();

if (l_parent != null && l_parent.getPrimaryKey() != null && l_parent.getIsLeaf()) {
Window.alert("Cannot add to child please add to header");
return;
}

m_primaryKeyAdded--;
MGLDimensionVO l_child = new MGLDimensionVO();
l_child.setPrimaryKey(m_primaryKeyAdded);
l_child.setChildren(new ArrayList<ModelData>());
l_child.setIsLeaf(Boolean.TRUE);
l_child.setCodeName("Added " + m_primaryKeyAdded);
l_child.setParentIdFk(l_parent.getPrimaryKey());
l_child.setParent(l_parent);
l_child.setParentCodeName(l_parent.getCodeName());
l_parent.getChildren().add(l_child);
m_dimensionTP.getStore().add(l_parent, l_child, true);
}
});


l_menuChildDelete.setText("Delete");
l_menuChildDelete.setIcon(Pictures.ICONS.delete());
l_menuChildDelete.addSelectionListener(new SelectionListener<MenuEvent>() {

@Override
public void componentSelected(MenuEvent ce) {
MGLDimensionVO l_parent = m_dimensionTP.getSelectionModel().getSelectedItem();

if (l_parent.getParent() == null) {
Window.alert("Cannot Delete Super Parent");
return;
}
m_dimensionTP.getStore().remove(l_parent);
}
});

l_dimensionMenu.add(l_menuChildHeader);
l_dimensionMenu.add(l_menuChildLeaf);
l_dimensionMenu.add(l_menuChildDelete);
m_dimensionTP.setContextMenu(l_dimensionMenu);
}

@Override
public void createSequenceDimensionAction() {
throw new UnsupportedOperationException("Not supported yet.");
}

@Override
public void createSequenceFormUploadAction() {
throw new UnsupportedOperationException("Not supported yet.");
}

private void createButtonsEvent() {
m_dimensionRefreshBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {
m_treeLoader.load();
}
});

m_dimensionSaveChangesBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {
}
});
}

private void createFormDimension() {
TableData l_tblData = new TableData();

m_dimensionFrmCP.setHeading("Dimension Detail Data");
LabelField l_lblField = new LabelField();

l_lblField.setText("<b>Parent Tree</b>");
m_dimensionFrmCP.add(l_lblField, new TableData());
l_tblData.setColspan(4);
m_dimensionFrmCP.add(m_parentCodeNameLblField, l_tblData);

l_lblField = new LabelField();
l_lblField.setText("<b>Code Name</b>");
l_lblField.setToolTip("Must Be Unique");
m_dimensionFrmCP.add(l_lblField, new TableData());
m_dimensionFrmCP.add(m_codeNameTxtField, new TableData());

m_isLeafCB.setBoxLabel("Is Leaf");
m_dimensionFrmCP.add(m_isLeafCB, new TableData());

l_lblField = new LabelField();
l_lblField.setText("Company Name");
m_dimensionFrmCP.add(l_lblField, new TableData());
m_dimensionFrmCP.add(m_companyNameTxtField, new TableData());

l_lblField = new LabelField();
l_lblField.setText("Email");
m_dimensionFrmCP.add(l_lblField, new TableData());
m_dimensionFrmCP.add(m_emailTxtField, new TableData());

l_lblField = new LabelField();
l_lblField.setText("Currency");
m_dimensionFrmCP.add(l_lblField);
l_tblData = new TableData();
l_tblData.setColspan(2);
m_currencyCB = m_generalFacilityObj.createComboBoxFromList(m_proxyCurrency);
m_currencyCB.setDisplayField("currencyCode");
m_currencyCB.setValueField("primaryKey");
m_dimensionFrmCP.add(m_currencyCB, l_tblData);

l_lblField = new LabelField();
l_lblField.setText("Address");
m_dimensionFrmCP.add(l_lblField);
m_addressTxtArea.setSize(150, 150);
m_dimensionFrmCP.add(m_addressTxtArea, new TableData());

RowData l_rowData = new RowData();
l_rowData.setWidth(1.0d);
l_rowData.setMargins(new Margins(5));
add(m_dimensionFrmCP, l_rowData);
}

@Override
protected void onRender(Element p_parent, int p_index) {
super.onRender(p_parent, p_index);
createRpcAndLoader();
createDimensionTreePanel();
createContextMenu();
createButtonsEvent();
createFormDimension();
}
}