This is an automated email from the ASF dual-hosted git repository.

ebakke pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git


The following commit(s) were added to refs/heads/master by this push:
     new 8539677f39 implemented closing of a whole document
8539677f39 is described below

commit 8539677f39ced4c58daaf35745b16f491ef0582d
Author: Richard Michalik <[email protected]>
AuthorDate: Sat Oct 15 14:12:08 2022 +0200

    implemented closing of a whole document
    
    fixed code according to review
    
    added missing condition, refactored code
    
    extracted method to close document list
    
    removed unused code and variable
    
    renamed closing method
    
    fixed bug where popup was closing when only one tab was remaining in other 
project
    
    Made changes based on review
---
 .../core/multitabs/impl/ButtonPopupSwitcher.java   | 13 +++--
 .../core/multitabs/impl/DocumentSwitcherTable.java | 59 +++++++++++++++++-----
 2 files changed, 55 insertions(+), 17 deletions(-)

diff --git 
a/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/ButtonPopupSwitcher.java
 
b/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/ButtonPopupSwitcher.java
index 5cc8eb9889..c24c95e74d 100644
--- 
a/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/ButtonPopupSwitcher.java
+++ 
b/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/ButtonPopupSwitcher.java
@@ -205,8 +205,7 @@ final class ButtonPopupSwitcher implements 
MouseInputListener, AWTEventListener,
     
     @Override
     public void mousePressed(MouseEvent e) {
-        int tabCount = controller.getTabModel().size();
-        if( pTable.onMouseEvent(e) && tabCount == 1 ) {
+        if( pTable.onMouseEvent(e) ) {
             hideCurrentPopup();
         }
         e.consume();
@@ -230,8 +229,8 @@ final class ButtonPopupSwitcher implements 
MouseInputListener, AWTEventListener,
         p = SwingUtilities.convertPoint((Component) e.getSource(), p, pTable);
         if (pTable.contains(p)) {
             if( !pTable.onMouseEvent(e) ) {
-                final SwitcherTableItem item = pTable.getSelectedItem();
-                if (item != null) {
+                final DocumentSwitcherTable.Item item = 
pTable.getSelectedItem();
+                if (item != null && !item.isSeparator()) {
                     hideCurrentPopup();
                     item.activate();
                 }
@@ -343,7 +342,7 @@ final class ButtonPopupSwitcher implements 
MouseInputListener, AWTEventListener,
                 break;
             case KeyEvent.VK_DELETE: {
                 final Item item = ( Item ) pTable.getSelectedItem();
-                if (item != null && TabDataRenderer.isClosable( 
item.getTabData() )) {
+                if (item != null) {
                     TabData tab = item.getTabData();
                     int tabIndex = controller.getTabModel().indexOf( tab );
                     if( tabIndex >= 0 ) {
@@ -355,6 +354,10 @@ final class ButtonPopupSwitcher implements 
MouseInputListener, AWTEventListener,
                         selRow = Math.min( pTable.getModel().getRowCount()-1, 
selRow );
                         selCol = Math.min( 
pTable.getModel().getColumnCount()-1, selCol );
                         switched = true;
+                    } else if ( item.isSeparator() ){
+                        if( pTable.closeSelectedDocumentList() ) {
+                            SwingUtilities.invokeLater(this::hideCurrentPopup);
+                        }
                     }
                 }
                 break;
diff --git 
a/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/DocumentSwitcherTable.java
 
b/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/DocumentSwitcherTable.java
index d853f14dea..006a3afc8f 100644
--- 
a/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/DocumentSwitcherTable.java
+++ 
b/platform/core.multitabs/src/org/netbeans/core/multitabs/impl/DocumentSwitcherTable.java
@@ -27,6 +27,8 @@ import java.awt.Insets;
 import java.awt.Point;
 import java.awt.Rectangle;
 import java.awt.event.MouseEvent;
+import java.util.ArrayList;
+import java.util.List;
 import javax.swing.BorderFactory;
 import javax.swing.DefaultButtonModel;
 import javax.swing.Icon;
@@ -77,10 +79,8 @@ class DocumentSwitcherTable extends SwitcherTable {
     }
 
     @Override
-    public SwitcherTableItem getSelectedItem() {
+    public Item getSelectedItem() {
         Item res = ( Item ) getValueAt(getSelectedRow(), getSelectedColumn());
-        if( null != res && res.isSeparator )
-            return null; //don't hide the popup when a project separator is 
selected
         return res;
     }
 
@@ -90,7 +90,7 @@ class DocumentSwitcherTable extends SwitcherTable {
 
         boolean selected = row == getSelectedRow() &&
                 column == getSelectedColumn() && item != null;
-        boolean separator = null != item && item.isSeparator;
+        boolean separator = null != item && item.isSeparator();
 
         ITEM_BORDER.color = null;
         Component renComponent = super.prepareRenderer( renderer, row, column 
);
@@ -105,10 +105,10 @@ class DocumentSwitcherTable extends SwitcherTable {
                 lbl.setBorder( ITEM_BORDER );
             }
         }
-        if( selected && !separator ) {
+        if( selected ) {
             JPanel res = new JPanel( new BorderLayout(5, 0) );
             res.add( renComponent, BorderLayout.CENTER );
-            if( TabDataRenderer.isClosable( item.getTabData() ) ) {
+            if( separator || TabDataRenderer.isClosable( item.getTabData() )) {
                 res.add( btnClose, BorderLayout.EAST );
             }
             res.setBackground( renComponent.getBackground() );
@@ -127,15 +127,20 @@ class DocumentSwitcherTable extends SwitcherTable {
     private int lastCol = -1;
     private boolean inCloseButtonRect = false;
 
+    /**
+     * 
+     * @return True if popup window should be closed. (e.g. when no more 
+     * tabs are left opened)
+     */
     boolean onMouseEvent( MouseEvent e ) {
         Point p = e.getPoint();
         p = SwingUtilities.convertPoint((Component) e.getSource(), p, this);
         int selRow = getSelectedRow();
         int selCol = getSelectedColumn();
-        if( selRow < 0 || selCol < 0 )
+        if ( selRow < 0 || selCol < 0 )
             return false;
         Rectangle rect = getCellRect( selRow, selCol, false );
-        if( rect.contains( p ) ) {
+        if ( rect.contains( p ) ) {
             Dimension size = btnClose.getPreferredSize();
             int x = rect.x+rect.width-size.width;
             int y = rect.y + (rect.height-size.height)/2;
@@ -143,15 +148,18 @@ class DocumentSwitcherTable extends SwitcherTable {
             boolean inButton = btnRect.contains( p );
             boolean mustRepaint = inCloseButtonRect != inButton;
             inCloseButtonRect = inButton;
-            if( inButton ) {
-                if( e.getID() == MouseEvent.MOUSE_PRESSED ) {
+            if ( inButton ) {
+                if ( e.getID() == MouseEvent.MOUSE_PRESSED ) {
                     Item item = ( Item ) getModel().getValueAt( selRow, selCol 
);
                     TabData tab = item.getTabData();
+                    int tabSize = controller.getTabModel().size();
                     int tabIndex = controller.getTabModel().indexOf( tab );
                     if( tabIndex >= 0 ) {
                         TabActionEvent tae = new TabActionEvent( this, 
TabbedContainer.COMMAND_CLOSE, tabIndex );
                         controller.postActionEvent( tae );
-                        return true;
+                        return tabSize == 1;
+                    } else if ( item.isSeparator() ) {
+                        return closeSelectedDocumentList();
                     }
                 }
             }
@@ -174,7 +182,30 @@ class DocumentSwitcherTable extends SwitcherTable {
         }
         return null;
     }
-
+    /**
+     * Method closes tabs in selected document list
+     * 
+     * @return True if there are no more tabs left opened in other projects
+     */
+    boolean closeSelectedDocumentList() {
+        List<TabData> tabs = new 
ArrayList<>(controller.getTabModel().getTabs());
+        Item item = ( Item ) getModel().getValueAt( getSelectedRow(), 
getSelectedColumn());
+        ProjectProxy project = item.getProject();
+        ProjectSupport projectSupport = ProjectSupport.getDefault();
+        int numOfOtherTabs = 0;
+        for ( TabData tab : tabs ) {  
+            ProjectProxy projectForTab = projectSupport.getProjectForTab( tab 
);
+            if (( project == null && projectForTab == null ) || ( 
projectForTab != null && projectForTab.equals( project ))) {
+                int tabIndex = controller.getTabModel().indexOf( tab );
+                TabActionEvent tae = new TabActionEvent( this, 
TabbedContainer.COMMAND_CLOSE, tabIndex );
+                controller.postActionEvent( tae );
+            } else {
+                numOfOtherTabs++;
+            }
+        }
+        return numOfOtherTabs == 0;
+    }
+    
     private JButton createCloseButton() {
         JButton res = CloseButtonFactory.createBigCloseButton();
         res.setModel( new DefaultButtonModel() {
@@ -223,6 +254,10 @@ class DocumentSwitcherTable extends SwitcherTable {
         public TabData getTabData() {
             return tabData;
         }
+        
+        public boolean isSeparator() {
+            return isSeparator;
+        }
 
         @Override
         public int compareTo( Object o ) {


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to