[Schmitzm-commits] r2088 - in trunk/schmitzm-core/src/main: java/de/schmitzm/swing java/de/schmitzm/swing/tree resources/de/schmitzm/swing/resource/icons/small

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sun Sep 30 00:42:01 CEST 2012


Author: mojays
Date: 2012-09-30 00:42:01 +0200 (Sun, 30 Sep 2012)
New Revision: 2088

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/monitor.png
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java
Log:
ApplicationFrame: Avoid collapse of status bar JLabel on empty status message
new DirectoryTree: Nice panel e.g. for directory-only chooser-dialog

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java	2012-09-29 08:08:56 UTC (rev 2087)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/ApplicationFrame.java	2012-09-29 22:42:01 UTC (rev 2088)
@@ -17,6 +17,8 @@
 import javax.swing.JMenuBar;
 import javax.swing.text.JTextComponent;
 
+import org.apache.commons.lang.StringUtils;
+
 import de.schmitzm.lang.LangUtil;
 import de.schmitzm.swing.menu.ToolBarsPanel;
 
@@ -151,9 +153,11 @@
    * @param statusMess a message
    */
   public void setStatusMess(String statusMess) {
-    if ( statusBar instanceof JLabel )
+    if ( statusBar instanceof JLabel ) {
+      if ( StringUtils.isBlank(statusMess) )
+        statusMess = " "; // avoid that JLabel collapse on empty string!
       ((JLabel)statusBar).setText(statusMess);
-    else if ( statusBar instanceof ProgressBarWithText )
+    } else if ( statusBar instanceof ProgressBarWithText )
       ((ProgressBarWithText)statusBar).setText(statusMess);
     else if ( statusBar instanceof JTextComponent )
       ((JTextComponent)statusBar).setText(statusMess);

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/tree/DirectoryTree.java	2012-09-29 22:42:01 UTC (rev 2088)
@@ -0,0 +1,274 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.swing.tree;
+
+import java.awt.Component;
+import java.io.File;
+import java.util.Enumeration;
+
+import javax.swing.Icon;
+import javax.swing.JTree;
+import javax.swing.LookAndFeel;
+import javax.swing.UIManager;
+import javax.swing.filechooser.FileSystemView;
+import javax.swing.tree.DefaultMutableTreeNode;
+import javax.swing.tree.DefaultTreeCellRenderer;
+import javax.swing.tree.DefaultTreeModel;
+import javax.swing.tree.TreeModel;
+import javax.swing.tree.TreeNode;
+import javax.swing.tree.TreeSelectionModel;
+
+import org.apache.commons.lang.StringUtils;
+
+import de.schmitzm.io.FilterUtil;
+import de.schmitzm.swing.SwingUtil;
+
+/**
+ * This class provides a {@link JTree} which shows a directory tree.
+ * @author Martin O.J. Schmitz
+ */
+public class DirectoryTree extends JTree {
+  /** Description used if root node text is empty (e.g. "Computer") */
+  protected String emptyRootDesc = ""; 
+  
+  /**
+   * Creates a tree with the disk drives as root folders. 
+   */
+  public DirectoryTree() {
+    this(null);
+  }
+  
+  /**
+   * Creates a tree with the a specific folder as root folder.
+   * @param  rootDir directory which will be shown as root folder (it will be impossible to
+   *                 navigate above this folder); if {@code null} the disk drives will be
+   *                 the root folders
+   */
+  public DirectoryTree(File rootDir) {
+    super( new DirectoryTreeModel(rootDir) );
+//    // show root only if a specific folder is set
+//    setRootVisible(rootDir != null);
+    // Default: Single selection
+    getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
+    // Extend renderer to show system icons for directories
+    setCellRenderer( new DefaultTreeCellRenderer() {
+      public Component getTreeCellRendererComponent(JTree tree, Object value,
+          boolean sel,
+          boolean expanded,
+          boolean leaf, int row,
+          boolean hasFocus) {
+        Component c = super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
+        
+        DirectoryTreeNode node = (DirectoryTreeNode)value;
+        Icon icon = FileSystemView.getFileSystemView().getSystemIcon((File)node.getUserObject());
+        
+        if ( node.isRoot() ) {
+          icon = SwingUtil.createImageIconFromResourcePath("resource/icons/small/monitor.png", null);
+          if ( StringUtils.isBlank(getText()) )
+            setText( getEmptyRootDescription() );
+        }
+        if ( icon != null ) {
+          // !!! TAKEN FROM SUPER CLASS !!!
+          if (!tree.isEnabled()) {
+            setEnabled(false);
+            LookAndFeel laf = UIManager.getLookAndFeel();
+            Icon disabledIcon = laf.getDisabledIcon(tree, icon);
+            if (disabledIcon != null) icon = disabledIcon;
+            setDisabledIcon(icon);
+          } else {
+            setEnabled(true);
+            setIcon(icon);
+          }        
+          //////////////////////////////////
+        }        
+        return c;
+      }
+    });
+  }
+  
+  /**
+   * Returns the description used if root node text is empty
+   * (e.g. "Computer")
+   */
+  public String getEmptyRootDescription() {
+    return emptyRootDesc;
+  }
+  
+  /**
+   * Sets the description used if root node text is empty
+   * (e.g. "Computer")
+   */
+  public void setEmptyRootDescription(String desc) {
+    this.emptyRootDesc = desc;
+  }
+
+  /**
+   * {@link TreeModel} for the directory tree.
+   * @author Martin O.J. Schmitz
+   */
+  public static class DirectoryTreeModel extends DefaultTreeModel {
+
+    /**
+     * Creates a tree model with the disk drives as root folders. 
+     */
+    public DirectoryTreeModel() {
+      this(null);
+    }
+    
+    /**
+     * Creates a tree model with the a specific folder as root folder.
+     * @param  rootDir directory which will be shown as root folder (it will be impossible to
+     *                 navigate above this folder); if {@code null} the disk drives will be
+     *                 the root folders
+     */  
+    public DirectoryTreeModel(File rootDir) {
+      super( new DirectoryTreeNode(rootDir), true );
+    }
+  }
+  
+  /**
+   * This class represents one node (directory) in the tree.
+   * @author Martin O.J. Schmitz
+   */
+  public static class DirectoryTreeNode extends DefaultMutableTreeNode {
+    /** Indicates whether the children of the node are already created.
+     *  @see #isValid()
+     *  @see #invalidate()  */
+    protected boolean initialized = false; 
+    
+    /**
+     * Creates a root note which children are the disk drives. 
+     */
+    public DirectoryTreeNode() {
+      super(null);
+    }
+
+    /**
+     * Creates a root note which children are the disk drives.
+     * @param dir directory which is represented by the node (if {@code null}
+     *            the note represents a root node with the disk drives as
+     *            children) 
+     */
+    public DirectoryTreeNode(File dir) {
+      super(dir == null ? null : (dir.isDirectory() || dir.getParent() == null) ? dir : dir.getParentFile());
+    }
+    
+    
+    /**
+     * 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() {
+      if ( !isValid() )
+        generateChildren();
+      return super.children();
+    }
+    
+    /**
+     * Return a child node (representing a sub-folder). If necessary (note
+     * not valid) the children are generated first.
+     */
+    @Override
+    public TreeNode getChildAt(int i) {
+      if ( !isValid() )
+        generateChildren();
+      return super.getChildAt(i);
+    }
+
+    /**
+     * Return the number of child nodes (representing the sub-folders). If necessary (note
+     * not valid) the children are generated first.
+     */
+    @Override
+    public int getChildCount() {
+      if ( !isValid() )
+        generateChildren();
+      return super.getChildCount();
+    }
+    
+    /**
+     * Returns whether the child nodes are already generated.
+     */
+    public boolean isValid() {
+      return initialized;
+    }
+    
+    /**
+     * Resets the node to invalid, so that the children (sub-folders) are
+     * re-generated on the next access.
+     */
+    public void invalidate() {
+      this.initialized = false;
+    }
+    
+    /**
+     * Returns the simple file name as representation of the node.
+     */
+    @Override
+    public String toString() {
+      File dir = (File)getUserObject();
+      if ( dir == null )
+        return "";
+      if ( isRoot() )
+        return dir.getAbsolutePath();
+      String dirName = dir.getName();
+      // If no directory name exists, file represents a disk drive root, so we
+      // use the drive name (which already includes the drive letter)
+      if ( StringUtils.isBlank(dirName) )
+        dirName = FileSystemView.getFileSystemView().getSystemDisplayName(dir);
+      // If no disk drive name exists, use the drive description
+      if ( StringUtils.isBlank(dirName) ) {
+        dirName = dir.toString(); // drive letter with trailing slash
+        if ( dirName.endsWith("\\") )
+          // remove trailing slash from drive letter
+          dirName = dirName.substring(0,dirName.length()-1);
+        dirName = FileSystemView.getFileSystemView().getSystemTypeDescription(dir) + " ("+dirName+")";
+      }
+      return dirName;
+    }
+
+    /**
+     * Generates / Updates the node children (sub-folders).
+     */
+    protected void generateChildren() {
+      // IMPORTANT: Set this flag to TRUE first to avoid recursion in 
+      // following add(.) calls!
+      this.initialized = true;
+      removeAllChildren();
+      File dir = (File)getUserObject();
+//      System.out.println(dir);
+      File[] subDirs = dir != null ? dir.listFiles(FilterUtil.ALL_DIRS_FILTER) : File.listRoots();
+      if ( subDirs != null )
+        for ( File d : subDirs )
+          this.add( new DirectoryTreeNode(d) );
+    }
+  }
+}

Added: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/monitor.png
===================================================================
(Binary files differ)


Property changes on: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/monitor.png
___________________________________________________________________
Added: svn:mime-type
   + application/octet-stream



More information about the Schmitzm-commits mailing list