changeset 8050:6d5af625b22a

[SCENEBUILDER] Moved content of EditorController.performAddTooltip() into AddTooltipToSelectionJob class.
author Eric Le Ponner <eric.le.ponner@oracle.com>
date Fri, 12 Sep 2014 11:43:40 +0200
parents 787b2e2d5c86
children 6cabbc998483
files apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/editor/EditorController.java apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/editor/job/AddTooltipToSelectionJob.java
diffstat 2 files changed, 156 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/editor/EditorController.java	Fri Sep 12 11:15:37 2014 +0200
+++ b/apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/editor/EditorController.java	Fri Sep 12 11:43:40 2014 +0200
@@ -35,7 +35,7 @@
 import com.oracle.javafx.scenebuilder.kit.editor.drag.DragController;
 import com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N;
 import com.oracle.javafx.scenebuilder.kit.editor.job.AddContextMenuToSelectionJob;
-import com.oracle.javafx.scenebuilder.kit.editor.job.BatchJob;
+import com.oracle.javafx.scenebuilder.kit.editor.job.AddTooltipToSelectionJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.ModifySelectionJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.BringForwardJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.BringToFrontJob;
@@ -45,7 +45,6 @@
 import com.oracle.javafx.scenebuilder.kit.editor.job.FitToParentSelectionJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.ImportFileJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.IncludeFileJob;
-import com.oracle.javafx.scenebuilder.kit.editor.job.InsertAsAccessoryJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.InsertAsSubComponentJob;
 import com.oracle.javafx.scenebuilder.kit.editor.job.Job;
 import com.oracle.javafx.scenebuilder.kit.editor.job.PasteIntoJob;
