private void initAdvancedModeFromLocalStorage(UnitVmModel model) {
    if (model.getIsAdvancedModeLocalStorageKey() == null) {
      return;
    }

    boolean isAdvancedMode =
        Boolean.parseBoolean(clientStorage.getLocalItem(model.getIsAdvancedModeLocalStorageKey()));
    model.getAdvancedMode().setEntity(isAdvancedMode);
  }
  private void storeAdvancedModeToLocalStorage(UnitVmModel model) {
    if (model.getIsAdvancedModeLocalStorageKey() == null) {
      return;
    }

    clientStorage.setLocalItem(
        model.getIsAdvancedModeLocalStorageKey(),
        Boolean.toString(model.getAdvancedMode().getEntity()));
  }
  private void initListeners(final UnitVmModel model) {
    model
        .getAdvancedMode()
        .getPropertyChangedEvent()
        .addListener(
            new IEventListener<EventArgs>() {

              @Override
              public void eventRaised(
                  Event<? extends EventArgs> ev, Object sender, EventArgs args) {
                storeAdvancedModeToLocalStorage(model);
                swithAccordingToMode(model);
              }
            });

    model
        .getAttachedToInstanceType()
        .getPropertyChangedEvent()
        .addListener(
            new IEventListener<EventArgs>() {
              @Override
              public void eventRaised(
                  Event<? extends EventArgs> ev, Object sender, EventArgs args) {
                swithAttachToInstanceType(model);
              }
            });

    model
        .getValid()
        .getPropertyChangedEvent()
        .addListener(
            new IEventListener<EventArgs>() {
              @Override
              public void eventRaised(
                  Event<? extends EventArgs> ev, Object sender, EventArgs args) {
                switchToAdvancedIfNeeded(model);
              }
            });

    registerHandler(
        getView()
            .getNumaSupportButton()
            .addClickHandler(
                new ClickHandler() {
                  @Override
                  public void onClick(ClickEvent event) {
                    getView().getNumaSupportButton().getCommand().execute();
                  }
                }));
  }
  private void initTabAvailabilityListeners(final UnitVmModel vm) {
    // TODO should be handled by the core framework
    vm.getPropertyChangedEvent()
        .addListener(
            new IEventListener() {
              @Override
              public void eventRaised(Event ev, Object sender, EventArgs args) {
                String propName = ((PropertyChangedEventArgs) args).PropertyName;
                if ("IsHostAvailable".equals(propName)) {
                  hostTab.setVisible(vm.getIsHostAvailable());
                } else if ("IsHostTabValid".equals(propName)) {
                  if (vm.getIsHostTabValid()) {
                    hostTab.markAsValid();
                  } else {
                    hostTab.markAsInvalid(null);
                  }
                }
              }
            });

    // High Availability only avail in server mode
    highAvailabilityTab.setVisible(false);

    // only avail for desktop mode
    isStatelessEditor.setVisible(true);
    numOfMonitorsEditor.setVisible(true);
  }
  private void initTabAvailabilityListeners(final UnitVmModel vm) {
    boolean isDesktop = vm.getVmType().equals(VmType.Desktop);

    // High Availability only available in server mode
    highAvailabilityTab.setVisible(!isDesktop);

    // only available for desktop mode
    isStatelessEditor.setVisible(isDesktop);
    numOfMonitorsEditor.setVisible(isDesktop);
  }
  private void switchToAdvancedIfNeeded(final UnitVmModel model) {
    if (model.getAdvancedMode().getEntity() || model.getValid().getEntity()) {
      return;
    }

    List<HasValidation> invalidWidgets = getView().getInvalidWidgets();

    if (invalidWidgets.size() == 0) {
      return;
    }

    for (HasValidation invalidWidget : invalidWidgets) {
      boolean isVisible =
          invalidWidget instanceof HasVisibility && ((HasVisibility) invalidWidget).isVisible();
      boolean isAttached =
          invalidWidget instanceof HasAttachHandlers
              && ((HasAttachHandlers) invalidWidget).isAttached();
      if (!isVisible || !isAttached) {
        model.getAdvancedMode().setEntity(true);
        break;
      }
    }
  }
 @Override
 public void edit(final UnitVmModel object) {
   super.edit(object);
   object
       .getPropertyChangedEvent()
       .addListener(
           new IEventListener() {
             @Override
             public void eventRaised(Event ev, Object sender, EventArgs args) {
               if (object.getProgress() == null) { // $NON-NLS-1$
                 disableAllTabs();
                 enableEditPoolFields();
               }
             }
           });
 }
 @Override
 protected void build(UnitVmModel model, VmBase vm) {
   super.build(model, vm);
   vm.setAllowConsoleReconnect(model.getAllowConsoleReconnect().getEntity());
   vm.setVmType(model.getVmType().getSelectedItem());
   vm.setVdsGroupId(
       model.getSelectedCluster() != null ? model.getSelectedCluster().getId() : null);
   vm.setTimeZone(
       model.getTimeZone().getIsAvailable() && model.getTimeZone().getSelectedItem() != null
           ? model.getTimeZone().getSelectedItem().getTimeZoneKey()
           : ""); //$NON-NLS-1$
   vm.setIsoPath(
       model.getCdImage().getIsChangable()
           ? model.getCdImage().getSelectedItem()
           : ""); //$NON-NLS-1$
   vm.setDeleteProtected(model.getIsDeleteProtected().getEntity());
   vm.setOsId(model.getOSType().getSelectedItem());
   Guid largeIconId = IconCache.getInstance().getId(model.getIcon().getEntity().getIcon());
   vm.setLargeIconId(largeIconId);
   vm.setSmallIconId(
       model.getIcon().getEntity().getSmallIconId() != null
           ? model.getIcon().getEntity().getSmallIconId()
           : AsyncDataProvider.getInstance().isCustomIconId(largeIconId)
               ? null
               : AsyncDataProvider.getInstance().getSmallByLargeOsDefaultIconId(largeIconId));
   vm.setVncKeyboardLayout(model.getVncKeyboardLayout().getSelectedItem());
   vm.setSerialNumberPolicy(model.getSerialNumberPolicy().getSelectedSerialNumberPolicy());
   vm.setCustomSerialNumber(model.getSerialNumberPolicy().getCustomSerialNumber().getEntity());
   vm.setBootMenuEnabled(model.getBootMenuEnabled().getEntity());
   vm.setSpiceFileTransferEnabled(
       Boolean.TRUE.equals(model.getSpiceFileTransferEnabled().getEntity()));
   vm.setSpiceCopyPasteEnabled(Boolean.TRUE.equals(model.getSpiceCopyPasteEnabled().getEntity()));
   vm.setAutoConverge(model.getAutoConverge().getSelectedItem());
   vm.setMigrateCompressed(model.getMigrateCompressed().getSelectedItem());
   vm.setCustomProperties(model.getCustomPropertySheet().serialize());
   vm.setConsoleDisconnectAction(model.getConsoleDisconnectAction().getSelectedItem());
   if (model.getCpuSharesAmount().getIsAvailable()
       && model.getCpuSharesAmount().getEntity() != null) {
     vm.setCpuShares(model.getCpuSharesAmount().getEntity());
   }
   vm.setCustomCompatibilityVersion(model.getCustomCompatibilityVersion().getSelectedItem());
 }
 private void initToCreateInstanceMode(UnitVmModel model) {
   if (model.isCreateInstanceOnly()) {
     // hide the admin-only widgets only for non-admin users
     getView().initToCreateInstanceMode();
   }
 }
 private void swithAccordingToMode(final UnitVmModel model) {
   getView().switchMode(model.getAdvancedMode().getEntity());
 }
 private void swithAttachToInstanceType(final UnitVmModel model) {
   getView().switchAttachToInstanceType(model.getAttachedToInstanceType().getEntity());
 }