DATABIND : Modify the delete functions in the databinding view 48/20448/2
authorjaeyeol lee <jaeyeol148.lee@samsung.com>
Wed, 7 May 2014 10:28:06 +0000 (19:28 +0900)
committerjaeyeol lee <jaeyeol148.lee@samsung.com>
Wed, 7 May 2014 10:41:19 +0000 (19:41 +0900)
If data source is deleted, the model is modified automatically
model, widget works similarly

Change-Id: I66038bed9cacc8b7ceb6b681deac22597312bb8e
Signed-off-by: jaeyeol lee <jaeyeol148.lee@samsung.com>
org.tizen.webuibuilder/src/org/tizen/webuibuilder/gef/commands/SetDataSourceCommand.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManager.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/model/app/AppManagerForHTML.java
org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/DataBindingPage.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

index 53809c1..6457fe8 100644 (file)
@@ -1,5 +1,8 @@
 package org.tizen.webuibuilder.gef.commands;
 
+import java.util.ArrayList;
+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;
@@ -14,6 +17,7 @@ public class SetDataSourceCommand extends Command {
        private BindingDataSetEventType type;
        private String value;
        private String oldName;
+       List<BindingData> oldModels = new ArrayList<BindingData>();
        
        public SetDataSourceCommand(BindingDataSetEventType type, AppManager appManager, BindingData bindingData, int index) {
                this.type = type;
@@ -43,18 +47,21 @@ public class SetDataSourceCommand extends Command {
        public void execute() {
         if (canExecute()) {
                if (type.equals(BindingDataSetEventType.DATASOURCE_REMOVED)) {
+                   for (BindingData model : appManager.getViewModels()) {
+                       oldModels.add(model.clone());
+                   }
                        appManager.removeDataSource(bindingData);
                } else if (type.equals(BindingDataSetEventType.DATASOURCE_CHANGED)) {
                        oldBindingData = appManager.getDataSource(bindingData.getItemName());
                        if (oldBindingData != null) {
                                appManager.removeDataSource(oldBindingData);
                        }
-                       appManager.addDataSource(bindingData);
+                       appManager.addDataSource(bindingData, null);
                } else if (type.equals(BindingDataSetEventType.DATASOURCE_RENAMED)) {
                        oldName = bindingData.getSourceName();
                        renameDataSource(value);
                } else {
-                       appManager.addDataSource(bindingData);
+                       appManager.addDataSource(bindingData, null);
                }
         }
     }
@@ -66,10 +73,10 @@ public class SetDataSourceCommand extends Command {
        @Override
     public void undo() {
                if (type.equals(BindingDataSetEventType.DATASOURCE_REMOVED)) {
-               appManager.addDataSource(bindingData);
+               appManager.addDataSource(bindingData, oldModels);
                } else if (type.equals(BindingDataSetEventType.DATASOURCE_CHANGED)) {
                appManager.removeDataSource(bindingData);
-               appManager.addDataSource(oldBindingData);
+               appManager.addDataSource(oldBindingData, null);
                } else if (type.equals(BindingDataSetEventType.DATASOURCE_RENAMED)) {
                        renameDataSource(oldName);
        } else {
index c5eee8c..6557eec 100644 (file)
@@ -769,8 +769,8 @@ public class AppManager implements IPageDataSetListener, IAppListener {
         return bindingDataSet.getViewModels();
     }
 
-    public void addDataSource(BindingData bindingData) {
-        bindingDataSet.addDataSource(bindingData);
+    public void addDataSource(BindingData bindingData, List<BindingData> oldModels) {
+        bindingDataSet.addDataSource(bindingData, oldModels);
     }
     
     public void removeDataSource(BindingData bindingData) {
index baf67a7..bf4966c 100644 (file)
@@ -233,7 +233,7 @@ public class AppManagerForHTML extends AppManager {
     private void setBindingInfos(HtmlMetaFileData fileData) {
         List<BindingData> dataSources = fileData.getDataSources();
         for (BindingData dataSource : dataSources) {
-            getBindingDataSet().addDataSource(dataSource);
+            getBindingDataSet().addDataSource(dataSource, null);
         }
 
         List<BindingData> viewModels = fileData.getViewModels();
index 52be132..934c1b2 100644 (file)
@@ -78,6 +78,7 @@ 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.SetPartPropertyCommand;
 import org.tizen.webuibuilder.gef.commands.SetViewModelCommand;
 import org.tizen.webuibuilder.model.Part;
 import org.tizen.webuibuilder.model.page.PageData;
@@ -648,7 +649,11 @@ public class DataBindingPage extends Page implements IBindingDataSetListener {
         if ((treeItems == null) || (treeItems.length < 1)) {
             return false;
         } else {
-            return true;
+            if (treeItems[0].getParentItem() == null) {
+                return true;
+            } else {
+                return false;
+            }
         }
     }
 
@@ -669,9 +674,15 @@ public class DataBindingPage extends Page implements IBindingDataSetListener {
             if (treeItem.getParentItem() == null) {
                 BindingData bindingData =
                         pageDesigner.getAppManager().getViewModel(treeItem.getText());
+                
                 Command command =
                         new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_REMOVED,
                                                 pageDesigner.getAppManager(), bindingData, 0);
+                
+                Command removeCommand = removeBindingInfo(treeItem.getText(), null);
+                if (removeCommand != null) {
+                    command = command.chain(removeCommand);
+                }
                 getCommandStackFromPageDesigner().execute(command);
             } else {
                 List<String> itemPath = makeItemPath(treeItem);
@@ -682,6 +693,10 @@ public class DataBindingPage extends Page implements IBindingDataSetListener {
                         new SetViewModelCommand(BindingDataSetEventType.VIEWMODEL_ITEM_REMOVED,
                                                 pageDesigner.getAppManager(), bindingData,
                                                 itemPath, treeItem.getText(), "", 0);
+                Command removeCommand = removeBindingInfo(treeItem.getText(), itemPath);
+                if (removeCommand != null) {
+                    command = command.chain(removeCommand);
+                }
                 getCommandStackFromPageDesigner().execute(command);
             }
 
@@ -779,6 +794,11 @@ public class DataBindingPage extends Page implements IBindingDataSetListener {
             delegater.valueChanged(part, BuilderConstants.ATTRIBUTE_DATA_BIND, "", true);
         }
     }
+    
+    public Command createRemovePropertyCommand(Part part) {
+        Command command = new SetPartPropertyCommand(part, BuilderConstants.ATTRIBUTE_DATA_BIND, "", true);
+        return command;
+    }
 
     /**
      * Gets the view model tree.
@@ -896,6 +916,55 @@ public class DataBindingPage extends Page implements IBindingDataSetListener {
             }
         }
     }
+    
+    public Command removeBindingInfo(String name, List<String> itemPath) {
+        Command command = null;
+        TableItem[] tableItems = infoTableViewer.getTable().getItems();
+        for (TableItem tableItem : tableItems) {
+            String[] modelPath = tableItem.getText().split("\\.");
+            if ((modelPath != null)&&(modelPath.length > 0)) {
+                Part part = null;
+                if (modelPath[0].equals(name)) {
+                    part = (Part) tableItem.getData("PART");
+                } else  {
+                    part = getPartFromInfoTabel(tableItem, modelPath, itemPath);
+                }
+                if (part != null) {
+                    if (command == null) {
+                        command = createRemovePropertyCommand(part);
+                    } else {
+                        command = command.chain(createRemovePropertyCommand(part));
+                    }
+                }
+            }
+        }
+        return command;
+    }
+
+    private Part getPartFromInfoTabel(TableItem tableItem, String[] modelPath, List<String> itemPath) {
+        Part part = null;
+        if ((itemPath == null) || (itemPath.isEmpty())) {
+            return null;
+        }
+        
+        if (modelPath.length == 0) {
+            return null;
+        }
+        
+        if (modelPath.length < itemPath.size()) {
+            return null;
+        }
+        
+        for (int i=0; i<itemPath.size(); i++) {
+            if (!itemPath.get(itemPath.size()-1-i).equals(modelPath[i])) {
+                return null;
+            } 
+        }
+        
+        part = (Part) tableItem.getData("PART");
+        
+        return part;
+    }
 
     public boolean canRemoveBindingInfo() {
         TableItem[] tableItems = infoTableViewer.getTable().getSelection();
@@ -1454,10 +1523,12 @@ public class DataBindingPage extends Page implements IBindingDataSetListener {
 
     @Override
     public void dataSourceRenamed(BindingDataSetEvent e) {
-        List<BindingData> models = pageDesigner.getAppManager().getViewModels();
-        for (BindingData model : models) {
-            if (model.getSourceName().equals(e.getOldValue())) {
-                model.setSourceName(e.getValue());
+        if (pageDesigner != null) {
+            List<BindingData> models = pageDesigner.getAppManager().getViewModels();
+            for (BindingData model : models) {
+                if (model.getSourceName().equals(e.getOldValue())) {
+                    model.setSourceName(e.getValue());
+                }
             }
         }
         
index 619deb2..5593776 100644 (file)
@@ -451,7 +451,7 @@ public class BindingData {
      * 
      * @param observableObject
      */
-    public void removeViewModelObjects( BindingObject observableObject ) {
+    public void removeViewModelObject( BindingObject observableObject ) {
         viewModelObjects.remove( observableObject );
     }
 
@@ -553,7 +553,7 @@ public class BindingData {
      * 
      * @param observableObject
      */
-    public void removeDataSourceObjects( BindingObject observableObject ) {
+    public void removeDataSourceObject( BindingObject observableObject ) {
         dataSourceObjects.remove( observableObject );
     }
 
@@ -563,6 +563,17 @@ public class BindingData {
     public void clearDataSourceObjects() {
         dataSourceObjects.clear();
     }
+    
+    public void removeAllDataSourceObject(List<BindingObject> dataSourceObjects) {
+        for (BindingObject dataSourceObject : dataSourceObjects) {
+            for (BindingObject viewModelObject : viewModelObjects) {
+                if (viewModelObject.getName().equals(dataSourceObject.getName())) {
+                    removeViewModelObject(viewModelObject);
+                    break;
+                }
+            }
+        }
+    }
 
     /**
      * Gets a updateOnStartup info.
index a7a2ad2..e1f73e7 100644 (file)
@@ -312,14 +312,14 @@ public class BindingDataSet {
                }
                
                if (viewModelObject.getItems().isEmpty()) {
-                       viewModelObject.setType(null);
+                       viewModelObject.setType("String");
                }
        }
        
        private void removeChildViewModelObject(BindingData bindingData, String childName, String value) {
                for (BindingObject object : bindingData.getViewModelObjects()) {
                        if (object.getName().equals(childName)) {
-                               bindingData.removeViewModelObjects(object);
+                               bindingData.removeViewModelObject(object);
                                break;
                        }
                }
@@ -356,7 +356,7 @@ public class BindingDataSet {
      * @param bindingData
      *            a data source
      */
-    public void addDataSource(BindingData bindingData) {
+    public void addDataSource(BindingData bindingData, List<BindingData> oldModels) {
         dataSources.add(bindingData);
         selectionPath.clear();
         if (bindingData.getItemName() == null) {
@@ -365,6 +365,10 @@ public class BindingDataSet {
             selectionPath.add(bindingData.getItemName());
         }
         
+        if ((oldModels != null) && (!oldModels.isEmpty())) {
+            addDataSourceObjectInModel(oldModels);
+        }
+        
         fireEvent(new BindingDataSetEvent(BindingDataSetEventType.DATASOURCE_ADDED, bindingData, getNumberOfDataSources() - 1 , null));
     }
     
@@ -378,7 +382,7 @@ public class BindingDataSet {
 //     
 //     return null;
 //    }
-    
+
     public BindingData getDataSource(String name) {
         List<BindingData> DataSources = getDataSources();
         for (BindingData DataSource : DataSources) {
@@ -408,11 +412,26 @@ public class BindingDataSet {
                 selectionPath.add(bindingData.getItemName());
             }
                
+               removeDataSourceObjectInModel(bindingData);
                dataSources.remove(index);
                fireEvent(new BindingDataSetEvent(BindingDataSetEventType.DATASOURCE_REMOVED, bindingData, index, null));
        }
     }
     
+    private void removeDataSourceObjectInModel(BindingData bindingData) {
+        for (BindingData model : viewModels) {
+            if ((model.getSourceName() != null) && (model.getSourceName().equals(bindingData.getSourceName()))) {
+                model.removeAllDataSourceObject(bindingData.getDataSourceObjects());
+                model.setSourceName(null);
+            }
+        }
+    }
+    
+    private void addDataSourceObjectInModel(List<BindingData> oldModels) {
+        viewModels.clear();
+        viewModels.addAll(oldModels);
+    }
+
     public void renameDataSource(BindingDataSetEventType type,
                        BindingData bindingData, String newName) {
                if (type.equals(BindingDataSetEventType.DATASOURCE_RENAMED)) {