private Map<String, Button> dataButtonMap = new HashMap<String,Button>();
- private List<String> types = Arrays.asList( new String[]{ "Boolean","Number","String" } );
+ private List<String> types = Arrays.asList( new String[]{ "Boolean","Number","String", "Object", "Array" } );
/**
* Constructor.
public void focusLost(FocusEvent event) {
treeItem.setText(column, text.getText());
text.dispose();
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
}
});
switch (event.keyCode) {
case SWT.CR:
treeItem.setText(column, text.getText());
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
+ text.dispose();
break;
case SWT.KEYPAD_CR:
// Enter hit--set the text into the tree and
// drop through
treeItem.setText(column, text.getText());
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
+ text.dispose();
break;
case SWT.ESC:
// End editing session
public void focusLost(FocusEvent event) {
item.setText(text.getText());
text.dispose();
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
}
});
// Enter hit--set the text into the tree and
// drop through
item.setText(text.getText());
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
+ text.dispose();
break;
case SWT.ESC:
// End editing session
combo.select( Arrays.asList(comboItems).indexOf(data) );
combo.pack();
- combo.addSelectionListener(new SelectionListener() {
+ combo.setData( "parentItem", item );
+
+ treeEditor.minimumWidth = dialogTree.getColumn(2).getWidth();
+ 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();
- TreeEditor treeEditor = (TreeEditor) ((Tree)combo.getParent()).getSelection()[0].getData( "TREEEDITORFORCOMBO" );
+ TreeItem parentItem = (TreeItem) combo.getData("parentItem");
+ TreeEditor treeEditor = (TreeEditor) parentItem.getData( "TREEEDITORFORCOMBO" );
String[] comboItems = { "String", "Number", "Boolean", "Object", "Array" };
- treeEditor.getItem().setText(2, comboItems[combo.getSelectionIndex()]);
- }
- @Override
- public void widgetDefaultSelected(SelectionEvent e) {
+ String selectedText = comboItems[combo.getSelectionIndex()];
+ treeEditor.getItem().setText(2, selectedText);
+
+ if( selectedText.equals("Object") || selectedText.equals("Array") ){
+ treeEditor.getItem().setText(1, "");
+ }
}
});
- treeEditor.minimumWidth = dialogTree.getColumn(2).getWidth();
- treeEditor.horizontalAlignment = SWT.CENTER;
- treeEditor.setEditor(combo, item, 2);
-
- item.setData("TREEEDITORFORCOMBO", treeEditor);
-
return combo;
}
if( dialogTree.getSelection().length > 0 )
parentItem = dialogTree.getSelection()[0];
+ String[] ableTypes = new String[]{ "Object", "Array", "Index" };
+
+ if( parentItem != null && !Arrays.asList(ableTypes).contains(parentItem.getText(2) ) ){
+ return;
+ }
+
TreeItem item;
if (parentItem == null) {
item = new TreeItem(dialogTree, SWT.NONE);
} else {
item = new TreeItem(parentItem, SWT.NONE);
}
- item.setText(new String[] { "data", "test", "String" });
+
+ String key = "data";
+ String value = "test";
+ String type = "String";
+
+ if( parentItem != null && currentSubPage instanceof StaticSubPage && parentItem.getText(2).equals("Array") ){
+ type = "Index";
+ value = "";
+ key = String.valueOf(parentItem.getItemCount()-1);
+ }
+
+ item.setText(new String[] { key, value, type });
TreeItemData treeItemData = new TreeItemData();
treeItemData.setSource( "custom" );
treeItemData.setObjectType("String");
- treeItemData.setObjectValue("String");
+ treeItemData.setObjectValue( type );
item.setData("TREEITEMDATA", treeItemData);
- addComboInTreeItem(item, "String");
+ if( !type.equals("Index") )
+ addComboInTreeItem(item, "String");
+
dialogTree.showItem(item);
+
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
}
private void removeAllItems() {
for (TreeItem treeItem : treeItems) {
removeAllChildrenRecursively(treeItem);
}
+
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
+
// for redraw
dialogTree.setSize(dialogTree.getSize().x, dialogTree.getSize().y - 1);
dialogTree.setSize(dialogTree.getSize().x, dialogTree.getSize().y + 1);
item.setText(str);
item.setData( "TREEITEMDATA", data);
dialogTree.select(item);
+
+ if( currentSubPage instanceof StaticSubPage ){
+ staticSubPage.updateJsonFromTree();
+ }
}
}
JsonObject json = Helper.readJsonFromFile( jsonFile );
+ for( Control control : parentDataTree.getChildren() ){
+ control.dispose();
+ }
+ parentDataTree.removeAll();
+
if (json != null) {
jsonString = prettyGson.toJson( json );
staticSourceStyledText.setText( jsonString );
}else{
jsonString = "";
staticSourceStyledText.setText( "" );
- for( Control control : parentDataTree.getChildren() ){
- control.dispose();
- }
- parentDataTree.removeAll();
}
}
};
staticSourceStyledText.addModifyListener(new ModifyListener() {
@Override
public void modifyText(ModifyEvent e) {
+
+ if( staticSourceStyledText.getData( "editing" ) != null && (Boolean) staticSourceStyledText.getData( "editing" ) )
+ return;
+
JsonObject json = null;
try{
json = normalGson.fromJson(staticSourceStyledText.getText(), JsonObject.class);
}catch(Exception ex){}
if(json != null){
+ for ( Control control : parentDataTree.getChildren() ) {
+ control.dispose();
+ }
parentDataTree.removeAll();
Helper.makeTreeItem( json, parentDataTree, null, true );
}
});
}
-// private void makeTreeFromJsonText( String text, Object object ) {
-//
-// JsonObject jsonObj = null;
-//
-// try{
-// jsonObj = normalGson.fromJson( text, JsonObject.class );
-// }catch(Exception e){
-// MessageBox messageBox = new MessageBox(parentGroup.getShell(), SWT.ICON_ERROR);
-// messageBox.setMessage("Invalid JSON Text");
-// messageBox.open();
-// return;
-// }
-// Helper.makeTreeItem( jsonObj, parentDataTree, null, true );
-// }
-
@Override
public void setButtonsState( Map<String, Button> dataButtonMap ) {
dataButtonMap.get("addTreeItem").setEnabled(true);
staticSourceStyledText.setText( prettyGson.toJson(json) );
jsonFile = dataModel.getStaticFilePath();
-
-// if( json != null )
-// Helper.makeTreeItem(json, parentDataTree, null, true);
}
public Composite getDataComposite() {
return dataComposite;
}
+
+ public void updateJsonFromTree(){
+ if (parentDataTree.getItemCount() > 0 ) {
+ JsonObject root = new JsonObject();
+ TreeItem[] items = parentDataTree.getItems();
+ for (TreeItem item : items) {
+ Helper.makeJsonFromTree( item, root);
+ }
+ staticSourceStyledText.setData( "editing", true );
+ staticSourceStyledText.setText(prettyGson.toJson(root));
+ staticSourceStyledText.setData( "editing", false );
+ }else{
+ staticSourceStyledText.setData( "editing", true );
+ staticSourceStyledText.setText( "" );
+ staticSourceStyledText.setData( "editing", false );
+ }
+ }
}