[Schmitzm-commits] r2098 - in trunk/schmitzm-core/src/main/java/de/schmitzm/swing: . tree

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Thu Oct 4 01:03:26 CEST 2012


Author: mojays
Date: 2012-10-04 01:03:26 +0200 (Thu, 04 Oct 2012)
New Revision: 2098

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java
Log:
SwingUtil: setWaitCursor(.) and resetCursor(.) modified to block application
ApplicationFrame: setWaitCursor(.) and resetCursor(.) linked to SwingUtil

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java	2012-10-03 20:05:38 UTC (rev 2097)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java	2012-10-03 23:03:26 UTC (rev 2098)
@@ -3,8 +3,10 @@
 import java.awt.AWTEvent;
 import java.awt.BorderLayout;
 import java.awt.Container;
+import java.awt.Cursor;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
 import java.awt.event.WindowEvent;
 
 import javax.swing.Action;
@@ -15,6 +17,7 @@
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JMenuBar;
+import javax.swing.RootPaneContainer;
 import javax.swing.text.JTextComponent;
 
 import org.apache.commons.lang.StringUtils;
@@ -166,6 +169,20 @@
   }
 
   /**
+   * Sets the cursor of the main frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
+   */
+  public void setWaitCursor() {
+    SwingUtil.setWaitCursor(this);
+}
+
+  /**
+   * Sets the cursor of the main frame to the default cursor.
+   */
+  public void resetCursor() {
+    SwingUtil.resetCursor(this);
+  }
+
+  /**
    * Initializes the actions, menu entries and tool bars.
    */
   protected abstract void initActions();

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-10-03 20:05:38 UTC (rev 2097)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-10-03 23:03:26 UTC (rev 2098)
@@ -50,6 +50,7 @@
 import java.awt.Window;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
+import java.awt.event.MouseAdapter;
 import java.awt.event.MouseWheelListener;
 import java.awt.font.TextAttribute;
 import java.awt.image.BufferedImage;
@@ -157,7 +158,11 @@
 	/** A dummy label for default color and font */
 	public static final JLabel DUMMY_LABEL = new JLabel("dummy");
 
-	/**
+    /** Dummy MouseAdapter which does nothing. Used for {@link #setWaitCursor(Component)}
+     *  and {@link #resetCursor(Component)} */
+    private static final MouseAdapter DUMMY_MOUSEADAPTER = new MouseAdapter() {};
+
+    /**
 	 * {@link ResourceProvider}, der die Lokalisation fuer GUI-Komponenten des
 	 * Package {@code schmitzm.swing} zur Verfuegung stellt. Diese sind in
 	 * properties-Dateien unter {@code schmitzm.swing.resource.locales}
@@ -2392,22 +2397,41 @@
       return headerRendererWithColumnTypes;
     }
     
+
     /**
      * Sets the cursor of the parent frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
      */
     public static void setWaitCursor(Component comp) {
+//    // Original code taken from http://www.javaspecialists.eu/archive/Issue065.html
+//    // applicable for JComponent      
+//    RootPaneContainer root = (RootPaneContainer)((JComponent)getContentPane()).getTopLevelAncestor();
+//    root.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
+//    root.getGlassPane().addMouseListener(DUMMY_MOUSEADAPTER);
+//    root.getGlassPane().setVisible(true);
+      
       RootPaneContainer rootPane = getParentRootPaneContainer(comp);
-      if ( rootPane != null && rootPane.getContentPane() != null )
-        rootPane.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
+      if ( rootPane != null && rootPane.getContentPane() != null ) {
+        rootPane.getGlassPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
+        rootPane.getGlassPane().addMouseListener(DUMMY_MOUSEADAPTER);
+        rootPane.getGlassPane().setVisible(true);
+      }
     }
 
     /**
      * Sets the cursor of the parent frame to the default cursor.
      */
     public static void resetCursor(Component comp) {
+//    // Original code taken from http://www.javaspecialists.eu/archive/Issue065.html
+//    // applicable for JComponent      
+//    RootPaneContainer root = (RootPaneContainer)((JComponent)getContentPane()).getTopLevelAncestor();
+//    root.getGlassPane().setCursor(Cursor.getDefaultCursor());
+//    root.getGlassPane().removeMouseListener(DUMMY_MOUSEADAPTER);
+//    root.getGlassPane().setVisible(false);
       RootPaneContainer rootPane = getParentRootPaneContainer(comp);
-      if ( rootPane != null && rootPane.getContentPane() != null )
-        rootPane.getContentPane().setCursor(Cursor.getDefaultCursor());
+      if ( rootPane != null && rootPane.getContentPane() != null ) {
+        rootPane.getGlassPane().setCursor(Cursor.getDefaultCursor());
+        rootPane.getGlassPane().removeMouseListener(DUMMY_MOUSEADAPTER);
+        rootPane.getGlassPane().setVisible(false);      }
     }
     
 

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java	2012-10-03 20:05:38 UTC (rev 2097)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java	2012-10-03 23:03:26 UTC (rev 2098)
@@ -31,7 +31,9 @@
 
 import java.awt.Component;
 import java.io.File;
+import java.util.ArrayList;
 import java.util.Enumeration;
+import java.util.List;
 
 import javax.swing.Icon;
 import javax.swing.JTree;
