[Schmitzm-commits] r1869 - in trunk/schmitzm-core/src/main: java/de/schmitzm/io java/de/schmitzm/swing java/de/schmitzm/swing/table resources/de/schmitzm/swing/resource/locales

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Feb 22 16:43:08 CET 2012


Author: mojays
Date: 2012-02-22 16:43:08 +0100 (Wed, 22 Feb 2012)
New Revision: 1869

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/FilesSelectionTable.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/swing/URLAddressPanel.java
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties
   trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties
Log:
IOUtil: new method createURL(.)
IOUtil.listFilesFromURL(.): ignore directory links
new FilesSelectionTable

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-02-22 13:06:10 UTC (rev 1868)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2012-02-22 15:43:08 UTC (rev 1869)
@@ -69,6 +69,7 @@
 
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.SystemUtils;
 import org.apache.log4j.Logger;
 
@@ -871,7 +872,28 @@
 					+ " temporary files and directories have been deleted.");
 		}
 	}
+	
+	/**
+	 * Creates an URL from string.
+	 * @param urlStr url definition (may be without protocol!)
+	 * @param defaultProtocol protocol used if {@code urlStr} does not contain protocol
+	 * @throws MalformedURLException if {@link URL} can not be created
+	 */
+	public static URL createURL(String urlStr, String... defaultProtocol) throws MalformedURLException {
+	  if ( StringUtils.isBlank(urlStr) )
+	    return null;
+	  
+	  // determine default protocol
+	  String prot = defaultProtocol.length < 1 ? null : defaultProtocol[0];
+	  // if url contains no protocol add default protocol (if given!)
+	  if ( urlStr.indexOf(':') < 0 && prot != null )
+	    urlStr = prot + "://" + urlStr;
 
+	  URL url = new URL(urlStr);
+
+	  return url;
+	}
+
 	/**
 	 * Test whether it is possible to access the given URL. Times-out after 3s
 	 */
