[Schmitzm-commits] r1927 - in trunk/schmitzm-core/src/main/java/de/schmitzm: lang swing

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Fri Mar 30 12:44:49 CEST 2012


Author: mojays
Date: 2012-03-30 12:44:45 +0200 (Fri, 30 Mar 2012)
New Revision: 1927

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/OkCancelDialog.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java
Log:
ApplicationProps.set(.): avoid NPE when NULL value is set (?!)
OkCancelDialog: maintain ESC-KeyStroke-Listener in global field
SwingUtil: methods to resize Dimension
WebFilesChooserPanel: methods providing access to files and directories table
WebAndLocalFilesChooser: integrated dialog for JFileChooser and WebFilesChooserPanel

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java	2012-03-28 13:05:43 UTC (rev 1926)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java	2012-03-30 10:44:45 UTC (rev 1927)
@@ -301,7 +301,7 @@
    * and store it.
    */
   public void set(KEYS key, Object value) {
-    properties.setProperty(key.toString(), value.toString());
+    properties.setProperty(key.toString(), value == null ? "" : value.toString());
     if ( getAutoSave() )
       store();
   }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/OkCancelDialog.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/OkCancelDialog.java	2012-03-28 13:05:43 UTC (rev 1926)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/OkCancelDialog.java	2012-03-30 10:44:45 UTC (rev 1927)
@@ -51,6 +51,17 @@
  * @author Martin O.J. Schmitz
  */
 public abstract class OkCancelDialog extends JDialog implements ActionListener, Disposable {
+  /** Key stroke for ESC key */
+  public static final KeyStroke ESC_STROKE = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
+  /** ActionListener which performs the CANCEL action. Used to react on {@link #ESC_STROKE}. */
+  protected final ActionListener escapeActionListener = new ActionListener() {
+    @Override
+    public void actionPerformed(ActionEvent e) {
+      cancelButton.doClick();
+    }
+  };
+  
+  
   /** Holds the buttons */
   protected JPanel buttonPanel = null;
   
@@ -114,14 +125,7 @@
     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
     
     // ActionListener fuer ESC-Taste
-    ActionListener escapeActionListener = new ActionListener() {
-      @Override
-      public void actionPerformed(ActionEvent e) {
-        cancelButton.doClick();
-      }
-    };
-    KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
-    buttonPanel.registerKeyboardAction(escapeActionListener, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
+    buttonPanel.registerKeyboardAction(escapeActionListener, ESC_STROKE, JComponent.WHEN_IN_FOCUSED_WINDOW);
 
     pack();
   }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-03-28 13:05:43 UTC (rev 1926)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java	2012-03-30 10:44:45 UTC (rev 1927)
@@ -478,7 +478,33 @@
 			return true;
 		return isChildComponent(child.getParent(), parent);
 	}
+	
+    /**
+     * Creates a new {@link Dimension} which is resized according to the given
+     * absolute lengths.
+     * @param dim     source dimension
+     * @param width   number of pixels the width is resized by
+     * @param height  number of pixels the height is resized by
+     * @return
+     */
+    public static Dimension resizeDimension(Dimension dim, int width, int height) {
+      return new Dimension( dim.width  + width,
+                            dim.height + height );
+    }
 