@@ -43,6 +45,7 @@
 import javax.swing.tree.DefaultTreeModel;
 import javax.swing.tree.TreeModel;
 import javax.swing.tree.TreeNode;
+import javax.swing.tree.TreePath;
 import javax.swing.tree.TreeSelectionModel;
 
 import org.apache.commons.lang.StringUtils;
@@ -113,6 +116,16 @@
     });
   }
   
+// TODO: Ueberdenken und testen
+//  /**
+//   * Invalidates the tree, so that the directory structure will be
+//   * reorganized on next update.
+//   */
+//  public void updateTreeStructure() {
+//    ((DirectoryTreeModel)getModel()).invalidate();
+//    repaint();
+//  }
+  
   /**
    * Returns the description used if root node text is empty
    * (e.g. "Computer")
@@ -130,6 +143,53 @@
   }
 
   /**
+   * Sets the selected tree path to a special directory.
+   */
+  public void setSelectedDirectory(File dir) {
+    this.clearSelection();
+    TreePath path = ((DirectoryTreeModel)getModel()).findPath(dir);
+    setSelectionPath(path);
+    scrollPathToVisible(path);
+  }
+  
+  /**
+   * Returns the currently selected directory.
+   */
+  public File getSelectedDirectory() {
+    TreePath selectionPath = getSelectionPath();
+    return convertTreePathLeafToFile(selectionPath);
+    
+  }
+
+  /**
+   * Returns all selected directories when multi-selection is
+   * enabled.
+   */
+  public File[] getSelectedDirectories() {
+    List<File> dirList = new ArrayList<File>();
+    if ( getSelectionPaths() != null )
+      for (TreePath path : getSelectionPaths() ) {
+        File dir = convertTreePathLeafToFile(path);
+        if ( path != null )
+          dirList.add(dir);
+      }
+    return (File[])dirList.toArray(new File[0]);
+    
+  }
+  
+  /**
+   * Returns the directory represented by a tree path.
+   * @return {@code null} if empty path is given
+   */
+  protected File convertTreePathLeafToFile(TreePath selectionPath) {
+    if ( selectionPath == null )
+      return null;
+    DirectoryTreeNode node = (DirectoryTreeNode)selectionPath.getLastPathComponent();
+    File dir = (File)node.getUserObject();
+    return dir;
+  }
+
+  /**
    * {@link TreeModel} for the directory tree.
    * @author Martin O.J. Schmitz
    */
@@ -153,6 +213,40 @@
       // Unterverzeichnisse) als Ordner dargestellt werden und nicht als Punkt (Blatt)
       super( new DirectoryTreeNode(rootDir), true );
     }
+    
+//    /**
+//     * Invalidates the tree, so that the directory structure will be
+//     * reorganized on next update.
+//     */
+//    public void invalidate() {
+//      ((DirectoryTreeNode)getRoot()).revalidate();
+//    }
+
+    /**
+     * Creates a {@link TreePath} for the specified directory.
+     */
+    public TreePath findPath(File dir) {
+      if ( dir != null && !dir.isDirectory() )
+        dir = dir.getParentFile();
+
+      // Generate list of files, which successively represent
+      // the sub-directories
+      List<File> dirPath = new ArrayList<File>();
+      if ( dir != null ) {
+        dirPath.add(dir);
+        while( dir.getParentFile() != null ) {
+          dir = dir.getParentFile();
+          dirPath.add(0, dir);
+        }
+      }
+
+      // Navigate to node
+      DirectoryTreeNode node = (DirectoryTreeNode)getRoot();
+      for (File subDir : dirPath) {
+        node = node.findChild(subDir);
+      }
+      return new TreePath( node.getPath() );
+    }
   }
   
   /**
@@ -182,13 +276,30 @@
       super(dir == null ? null : (dir.isDirectory() || dir.getParent() == null) ? dir : dir.getParentFile());
     }
     
+    /**
+     * Finds the child node which represents the specified (sub)directory.
+     * @return {@code null} if there is no child node which represents the
+     *         specified directory
+     */
+    public DirectoryTreeNode findChild(File dir) {
+      for (int i=0; i<getChildCount(); i++) {
+        DirectoryTreeNode childNode = (DirectoryTreeNode)getChildAt(i);
+        File childFile = (File)childNode.getUserObject();
+        if ( dir == null && childFile == null ||
+             dir != null && dir.equals(childFile) )
+          return childNode;
+      }        
+      return null;
+    }
+
     
+    
     /**
      * Returns an {@link Enumeration} of the node children (representing the
      * sub-folders). If necessary (note not valid) the children are generated first.
      */
     @Override
-    public Enumeration children() {
+    public Enumeration<DirectoryTreeNode> children() {
       if ( !isValid() )
         generateChildren();
       return super.children();
@@ -231,6 +342,19 @@
       this.initialized = false;
     }
     
+//    /**
+//     * If the node is already valid, its children are regenerated.
+//     */
+//    public void revalidate() {
+//      if ( isValid() ) {
+//        for (Object child : children) {
+//          DirectoryTreeNode childNode = (DirectoryTreeNode)child;
+//          childNode.revalidate();
+//        }
+//        generateChildren();
+//      }
+//    }
+    
     /**
      * Returns the simple file name as representation of the node.
      */



More information about the Schmitzm-commits mailing list