From 69b7cae812430b094d030b6d0220c650533064d4 Mon Sep 17 00:00:00 2001 From: parary Date: Thu, 8 May 2014 17:12:40 +0900 Subject: [PATCH] DATABIND : XML type file Support(Static Source/File System Source). Change-Id: I429932c742ac01a4648fe510529fb8cd0f306aec Signed-off-by: parary --- .../databinding/dialog/FileSystemSubPage.java | 128 ++++++++++++---- .../ui/views/databinding/dialog/Helper.java | 170 +++++++++++++++++++-- .../ui/views/databinding/dialog/SetSourcePage.java | 165 +------------------- .../ui/views/databinding/dialog/StaticSubPage.java | 101 +++++++++--- 4 files changed, 336 insertions(+), 228 deletions(-) diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/FileSystemSubPage.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/FileSystemSubPage.java index b35fc3d..2b24171 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/FileSystemSubPage.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/FileSystemSubPage.java @@ -28,6 +28,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FormAttachment; @@ -43,6 +44,7 @@ import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; +import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Arrays; import org.tizen.webuibuilder.ui.views.databinding.model.BindingData; import com.google.gson.Gson; @@ -57,8 +59,12 @@ public class FileSystemSubPage implements SourceDialogSubPage { private static Gson normalGson; private Text textPath; + private CCombo typeCombo; + + private static String[] comboItems; static{ + comboItems = new String[]{ "JSON", "XML" }; normalGson = new GsonBuilder().serializeNulls().create(); } @@ -80,6 +86,8 @@ public class FileSystemSubPage implements SourceDialogSubPage { dataModel.setJsonData( normalGson.toJson(root) ); dataModel.setStaticFilePath( textPath.getText() ); + dataModel.setSourceType( typeCombo.getText() ); + return dataModel; } @@ -97,27 +105,59 @@ public class FileSystemSubPage implements SourceDialogSubPage { settingComposite.setLayout(layout); + + Button loadButton = new Button( settingComposite, SWT.PUSH ); + loadButton.setText( "L" ); + loadButton.setToolTipText( "Load" ); + + FormData data = new FormData( 25, 25 ); + data.bottom = new FormAttachment( 100, -5 ); + data.right = new FormAttachment( 100, -5 ); + + loadButton.setLayoutData( data ); + + loadButton.addSelectionListener( new SelectionAdapter() { + @Override + public void widgetSelected( SelectionEvent e ) { + + IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + IFile editorFile = (IFile)workbenchPage.getActiveEditor().getEditorInput().getAdapter(IFile.class); + IPath path = new Path( textPath.getText() ); + + IFile file = editorFile.getProject().getFile(path); + + try { + JsonObject json = null; + + if( typeCombo.getSelectionIndex() == 0 ){ + json = Helper.readJsonFromStream( file.getContents() ); + }else{ + json = Helper.getJsonFromXML( Helper.streamToString( file.getContents() ) ); + } + + if( json != null ){ + Helper.makeTreeItem( json, parentDataTree, null, false); + }else{ + parentDataTree.removeAll(); + } + } catch (CoreException e1) { + parentDataTree.removeAll(); + e1.printStackTrace(); + } + } + } ); + Label label = new Label(settingComposite, SWT.NONE); label.setText( "Path" ); - FormData data = new FormData(); + data = new FormData(); data.top = new FormAttachment(0, 0); data.left = new FormAttachment(0, 0); label.setLayoutData(data); - textPath = new Text(settingComposite, SWT.SINGLE | SWT.BORDER); - textPath.setEnabled(false); - - data = new FormData(); - data.top = new FormAttachment(label, 0); - data.left = new FormAttachment(10, 0); - data.right = new FormAttachment(90, 0); - textPath.setLayoutData(data); - Button button = new Button(settingComposite, SWT.PUSH ); data = new FormData(20,20); - data.top = new FormAttachment(label, 0); - data.left = new FormAttachment(textPath, 5); - data.right = new FormAttachment(100, -5); + data.top = new FormAttachment(0, 0); + data.right = new FormAttachment(loadButton, -3); button.setLayoutData(data); button.setText( "I" ); @@ -126,26 +166,41 @@ public class FileSystemSubPage implements SourceDialogSubPage { button.addSelectionListener(new SelectionAdapter(){ public void widgetSelected(SelectionEvent e) { IFile file = Helper.getProjectfile(); - if(file != null ){ - textPath.setText( file.getProjectRelativePath().toPortableString() ); - - try { - JsonObject json = Helper.readJsonFromStream( file.getContents() ); - - if( json != null ){ - Helper.makeTreeItem( json, parentDataTree, null, false); - }else{ - parentDataTree.removeAll(); - } - } catch (CoreException e1) { - parentDataTree.removeAll(); - e1.printStackTrace(); - } } } }); + + textPath = new Text(settingComposite, SWT.SINGLE | SWT.BORDER); + textPath.setEnabled(false); + + data = new FormData(); + data.top = new FormAttachment(0, 0); + data.left = new FormAttachment(label, 5); + data.right = new FormAttachment(button, -5); + textPath.setLayoutData(data); + + Label typelabel = new Label( settingComposite, SWT.NONE ); + typelabel.setText( "Type" ); + + data = new FormData(); + data.top = new FormAttachment(label, 10); + data.left = new FormAttachment(0, 0); + + typelabel.setLayoutData(data); + + typeCombo = new CCombo( settingComposite, SWT.SINGLE | SWT.BORDER | SWT.FLAT | SWT.READ_ONLY ); + typeCombo.setItems( comboItems ); + typeCombo.setEditable( false ); + typeCombo.select( 0 ); + + data = new FormData(); + data.top = new FormAttachment(label, 10); + data.left = new FormAttachment(typelabel, 5); + data.right = new FormAttachment(loadButton, -3); + typeCombo.setLayoutData(data); + } @Override @@ -171,8 +226,13 @@ public class FileSystemSubPage implements SourceDialogSubPage { @Override public void loadData(BindingData dataModel) { + + parentDataTree.removeAll(); + textPath.setText( dataModel.getStaticFilePath() ); + typeCombo.select( Arrays.asList( comboItems ).indexOf( dataModel.getSourceType() ) ); + IWorkbenchPage workbenchPage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IFile editorFile = (IFile)workbenchPage.getActiveEditor().getEditorInput().getAdapter(IFile.class); IPath path = new Path( dataModel.getStaticFilePath() ); @@ -181,15 +241,19 @@ public class FileSystemSubPage implements SourceDialogSubPage { if( file != null){ try { - JsonObject json = Helper.readJsonFromStream( file.getContents() ); + + JsonObject json; + + if(typeCombo.getSelectionIndex() == 0 ){ + json = Helper.readJsonFromStream( file.getContents() ); + }else{ + json = Helper.getJsonFromXML( Helper.streamToString( file.getContents() ) ); + } if( json != null ){ Helper.makeTreeItem( json, parentDataTree, null, false); - }else{ - parentDataTree.removeAll(); } } catch (CoreException e) { - parentDataTree.removeAll(); e.printStackTrace(); } } diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/Helper.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/Helper.java index 66db5a2..5d76ad0 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/Helper.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/Helper.java @@ -29,9 +29,11 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.StringReader; +import java.io.StringWriter; import java.math.BigDecimal; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Arrays; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; @@ -39,11 +41,22 @@ import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerConfigurationException; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.TransformerFactoryConfigurationError; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.swt.SWT; import org.eclipse.swt.custom.CCombo; +import org.eclipse.swt.custom.TreeEditor; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FormAttachment; import org.eclipse.swt.layout.FormData; import org.eclipse.swt.layout.FormLayout; @@ -89,8 +102,25 @@ public class Helper { private static final String xmlConvertTextPrifix = ""; private static final String xmlConvertTextName = "text"; - private static Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); - + private static Gson gson; + private static DocumentBuilderFactory dbf; + private static Transformer transformer; + + static{ + + gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); + + dbf = DocumentBuilderFactory.newInstance(); + + try { + transformer = TransformerFactory.newInstance().newTransformer(); + transformer.setOutputProperty(OutputKeys.METHOD, "xml"); + } catch ( TransformerConfigurationException e ) { + e.printStackTrace(); + } catch ( TransformerFactoryConfigurationError e ) { + e.printStackTrace(); + } + } public static String callURL( String url, String method, TableItem[] items ) { BufferedReader br = null; OutputStreamWriter writer = null; @@ -201,6 +231,7 @@ public class Helper { indexItem.setExpanded( true ); } else { makeTreeItem( array.get( i ), tree, item, includeIndex ); + item.setExpanded( true ); break; } } @@ -228,15 +259,30 @@ public class Helper { type = "String"; careteTreeItem( treeItem, entry.getKey(), jp.getAsString(), type ); + if ( includeIndex ) + Helper.addComboInTreeItem(treeItem, type); } else if ( entry.getValue().isJsonObject() ) { careteTreeItem( treeItem, entry.getKey(), null, "Object" ); + + if ( includeIndex ) + Helper.addComboInTreeItem(treeItem, "Object"); + makeTreeItem( entry.getValue(), tree, treeItem, includeIndex ); } else if ( entry.getValue().isJsonArray() ) { careteTreeItem( treeItem, entry.getKey(), null, "Array" ); + + if ( includeIndex ) + Helper.addComboInTreeItem(treeItem, "Array"); + makeTreeItem( entry.getValue(), tree, treeItem, includeIndex ); } else if ( entry.getValue().isJsonNull() ) { careteTreeItem( treeItem, entry.getKey(), null, "undefined" ); + + if ( includeIndex ) + Helper.addComboInTreeItem(treeItem, "String"); + makeTreeItem( entry.getValue(), tree, treeItem, includeIndex ); + } treeItem.setExpanded( true ); } @@ -253,7 +299,13 @@ public class Helper { } if ( item.getParent().getColumnCount() > 0 ) { - Helper.careteTreeItem( new TreeItem( item, 0 ), null, jp.getAsString(), type ); + TreeItem primitiveItem = new TreeItem( item, 0 ); + Helper.careteTreeItem( primitiveItem, null, jp.getAsString(), type ); + + if ( includeIndex ) + Helper.addComboInTreeItem(primitiveItem, type); + + item.setExpanded( true ); } } } @@ -471,7 +523,19 @@ public class Helper { } public static JsonObject getJsonFromXML( String xml ) { - DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + + Document doc = parseXML( xml ); + + JsonObject root = new JsonObject(); + if ( doc != null ) { + NodeList nodeList = doc.getChildNodes(); + parseNodeList( root, nodeList ); + } + + return root; + } + + public static Document parseXML( String xml ){ DocumentBuilder db; Document doc = null; try { @@ -482,21 +546,14 @@ public class Helper { } catch ( ParserConfigurationException e ) { e.printStackTrace(); } catch ( SAXException e ) { - e.printStackTrace(); +// e.printStackTrace(); } catch ( IOException e ) { e.printStackTrace(); } - - JsonObject root = new JsonObject(); - - if ( doc != null ) { - NodeList nodeList = doc.getChildNodes(); - parseNodeList( root, nodeList ); - } - - return root; + + return doc; } - + /** * @param json * @param nodeList @@ -606,6 +663,89 @@ public class Helper { } return result; } + + public static CCombo addComboInTreeItem(TreeItem item, String data) { + + TreeEditor treeEditor = new TreeEditor( item.getParent() ); + treeEditor.horizontalAlignment = SWT.CENTER; + treeEditor.verticalAlignment = SWT.CENTER; + treeEditor.grabHorizontal = true; + treeEditor.grabVertical = true; + + CCombo combo = new CCombo( item.getParent(), SWT.SINGLE | SWT.BORDER | SWT.READ_ONLY); + treeEditor.setEditor(combo, item, 2); + final String[] comboItems = { "String", "Number", "Boolean", "Object", "Array" }; + combo.setItems(comboItems); + combo.select( Arrays.asList(comboItems).indexOf(data) ); + combo.setData( "parentItem", item ); + + item.setData("TREEEDITORFORCOMBO", treeEditor); + + combo.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + CCombo combo = (CCombo) e.getSource(); + TreeItem parentItem = (TreeItem) combo.getData("parentItem"); + String selectedText = comboItems[combo.getSelectionIndex()]; + + if( selectedText.equals("Object") || selectedText.equals("Array") ){ + parentItem.setText(1, ""); + } + + parentItem.setText(2, selectedText); + } + }); + return combo; + } + + public static String streamToString( InputStream is ){ + StringBuilder sb = new StringBuilder(); + + BufferedReader br = null; + String line = null; + + try { + br = new BufferedReader( new InputStreamReader( is ) ); + + while( ( line = br.readLine() ) != null ){ + sb.append( line ); + } + + br.close(); + is.close(); + } catch ( IOException e ) { + e.printStackTrace(); + }finally{ + if( br != null ){ try { br.close(); } catch ( IOException e ) { e.printStackTrace(); } } + if( is != null ){ try { is.close(); } catch ( IOException e ) { e.printStackTrace(); } } + } + + return sb.toString(); + } + + public static String getStringFromDocument(Document dom, boolean indented) { + String signedContent = null; + + if( !indented ){ + dom.normalize(); + dom.normalizeDocument(); + } + + try { + StringWriter sw = new StringWriter(); + DOMSource domSource = new DOMSource(dom); + transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); + transformer.setOutputProperty(OutputKeys.INDENT, indented ? "yes" : "no"); + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", indented ? "2" : "0" ); + transformer.transform(domSource, new StreamResult(sw)); + sw.flush(); + signedContent = sw.toString(); + } catch (TransformerException e) { + e.printStackTrace(); + } + + return signedContent; + } public static IFile getProjectfile() { diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/SetSourcePage.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/SetSourcePage.java index c7f58d0..a010e0a 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/SetSourcePage.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/SetSourcePage.java @@ -27,7 +27,6 @@ import java.util.List; import java.util.Map; import org.eclipse.swt.SWT; -import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.StackLayout; import org.eclipse.swt.custom.TreeEditor; @@ -58,7 +57,6 @@ import org.eclipse.swt.widgets.Text; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeColumn; import org.eclipse.swt.widgets.TreeItem; -import org.tizen.webuibuilder.ui.views.databinding.DataBindingDnDManager; 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; @@ -208,7 +206,6 @@ public class SetSourcePage extends Dialog { dataModel.setModelType( dataTypeCombo.getText() ); dataModel.setUpdateOnStartup( updateOnStartUp.getSelection() ); dataModel.setSourceName( dataSourceTreeItem.getText() ); - dataModel.setItemName( dataModel.getSourceName() ); TreeItemData treeItemData = (TreeItemData) dataSourceTreeItem.getData( "TREEITEMDATA" ); if (treeItemData == null) { @@ -768,7 +765,7 @@ public class SetSourcePage extends Dialog { } }); - DataBindingDnDManager.addDragAndDropListenerToTree(dialogTree); +// DataBindingDnDManager.addDragAndDropListenerToTree(dialogTree); } private Group makeSettingGroup( Group settingGroup ) { @@ -797,41 +794,6 @@ public class SetSourcePage extends Dialog { return settingGroup; } - private CCombo addComboInTreeItem(TreeItem item, String data) { - TreeEditor treeEditor = new TreeEditor(dialogTree); - CCombo combo = new CCombo(dialogTree, SWT.SINGLE | SWT.BORDER | SWT.FLAT | SWT.READ_ONLY); - String[] comboItems = { "String", "Number", "Boolean", "Object", "Array" }; - combo.setItems(comboItems); - combo.select( Arrays.asList(comboItems).indexOf(data) ); - combo.pack(); - - combo.setData( "parentItem", item ); - - treeEditor.minimumWidth = dialogTree.getColumn(2).getWidth()-1; - treeEditor.horizontalAlignment = SWT.CENTER; - treeEditor.setEditor(combo, item, 2); - - item.setData("TREEEDITORFORCOMBO", treeEditor); - - combo.addSelectionListener(new SelectionAdapter() { - @Override - public void widgetSelected(SelectionEvent e) { - CCombo combo = (CCombo) e.getSource(); - TreeItem parentItem = (TreeItem) combo.getData("parentItem"); - String[] comboItems = { "String", "Number", "Boolean", "Object", "Array" }; - String selectedText = comboItems[combo.getSelectionIndex()]; - - if( selectedText.equals("Object") || selectedText.equals("Array") ){ - parentItem.setText(1, ""); - } - - parentItem.setText(2, selectedText); - } - }); - - return combo; - } - private void addItem() { TreeItem parentItem = null; @@ -875,7 +837,7 @@ public class SetSourcePage extends Dialog { item.setData("TREEITEMDATA", treeItemData); if( !type.equals("Index") ) - addComboInTreeItem(item, "String"); + Helper.addComboInTreeItem(item, "String"); dialogTree.showItem(item); @@ -983,136 +945,15 @@ public class SetSourcePage extends Dialog { editor.dispose(); selectedItem.setData("TREEEDITORFORCOMBO", null ); selectedItem.dispose(); - addComboInTreeItem(targetItem, type ); + Helper.addComboInTreeItem(targetItem, type ); } if( !selectedItem.isDisposed() ) selectedItem.dispose(); -// int columnCount = dialogTree.getColumnCount(); -// String[] str = new String[columnCount]; -// TreeItemData data = (TreeItemData) dialogTree.getSelection()[0].getData( "TREEITEMDATA" ); -// -// for (int i = 0; i < columnCount; i++) { -// str[i] = dialogTree.getSelection()[0].getText(i); -// } -// -// TreeItem item; -// -// if (option) { -// item = moveItemToUp(dialogTree.getSelection()[0], false); -// } else { -// item = moveItemToBottom(dialogTree.getSelection()[0], false); -// } -// -// DataBindingDnDManager.setChildItems(item, dialogTree.getSelection()[0].getItems()); -// -// dialogTree.getSelection()[0].dispose(); -// item.setText(str); -// item.setData( "TREEITEMDATA", data); -// dialogTree.select(item); - if( currentSubPage instanceof StaticSubPage ){ staticSubPage.updateJsonFromTree(); } } } - -// private TreeItem moveItemToBottom(TreeItem selectedItem, boolean downToChild) { -// TreeItem parentItem = selectedItem.getParentItem(); -// TreeItem item; -// -// if (parentItem == null) { -// int index = dialogTree.indexOf(selectedItem); -// int itemCount = dialogTree.getItemCount(); -// -// if (index == itemCount - 1) { -// if (downToChild) { -// item = new TreeItem(dialogTree, SWT.NONE, index + 1); -// } else { -// item = new TreeItem(dialogTree, SWT.NONE, index); -// } -// -// } else { -// if (dialogTree.getItem(index + 1).getExpanded()) { -// item = new TreeItem(dialogTree.getItem(index + 1), SWT.NONE, 0); -// } else { -// if (downToChild) { -// item = new TreeItem(dialogTree, SWT.NONE, index + 1); -// } else { -// item = new TreeItem(dialogTree, SWT.NONE, index + 2); -// } -// } -// } -// } else { -// int index = parentItem.indexOf(selectedItem); -// int itemCount = parentItem.getItemCount(); -// -// if (index == itemCount - 1) { -// if (downToChild) { -// item = new TreeItem(parentItem, SWT.NONE, index + 1); -// } else { -// item = moveItemToBottom(parentItem, true); -// } -// } else { -// if (parentItem.getItem(index + 1).getExpanded()) { -// item = new TreeItem(parentItem.getItem(index + 1), SWT.NONE, 0); -// } else { -// if (downToChild) { -// item = new TreeItem(parentItem, SWT.NONE, index + 1); -// } else { -// item = new TreeItem(parentItem, SWT.NONE, index + 2); -// } -// } -// } -// -// } -// return item; -// } -// -// private TreeItem moveItemToUp(TreeItem selectedItem, boolean upToParent) { -// TreeItem parentItem = selectedItem.getParentItem(); -// TreeItem item; -// -// if (parentItem == null) { -// int index = dialogTree.indexOf(selectedItem); -// -// if (index == 0) { -// item = new TreeItem(dialogTree, SWT.NONE, index); -// } else { -// if (dialogTree.getItem(index - 1).getExpanded()) { -// int itemCount = dialogTree.getItem(index - 1).getItemCount(); -// item = new TreeItem(dialogTree.getItem(index - 1), SWT.NONE, itemCount); -// } else { -// if (upToParent) { -// item = new TreeItem(dialogTree, SWT.NONE, index); -// } else { -// item = new TreeItem(dialogTree, SWT.NONE, index - 1); -// } -// } -// } -// } else { -// int index = parentItem.indexOf(selectedItem); -// -// if (index == 0) { -// if (upToParent) { -// item = new TreeItem(parentItem, SWT.NONE, index); -// } else { -// item = moveItemToUp(parentItem, true); -// } -// } else { -// if (parentItem.getItem(index - 1).getExpanded()) { -// int itemCount = parentItem.getItem(index - 1).getItemCount(); -// item = new TreeItem(parentItem.getItem(index - 1), SWT.NONE, itemCount); -// } else { -// if (upToParent) { -// item = new TreeItem(parentItem, SWT.NONE, index); -// } else { -// item = new TreeItem(parentItem, SWT.NONE, index - 1); -// } -// } -// } -// } -// return item; -// } } diff --git a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/StaticSubPage.java b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/StaticSubPage.java index 2edb1a3..154c31b 100644 --- a/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/StaticSubPage.java +++ b/org.tizen.webuibuilder/src/org/tizen/webuibuilder/ui/views/databinding/dialog/StaticSubPage.java @@ -28,6 +28,7 @@ import java.io.FileNotFoundException; import java.util.Map; import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.CCombo; import org.eclipse.swt.custom.SashForm; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.ModifyEvent; @@ -44,6 +45,7 @@ import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Group; +import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Tree; import org.eclipse.swt.widgets.TreeItem; import org.eclipse.ui.IEditorSite; @@ -55,9 +57,11 @@ import org.eclipse.ui.internal.EditorSite; import org.eclipse.ui.internal.WorkbenchPage; import org.eclipse.ui.internal.registry.EditorDescriptor; import org.eclipse.wst.jsdt.internal.ui.javaeditor.CompilationUnitEditor; +import org.mozilla.javascript.edu.emory.mathcs.backport.java.util.Arrays; import org.tizen.webuibuilder.ui.editor.MemoryEditorInput; import org.tizen.webuibuilder.ui.editor.PageDesigner; import org.tizen.webuibuilder.ui.views.databinding.model.BindingData; +import org.w3c.dom.Document; import com.google.gson.Gson; import com.google.gson.GsonBuilder; @@ -72,12 +76,16 @@ public class StaticSubPage implements SourceDialogSubPage { private Composite dataComposite; private StyledText staticSourceStyledText; + private CCombo typeCombo; + private String jsonFile; - private String jsonString; private static Gson prettyGson; private static Gson normalGson; + private static String[] comboItems; + static{ + comboItems = new String[]{ "JSON", "XML" }; prettyGson = new GsonBuilder().setPrettyPrinting().serializeNulls().create(); normalGson = new GsonBuilder().serializeNulls().create(); } @@ -97,8 +105,9 @@ public class StaticSubPage implements SourceDialogSubPage { Helper.makeJsonFromTree( item, root); } - dataModel.setJsonData( normalGson.toJson(root) ); + dataModel.setJsonData( staticSourceStyledText.getText() ); dataModel.setStaticFilePath(jsonFile); + dataModel.setSourceType( typeCombo.getText() ); return dataModel; } @@ -162,8 +171,21 @@ public class StaticSubPage implements SourceDialogSubPage { formatButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - JsonObject jo = prettyGson.fromJson(staticSourceStyledText.getText(), JsonObject.class); - staticSourceStyledText.setText( prettyGson.toJson(jo) ); + if(typeCombo.getSelectionIndex() == 0 ){ + JsonObject jo = prettyGson.fromJson(staticSourceStyledText.getText(), JsonObject.class); + staticSourceStyledText.setData( "editing", true ); + staticSourceStyledText.setText( prettyGson.toJson(jo) ); + staticSourceStyledText.setData( "editing", false ); + }else{ + Document doc = Helper.parseXML( staticSourceStyledText.getText() ); + + if( doc == null ) + return; + + staticSourceStyledText.setData( "editing", true ); + staticSourceStyledText.setText( Helper.getStringFromDocument( doc, true ) ); + staticSourceStyledText.setData( "editing", false ); + } } }); @@ -180,11 +202,39 @@ public class StaticSubPage implements SourceDialogSubPage { compactButton.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { - JsonObject jo = normalGson.fromJson(staticSourceStyledText.getText(), JsonObject.class); - staticSourceStyledText.setText( normalGson.toJson(jo) ); + if(typeCombo.getSelectionIndex() == 0 ){ + JsonObject jo = normalGson.fromJson(staticSourceStyledText.getText(), JsonObject.class); + staticSourceStyledText.setData( "editing", true ); + staticSourceStyledText.setText( normalGson.toJson(jo) ); + staticSourceStyledText.setData( "editing", false ); + }else{ + staticSourceStyledText.setData( "editing", true ); + staticSourceStyledText.setText( staticSourceStyledText.getText().replaceAll(">\\s*<", "><") ); + staticSourceStyledText.setData( "editing", false ); + } } }); + Label typelabel = new Label( subSettingComposite, SWT.NONE ); + typelabel.setText( "Type" ); + + data = new FormData(); + data.top = new FormAttachment(2, 0); + data.left = new FormAttachment(compactButton, 10); + + typelabel.setLayoutData(data); + + typeCombo = new CCombo( subSettingComposite, SWT.SINGLE | SWT.BORDER | SWT.FLAT | SWT.READ_ONLY ); + typeCombo.setItems( comboItems ); + typeCombo.setEditable( false ); + typeCombo.select( 0 ); + + data = new FormData(); + data.top = new FormAttachment(2, 0); + data.left = new FormAttachment(typelabel, 5); + + typeCombo.setLayoutData(data); + Button openFileButton = new Button(subSettingComposite, SWT.PUSH); openFileButton.setText("O"); openFileButton.setToolTipText( "Open" ); @@ -212,20 +262,14 @@ public class StaticSubPage implements SourceDialogSubPage { } try { - JsonObject json = Helper.readJsonFromStream( new FileInputStream( new File( jsonFile ) ) ); for( Control control : parentDataTree.getChildren() ){ control.dispose(); } + parentDataTree.removeAll(); - if (json != null) { - jsonString = prettyGson.toJson( json ); - staticSourceStyledText.setText( jsonString ); - }else{ - jsonString = ""; - staticSourceStyledText.setText( "" ); - } + staticSourceStyledText.setText( Helper.streamToString( new FileInputStream( new File( jsonFile ) ) ) ); } catch (FileNotFoundException e) { e.printStackTrace(); @@ -343,16 +387,33 @@ public class StaticSubPage implements SourceDialogSubPage { staticSourceStyledText = editor.getViewer().getTextWidget(); + staticSourceStyledText.setData( "editing", false ); + staticSourceStyledText.addModifyListener(new ModifyListener() { @Override public void modifyText(ModifyEvent e) { + String inputText = staticSourceStyledText.getText(); + if( staticSourceStyledText.getData( "editing" ) != null && (Boolean) staticSourceStyledText.getData( "editing" ) ) return; + if( inputText.trim().length() == 0 ){ + for ( Control control : parentDataTree.getChildren() ) { + control.dispose(); + } + parentDataTree.removeAll(); + return; + } + JsonObject json = null; try{ - json = normalGson.fromJson(staticSourceStyledText.getText(), JsonObject.class); + + if(typeCombo.getSelectionIndex() == 0 ) + json = normalGson.fromJson(inputText, JsonObject.class); + else + json = Helper.getJsonFromXML( inputText ); + }catch(Exception ex){} if(json != null){ @@ -390,12 +451,14 @@ public class StaticSubPage implements SourceDialogSubPage { @Override public void loadData(BindingData dataModel) { - JsonObject json = prettyGson.fromJson(dataModel.getJsonData(), JsonObject.class ); - - if( json != null ) - staticSourceStyledText.setText( prettyGson.toJson(json) ); jsonFile = dataModel.getStaticFilePath(); + + if( dataModel.getSourceType() != null) + typeCombo.select( Arrays.asList( comboItems ).indexOf( dataModel.getSourceType() ) ); + + if( dataModel.getJsonData() != null ) + staticSourceStyledText.setText( dataModel.getJsonData() ); } public Composite getDataComposite() { -- 2.7.4