1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 
  33 package com.oracle.javafx.scenebuilder.kit.editor.job.togglegroup;
  34 
  35 import com.oracle.javafx.scenebuilder.kit.editor.EditorController;
  36 import com.oracle.javafx.scenebuilder.kit.editor.job.BatchDocumentJob;
  37 import com.oracle.javafx.scenebuilder.kit.editor.job.Job;
  38 import com.oracle.javafx.scenebuilder.kit.editor.job.atomic.AddPropertyJob;
  39 import com.oracle.javafx.scenebuilder.kit.editor.job.atomic.RemovePropertyJob;
  40 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
  41 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMInstance;
  42 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
  43 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMProperty;
  44 import com.oracle.javafx.scenebuilder.kit.fxom.FXOMPropertyT;
  45 import com.oracle.javafx.scenebuilder.kit.metadata.Metadata;
  46 import com.oracle.javafx.scenebuilder.kit.metadata.property.ValuePropertyMetadata;
  47 import com.oracle.javafx.scenebuilder.kit.metadata.property.value.ToggleGroupPropertyMetadata;
  48 import com.oracle.javafx.scenebuilder.kit.metadata.util.PrefixedValue;
  49 import com.oracle.javafx.scenebuilder.kit.metadata.util.PropertyName;
  50 import com.oracle.javafx.scenebuilder.kit.util.JavaLanguage;
  51 import java.util.ArrayList;
  52 import java.util.List;
  53 
  54 /**
  55  *
  56  */
  57 public class ModifyToggleGroupJob extends BatchDocumentJob {
  58 
  59     private static final PropertyName toggleGroupName
  60             = new PropertyName("toggleGroup"); //NOI18N
  61 
  62     private final FXOMObject targetObject;
  63     private final String toggleGroupId;
  64 
  65     public ModifyToggleGroupJob(FXOMObject fxomObject, String toggleGroupId,
  66             EditorController editorController) {
  67         super(editorController);
  68 
  69         assert fxomObject != null;
  70         assert (toggleGroupId == null) || JavaLanguage.isIdentifier(toggleGroupId);
  71 
  72         this.targetObject = fxomObject;
  73         this.toggleGroupId = toggleGroupId;
  74     }
  75 
  76     /*
  77      * CompositeJob
  78      */
  79 
  80     @Override
  81     protected List<Job> makeSubJobs() {
  82         final List<Job> result = new ArrayList<>();
  83 
  84         if (targetObject instanceof FXOMInstance) {
  85             final FXOMInstance targetInstance = (FXOMInstance) targetObject;
  86             final ValuePropertyMetadata vpm
  87                     = Metadata.getMetadata().queryValueProperty(targetInstance, toggleGroupName);
  88             if (vpm instanceof ToggleGroupPropertyMetadata) {
  89                 /*
  90                  * Case #0 : toggleGroupId is null
  91                  *      => removes toggleGroup FXOMProperty if needed
  92                  *
  93                  * Case #1 : targetObject.toggleGroup is undefined
  94                  *      => adds FXOMPropertyT for toggleGroup="$toggleGroupId"      //NOI18N
  95                  *
  96                  * Case #2 : targetObject defines the ToggleGroup instance
  97                  *      => removes toggleGroup FXOMPropertyC
  98                  *      => adds FXOMPropertyT for toggleGroup="$toggleGroupId"      //NOI18N
  99                  *
 100                  * Case #3 : targetObject refers to a ToggleGroup instance
 101                  *      => removes toggleGroup FXOMPropertyT
 102                  *      => adds FXOMPropertyT for toggleGroup="$toggleGroupId"      //NOI18N
 103                  */
 104 
 105                 final FXOMDocument fxomDocument
 106                         = targetInstance.getFxomDocument();
 107                 final FXOMProperty fxomProperty
 108                         = targetInstance.getProperties().get(toggleGroupName);
 109 
 110                 if (fxomProperty != null) { // Case #0 #2 or #3
 111                     final Job removePropertyJob
 112                             = new RemovePropertyJob(fxomProperty, getEditorController());
 113                     result.add(removePropertyJob);
 114                 }
 115 
 116                 // Case #1, #2 and #3
 117                 if (toggleGroupId != null) {
 118                     final PrefixedValue pv
 119                             = new PrefixedValue(PrefixedValue.Type.EXPRESSION, toggleGroupId);
 120                     final FXOMPropertyT newProperty
 121                             = new FXOMPropertyT(fxomDocument, toggleGroupName, pv.toString());
 122                     final Job addPropertyJob
 123                             = new AddPropertyJob(newProperty, targetInstance, -1, getEditorController());
 124                     result.add(addPropertyJob);
 125                 }
 126             }
 127         }
 128 
 129         return result;
 130     }
 131 
 132     @Override
 133     protected String makeDescription() {
 134         return getClass().getSimpleName(); // Should not reach the user
 135     }
 136 
 137 }