Commits

Anonymous committed 80db7e6

RT-24088: TabPane/Tab should allow veto its closing

Comments (0)

Files changed (2)

javafx-ui-controls/src/com/sun/javafx/scene/control/behavior/TabPaneBehavior.java

 
 import javafx.collections.ObservableList;
 import javafx.event.Event;
+import javafx.scene.Node;
 import javafx.scene.Parent;
-import javafx.scene.Node;
+import javafx.scene.control.SingleSelectionModel;
 import javafx.scene.control.Tab;
 import javafx.scene.control.TabPane;
 import javafx.scene.input.KeyCode;
 
 import com.sun.javafx.scene.control.skin.TabPaneSkin;
 import com.sun.javafx.scene.traversal.Direction;
-import javafx.scene.control.SingleSelectionModel;
 
 public class TabPaneBehavior extends BehaviorBase<TabPane> {
 
         return firstPopulatedEngine;
     }
 
-
     /***************************************************************************
      *                                                                         *
      * Mouse event handling                                                    *
         getControl().getSelectionModel().select(tab);
     }
 
+    public boolean canCloseTab(Tab tab) {
+        Event event = new Event(tab,tab,Tab.TAB_CLOSE_REQUEST);
+        Event.fireEvent(tab, event);
+        return ! event.isConsumed();
+    }
+    
     public void closeTab(Tab tab) {
         TabPane tabPane = getControl();
         // only switch to another tab if the selected tab is the one we're closing

javafx-ui-controls/src/javafx/scene/control/Tab.java

  */
 package javafx.scene.control;
 
-import com.sun.javafx.css.Styleable;
 import com.sun.javafx.css.CssMetaData;
 import javafx.beans.property.BooleanProperty;
 import javafx.beans.property.ObjectProperty;
 import javafx.event.EventType;
 import javafx.scene.Node;
 
+import com.sun.javafx.css.Styleable;
 import com.sun.javafx.event.EventHandlerManager;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import javafx.beans.DefaultProperty;
-import javafx.beans.property.*;
+import javafx.beans.property.BooleanPropertyBase;
+import javafx.beans.property.ReadOnlyBooleanProperty;
+import javafx.beans.property.ReadOnlyBooleanWrapper;
+import javafx.beans.property.ReadOnlyObjectProperty;
+import javafx.beans.property.ReadOnlyObjectWrapper;
 import javafx.collections.ObservableMap;
 
 /**
     /**
      * <p>Called when a user closes this tab. This is useful for freeing up memory.</p>
      */
-    public static final EventType<Event> CLOSED_EVENT = new EventType<Event> (Event.ANY, "TAB_CLOSED");
+    public static final EventType<Event> CLOSED_EVENT = new EventType<Event>(Event.ANY, "TAB_CLOSED");
     private ObjectProperty<EventHandler<Event>> onClosed;
 
     /**
         }
         return disabled;
     }
-
+    
     private void updateDisabled() {
         setDisabled(isDisable() || (getTabPane() != null && getTabPane().isDisabled()));
     }
     
+     /**
+     * Called when there is an external request to close this {@code Tab}.
+     * The installed event handler can prevent tab closing by consuming the
+     * received event.
+     */
+    public static final EventType<Event> TAB_CLOSE_REQUEST_EVENT = new EventType<Event> (Event.ANY, "TAB_CLOSE_REQUEST_EVENT");
+   
+    /**
+     * Called when there is an external request to close this {@code Tab}.
+     * The installed event handler can prevent tab closing by consuming the
+     * received event.
+     */
+    private ObjectProperty<EventHandler<Event>> onCloseRequest;
+    public final ObjectProperty<EventHandler<Event>> onCloseRequestProperty() {
+        if (onCloseRequest == null) {
+            onCloseRequest = new ObjectPropertyBase<EventHandler<Event>>() {
+                @Override protected void invalidated() {
+                    setEventHandler(TAB_CLOSE_REQUEST_EVENT, get());
+                }
+                
+                @Override public Object getBean() {
+                    return Tab.this;
+                }
+
+                @Override public String getName() {
+                    return "onCloseRequest";
+                }
+            };
+        }
+        return onCloseRequest;
+    }
+    
+    public EventHandler<Event> getOnCloseRequest() {
+        if( onCloseRequest == null ) {
+            return null;
+        }
+        return onCloseRequest.get();
+    }
+    
+    public void setOnCloseRequest(EventHandler<Event> value) {
+        onCloseRequestProperty().set(value);
+    }
+
+    
     // --- Properties
     private static final Object USER_DATA_KEY = new Object();
     
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.