PageRenderTime 63ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/edgytech/umongo/DocBuilderField.java

https://github.com/agirbal/umongo
Java | 169 lines | 112 code | 22 blank | 35 comment | 6 complexity | 3700f631c37e3ce269e73475cd39a71e MD5 | raw file
  1. /**
  2. * Copyright (C) 2010 EdgyTech LLC.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.edgytech.umongo;
  17. import com.edgytech.swingfast.*;
  18. import com.edgytech.umongo.DocBuilderField.Item;
  19. import com.mongodb.DBObject;
  20. import com.mongodb.util.JSON;
  21. import java.awt.event.FocusEvent;
  22. import java.awt.event.FocusListener;
  23. import java.util.logging.Level;
  24. /**
  25. *
  26. * @author antoine
  27. */
  28. public class DocBuilderField extends Div implements EnumListener, FocusListener {
  29. enum Item {
  30. expandText,
  31. jsonText,
  32. edit,
  33. validate,
  34. }
  35. @Serial
  36. public String dialogId;
  37. @Serial
  38. public boolean nonEmpty;
  39. @SerialStar
  40. public String value;
  41. DBObject doc;
  42. /**
  43. * Creates a new instance of FieldFile
  44. */
  45. public DocBuilderField() {
  46. nonEmpty = false;
  47. try {
  48. xmlLoad(Resource.getXmlDir(), Resource.File.docBuilderField, null);
  49. // need to still load fields from other config, and do a proper checkpoint
  50. setState(State.STRUCTURE);
  51. } catch (Exception ex) {
  52. getLogger().log(Level.SEVERE, null, ex);
  53. }
  54. setEnumBinding(Item.values(), this);
  55. }
  56. @Override
  57. public void actionPerformed(Enum enm, XmlComponentUnit unit, Object src) {
  58. }
  59. public void edit(ButtonBase button) {
  60. String txt = getComponentStringFieldValue(Item.jsonText);
  61. try {
  62. doc = (DBObject) JSON.parse(txt);
  63. } catch (Exception ex) {
  64. // this could be because of binary in field
  65. getLogger().log(Level.INFO, null, ex);
  66. }
  67. DocBuilderDialog dialog = UMongo.instance.getGlobalStore().getDocBuilderDialog();
  68. dialog.setDBObject(doc);
  69. if (!dialog.show()) {
  70. return;
  71. }
  72. doc = dialog.getDBObject();
  73. value = MongoUtils.getJSON(doc);
  74. setComponentStringFieldValue(Item.jsonText, value);
  75. notifyListener(getComponent());
  76. }
  77. public void expandText(ButtonBase button) {
  78. String txt = getComponentStringFieldValue(Item.jsonText);
  79. JSONTextDialog dia = UMongo.instance.getGlobalStore().getJSONTextDialog();
  80. dia.setText(txt);
  81. if (dia.show()) {
  82. setComponentStringFieldValue(Item.jsonText, dia.getText());
  83. }
  84. }
  85. @Override
  86. public void focusGained(FocusEvent e) {
  87. }
  88. @Override
  89. public void focusLost(FocusEvent e) {
  90. notifyListener(getComponent());
  91. }
  92. ////////////////////////////////////////////////////////////////////////
  93. // Component
  94. ////////////////////////////////////////////////////////////////////////
  95. @Override
  96. protected boolean checkComponentCustom(BoxPanel comp) {
  97. // String txt = _field.getText().trim();
  98. String txt = getComponentStringFieldValue(Item.jsonText);
  99. if (nonEmpty && txt.isEmpty()) {
  100. setDisplayError("Field cannot be empty");
  101. return false;
  102. }
  103. if (!getComponentBooleanFieldValue(Item.validate)) {
  104. return true;
  105. }
  106. try {
  107. // 1st parse with GSON to check, since our parser has bugs
  108. MongoUtils.getJsonParser().parse(txt);
  109. DBObject doc = (DBObject) JSON.parse(txt);
  110. return true;
  111. } catch (Exception e) {
  112. // this could be because of binary in field
  113. getLogger().log(Level.INFO, null, e);
  114. }
  115. setDisplayError("Invalid JSON format: correct or disable validation");
  116. return false;
  117. }
  118. @Override
  119. protected void updateComponentCustom(BoxPanel old) {
  120. setStringFieldValue(Item.jsonText, value);
  121. ((TextArea)getComponentBoundUnit(Item.jsonText)).editable = enabled;
  122. getComponentBoundUnit(Item.edit).enabled = enabled;
  123. getComponentBoundUnit(Item.expandText).enabled = enabled;
  124. }
  125. @Override
  126. protected void commitComponentCustom(BoxPanel comp) {
  127. // here we want to commit the string value, but doc is already uptodate
  128. try {
  129. // value = _field.getText();
  130. value = getStringFieldValue(Item.jsonText);
  131. doc = (DBObject) JSON.parse(value);
  132. } catch (Exception e) {
  133. // this could be because of binary in field
  134. // in this case the doc already has the correct inner value
  135. getLogger().log(Level.INFO, null, e);
  136. }
  137. }
  138. public void setDBObject(DBObject obj) {
  139. // it's safe to use obj, not a copy, since builder will build its own
  140. doc = obj;
  141. value = doc != null ? MongoUtils.getJSON(doc) : "";
  142. setStringFieldValue(Item.jsonText, value);
  143. }
  144. public DBObject getDBObject() {
  145. return doc;
  146. }
  147. }