@@ -2236,42 +2235,9 @@
      */
     public void performAddTooltip() {
         assert canPerformAddTooltip(); // (1)
-
-        // Build the Tooltip item from the library builtin items
-        final String tooltipFxmlPath = "builtin/Tooltip.fxml"; //NOI18N
-        final URL tooltipFxmlURL 
-                = BuiltinLibrary.class.getResource(tooltipFxmlPath);
-        assert tooltipFxmlURL != null;
-        try {
-            final String tooltipFxmlText
-                    = FXOMDocument.readContentFromURL(tooltipFxmlURL);
-
-            final AbstractSelectionGroup asg = selection.getGroup();
-            assert asg instanceof ObjectSelectionGroup; // Because of (1)
-            final ObjectSelectionGroup osg = (ObjectSelectionGroup) asg;
-
-            final BatchJob job = new BatchJob(this, true,
-                    I18N.getString("label.action.edit.add.tooltip"));
-            for (FXOMObject fxomObject : osg.getItems()) {
-                final FXOMDocument tooltipDocument = new FXOMDocument(
-                        tooltipFxmlText,
-                        tooltipFxmlURL, getLibrary().getClassLoader(), null);
-
-                assert tooltipDocument != null;
-                final FXOMObject tooltipObject = tooltipDocument.getFxomRoot();
-                assert tooltipObject != null;
-                tooltipObject.moveToFxomDocument(getFxomDocument());
-                assert tooltipDocument.getFxomRoot() == null;
-
-                final Job insertJob = new InsertAsAccessoryJob(
-                        tooltipObject, fxomObject, Accessory.TOOLTIP, this);
-                job.addSubJob(insertJob);
-            }
-            getJobManager().push(job);
-        } catch (IOException x) {
-            throw new IllegalStateException("Bug in " + getClass().getSimpleName(), x); //NOI18N
-        }
-    }
+        final Job addTooltipJob = new AddTooltipToSelectionJob(this);
+        getJobManager().push(addTooltipJob);
+   }
     
     /**
      * Returns the URL of the CSS style associated to EditorController class.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/apps/scenebuilder/SceneBuilderKit/src/com/oracle/javafx/scenebuilder/kit/editor/job/AddTooltipToSelectionJob.java	Fri Sep 12 11:43:40 2014 +0200
@@ -0,0 +1,152 @@
+/*
+ * Copyright (c) 2012, 2014, Oracle and/or its affiliates.
+ * All rights reserved. Use is subject to license terms.
+ *
+ * This file is available and licensed under the following license:
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ *  - Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  - Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the distribution.
+ *  - Neither the name of Oracle Corporation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package com.oracle.javafx.scenebuilder.kit.editor.job;
+
+import com.oracle.javafx.scenebuilder.kit.editor.EditorController;
+import com.oracle.javafx.scenebuilder.kit.editor.i18n.I18N;
+import com.oracle.javafx.scenebuilder.kit.editor.selection.AbstractSelectionGroup;
+import com.oracle.javafx.scenebuilder.kit.editor.selection.ObjectSelectionGroup;
+import com.oracle.javafx.scenebuilder.kit.fxom.FXOMDocument;
+import com.oracle.javafx.scenebuilder.kit.fxom.FXOMObject;
+import com.oracle.javafx.scenebuilder.kit.library.BuiltinLibrary;
+import com.oracle.javafx.scenebuilder.kit.library.Library;
+import com.oracle.javafx.scenebuilder.kit.metadata.util.DesignHierarchyMask;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ */
+public class AddTooltipToSelectionJob extends BatchSelectionJob {
+    
+    private Map<FXOMObject, FXOMObject> tooltipMap; // Initialized lazily
+
+    public AddTooltipToSelectionJob(EditorController editorController) {
+        super(editorController);
+    }
+    
+    public Collection<FXOMObject> getTooltips() {
+        constructTooltipMap();
+        return tooltipMap.values();
+    }
+
+    /*
+     * BatchSelectionJob
+     */
+
+    @Override
+    protected List<Job> makeSubJobs() {
+        
+        constructTooltipMap();
+        
+        final List<Job> result = new LinkedList<>();
+        for (Map.Entry<FXOMObject, FXOMObject> e : tooltipMap.entrySet()) {
+            final FXOMObject fxomObject = e.getKey();
+            final FXOMObject tooltipObject = e.getValue();
+            final Job insertJob = new InsertAsAccessoryJob(
+                    tooltipObject, fxomObject, 
+                    DesignHierarchyMask.Accessory.TOOLTIP, 
+                    getEditorController());
+            result.add(insertJob);
+        }
+        
+        return result;
+    }
+
+    @Override
+    protected AbstractSelectionGroup getNewSelectionGroup() {
+        final Collection<FXOMObject> contextMenus = tooltipMap.values();
+        assert contextMenus.isEmpty() == false;
+        final FXOMObject hitMenu = contextMenus.iterator().next();
+        
+        return new ObjectSelectionGroup(contextMenus, hitMenu, null);
+    }
+
+    /*
+     * CompositeJob
+     */
+    
+    @Override
+    protected String makeDescription() {
+        return I18N.getString("label.action.edit.add.tooltip");
+    }
+    
+    
+    /*
+     * Private
+     */
+    
+    private void constructTooltipMap() {
+        if (tooltipMap == null) {
+            tooltipMap = new LinkedHashMap<>();
+            
+            // Build the ContextMenu item from the library builtin items
+            final String tooltipFxmlPath = "builtin/Tooltip.fxml"; //NOI18N
+            final URL tooltipFxmlURL 
+                    = BuiltinLibrary.class.getResource(tooltipFxmlPath);
+            assert tooltipFxmlURL != null;
+
+            final AbstractSelectionGroup asg = getEditorController().getSelection().getGroup();
+            assert asg instanceof ObjectSelectionGroup; // Because of (1)
+            final ObjectSelectionGroup osg = (ObjectSelectionGroup) asg;
+
+            try {
+                final String contextMenuFxmlText
+                        = FXOMDocument.readContentFromURL(tooltipFxmlURL);
+
+                final FXOMDocument fxomDocument = getEditorController().getFxomDocument();
+                final Library library = getEditorController().getLibrary();
+                for (FXOMObject fxomObject : osg.getItems()) {
+                    final FXOMDocument contextMenuDocument = new FXOMDocument(
+                            contextMenuFxmlText,
+                            tooltipFxmlURL, library.getClassLoader(), null);
+
+                    assert contextMenuDocument != null;
+                    final FXOMObject contextMenuObject = contextMenuDocument.getFxomRoot();
+                    assert contextMenuObject != null;
+                    contextMenuObject.moveToFxomDocument(fxomDocument);
+                    assert contextMenuDocument.getFxomRoot() == null;
+
+                    tooltipMap.put(fxomObject, contextMenuObject);
+                }
+            } catch(IOException x) {
+                throw new IllegalStateException("Bug in " + getClass().getSimpleName(), x); //NOI18N
+            }
+        }
+    }
+}