Package com.onpositive.gae.baseviewer

Source Code of com.onpositive.gae.baseviewer.BlobStoreTableUI$CreateAction

package com.onpositive.gae.baseviewer;

import java.io.File;
import java.util.HashSet;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.google.appengine.api.datastore.Entity;
import com.onpositive.commons.ui.dialogs.TitledDialog;
import com.onpositive.commons.ui.tableeditor.Field;
import com.onpositive.commons.ui.tableeditor.OperationCallback;
import com.onpositive.gae.baseviewer.EditorUtility.IUpdateList;
import com.onpositive.semantic.model.binding.Binding;
import com.onpositive.semantic.model.realm.IFactory;
import com.onpositive.semantic.model.ui.property.editors.CompositeEditor;
import com.onpositive.semantic.model.ui.property.editors.DisposeBindingListener;
import com.onpositive.semantic.model.ui.property.editors.OneLineTextElement;

public class BlobStoreTableUI extends DataStoreTableUI {

  public final class CreateAction extends Action {
    {
      setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(
          Activator.PLUGIN_ID, "/icons/add_exc.gif"));
    }

    private CreateAction(String text) {
      super(text);
    }

    public void run() {
      com.google.apphosting.api.ApiProxy
          .setEnvironmentForCurrentThread(new FakeEnvironment(
              getAppId()));
      Entity e = new Entity(facade.getKind());
      openDialog(e, false);
    }
  }

 
  public IAction createEditAction() {
    IAction editAction = new Action("Store Current Blob") {

      {
        setImageDescriptor(AbstractUIPlugin.imageDescriptorFromPlugin(
            Activator.PLUGIN_ID, "/icons/text_edit.gif"));

      }

      public void run() {
        openDialog(currentSelection.getFirstElement(), true);
      }

    };
    return editAction;
  }

  private final class EditDialog extends TitledDialog {
    private final CompositeEditor editor;
    private IUpdateList callback;

    public void setUpdatingCallback(IUpdateList callback) {
      this.callback = callback;
    }

    private EditDialog(CompositeEditor editor) {
      super(editor);
      this.editor = editor;
    }

    protected void okPressed() {
      ((Binding) bnd).commit();
      if (callback != null) {
        Entity e = (Entity) ((Binding) bnd).getObject();
        callback.update(e);
      }
      // check if
      if (((Binding) bnd).isParsingEnabled()) {
        ((Binding) bnd).switchParsingMode(false);
      }
      super.okPressed();
    }

  }

  private final class EditingBinding extends Binding {
    private final Entity e;
    private boolean edit;

    protected EditingBinding(Object object, Entity e, boolean edit) {
      super(object);
      this.e = e;
      this.edit = edit;
    }

    public void commit() {
      String storedFilePath = "";
      for (String b : getKnownChilds()) {
        // if(facade.getKindsOfChildren().containsKey(b)){
        // continue;
        // }
        Binding binding = getBinding(b);
        Object value = binding.getValue();
        int count = 0;
        try {
          if (b.equals(BlobStoreDataFacade.FILENAME_PROP)) {
            storedFilePath = (String) value;// + File.pathSeparator
                            // +
                            // (String)e.getProperty(b);
          }
          if (!edit) {
            e.setProperty(b, value);
          }

        } catch (Exception e) {
          MessageDialog.openError(Display.getDefault()
              .getActiveShell(), "Error", e.getMessage());
          throw new RuntimeException(e);
        }
      }
      OperationCallback callback = new OperationCallback() {

        public void passed(Object object) {
          doRefresh();
        }

        public void failed(Exception exception) {
          MessageDialog.openError(Display.getDefault()
              .getActiveShell(), "Error", exception.getMessage());
        }
      };
      if (edit) {
        ((BlobStoreDataFacade) facade).store(e, callback,
            storedFilePath);
      } else {
        facade.add(e, callback);
      }

    }
  }

  public void doRefresh() {
    facade.refresh();
  }

  private boolean isSupported(Class clz) {
    boolean flag = false;
    for (Class z : EditorUtility.SUPPORTED_TYPES) {
      if (z.isAssignableFrom(clz)) {
        flag = true;
      }
    }
    return flag;
  }

 
  public IAction createCreateAction() {
    return new CreateAction("Add new Blob");
  }

  public Action createActions(ToolBarManager toolBarManager, boolean full) {
    toolBarManager.add(new CreateAction("Create new blob"));
    DeleteAction action = new DeleteAction("Delete selected");
    toolBarManager.add(action);
    toolBarManager.add(new RefreshAction("Refresh view"));
    return action;
  }

  protected boolean openDialog(Object selection, final boolean edit) {
    com.google.apphosting.api.ApiProxy
        .setEnvironmentForCurrentThread(new FakeEnvironment(getAppId()));

    final Entity entity = (Entity) selection;

    CompositeEditor editor = new CompositeEditor();
    final EditingBinding bnd = new EditingBinding(entity, entity, edit);
    if (!edit) {
      bnd.setName("New " + facade.kind);
    } else {
      bnd.setName("Store " + facade.kind);
    }
    editor.setBinding(bnd);
    for (Field f : facade.getFields()) {
      final Binding binding = bnd.getBinding(f.name);
      if (!edit) {
        binding.setName(f.getTitle());
      } else {
        binding.setName(f.getTitle());
      }
      // Object o = binding.getValue();
      if (f.name.equals(BlobStoreDataFacade.FILENAME_PROP)) {
        binding.setReadOnly(false);
        String tmpS = "";
        if (edit && false) {
          tmpS = (String) entity.getProperty(f.name);
          binding.setValue(tmpS, null);
        }
        final String fname = tmpS;

        binding.setFactory(new IFactory() {

          public Object getValue(Object context) {
            FileDialog fd = null;
            if (!edit) {
              fd = new FileDialog(Display.getCurrent()
                  .getActiveShell(), SWT.OPEN);
              fd.setText("Select file");
              String open = ((FileDialog) fd).open();
              return open;
            } else {
              DirectoryDialog dd = new DirectoryDialog(Display
                  .getCurrent().getActiveShell(), SWT.SAVE);
              dd.setText("Store file");
              String choosenDir = dd.open();

              binding.setValue(fname, null);
              if (choosenDir == null) {
              }
              return choosenDir;// + File.pathSeparator + fname;
            }

          }

          public String getName() {
            if (!edit) {
              return "Import File";
            } else {
              return "Change Directory";
            }
          }

          public String getDescription() {
            return "";
          }

        });
        OneLineTextElement<Object> m = new OneLineTextElement<Object>(
            binding) {
          protected void onValue(Object value) {
            commitToBinding(value);
          }

        };
        m.setReadOnly(true);
        editor.add(m);

      } /*
       * else if (f.name.equals(BlobStoreDataFacade.BLOBKEY_PROP)) {
       * binding.setReadOnly(true); OneLineTextElement<String> t = new
       * OneLineTextElement<String>(); t.setBinding(binding);
       * editor.add(t); }
       */
    }

    final TitledDialog dlg = new EditDialog(editor);
    dlg.setResizable(false);
    DisposeBindingListener.linkBindingLifeCycle(bnd, editor);
    int code = dlg.open();
    if (code == Window.OK) {
      return true;
    } else {
      return false;
    }
  }

 
  public boolean open(Object selection, boolean drawParentLink) {
    boolean flg = openDialog(selection, true);
    return flg;
  }
}
TOP

Related Classes of com.onpositive.gae.baseviewer.BlobStoreTableUI$CreateAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.