DATABIND : refactoring databinding view 31/18231/2
authorjaeyeol lee <jaeyeol148.lee@samsung.com>
Wed, 19 Mar 2014 06:28:40 +0000 (15:28 +0900)
committerjaeyeol lee <jaeyeol148.lee@samsung.com>
Wed, 19 Mar 2014 06:37:05 +0000 (15:37 +0900)
Undo/redo is available in the DataSource panel
Modify the save policy

Change-Id: If105a9c8d8209210bb0e86f1a57bd347266f5681
Signed-off-by: jaeyeol lee <jaeyeol148.lee@samsung.com>
20 files changed:
org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetDataSourceCommand.java [new file with mode: 0644]
org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetViewModelCommand.java [new file with mode: 0644]
org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManager.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/DataBindingDnDManager.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/DataBindingPage.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/SetSourceDialog.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/AddDataSourceAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/AddViewModelAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/AddViewModelItemAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/EditDataSourceAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/GetSourceAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/RemoveDataSourceAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/RemoveViewModelAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/actions/SetTargetAction.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/BindingData.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/BindingDataSet.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/BindingDataSetEvent.java [new file with mode: 0644]
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/BindingObject.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/IBindingDataSetListener.java [new file with mode: 0644]
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/properties/events/HandlerCodeWriter.java

diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetDataSourceCommand.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetDataSourceCommand.java
new file mode 100644 (file)
index 0000000..1f5c0ad
--- /dev/null
@@ -0,0 +1,58 @@
+package org.tizen.webuibuilder.gef.commands;
+
+import org.eclipse.gef.commands.Command;
+import org.tizen.webuibuilder.model.app.AppManager;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSetEvent.BindingDataSetEventType;
+
+public class SetDataSourceCommand extends Command {
+       
+       private AppManager appManager;
+       private BindingData bindingData;
+       private BindingData oldBindingData;
+       private int index;
+       private BindingDataSetEventType type;
+       
+       public SetDataSourceCommand(BindingDataSetEventType type, AppManager appManager, BindingData bindingData, int index) {
+               this.type = type;
+               this.appManager = appManager;
+               this.bindingData = bindingData;
+               this.index = index;
+       }
+       
+       @Override
+       public boolean canExecute() {
+        if ((bindingData == null) || (appManager == null)) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+       
+       @Override
+       public void execute() {
+        if (canExecute()) {
+               if (type.equals(BindingDataSetEventType.DATASOURCE_REMOVED)) {
+                       appManager.removeDataSource(bindingData);
+               } else if (type.equals(BindingDataSetEventType.DATASOURCE_CHANGED)) {
+                       oldBindingData = appManager.getDataSource(bindingData.getSourceName());
+                       appManager.removeDataSource(oldBindingData);
+                       appManager.addDataSource(bindingData);
+               } else {
+                       appManager.addDataSource(bindingData);
+               }
+        }
+    }
+       
+       @Override
+    public void undo() {
+               if (type.equals(BindingDataSetEventType.DATASOURCE_REMOVED)) {
+               appManager.addDataSource(bindingData);
+               } else if (type.equals(BindingDataSetEventType.DATASOURCE_CHANGED)) {
+               appManager.removeDataSource(bindingData);
+               appManager.addDataSource(oldBindingData);
+       } else {
+               appManager.removeDataSource(bindingData);
+       }
+    }
+}
diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetViewModelCommand.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetViewModelCommand.java
new file mode 100644 (file)
index 0000000..30cead3
--- /dev/null
@@ -0,0 +1,122 @@
+package org.tizen.webuibuilder.gef.commands;
+
+import java.util.List;
+
+import org.eclipse.gef.commands.Command;
+import org.tizen.webuibuilder.model.app.AppManager;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingObject;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSetEvent.BindingDataSetEventType;
+
+public class SetViewModelCommand extends Command {
+       private AppManager appManager;
+       private BindingData bindingData;
+       private List<String> itemPath;
+       private String childName;
+       private String value;
+       private int index;
+       private BindingDataSetEventType type;
+       private BindingObject oldViewModelObject;
+       private BindingData oldBindingData;
+       
+       public SetViewModelCommand(BindingDataSetEventType type, AppManager appManager, BindingData bindingData, int index) {
+               this.type = type;
+               this.appManager = appManager;
+               this.bindingData = bindingData;
+               this.index = index;
+       }
+       
+       public SetViewModelCommand(BindingDataSetEventType type, AppManager appManager, BindingData bindingData, List<String> itemPath, String childName, String value, int index) {
+               this.type = type;
+               this.appManager = appManager;
+               this.bindingData = bindingData;
+               this.itemPath = itemPath;
+               this.childName = childName;
+               this.value = value;
+               this.index = index;
+       }
+       
+       public SetViewModelCommand(BindingDataSetEventType type, AppManager appManager, BindingData bindingData, BindingData oldBindingData, int index) {
+               this.type = type;
+               this.appManager = appManager;
+               this.bindingData = bindingData;
+               this.oldBindingData = oldBindingData;
+               this.index = index;
+       }
+
+       @Override
+       public boolean canExecute() {
+        if ((bindingData == null) || (appManager == null)) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+       
+       @Override
+       public void execute() {
+        if (canExecute()) {
+               if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_ADDED)) {
+                       setViewModelItem(type);
+               } else if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_REMOVED)) {
+                       oldViewModelObject = getViewModelObject(bindingData, itemPath, childName);
+                       setViewModelItem(type);
+               } else if (type.equals(BindingDataSetEventType.VIEWMODEL_SET_DATASOURCE)) {
+                       BindingData data = getViewModel(bindingData.getName());
+                       if (data != null) {
+                               setDataSource(type, data, bindingData);
+                       } else {
+                               setDataSource(type, bindingData, null);
+                       }
+               } else {
+                       setViewModel(type);
+               }       
+        }
+    }
+       
+       private BindingData getViewModel(String name) {
+               return appManager.getViewModel(name);
+       }
+
+       private BindingObject getViewModelObject(BindingData bindingData,
+                       List<String> itemPath, String childName) {
+
+               return appManager.getViewModelObject(bindingData, itemPath, childName);
+       }
+       
+       private void setViewModelItem(BindingDataSetEventType type) {
+               appManager.changeViewModel(type, bindingData, itemPath, childName, value);
+       }
+       
+       private void setViewModelItem(BindingDataSetEventType type, BindingObject oldViewModelObject) {
+               appManager.changeViewModel(type, bindingData, itemPath, oldViewModelObject, value);
+       }
+       
+       private void setDataSource(BindingDataSetEventType type, BindingData bindingData,  BindingData oldBindingData) {
+               appManager.changeViewModel(type, bindingData, oldBindingData);
+       }
+
+       private void setViewModel(BindingDataSetEventType type) {
+               if (type.equals(BindingDataSetEventType.VIEWMODEL_REMOVED)) {
+               appManager.removeViewModel(bindingData);
+       } else {
+               appManager.addViewModel(bindingData);
+       }
+       }
+
+       @Override
+    public void undo() {
+               if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_ADDED)) {
+               setViewModelItem(BindingDataSetEventType.VIEWMODEL_ITEM_REMOVED);
+               } else if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_REMOVED)) {
+                       setViewModelItem(BindingDataSetEventType.VIEWMODEL_ITEM_ADDED, oldViewModelObject);
+               } else if (type.equals(BindingDataSetEventType.VIEWMODEL_ADDED)) {
+                       setViewModel(BindingDataSetEventType.VIEWMODEL_REMOVED);
+               } else if (type.equals(BindingDataSetEventType.VIEWMODEL_REMOVED)) {
+                       setViewModel(BindingDataSetEventType.VIEWMODEL_ADDED);
+       } else if (type.equals(BindingDataSetEventType.VIEWMODEL_SET_DATASOURCE)) {
+               setDataSource(BindingDataSetEventType.VIEWMODEL_SET_DATASOURCE, bindingData, oldBindingData);
+       }
+    }
+
+}
index 5b41025..5ad3518 100644 (file)
@@ -54,6 +54,8 @@ import org.tizen.webuibuilder.model.page.PageDataSetEvent;
 import org.tizen.webuibuilder.model.project.ProjectManager;
 import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
 import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSet;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSetEvent.BindingDataSetEventType;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingObject;
 
 
 public class AppManager implements IPageDataSetListener, IAppListener {
@@ -717,10 +719,37 @@ public class AppManager implements IPageDataSetListener, IAppListener {
     public BindingDataSet getBindingDataSet() {
         return bindingDataSet;
     }
+    
+    public BindingData getViewModel(String name) {
+       return bindingDataSet.getViewModel(name);
+    }
+    
+    public BindingObject getViewModelObject(BindingData bindingData,
+                       List<String> itemPath, String childName) {
+               return bindingDataSet.getViewModelObject(bindingData, itemPath, childName);
+       }
 
     public void addViewModel(BindingData bindingData) {
         bindingDataSet.addViewModel(bindingData);
-        ;
+    }
+    
+    public void changeViewModel(BindingDataSetEventType type, BindingData bindingData, List<String> itemPath, String childName, String value) {
+        bindingDataSet.changeViewModel(type, bindingData, itemPath, childName, value);
+    }
+    
+    public void changeViewModel(BindingDataSetEventType type, BindingData bindingData, List<String> itemPath,
+                       BindingObject viewModelObject, String value) {
+       bindingDataSet.changeViewModel(type, bindingData, itemPath, viewModelObject, value);
+       }
+    
+    public void changeViewModel(BindingDataSetEventType type,
+                       BindingData bindingData, BindingData oldBindingData) {
+       bindingDataSet.changeViewModel(type, bindingData, oldBindingData);
+       }
+
+    
+    public void removeViewModel(BindingData bindingData) {
+        bindingDataSet.removeViewModel(bindingData);
     }
 
     public void clearViewModel() {
@@ -734,10 +763,18 @@ public class AppManager implements IPageDataSetListener, IAppListener {
     public void addDataSource(BindingData bindingData) {
         bindingDataSet.addDataSource(bindingData);
     }
+    
+    public void removeDataSource(BindingData bindingData) {
+        bindingDataSet.removeDataSource(bindingData);
+    }
 
     public void clearDataSources() {
         bindingDataSet.clearDataSources();
     }
+    
+    public BindingData getDataSource(String name) {
+       return bindingDataSet.getDataSource(name);
+    }
 
     public List<BindingData> getDataSources() {
         return bindingDataSet.getDataSources();
@@ -810,4 +847,5 @@ public class AppManager implements IPageDataSetListener, IAppListener {
     public List<String> getFilePackage(int pageIndex) {
         return null;
     }
+
 }
index 7e07753..419d62c 100644 (file)
@@ -41,7 +41,6 @@ import org.eclipse.swt.widgets.TreeItem;
 import org.eclipse.ui.part.IPage;
 import org.tizen.webuibuilder.BuilderConstants;
 import org.tizen.webuibuilder.gef.dnd.model.DataBindingTemplate;
-import org.tizen.webuibuilder.model.Part;
 import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
 import org.tizen.webuibuilder.ui.views.databinding.model.TreeItemData;
 import org.tizen.webuibuilder.utility.ResourceManager;
@@ -97,53 +96,6 @@ public class DataBindingDnDManager {
         });
     }
 
-    /*
-     * public static void addDropListenerToViewModelTree(final Tree tree) { Transfer[] targetTypes =
-     * new Transfer[] { TreeItemTransfer.getInstance() }; int operations = DND.DROP_MOVE |
-     * DND.DROP_COPY | DND.DROP_LINK;
-     * 
-     * DropTarget target = new DropTarget(tree, operations); target.setTransfer(targetTypes);
-     * target.addDropListener(new DropTargetAdapter() { public void dragOver(DropTargetEvent event)
-     * { event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL; if (event.item != null) {
-     * TreeItem item = (TreeItem) event.item; TreeItem dataModel = getRootItemFromTree(item); if
-     * (dataModel.equals(item)) { event.feedback |= DND.FEEDBACK_SELECT; } else { event.feedback |=
-     * DND.FEEDBACK_NONE; } } else { event.feedback |= DND.FEEDBACK_NONE; } }
-     * 
-     * public void drop(DropTargetEvent event) { if (event.data == null) { event.detail =
-     * DND.DROP_NONE; return; }
-     * 
-     * TreeItem sourceItem = (TreeItem) event.data;
-     * 
-     * if (sourceItem.equals((TreeItem) event.item)) { event.detail = DND.DROP_NONE; return; }
-     * 
-     * if ((event.item != null) && (getRootItemFromTree((TreeItem) event.item).equals(event.item)))
-     * { TreeItem item = (TreeItem) event.item; TreeItem parent = item.getParentItem(); if (parent
-     * != null) { event.detail = DND.DROP_NONE; } else { TreeItem[] viewModelItems =
-     * item.getItems(); for (int i = 0; i < viewModelItems.length; i++) {
-     * viewModelItems[i].dispose(); } //item.clearAll(true);
-     * 
-     * item.setText(item.getData("DEFAULTTEXT") + " (" + sourceItem.getText() + ")");
-     * 
-     * TreeItem[] items = sourceItem.getItems(); for (int i = 0; i < items.length; i++) { TreeItem
-     * newItem = new TreeItem(item, SWT.NONE); setValuesOfItem(newItem, items[i]); } event.detail =
-     * DND.DROP_COPY; } } else { event.detail = DND.DROP_NONE; } } }); }
-     */
-    /*
-     * public static String makeViewModelInfo(TreeItem treeItem) { String str = ""; while
-     * ((treeItem.getParentItem() != null)) { if (str.isEmpty()) { if
-     * ((treeItem.getParentItem().getData("TYPE") !=
-     * null)&&(treeItem.getParentItem().getData("TYPE").equals("JSONObject"))) { str =
-     * "()."+treeItem.getText(); } else { str = treeItem.getText(); //str = "."+treeItem.getText();
-     * }
-     * 
-     * } else { if ((treeItem.getParentItem().getData("TYPE") !=
-     * null)&&(treeItem.getParentItem().getData("TYPE").equals("JSONObject"))) { str =
-     * "()."+treeItem.getText()+str; } else { str = treeItem.getText()+str; }
-     * 
-     * } treeItem = treeItem.getParentItem(); } str = treeItem.getData("DEFAULTTEXT")+"."+ str;
-     * return str; }
-     */
-
     /**
      * Creates a view model information.
      * 
@@ -175,29 +127,6 @@ public class DataBindingDnDManager {
     public static void addDragAndDropListenerToViewModelTree(final Tree tree,
                                                              final DataBindingView dataBindingView,
                                                              final DataBindingPage dataBindingPage) {
-        /*
-         * Transfer[] types = new Transfer[] { ViewModelTreeItemTransfer.getInstance(),
-         * TemplateTransfer.getInstance() }; int operations = DND.DROP_MOVE | DND.DROP_COPY |
-         * DND.DROP_LINK;
-         * 
-         * final DragSource source = new DragSource(tree, operations); source.setTransfer(types);
-         * final TreeItem[] dragSourceItem = new TreeItem[1]; source.addDragListener(new
-         * DragSourceListener() {
-         * 
-         * @Override public void dragStart(DragSourceEvent event) { TreeItem[] selection =
-         * tree.getSelection(); if (selection.length == 1) {// && selection[0].getItemCount() == 0)
-         * { event.doit = true; //selection[0].setData("VIEWMODELNODE", true); dragSourceItem[0] =
-         * selection[0]; //TODO multiselect } else { event.doit = false; }
-         * 
-         * }
-         * 
-         * @Override public void dragSetData(DragSourceEvent event) { //event.data =
-         * dragSourceItem[0].getText(); event.data = dragSourceItem[0]; }
-         * 
-         * @Override public void dragFinished(DragSourceEvent event) { // TODO only copy? if
-         * ((event.detail == DND.DROP_MOVE) && (event.doit)) { dragSourceItem[0].dispose(); }
-         * dragSourceItem[0] = null; } });
-         */
 
         Transfer[] types = new Transfer[] { TemplateTransfer.getInstance() };
         int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_LINK;
@@ -264,24 +193,9 @@ public class DataBindingDnDManager {
                     dragSourceItem[0].dispose();
                 }
 
-                Object template = TemplateTransfer.getInstance().getTemplate();
-                if ((template != null) && (!dragSourceItem[0].isDisposed())) {
-                    if (template instanceof DataBindingTemplate) {
-                        DataBindingTemplate dataBindingTemplate = (DataBindingTemplate) template;
-                        Part targetWidget = dataBindingTemplate.getTargetWidget();
-                        if (targetWidget == null) {
-                            return;
-                        }
-                        String viewModelInfo = makeViewModelInfo(dragSourceItem[0]);
-                        String str = targetWidget.getPropertyValue(BuilderConstants.ATTRIBUTE_DATA_BIND);
-                        if ((str == null) || (str.isEmpty())) {
-                            return;
-                        }
-                        //TODO add -> refresh
-                        String[] bindingValue = str.split(":");
-                        addInfoTableItem(dataBindingView, targetWidget, viewModelInfo,
-                                         targetWidget.getIdPropertyValue(), bindingValue[0]);
-                    }
+                DataBindingPage dataBindingPage = getDataBindingPage(dataBindingView);
+                if (dataBindingPage != null) {
+                       dataBindingPage.refresh();
                 }
 
                 dragSourceItem[0] = null;
@@ -338,7 +252,7 @@ public class DataBindingDnDManager {
                     }
                 }
             }
-
+            
             public void drop(DropTargetEvent event) {
                 if (event.data == null) {
                     event.detail = DND.DROP_NONE;
@@ -354,25 +268,9 @@ public class DataBindingDnDManager {
 
                 if (TreeItemTransfer.getInstance().isSupportedType(event.currentDataType)) {
                     if (event.item == null) {
-                        Tree viewModelTree = (Tree) ((DropTarget) event.getSource()).getControl();
-                        TreeItem viewModelItem = new TreeItem(viewModelTree, 0);
-                        TreeItemData treeData = new TreeItemData();
-                        TreeItemData treeItemData = new TreeItemData();
-
-                        int idIndex = 0;
-
-                        while (!canSetTreeItemName(viewModelTree, "model" + idIndex)) {
-                               idIndex++;
-                        }
-                        viewModelItem.setText("model" + idIndex);
-
-                        treeItemData.setOriginModelName(viewModelItem.getText());
-                        viewModelItem.setData("TREEITEMDATA", treeItemData);
-                        viewModelTree.setData("TREEITEMDATA", treeData);
-                        viewModelTree.select(viewModelItem);
-
-                        makeViewModel(viewModelItem, sourceItem);
-
+                       BindingData dataSource = dataBindingPage.getDataSource(sourceItem.getText());
+                       dataBindingPage.setDataSourceToViewModel(dataSource, null);
+                       
                         event.detail = DND.DROP_COPY;
 
                     } else if ((event.item != null)
@@ -382,12 +280,10 @@ public class DataBindingDnDManager {
                         if (parent != null) {
                             event.detail = DND.DROP_NONE;
                         } else {
-                            TreeItem[] viewModelItems = item.getItems();
-                            for (int i = 0; i < viewModelItems.length; i++) {
-                                viewModelItems[i].dispose();
-                            }
-                            // item.clearAll(true);
-                            makeViewModel(item, sourceItem);
+//                           
+                               BindingData viewModel = dataBindingPage.getViewModel(item.getText());
+                               BindingData dataSource = dataBindingPage.getDataSource(sourceItem.getText());
+                               dataBindingPage.setDataSourceToViewModel(dataSource, viewModel);
 
                             event.detail = DND.DROP_COPY;
                         }
@@ -454,62 +350,9 @@ public class DataBindingDnDManager {
                 } else {
                     event.detail = DND.DROP_NONE;
                 }
-
-                dataBindingPage.updateModel();
             }
         });
     }
-    
-    /**
-     * Returns TRUE if tree item name can be set.
-     * @param viewModelTree 
-     *                           
-     * @param itemName
-     *            a tree item name
-     * @return true if tree item name can be set
-     */
-    private static boolean canSetTreeItemName(Tree tree, String itemName) {
-       TreeItem[] childItems = tree.getItems();
-       for (TreeItem childItem : childItems) {
-               TreeItemData treeItemData = (TreeItemData) childItem.getData("TREEITEMDATA");
-               if ((treeItemData != null) && (treeItemData.getOriginModelName().equals(itemName))) {
-                       return false;
-               }
-       }
-       return true;
-    }
-
-    private static void makeViewModel(TreeItem item, TreeItem sourceItem) {
-        TreeItemData treeItemData = (TreeItemData) item.getData("TREEITEMDATA");
-        TreeItemData sourceTreeItemData = (TreeItemData) sourceItem.getData("TREEITEMDATA");
-
-        item.setText(treeItemData.getOriginModelName() + " (" + sourceItem.getText() + ")");
-        BindingData sourceDataModel = sourceTreeItemData.getModel();
-        BindingData dataModel = makeDataModel(sourceDataModel);
-        // item.setData("DATAMODEL", dataModel);
-        treeItemData.setModel(dataModel);
-        item.setData("TREEITEMDATA", treeItemData);
-
-        TreeItem[] items = sourceItem.getItems();
-        for (int i = 0; i < items.length; i++) {
-            TreeItem newItem = new TreeItem(item, SWT.NONE);
-            setValuesOfItem(newItem, items[i]);
-        }
-    }
-
-    private static BindingData makeDataModel(BindingData sourceDataModel) {
-        if (sourceDataModel == null) {
-            return null;
-        }
-
-        BindingData dataModel = new BindingData();
-
-        dataModel.setSourceName(sourceDataModel.getSourceName());
-        dataModel.setModelType(sourceDataModel.getModelType());
-
-        return dataModel;
-
-    }
 
     /**
      * Adds a drag and drop listener to the SetSourceDialog tree.
@@ -644,67 +487,6 @@ public class DataBindingDnDManager {
 
     }
 
-    /*
-     * public static void addDragAndDropListenerToTree(final Tree tree) { Transfer[] types = new
-     * Transfer[] { TreeItemTransfer.getInstance() }; int operations = DND.DROP_MOVE | DND.DROP_COPY
-     * | DND.DROP_LINK;
-     * 
-     * final DragSource source = new DragSource(tree, operations); source.setTransfer(types); final
-     * TreeItem[] dragSourceItem = new TreeItem[1]; source.addDragListener(new DragSourceListener()
-     * {
-     * 
-     * @Override public void dragStart(DragSourceEvent event) { TreeItem[] selection =
-     * tree.getSelection(); if (selection.length == 1) { //> 0 && selection[0].getItemCount() == 0)
-     * { event.doit = true; dragSourceItem[0] = selection[0]; } else { event.doit = false; }
-     * 
-     * }
-     * 
-     * @Override public void dragSetData(DragSourceEvent event) { event.data = dragSourceItem[0]; }
-     * 
-     * @Override public void dragFinished(DragSourceEvent event) { if ((event.detail ==
-     * DND.DROP_MOVE) && (event.doit)) { dragSourceItem[0].dispose(); } dragSourceItem[0] = null; }
-     * });
-     * 
-     * DropTarget target = new DropTarget(tree, operations); target.setTransfer(types);
-     * target.addDropListener(new DropTargetAdapter() { public void dragOver(DropTargetEvent event)
-     * { event.feedback = DND.FEEDBACK_EXPAND | DND.FEEDBACK_SCROLL; if (event.item != null) {
-     * TreeItem item = (TreeItem) event.item; Point pt = tree.getDisplay().map(null, tree, event.x,
-     * event.y); Rectangle bounds = item.getBounds(); if (pt.y < bounds.y + bounds.height / 3) {
-     * event.feedback |= DND.FEEDBACK_INSERT_BEFORE; } else if (pt.y > bounds.y + 2 * bounds.height
-     * / 3) { event.feedback |= DND.FEEDBACK_INSERT_AFTER; } else { event.feedback |=
-     * DND.FEEDBACK_SELECT; } } }
-     * 
-     * public void drop(DropTargetEvent event) { if (event.data == null) { event.detail =
-     * DND.DROP_NONE; return; }
-     * 
-     * TreeItem sourceItem = (TreeItem) event.data;
-     * 
-     * if (sourceItem.equals((TreeItem)event.item)) {//|| (sourceItem.getData("VIEWMODELNODE") !=
-     * null) ) { event.detail = DND.DROP_NONE; return; }
-     * 
-     * if (event.item == null) { TreeItem newItem = new TreeItem(tree, SWT.NONE);
-     * setValuesOfItem(newItem, sourceItem); } else { TreeItem item = (TreeItem) event.item; Point
-     * pt = tree.getDisplay().map(null, tree, event.x, event.y); Rectangle bounds =
-     * item.getBounds(); TreeItem parent = item.getParentItem(); if (parent != null) { TreeItem[]
-     * items = parent.getItems(); int index = 0; for (int i = 0; i < items.length; i++) { if
-     * (items[i] == item) { index = i; break; } } if (pt.y < bounds.y + bounds.height / 3) {
-     * TreeItem newItem = new TreeItem(parent, SWT.NONE, index); setValuesOfItem(newItem,
-     * sourceItem); } else if (pt.y > bounds.y + 2 * bounds.height / 3) { TreeItem newItem = new
-     * TreeItem(parent, SWT.NONE, index + 1); setValuesOfItem(newItem, sourceItem); } else {
-     * TreeItem newItem = new TreeItem(item, SWT.NONE); setValuesOfItem(newItem, sourceItem); }
-     * 
-     * } else { TreeItem[] items = tree.getItems(); int index = 0; for (int i = 0; i < items.length;
-     * i++) { if (items[i] == item) { index = i; break; } } if (pt.y < bounds.y + bounds.height / 3)
-     * { TreeItem newItem = new TreeItem(tree, SWT.NONE, index); setValuesOfItem(newItem,
-     * sourceItem); } else if (pt.y > bounds.y + 2 * bounds.height / 3) { TreeItem newItem = new
-     * TreeItem(tree, SWT.NONE, index + 1); setValuesOfItem(newItem, sourceItem); } else { TreeItem
-     * newItem = new TreeItem(item, SWT.NONE); setValuesOfItem(newItem, sourceItem); } }
-     * 
-     * } } });
-     * 
-     * }
-     */
-
     private static TreeItem getRootItemFromTree(TreeItem item) {
 
         while (item.getParentItem() != null) {
@@ -757,6 +539,10 @@ public class DataBindingDnDManager {
                 } else {
                     item.setText(treeItem.getText());
                 }
+                
+                if (sourceTreeItemData == null) {
+                       return;
+                }
 
                 treeItemData.setObjectValue(sourceTreeItemData.getObjectValue());
 
@@ -787,25 +573,4 @@ public class DataBindingDnDManager {
         }
         return (DataBindingPage) page;
     }
-
-    //TODO add -> refresh
-    private static void addInfoTableItem(DataBindingView dataBindingView, Part part,
-                                         String modelName, String widgetName, String bindingInfoStr) {
-        DataBindingPage dataBindingPage = getDataBindingPage(dataBindingView);
-
-        dataBindingPage.removeDuplicatedTableItem(modelName, widgetName, bindingInfoStr);
-        dataBindingPage.addInfoTableItem(part, modelName, widgetName, bindingInfoStr);
-        dataBindingPage.updateModel();
-    }
-
-    /*
-     * private static TreeItem getSelectedTreeItem(DataBindingView dataBindingView) {
-     * 
-     * DataBindingPage dataBindingPage = getDataBindingPage(dataBindingView); Tree tree =
-     * dataBindingPage.getViewModelTree(); TreeItem[] treeItem = tree.getSelection();
-     * 
-     * if ((treeItem.length == 0) || (treeItem[0].getParentItem() == null)) { return null; }
-     * 
-     * return treeItem[0]; }
-     */
 }
index 2bb4165..ca28208 100644 (file)
@@ -29,6 +29,7 @@ import java.util.List;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.gef.EditPart;
+import org.eclipse.gef.commands.Command;
 import org.eclipse.gef.commands.CommandStack;
 import org.eclipse.gef.ui.actions.ActionRegistry;
 import org.eclipse.gef.ui.actions.UpdateAction;
@@ -72,6 +73,8 @@ import org.eclipse.ui.actions.ActionFactory;
 import org.eclipse.ui.part.Page;
 import org.eclipse.ui.views.contentoutline.ContentOutline;
 import org.tizen.webuibuilder.BuilderConstants;
+import org.tizen.webuibuilder.gef.commands.SetDataSourceCommand;
+import org.tizen.webuibuilder.gef.commands.SetViewModelCommand;
 import org.tizen.webuibuilder.model.Part;
 import org.tizen.webuibuilder.model.page.PageData;
 import org.tizen.webuibuilder.model.project.ProjectManager;
@@ -85,12 +88,15 @@ import org.tizen.webuibuilder.ui.views.databinding.actions.RemoveDataSourceActio
 import org.tizen.webuibuilder.ui.views.databinding.actions.RemoveViewModelAction;
 import org.tizen.webuibuilder.ui.views.databinding.actions.SetTargetAction;
 import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSetEvent;
 import org.tizen.webuibuilder.ui.views.databinding.model.BindingObject;
+import org.tizen.webuibuilder.ui.views.databinding.model.IBindingDataSetListener;
 import org.tizen.webuibuilder.ui.views.databinding.model.TreeItemData;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSetEvent.BindingDataSetEventType;
 import org.tizen.webuibuilder.utility.ResourceManager;
 
 
-public class DataBindingPage extends Page {
+public class DataBindingPage extends Page implements IBindingDataSetListener {
 
     private IProject project;
     private ProjectManager projectManager;
@@ -143,34 +149,10 @@ public class DataBindingPage extends Page {
             updateViewModelToAppManager(viewModelTree);
 
             //FIXME
-            pageDesigner.getAppManager().save();
+            //pageDesigner.getAppManager().save();
         }
     }
 
-    // private void updateDataSource(Tree tree) {
-    // TreeItem[] dataSourceTreeItems = tree.getItems();
-    //
-    // for (TreeItem dataSourceTreeItem : dataSourceTreeItems) {
-    // if (dataSourceTreeItem.getParentItem() == null) {
-    //
-    // BindingData dataModel = (BindingData)
-    // dataSourceTreeItem.getData("DATAMODEL");
-    // if (dataModel == null) {
-    // dataModel = makeDataModel(dataSourceTreeItem);
-    // } else {
-    // dataModel.clearDataSourceObjects();
-    // }
-    // dataModel.setSourceName(dataSourceTreeItem.getText());
-    //
-    // makeObservableObjectsFromDataSourceTreeItem(dataModel,
-    // dataSourceTreeItem);
-    //
-    // projectManager.getDefaultAppManager().addDataSource(dataModel);
-    // }
-    // }
-    //
-    // }
-
     private void updateDataSourceToAppManager(Tree tree) {
         TreeItem[] dataSourceTreeItems = tree.getItems();
 
@@ -193,7 +175,9 @@ public class DataBindingPage extends Page {
 
                 makeObservableObjectsFromDataSourceTreeItem(dataModel, dataSourceTreeItem);
 
-                pageDesigner.getAppManager().addDataSource(dataModel);
+                //pageDesigner.getAppManager().addDataSource(dataModel);
+                Command command = new SetDataSourceCommand(BindingDataSetEventType.DATASOURCE_ADDED, pageDesigner.getAppManager(), dataModel, 0);
+                getCommandStackFromPageDesigner().execute(command);
             }
         }
 
@@ -285,7 +269,7 @@ public class DataBindingPage extends Page {
 
                 if (!viewModel.getDataSourceObjects().isEmpty()) {
                        makeSubTreeItem(treeItem, viewModel.getDataSourceObjects());
-                } else {
+                } else if (!viewModel.getViewModelObjects().isEmpty()) {
                        makeSubTreeItem(treeItem, viewModel.getViewModelObjects());                     
                 }
                 
@@ -328,7 +312,7 @@ public class DataBindingPage extends Page {
     }
 
     private void makeSubTreeItem(TreeItem parentTreeItem, List<BindingObject> list) {
-        if (list == null) {
+        if ((list == null) || (list.isEmpty())) {
             return;
         }
 
@@ -348,33 +332,12 @@ public class DataBindingPage extends Page {
                 treeItem.setImage(ResourceManager.getImage(BuilderConstants.ICON_DIR, "temp.png"));
             }
 
-            if (observableObject.getItems() != null) {
+            if (!observableObject.getItems().isEmpty()) {
                 makeSubTreeItem(treeItem, observableObject.getItems());
             }
         }
     }
 
-    // private void updateViewModel(Tree tree) {
-    //
-    // TreeItem[] treeItems = tree.getItems();
-    // for (TreeItem treeItem : treeItems) {
-    // if (treeItem.getParentItem() == null) {
-    //
-    // BindingData dataModel = (BindingData) treeItem.getData("DATAMODEL");
-    // if (dataModel == null) {
-    // dataModel = makeDataModel(treeItem);
-    // } else {
-    // dataModel.clearViewModelObjects();
-    // dataModel.setName((String) treeItem.getData("DEFAULTTEXT"));
-    // }
-    //
-    // makeObservableObjectsFromViewModelTreeItem(dataModel, treeItem);
-    //
-    // projectManager.getDefaultAppManager().addViewModel(dataModel);
-    // }
-    // }
-    //
-    // }
 
     private void updateViewModelToAppManager(Tree tree) {
 
@@ -403,13 +366,6 @@ public class DataBindingPage extends Page {
 
     }
 
-    // FIXME will be removed
-    // private void makeDataModelFromRemoteCall(BindingData dataModel, TreeItem
-    // treeItem) {
-    // dataModel.setURL((String) treeItem.getData("URL"));
-    // dataModel.setModelType("REMOTECALL");
-    // }
-
     // Using to make a Data Source
     private void makeObservableObjectsFromViewModelTreeItem(BindingData dataModel, TreeItem treeItem) {
         makeObservableObjectsFromTreeItem(dataModel, treeItem, true);
@@ -456,13 +412,6 @@ public class DataBindingPage extends Page {
         for (TreeItem item : subTreeItems) {
             makeObservableObjectsFromSubTreeItem(observableObject, item);
         }
-
-        /*
-         * if (subTreeItems.length == 0) { ObservableObject observableObject =
-         * makeObservableObject(treeItem, null); parent.add(observableObject); } else { for (int
-         * j=0; j<subTreeItems.length; j++) { makeObservableObjectsFromTreeItem2(parent,
-         * subTreeItems[j]); } }
-         */
     }
 
     private BindingObject makeObservableObject(TreeItem treeItem, String type) {
@@ -483,37 +432,19 @@ public class DataBindingPage extends Page {
 
     /**
      * Adds a tree item in the view model panel.
-     * 
-     * @return tree item
      */
-    public TreeItem addViewModel() {
-
-        TreeItem item = new TreeItem(viewModelTree, 0);
-        TreeItemData treeItemData = new TreeItemData();
-        TreeItemData modelTreeItemData = (TreeItemData) viewModelTree.getData("TREEITEMDATA");
-
+    public void addViewModel() {
         int idIndex = 0;
-
         while (!canSetTreeItemName(viewModelTree, "model" + idIndex)) {
                idIndex++;
         }
-
-        item.setText("model" + idIndex);
-        if (modelTreeItemData == null) {
-            modelTreeItemData = new TreeItemData();
-        }
-
-        viewModelTree.setData("TREEITEMDATA", modelTreeItemData);
-
-        treeItemData.setOriginModelName(item.getText());
-
+        
         BindingData bindingData = new BindingData();
         bindingData.setModelType("STATIC");
-        treeItemData.setModel(bindingData);
-        item.setData("TREEITEMDATA", treeItemData);
-        viewModelTree.select(item);
-        return item;
-
+        bindingData.setName("model" + idIndex);
+        
+        Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_ADDED, pageDesigner.getAppManager(), bindingData, 0);
+        getCommandStackFromPageDesigner().execute(command);
     }
     
     /**
@@ -521,48 +452,34 @@ public class DataBindingPage extends Page {
      * 
      * @return tree item
      */
-    public TreeItem addViewModelItem() {
+    private List<String> makeItemPath(TreeItem selectedItem) {
+       List<String> itemPath = new ArrayList<String>();
+       
+       TreeItem treeItem = selectedItem;
+        itemPath.add(treeItem.getText());
+        while (treeItem.getParentItem() != null) {
+               treeItem = treeItem.getParentItem();
+               itemPath.add(treeItem.getText());
+        }
+        return itemPath;
+    }
+    
+    public void addViewModelItem() {
 
-        TreeItem[] selectedItems = viewModelTree.getSelection();
+       TreeItem[] selectedItems = viewModelTree.getSelection();
 
         if (selectedItems.length != 0) {
-            TreeItem item = new TreeItem(selectedItems[0], 0);
             int idIndex = 0;
-
             while (!canSetTreeItemName(selectedItems[0], "viewModelItem" + idIndex)) {
                idIndex++;
             }
-            
-            TreeItem parentItem = selectedItems[0];
-            if (parentItem != null) { 
-               TreeItemData treeParentItemData = (TreeItemData) parentItem.getData("TREEITEMDATA");
-                if (treeParentItemData != null) {
-                    if (treeParentItemData.getObjectType() == null) {
-                       treeParentItemData.setObjectType("JSONObject");
-                       parentItem.setData("TREEITEMDATA", treeParentItemData);
-
-                    } else {
-                       //do nothing
-                    }
-                    
-                } else {
-                       treeParentItemData = new TreeItemData();
-                       treeParentItemData.setObjectType("JSONObject");
-                       parentItem.setData("TREEITEMDATA", treeParentItemData);
-                }
-            }
-            
-//            TreeItemData treeItemData = new TreeItemData();
-//            item.setData("TREEITEMDATA", treeItemData);
-            
-
-            item.setText("viewModelItem"+ idIndex);
-            viewModelTree.showItem(item);
-            return item;
-        } else {
-               return null;
-        }
 
+            List<String> itemPath = makeItemPath(selectedItems[0]);                                    
+            BindingData bindingData = pageDesigner.getAppManager().getViewModel(itemPath.get(itemPath.size()-1));
+
+            Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_ITEM_ADDED, pageDesigner.getAppManager(), bindingData, itemPath, "viewModelItem"+ idIndex, "",0);
+            getCommandStackFromPageDesigner().execute(command);
+        } 
     }
     
     /**
@@ -618,100 +535,104 @@ public class DataBindingPage extends Page {
     /**
      * Adds a tree item in the data source panel.
      * 
-     * @return tree item
      */
-    public TreeItem addDataSource() {
-        TreeItem item = new TreeItem(dataSourceTree, 0);
-        TreeItemData treeItemData = new TreeItemData();
-        TreeItemData sourceTreeItemData = (TreeItemData) dataSourceTree.getData("TREEITEMDATA");
+    public void addDataSource() {
+       int idIndex = 0;
 
-        int idIndex = 0;
-
-        while (!canSetTreeItemName(dataSourceTree, "source" + idIndex)) {
-               idIndex++;
-        }
-
-        item.setText("source" + idIndex);
-        if (sourceTreeItemData == null) {
-            sourceTreeItemData = new TreeItemData();
-        }
-
-        dataSourceTree.setData("TREEITEMDATA", sourceTreeItemData);
-
-        BindingData bindingData = new BindingData();
-        bindingData.setModelType("STATIC");
-        treeItemData.setModel(bindingData);
-        item.setData("TREEITEMDATA", treeItemData);
+       while (!canSetTreeItemName(dataSourceTree, "source" + idIndex)) {
+               idIndex++;
+       }
 
-        dataSourceTree.select(item);
-        return item;
+       BindingData bindingData = new BindingData();
+       bindingData.setModelType("STATIC");
+       bindingData.setSourceName("source" + idIndex);
+       
+       Command command = new SetDataSourceCommand(BindingDataSetEventType.DATASOURCE_ADDED, pageDesigner.getAppManager(), bindingData, 0);
+        getCommandStackFromPageDesigner().execute(command);
     }
+    
+    public void editDataSource(BindingData dataSource) {
+       Command command = new SetDataSourceCommand(BindingDataSetEventType.DATASOURCE_CHANGED, pageDesigner.getAppManager(), dataSource, 0);
+        getCommandStackFromPageDesigner().execute(command);
+       }
+    
+    public void editViewModel(BindingData viewModel) {
+       Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_CHANGED, pageDesigner.getAppManager(), viewModel, 0);
+        getCommandStackFromPageDesigner().execute(command);
+       }
+    
+    public void setDataSourceToViewModel(BindingData dataSource, BindingData viewModel) {
+       BindingData bindingData;
+       if (viewModel == null) {
+               bindingData = new BindingData();
+               int idIndex = 0;
+            while (!canSetTreeItemName(viewModelTree, "model" + idIndex)) {
+               idIndex++;
+            }
+            bindingData.setName("model" + idIndex);
+            bindingData.setSourceName(dataSource.getSourceName());
+               bindingData.setModelType(dataSource.getModelType());
+               bindingData.setUpdateOnStartup(dataSource.getUpdateOnStartup());
+               bindingData.addAllHandlerInfos(dataSource.getHandlerInfos());
+               bindingData.addAllViewModelObjects(dataSource.getDataSourceObjects());
+               
+               Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_ADDED, pageDesigner.getAppManager(), bindingData, 0);
+            getCommandStackFromPageDesigner().execute(command);
+       } else {
+               BindingData oldBindingData = viewModel.clone();
+               bindingData = viewModel; 
+               bindingData.setSourceName(dataSource.getSourceName());
+               bindingData.setModelType(dataSource.getModelType());
+               bindingData.setUpdateOnStartup(dataSource.getUpdateOnStartup());
+               bindingData.addAllHandlerInfos(dataSource.getHandlerInfos());
+               bindingData.addAllViewModelObjects(dataSource.getDataSourceObjects());
+               
+               Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_SET_DATASOURCE, pageDesigner.getAppManager(), bindingData, oldBindingData, 0);
+            getCommandStackFromPageDesigner().execute(command);
+       }
+       
+       }
 
     /**
      * Removes a tree item in the data source panel.
      */
     public void removeDataSource() {
-        removeSelectedTreeItem(dataSourceTree);
+        TreeItem[] treeItems = dataSourceTree.getSelection();
+         for (TreeItem treeItem : treeItems) {
+                BindingData bindingData = pageDesigner.getAppManager().getDataSource(treeItem.getText());
+                Command command = new SetDataSourceCommand(BindingDataSetEventType.DATASOURCE_REMOVED, pageDesigner.getAppManager(), bindingData, 0);
+             getCommandStackFromPageDesigner().execute(command);
+         }
+    }
+    
+    public BindingData getDataSource(String name) {
+       return pageDesigner.getAppManager().getDataSource(name);
+    }
+    
+    public BindingData getViewModel(String name) {
+       return pageDesigner.getAppManager().getViewModel(name);
     }
 
     /**
      * Removes a tree item in the view model panel.
      */
     public void removeViewModel() {
-        TreeItem[] treeItems = viewModelTree.getSelection();
+       TreeItem[] treeItems = viewModelTree.getSelection();
         for (TreeItem treeItem : treeItems) {
-            String str = makePropertyValue(treeItem);
-            TableItem[] tableItems = infoTableViewer.getTable().getItems();
-
-            for (TableItem tableItem : tableItems) {
-                if (tableItem.getText(0).equals(str)) {
-                    removeInfoTableItem((Part) tableItem.getData("PART"), tableItem,
-                                        (TableEditor) tableItem.getData("TABLEEDITOR"));
-                }
-            }
-        }
-        removeSelectedTreeItem(viewModelTree);
-    }
-
-    private void removeSelectedTreeItem(Tree tree) {
-        TreeItem[] treeItems = tree.getSelection();
-        for (TreeItem treeItem : treeItems) {
-               if (treeItem.getParentItem() != null) {
-                       if (treeItem.getParentItem().getItems().length == 1) { 
-                               TreeItemData treeItemData = (TreeItemData)treeItem.getParentItem().getData("TREEITEMDATA");
-                               
-                               if (treeItemData != null) {
-                                       treeItemData.setObjectType(null);
-                                       treeItem.getParentItem().setData("TREEITEMDATA", treeItemData);
-                               }
-                       }
+               if (treeItem.getParentItem() == null) {
+                               BindingData bindingData = pageDesigner.getAppManager().getViewModel(treeItem.getText());
+                               Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_REMOVED, pageDesigner.getAppManager(), bindingData, 0);
+                               getCommandStackFromPageDesigner().execute(command);
+               } else {
+                       List<String> itemPath = makeItemPath(treeItem);
+                       BindingData bindingData = pageDesigner.getAppManager().getViewModel(itemPath.get(itemPath.size()-1));
+                       Command command = new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_ITEM_REMOVED, pageDesigner.getAppManager(), bindingData, itemPath, treeItem.getText(), "",0);
+                getCommandStackFromPageDesigner().execute(command);
                }
-            treeItem.dispose();
-        }
-    }
-
-    private String makePropertyValue(TreeItem treeItem) {
-        String str = "";
-        while ((treeItem.getParentItem() != null)) {
-            if (str == "") {
-                str = treeItem.getText();
-            } else {
-                str = treeItem.getText() + "." + str;
-            }
-            treeItem = treeItem.getParentItem();
+               
         }
-        str = treeItem.getData("DEFAULTTEXT") + "." + str;
-        return str;
     }
 
-    // FIXME
-    /*
-     * public TreeItem addRemoteCallViewModel() { TreeItem item = new TreeItem(viewModelTree, 0);
-     * item.setText("model" + viewModelCount++); //item.setData("SOURCETYPE", "REMOTECALL");
-     * 
-     * return item; }
-     */
-
     /**
      * Check the duplicated binding information.
      * 
@@ -860,6 +781,12 @@ public class DataBindingPage extends Page {
 
         refresh();
     }
+    
+    @Override
+    public void dispose() {
+        pageDesigner.getAppManager().getBindingDataSet().removePageListener(this);
+        super.dispose();
+    }
 
     private void makeBindingInfoPanel(SashForm sashForm) {
         Composite dataInfoPanel = new Composite(sashForm, SWT.NONE);
@@ -1049,10 +976,15 @@ public class DataBindingPage extends Page {
                         public void keyPressed(KeyEvent event) {
                             switch (event.keyCode) {
                                 case SWT.CR:
+                                       treeItem.setText(text.getText());
+                                    text.dispose();
+                                    break;
                                 case SWT.KEYPAD_CR:
                                     // Enter hit--set the text into the tree and
                                     // drop through
                                     treeItem.setText(text.getText());
+                                    text.dispose();
+                                    break;
                                 case SWT.ESC:
                                     // End editing session
                                     text.dispose();
@@ -1105,11 +1037,17 @@ public class DataBindingPage extends Page {
                         public void keyPressed(KeyEvent event) {
                             switch (event.keyCode) {
                                 case SWT.CR:
-                                case SWT.KEYPAD_CR:
                                     // Enter hit--set the text into the tree and
                                     // drop through
+                                       item.setText(text.getText());
+                                    updateModel();
+                                    text.dispose();
+                                    break;
+                                case SWT.KEYPAD_CR:
                                     item.setText(text.getText());
                                     updateModel();
+                                    text.dispose();
+                                    break;
                                 case SWT.ESC:
                                     // End editing session
                                     text.dispose();
@@ -1194,9 +1132,12 @@ public class DataBindingPage extends Page {
         this.pageDesigner = pageDesigner;
         this.delegater = new DataBindingEventDelegater(getCommandStackFromPageDesigner(), this);
         
-        this.undoHandler = getActionRegistryFromPageDesigner().getAction(ActionFactory.UNDO.getId());
-        this.redoHandler = getActionRegistryFromPageDesigner().getAction(ActionFactory.REDO.getId());
+        if (getActionRegistryFromPageDesigner() != null) {
+               this.undoHandler = getActionRegistryFromPageDesigner().getAction(ActionFactory.UNDO.getId());
+               this.redoHandler = getActionRegistryFromPageDesigner().getAction(ActionFactory.REDO.getId());
+        }
         
+        pageDesigner.getAppManager().getBindingDataSet().addBindingDataListener(this);
         
     }
 
@@ -1243,5 +1184,60 @@ public class DataBindingPage extends Page {
         actionBars.setGlobalActionHandler(ActionFactory.REDO.getId(), redoHandler);
         actionBars.updateActionBars();
     }
+
+       @Override
+       public void allDataSourcesReseted(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void dataSourceAdded(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void dataSourceRemoved(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void dataSourceRenamed(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void dataSourceMoved(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void allViewModelReseted(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void viewModelAdded(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void viewModelRemoved(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void viewModelRenamed(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void viewModelMoved(BindingDataSetEvent e) {
+               refresh();
+       }
+
+       @Override
+       public void viewModelChanged(BindingDataSetEvent e) {
+               refresh();
+       }
        
 }
\ No newline at end of file
index 0e5610e..863b95e 100644 (file)
@@ -70,6 +70,7 @@ import org.json.simple.JSONObject;
 import org.json.simple.JSONValue;
 import org.tizen.webuibuilder.BuilderConstants;
 import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingObject;
 import org.tizen.webuibuilder.ui.views.databinding.model.TreeItemData;
 import org.tizen.webuibuilder.utility.ResourceManager;
 import org.tizen.webuibuilder.utility.ResourceUtil;
@@ -80,6 +81,7 @@ public class SetSourceDialog extends Dialog {
     protected DataBindingView view;
     private Shell shell;
     private TreeItem dataSourceTreeItem;
+    private    BindingData dataSource;
 
     private String message;
     private String importMode = null;
@@ -136,7 +138,7 @@ public class SetSourceDialog extends Dialog {
      * 
      * @return message
      */
-    public String open() {
+    public BindingData open() {
         // Create the dialog window
         Shell shell = new Shell(getParent(), SWT.TITLE | SWT.CLOSE | SWT.BORDER | SWT.RESIZE);
         shell.setText(getText());
@@ -149,8 +151,8 @@ public class SetSourceDialog extends Dialog {
                 display.sleep();
             }
         }
-        // Return the entered value, or null
-        return message;
+
+        return dataSource;
     }
 
     private TreeItem getSelectedTreeItem() {
@@ -233,36 +235,36 @@ public class SetSourceDialog extends Dialog {
         okButton.addSelectionListener(new SelectionAdapter() {
             public void widgetSelected(SelectionEvent event) {
 
-                BindingData dataModel = new BindingData();
-                dataModel.setModelType(importMode);
+                BindingData bindingData = new BindingData();
+                bindingData.setModelType(importMode);
                 if ((importMode != null) && (importMode.equals("REMOTECALL"))) {
-                    dataModel.setURL(urlText.getText());
-                    dataModel.setMethod(methodSettingCombo.getText());
-                    dataModel.setProxy(proxySettingCombo.getText());
-                    dataModel.setQuery(queryText.getText());
-                    dataModel.setSourceType(sourceTypeText.getText());
-                    dataModel.setTimeout(timeoutText.getText());
+                    bindingData.setURL(urlText.getText());
+                    bindingData.setMethod(methodSettingCombo.getText());
+                    bindingData.setProxy(proxySettingCombo.getText());
+                    bindingData.setQuery(queryText.getText());
+                    bindingData.setSourceType(sourceTypeText.getText());
+                    bindingData.setTimeout(timeoutText.getText());
 
                     // TODO
                     if (dialogTree.getChildren() == null) {
                         makeDataModel(settingComposite);
                     }
                 } else if ((importMode != null) && (importMode.equals("STATIC"))) {
-                    dataModel.setJsonData(removeSourceBeautifier(staticSourceStyledText.getText()));
-                    dataModel.setStaticFilePath(staticFilePath);
+                    bindingData.setJsonData(removeSourceBeautifier(staticSourceStyledText.getText()));
+                    bindingData.setStaticFilePath(staticFilePath);
                 } else if ((importMode != null) && (importMode.equals("RUNTIMEAPI"))) {
-                    dataModel.setRuntimeApiName(runtimeApiName);
+                    bindingData.setRuntimeApiName(runtimeApiName);
                 }
 
-                dataModel.setUpdateOnStartup(updateOnStartUp);
-                dataModel.setSourceName(dataSourceTreeItem.getText());
+                bindingData.setUpdateOnStartup(updateOnStartUp);
+                bindingData.setSourceName(dataSourceTreeItem.getText());
 
                 TreeItemData treeItemData =
                         (TreeItemData) dataSourceTreeItem.getData("TREEITEMDATA");
                 if (treeItemData == null) {
                     treeItemData = new TreeItemData();
                 }
-                treeItemData.setModel(dataModel);
+                treeItemData.setModel(bindingData);
                 dataSourceTreeItem.setData("TREEITEMDATA", treeItemData);
                 /*
                  * //FIXME dataSourceTreeItem.setData("SOURCETYPE", importMode); if ((importMode !=
@@ -270,12 +272,14 @@ public class SetSourceDialog extends Dialog {
                  * input); } /////////////////
                  */
 
-                clearTree(dataSourceTreeItem);
+                //clearTree(dataSourceTreeItem);
 
                 TreeItem[] treeItems = dialogTree.getItems();
-                makeDataSourceFromDialogTree(dataSourceTreeItem, treeItems);
+//                makeDataSourceFromDialogTree(dataSourceTreeItem, treeItems);
+                makeDataSourceFromDialogTree(bindingData, treeItems);
 
-                message = "OK";
+                //message = "OK";
+                dataSource = bindingData;
                 shell.close();
             }
         });
@@ -397,6 +401,55 @@ public class SetSourceDialog extends Dialog {
             }
         }
     }
+    
+    
+    private void makeDataSourceFromDialogTree(BindingData dataModel, TreeItem[] treeItems) {
+
+        if (treeItems.length != 0) {
+            for (TreeItem treeItem : treeItems) {
+                TreeItemData sourceTreeItemData = (TreeItemData) treeItem.getData("TREEITEMDATA");
+                if (sourceTreeItemData == null) {
+                    return;
+                }
+                
+                BindingObject observableObject = makeObservableObject(treeItem);
+
+                TreeItem[] treeSubItems = treeItem.getItems();
+                for (TreeItem treeSubItem : treeSubItems) {
+                       makeObservableObjectsFromTreeItem(observableObject, treeSubItem);
+                }
+                
+                dataModel.addDataSourceObjects(observableObject);
+            }
+            
+        }
+    }
+    
+    private void makeObservableObjectsFromTreeItem(BindingObject parent, TreeItem treeItem) {
+        TreeItem[] subTreeItems = treeItem.getItems();
+        
+        BindingObject observableObject = makeObservableObject(treeItem);
+        parent.add(observableObject);
+
+        for (TreeItem item : subTreeItems) {
+               makeObservableObjectsFromTreeItem(observableObject, item);
+        }
+    }
+    
+    private BindingObject makeObservableObject(TreeItem treeItem) {
+       TreeItemData treeItemData = (TreeItemData)treeItem.getData("TREEITEMDATA"); 
+        String treeItemValue;
+        String treeItemType;
+        if (treeItemData == null) {
+            treeItemValue = "";
+            treeItemType = "";
+        } else {
+            treeItemValue = treeItemData.getObjectValue();
+            treeItemType = treeItemData.getObjectType();
+        }
+        return new BindingObject(treeItem.getText(), treeItemValue, treeItemType);
+    }
+    
 
     private void makeDialogTreeFromDataSource(Tree dialogTree, TreeItem parentItem,
                                               TreeItem[] dataModelItems) {
@@ -419,6 +472,10 @@ public class SetSourceDialog extends Dialog {
                 } else {
                     item.setText(treeItem.getText());
                 }
+                
+                if (sourceTreeItemData == null) {
+                       return;
+                }
 
                 treeItemData.setObjectValue(sourceTreeItemData.getObjectValue());
 
@@ -1390,6 +1447,7 @@ public class SetSourceDialog extends Dialog {
         BufferedReader rd = new BufferedReader(new FileReader(file));
         String jsonText = readAll(rd);
         JSONObject json = (JSONObject) JSONValue.parse(jsonText);
+        rd.close();
         return json;
 
     }
@@ -1618,7 +1676,7 @@ public class SetSourceDialog extends Dialog {
     }
 
     private void makeDataModelFromUrl(String text, TreeItem parentTreeItem) {
-        if ((text == null) || (text == "")) {
+        if ((text == null) || (text.equals(""))) {
             return;
         }
 
@@ -1634,7 +1692,7 @@ public class SetSourceDialog extends Dialog {
     }
 
     private void makeJsonFromFile(String file, TreeItem parentTreeItem) {
-        if ((file == null) || (file == "")) {
+        if ((file == null) || (file.equals(""))) {
             return;
         }
 
@@ -1646,9 +1704,8 @@ public class SetSourceDialog extends Dialog {
         }
     }
 
-    // FIXME
     private void makeDataModelFromFile(String file, TreeItem parentTreeItem) {
-        if ((file == null) || (file == "")) {
+        if ((file == null) || (file.equals(""))) {
             return;
         }
 
index 483bfbb..96bdecf 100644 (file)
@@ -69,6 +69,5 @@ public class AddDataSourceAction extends SelectionAction {
         }
         DataBindingPage dataBindingPage = (DataBindingPage) page;
         dataBindingPage.addDataSource();
-        dataBindingPage.updateModel();
     }
 }
index ea98704..40855b6 100644 (file)
@@ -70,7 +70,6 @@ public class AddViewModelAction extends SelectionAction {
         }
         DataBindingPage dataBindingPage = (DataBindingPage) page;
         dataBindingPage.addViewModel();
-        dataBindingPage.updateModel();
     }
 
 }
index b56416c..4213077 100644 (file)
@@ -68,9 +68,7 @@ public class AddViewModelItemAction extends SelectionAction {
             return;
         }
         DataBindingPage dataBindingPage = (DataBindingPage) page;
-        if (dataBindingPage.addViewModelItem() != null) {
-               dataBindingPage.updateModel();
-        }
+        dataBindingPage.addViewModelItem();
     }
 
 }
index ae27a2e..ef73a93 100644 (file)
@@ -33,6 +33,7 @@ import org.eclipse.ui.part.IPage;
 import org.tizen.webuibuilder.ui.views.databinding.DataBindingPage;
 import org.tizen.webuibuilder.ui.views.databinding.DataBindingView;
 import org.tizen.webuibuilder.ui.views.databinding.SetSourceDialog;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingData;
 
 
 public class EditDataSourceAction extends SelectionAction {
@@ -111,17 +112,19 @@ public class EditDataSourceAction extends SelectionAction {
          * }
          */
 
+        
+        //TODO remove treeItem
         SetSourceDialog dlg =
                 new SetSourceDialog(Display.getCurrent().getActiveShell(), view, treeItem);
-        String message = dlg.open();
+        BindingData dataSource = dlg.open();
 
-        if (message != null) {
+        if (dataSource != null) {
             IPage page = view.getCurrentPage();
             if (!(page instanceof DataBindingPage)) {
                 return;
             }
             DataBindingPage dataBindingPage = (DataBindingPage) page;
-            dataBindingPage.updateModel();
+            dataBindingPage.editDataSource(dataSource);
         }
     }
 
index c65cd06..b3a9e82 100644 (file)
@@ -233,7 +233,7 @@ public class GetSourceAction extends SelectionAction {
                     return;
                 }
                 DataBindingPage dataBindingPage = (DataBindingPage) page;
-                dataBindingPage.updateModel();
+                //dataBindingPage.updateModel();
             }
         }
 
index 8c99a04..db1a201 100644 (file)
@@ -66,6 +66,6 @@ public class RemoveDataSourceAction extends SelectionAction {
         }
         DataBindingPage dataBindingPage = (DataBindingPage) page;
         dataBindingPage.removeDataSource();
-        dataBindingPage.updateModel();
+        //dataBindingPage.updateModel();
     }
 }
index 6a32c8c..145bac9 100644 (file)
@@ -66,6 +66,5 @@ public class RemoveViewModelAction extends SelectionAction {
         }
         DataBindingPage dataBindingPage = (DataBindingPage) page;
         dataBindingPage.removeViewModel();
-        dataBindingPage.updateModel();
     }
 }
index aa9af1f..cd67214 100644 (file)
@@ -128,8 +128,11 @@ public class SetTargetAction extends SelectionAction {
     }
 
     private TreeItem getSelectedTreeItem() {
-
         DataBindingPage dataBindingPage = getDataBindingPage();
+        if (dataBindingPage == null) {
+               return null;
+        }
+               
         Tree tree = dataBindingPage.getViewModelTree();
         TreeItem[] treeItem = tree.getSelection();
 
@@ -145,12 +148,12 @@ public class SetTargetAction extends SelectionAction {
                                   String bindingInfoStr) {
 
         DataBindingPage dataBindingPage = getDataBindingPage();
-
-        if (dataBindingPage.checkDuplicatedTableItem(modelName, widgetName, bindingInfoStr)) {
-            dataBindingPage.addInfoTableItem(part, modelName, widgetName, bindingInfoStr);
-            dataBindingPage.updateModel();
+        if (dataBindingPage != null) {
+               if (dataBindingPage.checkDuplicatedTableItem(modelName, widgetName, bindingInfoStr)) {
+                   dataBindingPage.addInfoTableItem(part, modelName, widgetName, bindingInfoStr);
+                   //dataBindingPage.updateModel();
+               }
         }
-
     }
 
     @Override
index dcd35f0..f88dc5c 100644 (file)
@@ -144,6 +144,29 @@ public class BindingData {
     public BindingData() {
 
     }
+    
+    public BindingData clone() {
+       
+       BindingData newBindingData = new BindingData();
+       newBindingData.setName(name);
+       newBindingData.setSourceName(sourceName);
+       newBindingData.setModelType(modelType);
+       newBindingData.setUpdateOnStartup(updateOnStartup);
+       newBindingData.addAllHandlerInfos(handlerInfos);
+       newBindingData.addAllViewModelObjects(viewModelObjects);
+       newBindingData.addAllDataSourceObjects(dataSourceObjects);
+       newBindingData.setStaticFilePath(staticFilePath);
+       newBindingData.setJsonData(jsonData);
+       newBindingData.setURL(url);
+       newBindingData.setMethod(method);
+       newBindingData.setProxy(proxy);
+       newBindingData.setQuery(query);
+       newBindingData.setSourceType(sourceType);
+       newBindingData.setTimeout(timeout);
+       newBindingData.setRuntimeApiName(runtimeApiName);
+       
+               return newBindingData;
+    }
 
     /**
      * Sets a name.
@@ -369,6 +392,16 @@ public class BindingData {
     public List<BindingObject> getViewModelObjects() {
         return viewModelObjects;
     }
+    
+    public void addAllViewModelObjects(List<BindingObject> viewModels) {
+       clearViewModelObjects();
+       viewModelObjects.addAll(viewModels);
+    }
+    
+    public void addAllDataSourceObjects(List<BindingObject> dataSources) {
+       clearDataSourceObjects();
+       dataSourceObjects.addAll(dataSources);
+    }
 
     /**
      * Adds a view model object.
@@ -378,6 +411,15 @@ public class BindingData {
     public void addViewModelObjects(BindingObject observableObject) {
         viewModelObjects.add(observableObject);
     }
+    
+    /**
+     * Remove a view model object.
+     * 
+     * @param observableObject
+     */
+    public void removeViewModelObjects(BindingObject observableObject) {
+        viewModelObjects.remove(observableObject);
+    }
 
     /**
      * Clear view model objects.
@@ -441,6 +483,11 @@ public class BindingData {
     public void addHandlerInfos(String handlerInfoName, List<String> values) {
         handlerInfos.put(handlerInfoName, values);
     }
+    
+    public void addAllHandlerInfos(Map<String, List<String>> handlerInfos) {
+       clearHandlerInfos();
+        handlerInfos.putAll(handlerInfos);
+    }
 
     /**
      * Clear handler informations.
@@ -466,6 +513,15 @@ public class BindingData {
     public void addDataSourceObjects(BindingObject observableObject) {
         dataSourceObjects.add(observableObject);
     }
+    
+    /**
+     * Remove a data source object.
+     * 
+     * @param observableObject
+     */
+    public void removeDataSourceObjects(BindingObject observableObject) {
+        dataSourceObjects.remove(observableObject);
+    }
 
     /**
      * Clear data source objects.
index b829e79..51e5e01 100644 (file)
 package org.tizen.webuibuilder.ui.views.databinding.model;
 
 import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import org.eclipse.swt.widgets.Display;
+import org.tizen.webuibuilder.ui.views.databinding.model.BindingDataSetEvent.BindingDataSetEventType;
+
 
 
 public class BindingDataSet {
 
-    private List<BindingData> viewModels = new ArrayList<BindingData>();
+       private List<BindingData> viewModels = new ArrayList<BindingData>();
     private List<BindingData> dataSources = new ArrayList<BindingData>();
+    
+    private List<IBindingDataSetListener> listeners = new ArrayList<IBindingDataSetListener>();
+    private List<IBindingDataSetListener> removeListeners = new ArrayList<IBindingDataSetListener>();
+    private boolean processEventFlag = false;
 
     /**
      * Adds a view model.
      * 
-     * @param datamodel
+     * @param bindingData
      *            a view model
      */
-    public void addViewModel(BindingData datamodel) {
-        viewModels.add(datamodel);
+    public void addViewModel(BindingData bindingData) {
+        viewModels.add(bindingData);
+        
+        fireEvent(new BindingDataSetEvent(BindingDataSetEventType.VIEWMODEL_ADDED, bindingData, getNumberOfViewModels() - 1 , null));
+    }
+    
+    public BindingData getViewModel(String name) {
+       List<BindingData> viewModels = getViewModels();
+       for (BindingData viewModel : viewModels) {
+               if (viewModel.getName().equals(name)) {
+                       return viewModel;
+               }
+       }
+       
+       return null;
+    }
+    
+    public void changeViewModel(BindingDataSetEventType type, BindingData bindingData, List<String> itemPath, String childName, String value) {
+       if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_ADDED)) {
+               List<BindingData> viewModels = getViewModels();
+               for (BindingData viewModel : viewModels) {
+                       if (viewModel.equals(bindingData)) {
+                               BindingObject viewModelObject = getViewModelObject(bindingData, itemPath, childName);
+                               if (viewModelObject != null) {
+                                       addChildViewModelObject(viewModelObject, childName, value);
+                               } else if (childName != null) {
+                                       addChildViewModelObject(bindingData, childName, value);
+                               }
+                               fireEvent(new BindingDataSetEvent(BindingDataSetEventType.VIEWMODEL_CHANGED, viewModel, value, getNumberOfViewModels() - 1 , null));
+                       }
+               }
+       } else if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_REMOVED)) {
+               List<BindingData> viewModels = getViewModels();
+               for (BindingData viewModel : viewModels) {
+                       if (viewModel.equals(bindingData)) {
+                               BindingObject viewModelObject = getViewModelParentObject(bindingData, itemPath, childName);
+                               if (viewModelObject != null) {
+                                       removeChildViewModelObject(viewModelObject, childName, value);
+                               } else if (childName != null) {
+                                       removeChildViewModelObject(bindingData, childName, value);
+                               } else {
+                                       //TODO remove?
+                                       removeAllChildViewModelObject(bindingData);
+                               }
+                               fireEvent(new BindingDataSetEvent(BindingDataSetEventType.VIEWMODEL_CHANGED, viewModel, value, -1 , null));
+                       }
+               }
+       } 
     }
+    
 
-    /**
+       public void changeViewModel(BindingDataSetEventType type, BindingData bindingData, List<String> itemPath,
+                       BindingObject viewModelObject, String value) {
+       if (type.equals(BindingDataSetEventType.VIEWMODEL_ITEM_ADDED)) {
+               List<BindingData> viewModels = getViewModels();
+               for (BindingData viewModel : viewModels) {
+                       if (viewModel.equals(bindingData)) {
+                               BindingObject viewModelParentObject = getViewModelParentObject(bindingData, itemPath, viewModelObject.getName());
+                               if (viewModelParentObject != null) {
+                                       viewModelParentObject.add(viewModelObject);
+                               } else {
+                                       bindingData.addViewModelObjects(viewModelObject);
+                               }
+                       
+                       fireEvent(new BindingDataSetEvent(BindingDataSetEventType.VIEWMODEL_CHANGED, viewModel, value, getNumberOfViewModels() - 1 , null));
+                       }
+               }
+       }
+       }
+       
+       public void changeViewModel(BindingDataSetEventType type, BindingData bindingData, BindingData oldBindingData) {
+               if (type.equals(BindingDataSetEventType.VIEWMODEL_SET_DATASOURCE)) {
+                       if (oldBindingData != null) {
+                               viewModels.remove(bindingData);
+                               viewModels.add(oldBindingData);
+                       } else {
+                               viewModels.add(bindingData);
+                       }
+                       
+                       fireEvent(new BindingDataSetEvent(BindingDataSetEventType.VIEWMODEL_CHANGED, oldBindingData, null, getNumberOfViewModels() - 1 , null));
+       }
+       }
+
+    
+    private BindingObject getViewModelParentObject(BindingData bindingData,
+               List<String> itemPath, String childName) {
+       if ((itemPath == null) || (itemPath.isEmpty())) {
+               return null;
+       }
+       
+       List<String> modelPath = new ArrayList<String>();
+       for (int i=0; i<itemPath.size()-1; i++) {
+               modelPath.add(itemPath.get(i));
+       }
+       return getViewModelObject(bindingData, modelPath, childName);
+    }
+    
+    public BindingObject getViewModelObject(BindingData bindingData,
+               List<String> itemPath, String childName) {
+       List<BindingObject> viewModelObjects = bindingData.getViewModelObjects();
+       BindingObject object = null;
+       List<String> modelPath = new ArrayList<String>();
+       
+       if ((itemPath == null) || (itemPath.isEmpty())) {
+               return null;
+       }
+       
+       for (String path : itemPath) {
+               modelPath.add(path);
+       }
+       
+       if (modelPath.isEmpty()) {
+               return null;
+       }
+       
+       modelPath.remove(modelPath.size()-1);
+       for (int i=0; i<modelPath.size(); i++) {
+               String name = modelPath.get(modelPath.size()-1);
+               modelPath.remove(modelPath.size()-1);
+               for (BindingObject viewModelObject : viewModelObjects) {
+                       if (viewModelObject.getName().equals(name)) {
+                               object = getViewModelObject(viewModelObject, modelPath);
+                               if (object == null) {
+                                       object = viewModelObject;
+                               }
+                       }
+               }
+       }
+               return object;
+       }
+
+       private BindingObject getViewModelObject(BindingObject viewModelObject,
+                       List<String> itemPath) {
+               List<BindingObject> subViewModelObjects = viewModelObject.getItems();
+               BindingObject object = null;
+               
+               for (int i=0; i<itemPath.size(); i++) {
+               String name = itemPath.get(itemPath.size()-1);
+               itemPath.remove(itemPath.size()-1);
+               for (BindingObject subViewModelObject : subViewModelObjects) {
+                       if (subViewModelObject.getName().equals(name)) {
+                               object = getViewModelObject(subViewModelObject, itemPath);
+                               if (object == null) {
+                                       object = subViewModelObject;
+                               }
+                       }
+               }
+       }
+               return object;
+       }
+
+       public void removeViewModel(BindingData bindingData) {
+       List<BindingData> viewModels = getViewModels();
+       int index;
+       if (!viewModels.isEmpty()) {
+               for (index = 0; index < getNumberOfViewModels(); index++) {
+                       BindingData viewModel = viewModels.get(index);
+                       if (viewModel.getName().equals(bindingData.getName())) {
+                               break;
+                       }
+               }
+               viewModels.remove(index);
+               fireEvent(new BindingDataSetEvent(BindingDataSetEventType.VIEWMODEL_REMOVED, bindingData, index, null));
+       }
+    }
+    
+//    private void changeViewModelItem(BindingData bindingData, String parentName, String childName, String value) {
+//     
+//     List<BindingObject> viewModelObjects = bindingData.getViewModelObjects();
+//     
+//     if (viewModelObjects.isEmpty()) {
+//             bindingData.addViewModelObjects(new BindingObject(childName, value, ""));
+//     }
+//     
+//     for(BindingObject viewModelObject : viewModelObjects) {
+//             if (viewModelObject.getName().equals(parentName)) {
+//                     addChildViewModelObject(viewModelObject, childName, value);
+//             } else {
+//                     if (viewModelObject.getItems() != null) {
+//                             changeViewModelSubItem(viewModelObject, parentName, childName, value);
+//                     }
+//             }
+//     }
+//     
+//    }
+       
+//     private void changeViewModelItem(BindingObject viewModelObject, String childName, String value) {
+//     
+//             addChildViewModelObject(viewModelObject, childName, value);
+//     
+//    }
+//
+//    private void changeViewModelSubItem(BindingObject viewModelObject,
+//                     String parentName, String childName, String value) {
+//     List<BindingObject> viewModelObjects = viewModelObject.getItems();
+//     for(BindingObject bindingObject : viewModelObjects) {
+//             if (bindingObject.getName().equals(parentName)) {
+//                     addChildViewModelObject(bindingObject, childName, value);
+//             } else {
+//                     if (bindingObject.getItems() != null) {
+//                             changeViewModelSubItem(bindingObject, parentName, childName, value);
+//                     }
+//             }
+//     }
+//             
+//     }
+
+       private void addChildViewModelObject(BindingObject viewModelObject, String childName, String value) {
+               if ((viewModelObject.getType().equals("JSONObject")) || (viewModelObject.getType().equals("JSONArray"))) {
+                       
+               } else {
+                       viewModelObject.setType("JSONObject");
+               }
+               viewModelObject.add(new BindingObject(childName, value, ""));
+       }
+       
+       private void addChildViewModelObject(BindingData bindingData, String childName, String value) {
+               bindingData.addViewModelObjects(new BindingObject(childName, value, ""));
+       }
+       
+       private void removeChildViewModelObject(BindingObject viewModelObject, String childName, String value) {
+               for (BindingObject object : viewModelObject.getItems()) {
+                       if (object.getName().equals(childName)) {
+                               viewModelObject.remove(object);
+                               break;
+                       }
+               }
+               
+               if (viewModelObject.getItems().isEmpty()) {
+                       viewModelObject.setType(null);
+               }
+       }
+       
+       private void removeChildViewModelObject(BindingData bindingData, String childName, String value) {
+               for (BindingObject object : bindingData.getViewModelObjects()) {
+                       if (object.getName().equals(childName)) {
+                               bindingData.removeViewModelObjects(object);
+                               break;
+                       }
+               }
+       }
+       
+       private void removeAllChildViewModelObject(BindingData bindingData) {
+               bindingData.clearViewModelObjects();
+               bindingData.setSourceName(null);
+       }
+
+       /**
      * Clear view model.
      */
     public void clearViewModel() {
@@ -57,15 +308,48 @@ public class BindingDataSet {
     public List<BindingData> getViewModels() {
         return viewModels;
     }
+    
+    public int getNumberOfViewModels() {
+        return getViewModels().size();
+    }
 
     /**
      * Adds a data source.
      * 
-     * @param datamodel
+     * @param bindingData
      *            a data source
      */
-    public void addDataSource(BindingData datamodel) {
-        dataSources.add(datamodel);
+    public void addDataSource(BindingData bindingData) {
+        dataSources.add(bindingData);
+        
+        fireEvent(new BindingDataSetEvent(BindingDataSetEventType.DATASOURCE_ADDED, bindingData, getNumberOfDataSources() - 1 , null));
+    }
+    
+    public BindingData getDataSource(String name) {
+       List<BindingData> DataSources = getDataSources();
+       for (BindingData DataSource : DataSources) {
+               if (DataSource.getSourceName().equals(name)) {
+                       return DataSource;
+               }
+       }
+       
+       return null;
+    }
+    
+    public void removeDataSource(BindingData bindingData) {
+       
+       List<BindingData> dataSources = getDataSources();
+       int index;
+       if (!dataSources.isEmpty()) {
+               for (index = 0; index < getNumberOfDataSources(); index++) {
+                       BindingData dataSource = dataSources.get(index);
+                       if (dataSource.getSourceName().equals(bindingData.getSourceName())) {
+                               break;
+                       }
+               }
+               dataSources.remove(index);
+               fireEvent(new BindingDataSetEvent(BindingDataSetEventType.DATASOURCE_REMOVED, bindingData, index, null));
+       }
     }
 
     /**
@@ -83,5 +367,93 @@ public class BindingDataSet {
     public List<BindingData> getDataSources() {
         return dataSources;
     }
+    
+    public int getNumberOfDataSources() {
+        return getDataSources().size();
+    }
+    
+    public void removePageListener(IBindingDataSetListener listener) {
+        if (!processEventFlag) {
+            listeners.remove(listener);
+        } else {
+            removeListeners.add(listener);
+        }
+    }
+    
+    private void fireEvent(final BindingDataSetEvent bindingDataEvent) {
+        Display.getDefault().syncExec(new Runnable() {
+            @Override
+            public void run() {
+                fireEvent(bindingDataEvent, listeners);
+            }
+        });
+    }
+
+    private void fireEvent(BindingDataSetEvent bindingEvent, List<IBindingDataSetListener> listeners) {
+        Iterator<IBindingDataSetListener> itr = listeners.iterator();
+        IBindingDataSetListener listener = null;
+
+        processEventFlag = true;
+        while (itr.hasNext()) {
+            listener = itr.next();
+            switch (bindingEvent.getType()) {
+                case ALL_DATASOURCE_RESETED:
+                    listener.allDataSourcesReseted(bindingEvent);
+                    break;
+                case DATASOURCE_ADDED:
+                    listener.dataSourceAdded(bindingEvent);
+                    break;
+                case DATASOURCE_REMOVED:
+                    listener.dataSourceRemoved(bindingEvent);
+                    break;
+                case DATASOURCE_RENAMED:
+                    listener.dataSourceRenamed(bindingEvent);
+                    break;
+                case DATASOURCE_MOVED:
+                    listener.dataSourceMoved(bindingEvent);
+                    break;
+                case ALL_VIEWMODEL_RESETED:
+                    listener.allViewModelReseted(bindingEvent);
+                    break;
+                case VIEWMODEL_ADDED:
+                    listener.viewModelAdded(bindingEvent);
+                    break;
+                case VIEWMODEL_REMOVED:
+                    listener.viewModelRemoved(bindingEvent);
+                    break;
+                case VIEWMODEL_RENAMED:
+                    listener.viewModelRenamed(bindingEvent);
+                    break;
+                case VIEWMODEL_MOVED:
+                    listener.viewModelMoved(bindingEvent);
+                    break;
+                case VIEWMODEL_CHANGED:
+                    listener.viewModelChanged(bindingEvent);
+                    break;
+                default:
+                    assert (false);
+            }
+        }
+        processEventFlag = false;
+
+        if (!removeListeners.isEmpty()) {
+            for (IBindingDataSetListener removeListener : removeListeners) {
+                listeners.remove(removeListener);
+            }
+            removeListeners.clear();
+        }
+    }
+    
+    public void addBindingDataListener(IBindingDataSetListener listener) {
+        listeners.add(listener);
+    }
+
+    public void removeBindingDataListener(IBindingDataSetListener listener) {
+        if (!processEventFlag) {
+            listeners.remove(listener);
+        } else {
+            removeListeners.add(listener);
+        }
+    }
 
 }
diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/BindingDataSetEvent.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/BindingDataSetEvent.java
new file mode 100644 (file)
index 0000000..9ce1c7c
--- /dev/null
@@ -0,0 +1,56 @@
+package org.tizen.webuibuilder.ui.views.databinding.model;
+
+
+public class BindingDataSetEvent {
+       
+       public enum BindingDataSetEventType {
+        ALL_DATASOURCE_RESETED, DATASOURCE_ADDED, DATASOURCE_REMOVED, DATASOURCE_RENAMED, DATASOURCE_MOVED,
+        ALL_VIEWMODEL_RESETED, VIEWMODEL_ADDED, VIEWMODEL_REMOVED, VIEWMODEL_RENAMED, VIEWMODEL_MOVED,
+        VIEWMODEL_ITEM_ADDED, VIEWMODEL_ITEM_REMOVED, VIEWMODEL_CHANGED, DATASOURCE_CHANGED, VIEWMODEL_SET_DATASOURCE
+    }
+
+       private BindingDataSetEventType type;
+    private BindingData bindingData;
+    private int index;
+    private String value;
+    private BindingData oldBindingData;
+
+    public BindingDataSetEvent(BindingDataSetEventType type, BindingData page, int index, BindingData oldPage) {
+        this.type = type;
+        this.bindingData = page;
+        this.index = index;
+        this.oldBindingData = oldPage;
+    }
+
+    public BindingDataSetEvent(BindingDataSetEventType type, BindingData page, String value, int index, BindingData oldPage) {
+        this.type = type;
+        this.bindingData = page;
+        this.value = value;
+        this.index = index;
+        this.oldBindingData = oldPage;
+       }
+
+       public BindingDataSetEventType getType() {
+        return type;
+    }
+
+    public BindingData getBindingData() {
+        return bindingData;
+    }
+
+    public int getIndex() {
+        return index;
+    }
+    
+    public String getValue() {
+        return value;
+    }
+
+    public BindingData getOldBindingData() {
+        return oldBindingData;
+    }
+
+    public String getName() {
+        return bindingData.getName();
+    }
+}
index 97683b3..739826c 100644 (file)
@@ -151,5 +151,14 @@ public class BindingObject {
     public void add(BindingObject observableObject) {
         observableObjects.add(observableObject);
     }
+    
+    /**
+     * Removes a observableObject.
+     * 
+     * @param observableObject
+     */
+    public void remove(BindingObject observableObject) {
+        observableObjects.remove(observableObject);
+    }
 
 }
diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/IBindingDataSetListener.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/model/IBindingDataSetListener.java
new file mode 100644 (file)
index 0000000..e692571
--- /dev/null
@@ -0,0 +1,29 @@
+package org.tizen.webuibuilder.ui.views.databinding.model;
+
+import java.util.EventListener;
+
+public interface IBindingDataSetListener extends EventListener {
+
+       void allDataSourcesReseted(BindingDataSetEvent e);
+       
+       void dataSourceAdded(BindingDataSetEvent e);
+       
+       void dataSourceRemoved(BindingDataSetEvent e);
+       
+       void dataSourceRenamed(BindingDataSetEvent e);
+       
+       void dataSourceMoved(BindingDataSetEvent e);
+       
+       void allViewModelReseted(BindingDataSetEvent e);
+       
+       void viewModelAdded(BindingDataSetEvent e);
+       
+       void viewModelRemoved(BindingDataSetEvent e);
+       
+       void viewModelRenamed(BindingDataSetEvent e);
+       
+       void viewModelMoved(BindingDataSetEvent e);
+       
+       void viewModelChanged(BindingDataSetEvent e);
+       
+}
index 441538c..37d6a78 100644 (file)
@@ -247,7 +247,7 @@ public class HandlerCodeWriter {
     private boolean initEditor(String pageID) {
         // IFile ifile = getFile(pageID);
         IFile ifile = getFile();
-        if (!ifile.exists()) {
+        if ((ifile == null) || (!ifile.exists())) {
             return false;
         }