/**
   * Create contents of the dialog.
   *
   * @param parent
   */
  @Override
  protected Control createDialogArea(Composite parent) {
    Composite container = (Composite) super.createDialogArea(parent);
    GridLayout gridLayout = (GridLayout) container.getLayout();
    gridLayout.verticalSpacing = 2;
    gridLayout.horizontalSpacing = 2;
    gridLayout.marginHeight = 2;
    gridLayout.marginWidth = 2;

    Composite compositeBody = new Composite(container, SWT.NONE);
    compositeBody.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    compositeBody.setLayout(new GridLayout(1, false));

    tadpoleEditor =
        new TadpoleEditorWidget(
            compositeBody, SWT.BORDER, EditorDefine.EXT_JSON, JSONUtil.getPretty(content), "");
    tadpoleEditor.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    initUI();

    // google analytic
    AnalyticCaller.track(this.getClass().getName());

    return container;
  }
  /** execute map reduce */
  private void executeMapReduce() throws Exception {
    String strMap = textMap.getText();
    String strReduce = textReduce.getText();
    String strFinilize = textFinalize.getText();
    String strOutputTarget = textOutputTarget.getText();
    MapReduceCommand.OutputType outputType =
        (MapReduceCommand.OutputType) comboOutputType.getData(comboOutputType.getText());

    DBObject dbQuery = null;
    if (!"".equals(textQuery.getText())) dbQuery = (DBObject) JSON.parse(textQuery.getText());

    DBObject dbSort = null;
    if (!"".equals(textSort.getText())) dbSort = (DBObject) JSON.parse(textSort.getText());

    // 쿼리 합니다.
    DBCollection dbCol = MongoDBQuery.findCollection(userDB, initColName);
    MapReduceCommand mrCmd =
        new MapReduceCommand(dbCol, strMap, strReduce, strOutputTarget, outputType, dbQuery);
    if (!"".equals(strFinilize)) mrCmd.setFinalize(strFinilize);
    if (dbSort != null) mrCmd.setSort(dbSort);
    if (getLimit() > 0) mrCmd.setLimit(getLimit());
    if (btnJsMode.getSelection()) mrCmd.addExtraOption("jsMode", true);

    final BasicDBObject searchObj = (BasicDBObject) mrCmd.toDBObject();
    if (btnSharded.getSelection()) ((BasicDBObject) searchObj.get("out")).put("sharded", true);
    if (btnNoneAtomic.getSelection()) ((BasicDBObject) searchObj.get("out")).put("nonAtomic", true);

    goMapReduce(dbCol, searchObj, outputType);
  }
  /** ui초기화 작업을 합니다. */
  private void initUI() {
    //		textMap.setText(TEMPLATE_MAP_SRC);
    //		textReduce.setText(TEMPLATE_REDUCE_SRC);
    //		textFinalize.setText(TEMPLATE_FINALIZE_SRC);

    textMap.setFocus();

    // google analytic
    AnalyticCaller.track(MapReduceEditor.ID);
  }
  @Override
  public void createPartControl(Composite parent) {
    GridLayout gl_parent = new GridLayout(1, false);
    gl_parent.verticalSpacing = 0;
    gl_parent.horizontalSpacing = 0;
    gl_parent.marginHeight = 0;
    gl_parent.marginWidth = 0;
    parent.setLayout(gl_parent);

    SashForm sashFormMain = new SashForm(parent, SWT.VERTICAL);
    sashFormMain.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Composite compositeMapReduceFinalize = new Composite(sashFormMain, SWT.NONE);
    GridLayout gl_compositeMapReduceFinalize = new GridLayout(1, false);
    gl_compositeMapReduceFinalize.verticalSpacing = 1;
    gl_compositeMapReduceFinalize.horizontalSpacing = 1;
    gl_compositeMapReduceFinalize.marginHeight = 1;
    gl_compositeMapReduceFinalize.marginWidth = 1;
    compositeMapReduceFinalize.setLayout(gl_compositeMapReduceFinalize);

    SashForm sashFormMRF = new SashForm(compositeMapReduceFinalize, SWT.NONE);
    sashFormMRF.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Group grpMap = new Group(sashFormMRF, SWT.NONE);
    grpMap.setText("Map");
    GridLayout gl_grpMap = new GridLayout(1, false);
    gl_grpMap.verticalSpacing = 1;
    gl_grpMap.horizontalSpacing = 1;
    gl_grpMap.marginHeight = 1;
    gl_grpMap.marginWidth = 1;
    grpMap.setLayout(gl_grpMap);

    String strAssist = CollectionUtils.getAssistList(userDB, initColName);

    textMap = new TadpoleEditorWidget(grpMap, SWT.BORDER, EditorDefine.EXT_JSON, "", strAssist);
    textMap.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Group grpReduce = new Group(sashFormMRF, SWT.NONE);
    grpReduce.setText("Reduce");
    GridLayout gl_grpReduce = new GridLayout(1, false);
    gl_grpReduce.verticalSpacing = 1;
    gl_grpReduce.horizontalSpacing = 1;
    gl_grpReduce.marginHeight = 1;
    gl_grpReduce.marginWidth = 1;
    grpReduce.setLayout(gl_grpReduce);

    textReduce =
        new TadpoleEditorWidget(grpReduce, SWT.BORDER, EditorDefine.EXT_JSON, "", strAssist);
    textReduce.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Group grpFinalize = new Group(sashFormMRF, SWT.NONE);
    grpFinalize.setText("Finalize");
    GridLayout gl_grpFinalize = new GridLayout(1, false);
    gl_grpFinalize.verticalSpacing = 1;
    gl_grpFinalize.horizontalSpacing = 1;
    gl_grpFinalize.marginHeight = 1;
    gl_grpFinalize.marginWidth = 1;
    grpFinalize.setLayout(gl_grpFinalize);

    textFinalize =
        new TadpoleEditorWidget(grpFinalize, SWT.BORDER, EditorDefine.EXT_JSON, "", strAssist);
    textFinalize.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    sashFormMRF.setWeights(new int[] {4, 4, 2});

    Composite compositeInOut = new Composite(sashFormMain, SWT.NONE);
    GridLayout gl_compositeInOut = new GridLayout(1, false);
    gl_compositeInOut.verticalSpacing = 1;
    gl_compositeInOut.horizontalSpacing = 1;
    gl_compositeInOut.marginHeight = 1;
    gl_compositeInOut.marginWidth = 1;
    compositeInOut.setLayout(gl_compositeInOut);

    SashForm sashForm = new SashForm(compositeInOut, SWT.NONE);
    sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Group grpInput = new Group(sashForm, SWT.NONE);
    grpInput.setText("Input");
    GridLayout gl_grpInput = new GridLayout(2, false);
    gl_grpInput.verticalSpacing = 2;
    gl_grpInput.horizontalSpacing = 2;
    gl_grpInput.marginHeight = 2;
    gl_grpInput.marginWidth = 2;
    grpInput.setLayout(gl_grpInput);

    Label lblQuery = new Label(grpInput, SWT.NONE);
    lblQuery.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblQuery.setText("Query");

    textQuery = new TadpoleEditorWidget(grpInput, SWT.BORDER, EditorDefine.EXT_JSON, "", strAssist);
    textQuery.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Label lblSort = new Label(grpInput, SWT.NONE);
    lblSort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblSort.setText("Sort");

    textSort = new TadpoleEditorWidget(grpInput, SWT.BORDER, EditorDefine.EXT_JSON, "", strAssist);
    textSort.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    Label lblLimit = new Label(grpInput, SWT.NONE);
    lblLimit.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblLimit.setText("Limit");

    textLimit = new Text(grpInput, SWT.BORDER);
    textLimit.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

    Group grpOutput_1 = new Group(sashForm, SWT.NONE);
    grpOutput_1.setText("Output");
    GridLayout gl_grpOutput_1 = new GridLayout(2, false);
    gl_grpOutput_1.verticalSpacing = 2;
    gl_grpOutput_1.horizontalSpacing = 2;
    gl_grpOutput_1.marginHeight = 2;
    gl_grpOutput_1.marginWidth = 2;
    grpOutput_1.setLayout(gl_grpOutput_1);

    Label lblType = new Label(grpOutput_1, SWT.NONE);
    lblType.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblType.setText("Type");

    comboOutputType = new Combo(grpOutput_1, SWT.READ_ONLY);
    comboOutputType.setEnabled(false);
    comboOutputType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    for (OutputType outputType : MapReduceCommand.OutputType.values()) {
      comboOutputType.add(outputType.toString());
      comboOutputType.setData(outputType.toString(), outputType);
    }
    comboOutputType.select(3);

    Label lblOutput = new Label(grpOutput_1, SWT.NONE);
    lblOutput.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    lblOutput.setText("Output");

    textOutputTarget = new Text(grpOutput_1, SWT.BORDER);
    textOutputTarget.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
    new Label(grpOutput_1, SWT.NONE);

    btnSharded = new Button(grpOutput_1, SWT.CHECK);
    btnSharded.setText("Sharded ");
    new Label(grpOutput_1, SWT.NONE);

    btnNoneAtomic = new Button(grpOutput_1, SWT.CHECK);
    btnNoneAtomic.setText("None Atomic");
    new Label(grpOutput_1, SWT.NONE);

    btnJsMode = new Button(grpOutput_1, SWT.CHECK);
    btnJsMode.setText("JS Mode");
    new Label(grpOutput_1, SWT.NONE);

    Button btnExecute = new Button(grpOutput_1, SWT.NONE);
    btnExecute.addSelectionListener(
        new SelectionAdapter() {
          @Override
          public void widgetSelected(SelectionEvent e) {
            try {
              executeMapReduce();
            } catch (Exception e1) {
              logger.error("MapReduce Error", e1); // $NON-NLS-1$
              Status errStatus =
                  new Status(
                      IStatus.ERROR, Activator.PLUGIN_ID, e1.getMessage(), e1); // $NON-NLS-1$
              ExceptionDetailsErrorDialog.openError(
                  null,
                  "Error",
                  "MapReduce Search exception",
                  errStatus); //$NON-NLS-1$ //$NON-NLS-2$
            }
          }
        });
    btnExecute.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
    btnExecute.setText("Search");

    sashForm.setWeights(new int[] {1, 1});

    compositeResult =
        new MongodbResultComposite(sashFormMain, SWT.NONE, userDB, initColName, false);
    compositeResult.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
    GridLayout gl_compositeResult = new GridLayout(1, false);
    gl_compositeResult.verticalSpacing = 0;
    gl_compositeResult.horizontalSpacing = 0;
    gl_compositeResult.marginHeight = 0;
    gl_compositeResult.marginWidth = 0;
    compositeResult.setLayout(gl_compositeResult);

    sashFormMain.setWeights(new int[] {40, 30, 30});

    initUI();

    // google analytic
    AnalyticCaller.track(this.getClass().getName());
  }