@@ -1174,8 +1196,12 @@
 	/**
 	 * Extracts all links (href tags) from website document.
 	 * @param url URL of a website
+	 * @return an empty list, if {@code null} URL is given
 	 */
 	public static List<String> extractLinksFromURL(URL url) throws IOException {
+	  if ( url == null )
+	    return new ArrayList<String>();
+	  
 	  // load document from URL
 	  String str = IOUtil.convertStreamToString(url.openStream());
 
@@ -1210,10 +1236,12 @@
 
       // extract links from URL
       List<String> fileNames = extractLinksFromURL(url);
-
       // combine file names with source URL
       List<URL> files = new ArrayList<URL>();
       for (String fileName : fileNames) {
+        // Ignore directory links!
+        if ( fileName.endsWith("/") )
+          continue; 
         URL fileURL = new URL(url,fileName);
         files.add(fileURL);
       }

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/URLAddressPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/URLAddressPanel.java	2012-02-22 13:06:10 UTC (rev 1868)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/URLAddressPanel.java	2012-02-22 15:43:08 UTC (rev 1869)
@@ -38,8 +38,7 @@
 
   /**
    * Called by the button action.
-   * Pipes call to {@link #performMailAction(String, String, String)}
-   * with subject/body <code>null</code>.
+   * Opens URL in desktop browser.
    */
   protected void performAction(ActionEvent e, String destAddr) {
     MailUtil.openDesktopBrowser(destAddr);

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/FilesSelectionTable.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/FilesSelectionTable.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/table/FilesSelectionTable.java	2012-02-22 15:43:08 UTC (rev 1869)
@@ -0,0 +1,238 @@
+/**
+ * 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.table;
+
+import java.io.File;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.swing.table.TableModel;
+
+import de.schmitzm.io.IOUtil;
+import de.schmitzm.lang.LangUtil;
+import de.schmitzm.swing.SortableJTable;
+import de.schmitzm.swing.SwingUtil;
+
+/**
+ * Table to show an select a list of files (or URLs).
+ * @author Martin O.J. Schmitz
+ * 
+ * @param <FILETYPE> defines whether the class works with {@link File} or {@link URL}; other
+ *                   values will cause an {@link Exception} when showing the table
+ *
+ */
+public class FilesSelectionTable<FILETYPE> extends SortableJTable {
+  
+  /**
+   * Creates a new table.
+   */
+  public FilesSelectionTable(FilesSelectionTableModel<FILETYPE> tableModel) {
+    super(tableModel);
+    setModel( new SelectionTableModel(tableModel, this));
+    getColumnModel().getColumn(0).setMaxWidth(30);
+//    getModel().fireTableStructureChanged();
+  }
+
+  /**
+   * Creates a new table.
+   * @param showCompletePath indicates whether the complete path is shown in table or only the file name
+   */
+  public FilesSelectionTable(boolean showCompletePath) {
+    this( new FilesSelectionTableModel<FILETYPE>(showCompletePath) );
+  }
+  
+  /**
+   * Creates a new table which shows only the filename.
+   */
+  public FilesSelectionTable() {
+    this( false );
+  }
+
+  /**
+   * Returns the table model.
+   */
+  public FilesSelectionTableModel<FILETYPE> getOriginModel() {
+    TableModel model = getModel();
+    if ( model instanceof PipedTableModel )
+      model = ((PipedTableModel)model).getPipedModel();
+    return (FilesSelectionTableModel<FILETYPE>)model;
+  }
+  
+  /**
+   * Indicates whether the complete path is shown in table or only the file name.
+   */
+  public boolean isCompletePathShown() {
+    return getOriginModel().isCompletePathShown();
+  }
+  
+  /**
+   * Sets the content of the table.
+   */
+  public void setFiles(List<FILETYPE> files) {
+    getOriginModel().setFiles(files);
+  }
+  
+  /**
+   * Returns the content of the table.
+   */
+  public List<FILETYPE> getFiles() {
+    return getOriginModel().getFiles();
+  }
+
+  /**
+   * Sets whether the complete path is shown in table or only the file name.
+   */
+  public void setCompletePathShown(boolean showCompletePath) {
+    getOriginModel().setCompletePathShown(showCompletePath);
+  }
+  
+  /**
+   * Returns the selected files.
+   */
+  public List<FILETYPE> getSelectedFiles() {
+    List<FILETYPE> allFiles       = getFiles();
+    int[]          selRows        = getSelectedModelRows();
+    List<FILETYPE> selectedFiles  = new ArrayList<FILETYPE>();
+    for (int row : selRows)
+      selectedFiles.add( allFiles.get(row) );
+    return selectedFiles;
+  }
+  
+  
+  /**
+   * Table model for the files shown in {@link FilesSelectionTable}. This model has only one
+   * column which shows the file name.
+   * @author Martin O.J. Schmitz
+   *
+   * @param <FILETYPE> defines whether the class works with {@link File} or {@link URL}; other
+   *                   values will cause an {@link Exception} when showing the table
+   */
+  protected static class FilesSelectionTableModel<FILETYPE> extends AbstractTableModel  {
+    
+    /** Holds the {@link File}- or {@link URL}-Objects. */
+    protected List<FILETYPE> files = null;
+    /** Indicates whether the complete path is shown in table or only the file name. */
+    protected boolean showCompletePath = false;
+    
+    /**
+     * Creates a new table model, which shows only the file names.
+     */
+    public FilesSelectionTableModel() {
+      this(false);
+    }
+
+    /**
+     * Creates a new table model.
+     * @param showCompletePath indicates whether the complete path is shown in table or only the file name
+     */
+    public FilesSelectionTableModel(boolean showCompletePath) {
+      super();
+      setCompletePathShown(showCompletePath);
+    }
+    
+    /**
+     * Indicates whether the complete path is shown in table or only the file name.
+     */
+    public boolean isCompletePathShown() {
+      return showCompletePath;
+    }
+    
+    /**
+     * Sets whether the complete path is shown in table or only the file name.
+     */
+    public void setCompletePathShown(boolean showCompletePath) {
+      this.showCompletePath = showCompletePath;;
+    }
+
+    /**
+     * Returns the column names.
+     */
+    @Override
+    public String[] createColumnNames() {
+      return new String[] {
+                           SwingUtil.R("FilesSelectionTable.Files")
+      };
+    }
+    
+    /**
+     * Sets the content of the table and calls {@link #fireTableDataChanged()}.
+     */
+    public void setFiles(List<FILETYPE> files) {
+      this.files = files;
+      fireTableDataChanged();
+    }
+    
+    /**
+     * Returns the content of the table.
+     */
+    public List<FILETYPE> getFiles() {
+      return this.files;
+    }
+    
+    /**
+     * Returns the number of listed files.
+     */
+    @Override
+    public int getRowCount() {
+      if ( files == null )
+        return 0;
+      return files.size();
+    }
+
+    /**
+     * Returns a table value.
+     */
+    @Override
+    public Object getValueAt(int row, int col) {
+      FILETYPE file = files.get(row);
+      switch (col) {
+        case 0: return getFileName(file);
+      }
+      return null;
+    }
+    
+    /**
+     * Returns the description of a file shown in the table column.
+     */
+    protected String getFileName(FILETYPE file) {
+      if ( file == null )
+        return null;
+      if ( file instanceof File )
+        return isCompletePathShown() ? ((File)file).getAbsolutePath() : ((File)file).getName();
+      if ( file instanceof URL )
+        return isCompletePathShown() ? ((URL)file).toExternalForm() : IOUtil.getFilename(((URL)file));
+      throw new UnsupportedOperationException("FILETYPE not supported: "+LangUtil.getSimpleClassName(file));
+    }
+
+    
+  }
+
+}

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2012-02-22 13:06:10 UTC (rev 1868)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties	2012-02-22 15:43:08 UTC (rev 1869)
@@ -274,3 +274,5 @@
 MailAddressPanel.Button.Tooltip=Create mail
 URLAddressPanel.Button.Label=Browse
 URLAddressPanel.Button.Tooltip=Open URL in browser
+
+FilesSelectionTable.Files=Files

Modified: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties
===================================================================
--- trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties	2012-02-22 13:06:10 UTC (rev 1868)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties	2012-02-22 15:43:08 UTC (rev 1869)
@@ -247,3 +247,5 @@
 MailAddressPanel.Button.Tooltip=Neue E-Mail erstellen
 URLAddressPanel.Button.Label=\u00D6ffnen
 URLAddressPanel.Button.Tooltip=URL in Browser \u00D6ffnen
+
+FilesSelectionTable.Files=Dateien



More information about the Schmitzm-commits mailing list