+    /**
+	 * Creates a new {@link Dimension} which is resized according to the given
+	 * factors
+	 * @param dim           source dimension
+	 * @param widthFactor   factor to resize the width with (>= 0)
+	 * @param heightFactor  factor to resize the height with (>= 0)
+	 * @return
+	 */
+	public static Dimension resizeDimension(Dimension dim, double widthFactor, double heightFactor) {
+	  return new Dimension( (int)(dim.width*widthFactor),
+	                        (int)(dim.height*heightFactor) );
+	}
+
 	/**
 	 * Zentriert ein Fenster auf dem Monitor.
 	 * 

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java	2012-03-30 10:44:45 UTC (rev 1927)
@@ -0,0 +1,252 @@
+/**
+ * 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;
+
+import java.awt.BorderLayout;
+import java.awt.Dialog;
+import java.awt.Dimension;
+import java.awt.Frame;
+import java.awt.event.KeyEvent;
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import javax.swing.JComponent;
+import javax.swing.JFileChooser;
+import javax.swing.JTabbedPane;
+import javax.swing.KeyStroke;
+import javax.swing.ListSelectionModel;
+
+import de.schmitzm.io.IOUtil;
+
+/**
+ * Integrates a {@link WebFilesChooserPanel} and a {@link JFileChooser}
+ * in a shared dialog.
+ * @author Martin O.J. Schmitz
+ */
+public class WebAndLocalFilesChooser extends OkCancelDialog {
+
+  public static final int TAB_LOCAL_FILES = 0;
+  public static final int TAB_WEB_FILES = 1;
+  
+  protected JTabbedPane tabPane;
+  protected JFileChooser localFileChooser;
+  protected WebFilesChooserPanel webFilesChooser;
+  
+  protected JPanel tabPaneContentLocalFiles;
+  protected JPanel tabPaneContentWebFiles;
+  
+  protected boolean multiSelection = false;
+  
+  /**
+   * Creates a new chooser.
+   * @param owner parent dialog
+   * @param title dialog title
+   */
+  public WebAndLocalFilesChooser(Dialog owner, String title) {
+    super(owner, title, true);
+  }
+
+  /**
+   * Creates a new chooser.
+   * @param owner parent window
+   * @param title dialog title
+   */
+  public WebAndLocalFilesChooser(Frame owner, String title) {
+    super(owner, title, true);
+  }
+  
+  /**
+   * Initializes the GUI.
+   */
+  @Override
+  protected void init() {
+    super.init();
+    
+    this.tabPane = new JTabbedPane();
+    this.tabPaneContentLocalFiles = new JPanel(new BorderLayout());
+    this.tabPaneContentWebFiles   = new JPanel(new BorderLayout());
+    tabPane.addTab("Local files", tabPaneContentLocalFiles);
+    tabPane.addTab("Files from web", tabPaneContentWebFiles);
+    getContentPane().add(tabPane,BorderLayout.CENTER);
+    
+    setLocalFileChooser( new JFileChooser() );
+    setWebFilesChooser( new WebFilesChooserPanel() );
+    
+    setPreferredSize( new Dimension(localFileChooser.getPreferredSize().width,
+                                    tabPane.getPreferredSize().height ) );
+    
+    pack();
+  }
+  
+  
+  /**
+   * Returns the file chooser used to select local files.
+   */
+  public JFileChooser getLocalFileChooser() {
+    return this.localFileChooser;
+  }
+
+  /**
+   * Sets the file chooser used to select local files.
+   */
+  public void setLocalFileChooser(JFileChooser fileChooser) {
+    if ( fileChooser == null )
+      throw new IllegalArgumentException("Local file chooser not allowed to be null.");
+    if ( this.localFileChooser != null )
+      tabPaneContentLocalFiles.remove(this.localFileChooser);
+    this.localFileChooser = fileChooser;
+    localFileChooser.setControlButtonsAreShown(false);
+// Klappt leider nicht (?!): Wenn JFileChooser tab angewaehlt ist, wird nicht auf ESC reagiert    
+//    localFileChooser.registerKeyboardAction(escapeActionListener,ESC_STROKE,JComponent.WHEN_IN_FOCUSED_WINDOW);
+    tabPaneContentLocalFiles.add(this.localFileChooser, BorderLayout.CENTER);
+    updateMultiSelectionMode();
+  }
+
+  /**
+   * Returns the file chooser used to select files from web directory.
+   */
+  public WebFilesChooserPanel getWebFilesChooser() {
+    return this.webFilesChooser;
+  }
+
+  /**
+   * Sets the file chooser used to select files from web directory.
+   */
+  public void setWebFilesChooser(WebFilesChooserPanel fileChooser) {
+    if ( fileChooser == null )
+      throw new IllegalArgumentException("Web files chooser not allowed to be null.");
+    if ( this.webFilesChooser != null )
+      tabPaneContentWebFiles.remove(this.webFilesChooser);
+    this.webFilesChooser = fileChooser;
+    tabPaneContentWebFiles.add(this.webFilesChooser, BorderLayout.CENTER);
+    updateMultiSelectionMode();
+  }
+  
+  /**
+   * Returns the selected files, depending of the selected tab.
+   * @return {@code null} if dialog was not closed by OK 
+   */
+  public List<URL> getSelectedFiles() {
+    if ( !isDialogApproved() )
+      return null;
+    List<URL> urls = null;
+    if ( tabPane.getSelectedIndex() == TAB_WEB_FILES )
+      urls = webFilesChooser.getSelectedFiles();
+    else {
+      urls = new ArrayList<URL>();
+      if ( localFileChooser.isMultiSelectionEnabled() )
+        for (File f : localFileChooser.getSelectedFiles())
+          urls.add( IOUtil.fileToURL(f) );
+      else
+        urls.add( IOUtil.fileToURL(localFileChooser.getSelectedFile()) );
+    }
+    return urls;
+  }
+
+  /**
+   * Sets the url currently set for the web files chooser tab.
+   * The files and directories table are updated immediately
+   * according to the URL.
+   */
+  public void setBaseURL(String baseURL) {
+    webFilesChooser.setBaseURL(baseURL);
+  }
+  
+  /**
+   * Sets the url currently set for the web files chooser tab.
+   * The files and directories table are updated immediately
+   * according to the URL.
+   */
+  public void setBaseURL(URL baseURL) {
+    webFilesChooser.setBaseURL(baseURL);
+  }
+
+  /**
+   * Returns the url currently entered in address text field.
+   */
+  public String getBaseURL() {
+    return webFilesChooser.getBaseURL();
+  }
+
+  /**
+   * Sets the directory for the local files chooser tab.
+   */
+  public void setCurrentDirectory(String directory) {
+    setCurrentDirectory(directory == null ? null : new File(directory));
+  }
+  
+  /**
+   * Sets the directory for the local files chooser tab.
+   */
+  public void setCurrentDirectory(File directory) {
+    if ( directory != null )
+      localFileChooser.setCurrentDirectory( directory );
+  }
+
+  /**
+   * Returns the directory currently set for the local files chooser tab.
+   */
+  public File getCurrentDirectory() {
+    File selFile = localFileChooser.getSelectedFile();
+    return selFile != null ? selFile.getParentFile() : null;
+  }
+  
+  /**
+   * Sets whether multiple files can be selected.
+   */
+  public void setMultiSelectionEnabled(boolean enabled) {
+    this.multiSelection = enabled;
+    updateMultiSelectionMode();
+  }
+
+  /**
+   * Returns whether multiple files can be selected.
+   */
+  public boolean isMultiSelectionEnabled() {
+    return multiSelection;
+  }
+
+  /**
+   * Updates the multi selection property of the {@link JFileChooser} and
+   * {@link WebFilesChooserPanel} according to {@link #isMultiSelectionEnabled()}.
+   */
+  protected void updateMultiSelectionMode() {
+    boolean enabled = isMultiSelectionEnabled();
+    if ( localFileChooser != null )
+      localFileChooser.setMultiSelectionEnabled(enabled);
+    if ( webFilesChooser != null ) {
+      int tableSelectionMode = enabled ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION; 
+      webFilesChooser.getWebFilesTable().setSelectionMode(tableSelectionMode);
+    }
+  }
+}

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java	2012-03-28 13:05:43 UTC (rev 1926)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java	2012-03-30 10:44:45 UTC (rev 1927)
@@ -178,6 +178,20 @@
   }
   
   /**
+   * Returns the table containing the web files.
+   */
+  public FilesSelectionTable<URL> getWebFilesTable() {
+    return filesTable;
+  }
+
+  /**
+   * Returns the table containing the web files.
+   */
+  public FilesSelectionTable<URL> getWebDirectoriesTable() {
+    return dirsTable;
+  }
+
+  /**
    * Returns the "update" button of the URL address field.
    * Allows the surrounding frame or dialog class to set this
    * button as default button. 
@@ -252,7 +266,7 @@
       actionCommand = "UPDATE_TABLE";
       super.init(buttonText, tooltipText, buttonIcon, actionCommand);
     }
-
+    
     /**
      * Called by the button action. Updates table by loading files from
      * URL.



More information about the Schmitzm-commits mailing list