[Schmitzm-commits] r1620 - in trunk/schmitzm-core/src/main: java/de/schmitzm/net/mail java/de/schmitzm/swing resources/de/schmitzm/swing/resource/icons/small
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Jul 7 22:22:07 CEST 2011
Author: mojays
Date: 2011-07-07 22:22:05 +0200 (Thu, 07 Jul 2011)
New Revision: 1620
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/AbstractTextPanel.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressPanel.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressTextField.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SimpleFormattedTextField.java
trunk/schmitzm-core/src/main/java/de/schmitzm/swing/TextPanel.java
trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/mail.jpg
trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/mail2.gif
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
Log:
new SimpleFormattedTextField, MailAddressTextField, MailAddressPanel
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java 2011-07-07 11:27:20 UTC (rev 1619)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/net/mail/MailUtil.java 2011-07-07 20:22:05 UTC (rev 1620)
@@ -134,6 +134,7 @@
*/
public static void sendDesktopMail(String mailDestAddr, String mailSubject,
String mailBody) {
+
if (!Desktop.isDesktopSupported()
|| !Desktop.getDesktop().isSupported(Desktop.Action.MAIL)) {
@@ -148,6 +149,8 @@
}
try {
+ if ( mailBody == null )
+ mailBody = "";
// Mail body can not be infinitely big
if (mailBody.length() > MAX_DESKTOP_MAIL_BODY_SIZE)
mailBody = mailBody.substring(0, MAX_DESKTOP_MAIL_BODY_SIZE)
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/AbstractTextPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/AbstractTextPanel.java 2011-07-07 11:27:20 UTC (rev 1619)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/AbstractTextPanel.java 2011-07-07 20:22:05 UTC (rev 1620)
@@ -0,0 +1,65 @@
+package de.schmitzm.swing;
+
+import java.awt.LayoutManager;
+
+import javax.swing.JComponent;
+
+/**
+ * Panel which holds any kind of text component.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public abstract class AbstractTextPanel extends JPanel implements TextPanel {
+
+ /**
+ * Creates a new panel.
+ */
+ public AbstractTextPanel() {
+ super();
+ }
+
+ /**
+ * Creates a new panel.
+ */
+ public AbstractTextPanel(boolean isDoubleBuffered) {
+ super(isDoubleBuffered);
+ }
+
+ /**
+ * Creates a new panel.
+ */
+ public AbstractTextPanel(LayoutManager layout, boolean isDoubleBuffered) {
+ super(layout, isDoubleBuffered);
+ }
+
+ /**
+ * Creates a new panel.
+ */
+ public AbstractTextPanel(LayoutManager layout, String borderTitle) {
+ super(layout, borderTitle);
+ }
+
+ /**
+ * Creates a new panel.
+ */
+ public AbstractTextPanel(LayoutManager layout) {
+ super(layout);
+ }
+
+ /**
+ * Returns {@code this} to implement the interface {@link TextPanel}.
+ */
+ @Override
+ public JComponent getComponent() {
+ return this;
+ }
+
+// /**
+// * Returns the content of the text component.
+// */
+// public abstract String getText();
+//
+// /**
+// * Sets the content of the text component.
+// */
+// public abstract void setText(String text);
+}
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressPanel.java 2011-07-07 11:27:20 UTC (rev 1619)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressPanel.java 2011-07-07 20:22:05 UTC (rev 1620)
@@ -0,0 +1,84 @@
+package de.schmitzm.swing;
+
+import java.awt.BorderLayout;
+import java.awt.Dimension;
+import java.awt.event.ActionEvent;
+
+import javax.swing.AbstractAction;
+import javax.swing.Action;
+import javax.swing.ImageIcon;
+import javax.swing.JButton;
+import javax.swing.JFormattedTextField;
+
+import de.schmitzm.net.mail.MailUtil;
+
+/**
+ * {@link JFormattedTextField} which accepts a mail address and
+ * provides a button to create a e-mail with the standard mail
+ * program of the OS.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+public class MailAddressPanel extends AbstractTextPanel {
+
+ protected JButton mailButton;
+ protected MailAddressTextField mailTextField;
+ protected ImageIcon mailIcon = SwingUtil.createImageIconFromResourcePath(SwingUtil.class, "resource/icons/small/mail2.gif", null);
+ protected Action mailAction;
+
+ /**
+ * Creates a new text field.
+ */
+ public MailAddressPanel() {
+ super( new BorderLayout() );
+ mailTextField = new MailAddressTextField();
+
+ mailAction = new AbstractAction("Mail",mailIcon) {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ performMailAction(getText(), null, null);
+ }
+ };
+ mailAction.putValue(AbstractAction.SMALL_ICON, mailIcon);
+ mailAction.putValue(AbstractAction.SHORT_DESCRIPTION, "Mail");
+ mailAction.putValue(AbstractAction.ACTION_COMMAND_KEY,"MAIL");
+ mailAction.putValue(AbstractAction.NAME, "Mail");
+
+ mailButton = new JButton(mailAction);
+ mailButton.getInsets().top = 1;
+ mailButton.getInsets().bottom = 1;
+ mailButton.setPreferredSize(new Dimension(mailIcon.getIconWidth()+15,mailTextField.getPreferredSize().height) );
+ mailButton.setText(null);
+
+ add( mailTextField, BorderLayout.CENTER );
+ add( mailButton, BorderLayout.EAST );
+ }
+
+ /**
+ * Piped to {@link #mailTextField}.
+ */
+ public void setText(String text) {
+ mailTextField.setText(text);
+ }
+
+ /**
+ * Piped to {@link #mailTextField}.
+ */
+ public String getText() {
+ return mailTextField.getText();
+ }
+
+ /**
+ * Called by the {@link #mailAction} with {@link #getText()} as
+ * destination address and subject/body <code>null</code>.
+ * This method simply pipes these 3 parameters to
+ * {@link MailUtil#sendDesktopMail(String, String, String)}.<br>
+ * Sub-classes can override this method to set an application
+ * dependent subject or body.
+ *
+ */
+ protected void performMailAction(String destAddr, String subject, String body) {
+ MailUtil.sendDesktopMail(
+ getText(), subject, body);
+ }
+}
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressTextField.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressTextField.java 2011-07-07 11:27:20 UTC (rev 1619)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/MailAddressTextField.java 2011-07-07 20:22:05 UTC (rev 1620)
@@ -0,0 +1,24 @@
+package de.schmitzm.swing;
+
+import java.text.MessageFormat;
+
+import javax.swing.JComponent;
+import javax.swing.JFormattedTextField;
+
+/**
+ * {@link JFormattedTextField} which accepts a mail address.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+public class MailAddressTextField extends SimpleFormattedTextField implements TextPanel {
+ public static final String MAIL_FORMAT_PATTERN = "{0}@{1}.{2}";
+ public static final MessageFormat MAIL_FORMAT = new MessageFormat(MAIL_FORMAT_PATTERN);
+
+ /**
+ * Creates a new text field.
+ */
+ public MailAddressTextField() {
+ super( MAIL_FORMAT );
+ }
+
+}
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SimpleFormattedTextField.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SimpleFormattedTextField.java 2011-07-07 11:27:20 UTC (rev 1619)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/SimpleFormattedTextField.java 2011-07-07 20:22:05 UTC (rev 1620)
@@ -0,0 +1,209 @@
+package de.schmitzm.swing;
+
+import java.awt.Color;
+import java.text.Format;
+import java.text.MessageFormat;
+import java.text.ParseException;
+
+import javax.swing.InputVerifier;
+import javax.swing.JComponent;
+import javax.swing.JFormattedTextField;
+import javax.swing.JTextField;
+import javax.swing.SwingUtilities;
+
+import org.apache.commons.lang.StringUtils;
+
+import de.schmitzm.lang.LangUtil;
+
+/**
+ * {@link JFormattedTextField} which resticts no input, but marks the
+ * invalid input in a highlight color.
+ * Optionally the focus of the text field can be hold until a valid
+ * input is given.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+public class SimpleFormattedTextField extends JFormattedTextField implements TextPanel {
+ /** Holds the text color for invalid inputs. Default: {@link Color#red} */
+ protected Color invalidForeground = Color.RED;
+ /** Holds the text color for valid inputs. */
+ protected Color validForeground = null;
+ /** Indicates that the focus will be hold until a valid input is given **/
+ protected boolean restrictInvalidInput = false;
+
+
+ /**
+ * Creates a new text field.
+ */
+ public SimpleFormattedTextField() {
+ super();
+ init();
+ }
+
+ /**
+ * Creates a new text field.
+ */
+ public SimpleFormattedTextField(Format format) {
+ super(format);
+ init();
+ }
+
+ /**
+ * Creates a new text field.
+ */
+ public SimpleFormattedTextField(AbstractFormatter formatter) {
+ super(formatter);
+ init();
+ }
+
+ /**
+ * Called by the constructor.
+ */
+ protected void init() {
+ this.validForeground = getForeground();
+
+ // Eingaben, die nicht dem Format entsprechen, werden zugelassen,
+ setFocusLostBehavior( PERSIST );
+ setInputVerifier( new FormattedTextFieldVerifier() );
+ }
+
+ /**
+ * Returns {@code this} to implement the interface {@link TextPanel}.
+ */
+ @Override
+ public JComponent getComponent() {
+ return this;
+ }
+
+ /**
+ * Sets the foreground color regardless of the valid/invalid
+ * color settings. Simply calls the super-method.
+ */
+ @Override
+ public void setForeground(Color color) {
+ super.setForeground(color);
+ }
+
+ /**
+ * Returns the current foreground color regardless of the valid/invalid
+ * color settings. Simply calls the super-method.
+ */
+ @Override
+ public Color getForeground() {
+ return super.getForeground();
+ }
+
+ /**
+ * Sets the foreground color for valid inputs.
+ */
+ public void setValidForeground(Color color) {
+ this.validForeground = color;
+ }
+
+ /**
+ * Returns the foreground color for valid inputs.
+ */
+ public Color getValidForeground() {
+ return this.validForeground;
+ }
+
+ /**
+ * Sets the forground color for invalid inputs.
+ */
+ public void setInvalidForeground(Color color) {
+ this.invalidForeground = color;
+ }
+
+ /**
+ * Sets the forground color for invalid inputs.
+ */
+ public Color getInvalidForeground() {
+ return this.invalidForeground;
+ }
+
+ /**
+ * Sets whether the focus will be hold until a valid input is
+ * given. Default: {@code false}.
+ */
+ public void setRestrictInvalidInput(boolean restrict) {
+ this.restrictInvalidInput = restrict;
+ }
+
+ /**
+ * Returns whether the focus will be hold until a valid input is
+ * given.
+ */
+ public boolean getRestrictInvalidInput() {
+ return this.restrictInvalidInput;
+ }
+
+ /**
+ * Besides calling the super method, the verifier is checks
+ * the new value.
+ */
+ @Override
+ public void setText(String text) {
+ super.setText(text);
+ if ( getInputVerifier() != null )
+ getInputVerifier().verify(this);
+ }
+
+
+ /**
+ * Called by the input verifier if the input is valid.
+ * Sets the foreground color to {@link #getValidForeground()}.
+ */
+ protected void processInvalidInput() {
+ setForeground( getInvalidForeground() );
+ }
+
+ /**
+ * Called by the input verifier if the input is not valid.
+ * Sets the foreground color to {@link #getInvalidForeground()}.
+ */
+ protected void processValidInput() {
+ setForeground( getValidForeground() );
+ }
+
+ /**
+ * Checks whether the given text is a valid input.
+ */
+ protected boolean validateInput(AbstractFormatter formatter, String input) {
+ if ( StringUtils.trimToNull(input) == null )
+ return true;
+ try {
+ formatter.stringToValue(input);
+ return true;
+ } catch (ParseException pe) {
+ return false;
+ }
+ }
+
+ /**
+ *
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ *
+ */
+ private class FormattedTextFieldVerifier extends InputVerifier {
+ public boolean verify(JComponent input) {
+ if (input instanceof JFormattedTextField) {
+ JFormattedTextField ftf = (JFormattedTextField)input;
+ AbstractFormatter formatter = ftf.getFormatter();
+ if (formatter != null) {
+ String text = ftf.getText();
+ if ( validateInput(formatter,text) ) {
+ processValidInput();
+ } else {
+ processInvalidInput();
+ if ( getRestrictInvalidInput() )
+ return false;
+ }
+ }
+ }
+ return true;
+ }
+ public boolean shouldYieldFocus(JComponent input) {
+ return verify(input);
+ }
+ }
+}
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/swing/TextPanel.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/swing/TextPanel.java 2011-07-07 11:27:20 UTC (rev 1619)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/swing/TextPanel.java 2011-07-07 20:22:05 UTC (rev 1620)
@@ -0,0 +1,25 @@
+package de.schmitzm.swing;
+
+import javax.swing.JComponent;
+
+/**
+ * Panel which holds any kind of text component.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public interface TextPanel {
+
+ /**
+ * Returns the content of the text component.
+ */
+ public abstract String getText();
+
+ /**
+ * Sets the content of the text component.
+ */
+ public abstract void setText(String text);
+
+ /**
+ * Should always return {@code this}.
+ */
+ public abstract JComponent getComponent();
+}
Added: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/mail.jpg
===================================================================
(Binary files differ)
Property changes on: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/mail.jpg
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
Added: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/mail2.gif
===================================================================
(Binary files differ)
Property changes on: trunk/schmitzm-core/src/main/resources/de/schmitzm/swing/resource/icons/small/mail2.gif
___________________________________________________________________
Name: svn:mime-type
+ application/octet-stream
More information about the Schmitzm-commits
mailing list