[Schmitzm-commits] r1934 - in trunk/schmitzm-core/src/main: java/de/schmitzm/io java/de/schmitzm/swing java/de/schmitzm/swing/input resources/de/schmitzm/swing/resource/locales
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Wed Apr 4 13:09:07 CEST 2012
Author: mojays
Date: 2012-04-04 13:09:07 +0200 (Wed, 04 Apr 2012)
New Revision: 1934
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/input/MultipleOptionPane.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 isInetConnectionAvailable() to check generally if web access is available
WebAndLocalFilesChooser: several improvements; e.g. disabled OK-Button if no file is selected
WebFilesChooserPanel: disable URL field if no internet connection is available
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2012-04-02 10:33:52 UTC (rev 1933)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java 2012-04-04 11:09:07 UTC (rev 1934)
@@ -47,11 +47,14 @@
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
+import java.net.UnknownHostException;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.security.MessageDigest;
@@ -1873,5 +1876,37 @@
return new CharacterDevice(reader, writer);
}
+
+ public static boolean isInetConnectionAvailable() {
+ final int timeoutMillis = 300;
+ final String[] addressesToTry = new String[] {"google.com",
+ "heise.de"
+ };
+ // Try to access site by PING
+ for (String addrStr : addressesToTry)
+ try {
+ InetAddress address = InetAddress.getByName(addrStr);
+ if ( address.isReachable(timeoutMillis) )
+ return true;
+ } catch (Exception e) {
+ // ignore exceptions, try the next address
+ }
+ // Maybe PING is blocked by destination server or local firewall
+ // In this case, try to connect address by http connection
+ for (String addrStr : addressesToTry)
+ try {
+ final URL url = new URL("http",addrStr,"");
+ final URLConnection conn = url.openConnection();
+ final InputStream input = conn.getInputStream();
+ input.close();
+ return true;
+// if ( urlExists(new URL("http",addrStr,""), timeoutMillis) )
+// return true;
+ } catch (Exception e) {
+ // ignore exceptions, try the next address
+ }
+ return false;
+ }
+
}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java 2012-04-02 10:33:52 UTC (rev 1933)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebAndLocalFilesChooser.java 2012-04-04 11:09:07 UTC (rev 1934)
@@ -33,18 +33,24 @@
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Frame;
-import java.awt.event.KeyEvent;
+import java.awt.event.ActionListener;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
import java.io.File;
import java.net.URL;
+import java.nio.charset.Charset;
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 javax.swing.SwingUtilities;
+import javax.swing.event.ChangeEvent;
+import javax.swing.event.ChangeListener;
+import javax.swing.event.DocumentListener;
+import javax.swing.event.ListSelectionEvent;
+import javax.swing.event.ListSelectionListener;
import de.schmitzm.io.IOUtil;
@@ -54,8 +60,9 @@
* @author Martin O.J. Schmitz
*/
public class WebAndLocalFilesChooser extends OkCancelDialog {
-
+ /** Index of tab which contains the local files chooser. */
public static final int TAB_LOCAL_FILES = 0;
+ /** Index of tab which contains the web files chooser. */
public static final int TAB_WEB_FILES = 1;
protected JTabbedPane tabPane;
@@ -67,6 +74,10 @@
protected boolean multiSelection = false;
+ /** Listens to selection changes of files list and calls {@link #updateActions()}
+ * on every selection change. */
+ protected FileSelectionChangeListener fileSelectionListener;
+
/**
* Creates a new chooser.
* @param owner parent dialog
@@ -92,9 +103,13 @@
protected void init() {
super.init();
+ this.fileSelectionListener = new FileSelectionChangeListener();
+
this.tabPane = new JTabbedPane();
this.tabPaneContentLocalFiles = new JPanel(new BorderLayout());
this.tabPaneContentWebFiles = new JPanel(new BorderLayout());
+ this.tabPane.addChangeListener(fileSelectionListener);
+
tabPane.addTab("Local files", tabPaneContentLocalFiles);
tabPane.addTab("Files from web", tabPaneContentWebFiles);
getContentPane().add(tabPane,BorderLayout.CENTER);
@@ -104,12 +119,37 @@
setPreferredSize( new Dimension(localFileChooser.getPreferredSize().width,
tabPane.getPreferredSize().height ) );
-
+ updateActions();
pack();
}
+ /**
+ * Updates the activation of the dialog actions.
+ */
+ protected void updateActions() {
+ okAction.setEnabled( getSelectedFiles(true).size() > 0 );
+ }
+
/**
+ * Sets the active tab.
+ * @see #TAB_LOCAL_FILES
+ * @see #TAB_WEB_FILES
+ */
+ public void setSelectedTab(int idx) {
+ tabPane.setSelectedIndex(idx);
+ }
+
+ /**
+ * Returns the active tab.
+ * @see #TAB_LOCAL_FILES
+ * @see #TAB_WEB_FILES
+ */
+ public int getSelectedTab() {
+ return tabPane.getSelectedIndex();
+ }
+
+ /**
* Returns the file chooser used to select local files.
*/
public JFileChooser getLocalFileChooser() {
@@ -125,6 +165,7 @@
if ( this.localFileChooser != null )
tabPaneContentLocalFiles.remove(this.localFileChooser);
this.localFileChooser = fileChooser;
+ localFileChooser.addPropertyChangeListener( fileSelectionListener );
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);
@@ -145,33 +186,69 @@
public void setWebFilesChooser(WebFilesChooserPanel fileChooser) {
if ( fileChooser == null )
throw new IllegalArgumentException("Web files chooser not allowed to be null.");
- if ( this.webFilesChooser != null )
+ if ( this.webFilesChooser != null ) {
tabPaneContentWebFiles.remove(this.webFilesChooser);
+ webFilesChooser.filesTable.getSelectionModel().removeListSelectionListener(fileSelectionListener);
+ }
this.webFilesChooser = fileChooser;
+ webFilesChooser.filesTable.getSelectionModel().addListSelectionListener(fileSelectionListener);
tabPaneContentWebFiles.add(this.webFilesChooser, BorderLayout.CENTER);
updateMultiSelectionMode();
}
/**
* Returns the selected files, depending of the selected tab.
+ * @param ignoreApprove if {@code false} the method returns {@code null} if dialog
+ * is not yet approved; if {@code true} the method returns
+ * the "currently" selected URLs even if dialog is still
+ * active.
+ */
+ protected List<URL> getSelectedFiles(boolean ignoreApprove) {
+ List<URL> urls = new ArrayList<URL>();
+ if ( tabPane.getSelectedIndex() == TAB_WEB_FILES ) {
+ if ( webFilesChooser != null )
+ urls = webFilesChooser.getSelectedFiles();
+ } else {
+ if ( localFileChooser != null ) {
+ if ( localFileChooser.isMultiSelectionEnabled() )
+ for (File f : localFileChooser.getSelectedFiles())
+ urls.add( IOUtil.fileToURL(f) );
+ else
+ if ( localFileChooser.getSelectedFile() != null )
+ urls.add( IOUtil.fileToURL(localFileChooser.getSelectedFile()) );
+ }
+ }
+ return urls;
+ }
+
+ /**
+ * Returns the selected files, depending of the selected tab.
* @return {@code null} if dialog was not closed by OK
*/
public List<URL> getSelectedFiles() {
+ return getSelectedFiles(false);
+ }
+
+ /**
+ * Returns whether the file selection was made from web.
+ * @return {@code null} if dialog was not closed by OK
+ */
+ public Boolean selectionFromWeb() {
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;
+ return tabPane.getSelectedIndex() == TAB_WEB_FILES;
}
+
+ /**
+ * Returns whether the file selection was made from local
+ * file system.
+ * @return {@code null} if dialog was not closed by OK
+ */
+ public Boolean selectionFromLocal() {
+ if ( !isDialogApproved() )
+ return null;
+ return tabPane.getSelectedIndex() == TAB_LOCAL_FILES;
+ }
/**
* Sets the url currently set for the web files chooser tab.
@@ -249,4 +326,42 @@
webFilesChooser.getWebFilesTable().setSelectionMode(tableSelectionMode);
}
}
+
+ /**
+ * Listener whichs reacts on selection changes in {@link WebFilesChooserPanel} or
+ * {@link JFileChooser} with {@link WebAndLocalFilesChooser#updateActions()}.
+ * @author Martin O.J. Schmitz
+ */
+ protected class FileSelectionChangeListener implements ListSelectionListener, PropertyChangeListener, ChangeListener {
+
+ /**
+ * Reacts on selection change of {@link JFileChooser}.
+ */
+ @Override
+ public void propertyChange(PropertyChangeEvent e) {
+ if ( e.getSource() == localFileChooser ) {
+ if ( JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(e.getPropertyName()) ||
+ JFileChooser.SELECTED_FILES_CHANGED_PROPERTY.equals(e.getPropertyName()) )
+ updateActions();
+ }
+ }
+
+ /**
+ * Reacts on selection change of {@link WebFilesChooserPanel}.
+ */
+ @Override
+ public void valueChanged(ListSelectionEvent e) {
+ if ( e.getSource() == webFilesChooser.getWebFilesTable().getSelectionModel() )
+ updateActions();
+ }
+
+ /**
+ * Reacts on change of active tab.
+ */
+ @Override
+ public void stateChanged(ChangeEvent e) {
+ if ( e.getSource() == tabPane )
+ updateActions();
+ }
+ }
}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java 2012-04-02 10:33:52 UTC (rev 1933)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/WebFilesChooserPanel.java 2012-04-04 11:09:07 UTC (rev 1934)
@@ -213,8 +213,18 @@
* according to the URL.
*/
public void setBaseURL(String baseURL) {
- baseUrl.setText(baseURL);
- baseUrl.performAction(null, baseUrl.getText());
+ try {
+ SwingUtil.setWaitCursor(this);
+ if ( !IOUtil.isInetConnectionAvailable() ) {
+ baseUrl.setText( SwingUtil.R("WebFilesChooserPanel.InetNotAvailable") );
+ setEnabled(false);
+ return;
+ }
+ baseUrl.setText(baseURL);
+ baseUrl.performAction(null, baseUrl.getText());
+ } finally {
+ SwingUtil.resetCursor(this);
+ }
}
/**
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/input/MultipleOptionPane.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/input/MultipleOptionPane.java 2012-04-02 10:33:52 UTC (rev 1933)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/input/MultipleOptionPane.java 2012-04-04 11:09:07 UTC (rev 1934)
@@ -140,8 +140,6 @@
MultipleOptionPane pane = new MultipleOptionPane(option);
JDialog dialog = pane.createDialog(parent,title);
dialog.setResizable(resizable);
-// dialog.validate();
-// dialog.pack();
InputOption invalidOption = null;
do {
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-04-02 10:33:52 UTC (rev 1933)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle.properties 2012-04-04 11:09:07 UTC (rev 1934)
@@ -295,3 +295,4 @@
InfoDialog.Timezone=Timezone
InfoDialog.Contact=Contact
+WebFilesChooserPanel.InetNotAvailable=No internet connection available!
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-04-02 10:33:52 UTC (rev 1933)
+++ trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/locales/SwingResourceBundle_de.properties 2012-04-04 11:09:07 UTC (rev 1934)
@@ -266,4 +266,6 @@
InfoDialog.JVM.Memory=JVM Speicher gesamt / belegt / frei
InfoDialog.JVM.Memory.available=verfügbar
InfoDialog.Timezone=Zeitzone
-InfoDialog.Contact=Kontakt
\ No newline at end of file
+InfoDialog.Contact=Kontakt
+
+WebFilesChooserPanel.InetNotAvailable=Keine Internet-Verbindung verfügbar!
More information about the Schmitzm-commits
mailing list