[Schmitzm-commits] r1906 - in trunk/schmitzm-core/src/main/java/de/schmitzm: io lang swing
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Fri Mar 23 00:08:07 CET 2012
Author: mojays
Date: 2012-03-23 00:08:06 +0100 (Fri, 23 Mar 2012)
New Revision: 1906
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.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/WebFilesChooser.java
Log:
IOUtil: new methods readStreamAsString(.) and downloadUrlToFile(.)
LangUtil: new method convertToMap(.) to convert a list to map
ApplicationProps: store string-Map in properties as semicolon-separated pair "key:value"
OkCancelDialog: methods to change action/button titles
SwingUtil: generic methods to set cursor; methods to create JSpinner; method to create extended table column header (with column class included)
WebFilesChooser: OK-Action only enabled if at least one file is selected
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2012-03-21 19:24:14 UTC (rev 1905)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2012-03-22 23:08:06 UTC (rev 1906)
@@ -512,14 +512,72 @@
*/
public static void writeStreamToFile(InputStream input, String file)
throws IOException {
- OutputStream output = new FileOutputStream(file);
- while (input.available() > 0)
- output.write(input.read());
- output.flush();
- output.close();
+// OutputStream output = new FileOutputStream(file);
+// while (input.available() > 0)
+// output.write(input.read());
+// output.flush();
+// output.close();
+ writeStreamToFile(input, new File(file));
}
- /**
+ /**
+ * Schreibt einen Eingabe-Stream in eine Datei.
+ *
+ * @param file
+ * Dateipfad fuer die Zieldatei
+ * @exception java.io.IOException
+ * falls das Lesen aus dem Stream oder das Schreiben in die
+ * Ausgabedatei scheitert
+ */
+ public static void writeStreamToFile(InputStream input, File file)
+ throws IOException {
+ OutputStream output = new FileOutputStream(file);
+ while (input.available() > 0)
+ output.write(input.read());
+ output.flush();
+ output.close();
+ }
+
+ /**
+ * Downloads an {@link URL} to local file.
+ * @param url remote URL
+ * @param file local file (if {@code null} a temporary file is created, which will be
+ * deleted after JVM exit!)
+ * @param timeout connection timeout in ms
+ */
+ public static File downloadUrlToFile(URL url, File file, int timeout) throws IOException {
+ if ( file == null ) {
+ file = File.createTempFile("java-schmitzm-",null);
+ file.deleteOnExit();
+ }
+
+ OutputStream output = null;
+ InputStream input = null;
+ try {
+ // Prepare output
+ output = new FileOutputStream(file);
+ // Prepare input from URL
+ URLConnection urlconn = url.openConnection();
+ if ( timeout >= 0 )
+ urlconn.setConnectTimeout(timeout);
+ urlconn.setDoOutput(true);
+ urlconn.connect();
+ input = urlconn.getInputStream();
+ // Read/write line-wise
+ byte[] buffer = new byte[2048];
+ int bufferSize = 0;
+ while ( (bufferSize = input.read(buffer)) > 0 )
+ output.write(buffer, 0, bufferSize);
+ output.flush();
+ } finally {
+ closeInputStream(input);
+ closeOutputStream(output);
+ }
+
+ return file;
+ }
+
+ /**
* A replacement for File.toURI().toURL().
* <p>
* The handling of file.toURL() is broken; the handling of
@@ -931,7 +989,25 @@
}
}
- /**
+ /**
+ * Reads the contents of a stream into one String. Watch the size!
+ *
+ * @param input a stream to read
+ * @return as String the content of the stream
+ *
+ * @throws java.io.IOException
+ */
+ public static String readStreamAsString(InputStream input) throws java.io.IOException {
+ byte[] buffer = new byte[input.available()];
+ try {
+ input.read(buffer);
+ return new String(buffer);
+ } finally {
+ input.close();
+ }
+ }
+
+ /**
* Reads the contents of a File into one String. Watch the size!
*
* @param file
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-21 19:24:14 UTC (rev 1905)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/ApplicationProps.java 2012-03-22 23:08:06 UTC (rev 1906)
@@ -38,11 +38,13 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
+import java.util.Map;
import java.util.Properties;
import javax.crypto.Cipher;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import de.schmitzm.crypt.CryptUtil;
@@ -248,6 +250,26 @@
}
/**
+ * Parses a map from string.
+ * @param mapStr semicolon-separated list of "key:value" pairs
+ */
+ public Map<String,String> getStringMap(KEYS key, String... defaultValue) {
+ Map<String,String> map = new SequentialOrderedMap<String, String>();
+ String mapStr = getString(key, defaultValue);
+ if ( mapStr == null )
+ return map;
+ String[] pairs = mapStr.split(";");
+ for (String pairStr : pairs)
+ if ( !StringUtils.isBlank(pairStr) ) {
+ String[] pair = mapStr.split(":");
+ String k = pair.length > 0 ? pair[0] : "";
+ String v = pair.length > 1 ? pair[1] : null;
+ map.put(k, v);
+ }
+ return map;
+ }
+
+ /**
* Returns a property value which was stored encrypted in the map.
* @param key properties key
* @param defaultValueDecrypted default value (already decrypted!)
@@ -285,6 +307,26 @@
}
/**
+ * Encodes a map as semicolon-separated list of "key:value" pairs
+ * and stores it in map
+ */
+ public <K,V >void setStringMap(KEYS key, Map<K,V> map) {
+ String mapStr = "";
+
+ if ( map != null ) {
+ for (K k : map.keySet()) {
+ V v = map.get(k);
+ String keyStr = k.toString();
+ String valueStr = v != null ? v.toString() : "";
+ if (!StringUtils.isBlank(mapStr))
+ mapStr += "; ";
+ mapStr += keyStr.trim()+":"+valueStr.trim();
+ }
+ }
+ set(key, mapStr);
+ }
+
+ /**
* Set a value in the underlying {@link Properties}. The value
* is encrypted by the encryption {@link Cipher}.
* @see #setCipher(Cipher, Cipher)
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2012-03-21 19:24:14 UTC (rev 1905)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2012-03-22 23:08:06 UTC (rev 1906)
@@ -2141,7 +2141,19 @@
Boolean b = parseStringDefaultNull(str);
return b != null ? b : false;
}
+
+ /**
+ * Converts a list to a map which contains the list items as
+ * keys and corresponding values.
+ */
+ public static <K> Map<K,K> convertToMap(List<K> list) {
+ Map<K,K> map = new SequentialOrderedMap<K,K>();
+ for (K item : list)
+ map.put(item, item);
+ return map;
+ }
+
/**
* Removes leading and tailing "\n" newline commands. Very similar: StringUtils.trimToEmpty(string);
*
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-21 19:24:14 UTC (rev 1905)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/OkCancelDialog.java 2012-03-22 23:08:06 UTC (rev 1906)
@@ -176,6 +176,21 @@
return dialogApproved;
}
+ /**
+ * Resets the caption of the OK action.
+ * @param caption new title
+ */
+ public void setOkCaption(String caption) {
+ okAction.putValue(Action.NAME, caption);
+ }
+
+ /**
+ * Resets the caption of the CANCEL action.
+ * @param caption new title
+ */
+ public void setCancelCaption(String caption) {
+ cancelAction.putValue(Action.NAME, caption);
+ }
}
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-21 19:24:14 UTC (rev 1905)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SwingUtil.java 2012-03-22 23:08:06 UTC (rev 1906)
@@ -91,6 +91,7 @@
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
+import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JScrollPane;
@@ -105,6 +106,8 @@
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.JViewport;
+import javax.swing.RootPaneContainer;
+import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;
import javax.swing.UIManager;
@@ -410,6 +413,24 @@
return comp;
}
+ /**
+ * Liefert den {@link RootPaneContainer}, der eine Kompoenente beinhaltet.
+ * Dabei kann es sich um die Komponente, die das {@linkplain RootPaneContainer#getContentPane() ContentPane}
+ * enthaelt, also ein {@link JFrame}, {@link JDialog} oder {@link Window}.
+ * @param comp eine GUI-Komponente
+ * @return <code>null</code> falls die Komponente in keinem {@link RootPaneContainer}
+ * enthalten ist
+ */
+ public static RootPaneContainer getParentRootPaneContainer(Component comp) {
+ // durch die Parents laufen, bis RootPaneContainer gefunden
+ while (comp != null && !(comp instanceof RootPaneContainer))
+ comp = comp.getParent();
+ // wenn kein Window gefunden -> null zurueckgeben
+ if (comp == null)
+ return null;
+ return (RootPaneContainer)comp;
+ }
+
/**
* Packt das Fenster, in dem eine Kompoenente plaziert ist.
*
@@ -2210,5 +2231,87 @@
Desktop.getDesktop().open(localFile);
}
+ /**
+ * Creates a {@link JSpinner} for {@link Integer} values.
+ */
+ public static JSpinner createIntSpinner(int min, int max, int... def) {
+ int defaultValue = min;
+ if (def.length > 0)
+ defaultValue = def[0];
+ defaultValue = Math.max(defaultValue, min);
+ defaultValue = Math.min(defaultValue, max);
+
+ SpinnerNumberModel model = new SpinnerNumberModel(defaultValue, min, max, 1);
+ JSpinner spinner = new JSpinner(model);
+
+ return spinner;
+ }
+ /**
+ * Creates an component to input a percentage amount.
+ */
+ public static JSpinner createDoubleSpinner(double min, double max,
+ double stepSize, double... def) {
+ double defaultValue = min;
+ if (def.length > 0)
+ defaultValue = def[0];
+ defaultValue = Math.max(defaultValue, min);
+ defaultValue = Math.min(defaultValue, max);
+
+ SpinnerNumberModel model = new SpinnerNumberModel(defaultValue, min, max,
+ stepSize);
+ JSpinner spinner = new JSpinner(model);
+
+ return spinner;
+ }
+
+ /**
+ * Creates a {@link TableCellRenderer} for table column headers, which shows the
+ * column type below the default column header.
+ * @param table table to take the default header renderer from (if {@code null} the
+ * renderer is taken from default {@link JTable})
+ */
+ public static TableCellRenderer createTableColumnHeaderRendererWithColumnTypes(JTable table) {
+ if ( table == null )
+ table = new JTable();
+ final TableCellRenderer defHeaderRenderer = table.getTableHeader().getDefaultRenderer();
+
+ TableCellRenderer headerRendererWithColumnTypes = new TableCellRenderer() {
+ @Override
+ public Component getTableCellRendererComponent(JTable table, Object value,
+ boolean isSelected, boolean hasFocus, int row, int col) {
+ JPanel panel = new JPanel(new BorderLayout(5,0));
+ Component c1 = defHeaderRenderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
+ JLabel c2 = new JLabel("["+table.getModel().getColumnClass(col).getSimpleName()+"]");
+ c2.setHorizontalAlignment(JLabel.CENTER);
+ c2.setFont( c2.getFont().deriveFont(Font.BOLD) );
+ panel.setBorder(BorderFactory.createRaisedBevelBorder());
+ panel.add(c1,BorderLayout.CENTER);
+ panel.add(c2,BorderLayout.SOUTH);
+ return panel;
+ }
+ };
+ return headerRendererWithColumnTypes;
+ }
+
+ /**
+ * Sets the cursor of the parent frame to {@link Cursor#WAIT_CURSOR}, to lock the frame.
+ */
+ public static void setWaitCursor(Component comp) {
+ RootPaneContainer rootPane = getParentRootPaneContainer(comp);
+ if ( rootPane != null && rootPane.getContentPane() != null )
+ rootPane.getContentPane().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
+ }
+
+ /**
+ * Sets the cursor of the parent frame to the default cursor.
+ */
+ public static void resetCursor(Component comp) {
+ RootPaneContainer rootPane = getParentRootPaneContainer(comp);
+ if ( rootPane != null && rootPane.getContentPane() != null )
+ rootPane.getContentPane().setCursor(Cursor.getDefaultCursor());
+ }
+
+
+
}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooser.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooser.java 2012-03-21 19:24:14 UTC (rev 1905)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooser.java 2012-03-22 23:08:06 UTC (rev 1906)
@@ -36,6 +36,9 @@
import java.net.URL;
import java.util.List;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
+
/**
* Dialog to select files from a web directory.
* <b>Note:</b>The panel expects that the base URL does specify a simple web
@@ -120,18 +123,32 @@
protected void init() {
super.init();
setPreferredSize( new Dimension(500,300) );
- SwingUtil.setRelativeFramePosition(this, getOwner(), 0.5, 0.5);
this.filesChooserPanel = new WebFilesChooserPanel();
+ filesChooserPanel.filesTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() {
+ @Override
+ public void valueChanged(ListSelectionEvent e) {
+ updateActions();
+ }
+ });
getRootPane().setDefaultButton(filesChooserPanel.getUpdateButton());
getContentPane().add(filesChooserPanel, BorderLayout.CENTER);
-
+ updateActions();
pack();
+ SwingUtil.setRelativeFramePosition(this, getOwner(), 0.5, 0.5);
}
+
/**
+ * Updates the activation of the dialog actions.
+ */
+ protected void updateActions() {
+ okAction.setEnabled( filesChooserPanel.filesTable.getSelectedRowCount() > 0 );
+ }
+
+ /**
* Returns the url currently entered in address text field.
*/
public String getBaseURL() {
More information about the Schmitzm-commits
mailing list