001    package de.mospace.swing.text;
002    
003    /* Mp3dings - manage mp3 meta-information
004     * Copyright (C) 2003 Moritz Ringler
005     * $Id: FullTextStringInputDialog.java 128 2008-02-11 08:24:56Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.awt.BorderLayout;
023    import java.awt.Dimension;
024    import java.awt.Frame;
025    import java.awt.Point;
026    import java.awt.Insets;
027    import java.awt.event.ActionEvent;
028    import javax.swing.AbstractAction;
029    import javax.swing.Action;
030    import javax.swing.JButton;
031    import javax.swing.JComponent;
032    import javax.swing.JDialog;
033    import javax.swing.JPanel;
034    import javax.swing.JScrollPane;
035    import javax.swing.JTextArea;
036    import javax.swing.KeyStroke;
037    import javax.swing.UIManager;
038    import de.mospace.lang.FullTextString;
039    
040    /**
041     * An input dialog for multiline strings.
042     * @author Moritz Ringler
043     * @version $Revision: 128 $ ($Date: 2008-02-11 09:24:56 +0100 (Mo, 11 Feb 2008) $)
044     * @see de.mospace.lang.FullTextString
045     **/
046    public class FullTextStringInputDialog extends JDialog{
047       private String result = null;
048    
049       private FullTextStringInputDialog(Frame f,String m, String v){
050            super(f, m, true);
051            getContentPane().setLayout(new BorderLayout());
052            final JTextArea jta = new JTextArea((v == null)? "":v.toString());
053            Action okAction = new AbstractAction("OK"){
054                public void actionPerformed(ActionEvent e){
055                    result = jta.getText();
056                    dispose();
057                }
058            };
059            jta.getInputMap().put(KeyStroke.getKeyStroke("shift ENTER"),"commit");
060            jta.getInputMap().put(KeyStroke.getKeyStroke("ctrl ENTER"),"commit");
061            jta.getInputMap().put(KeyStroke.getKeyStroke("alt ENTER"),"commit");
062            jta.getActionMap().put("commit", okAction);
063            jta.setLineWrap(true);
064            jta.setWrapStyleWord(true);
065            JButton ok = new JButton(okAction);
066            Action cancelAction = new AbstractAction(
067                    UIManager.getString("OptionPane.cancelButtonText")){
068                public void actionPerformed(ActionEvent e){
069                    result = null;
070                    dispose();
071                }
072            };
073            jta.getInputMap().put(KeyStroke.getKeyStroke("ESCAPE"),"cancel");
074            jta.getActionMap().put("cancel", cancelAction);
075            final JButton cancel = new JButton(cancelAction);
076            getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
077            JPanel buttons = new JPanel();
078            buttons.add(ok);
079            buttons.add(cancel);
080            getContentPane().add(buttons, BorderLayout.SOUTH);
081            ((JComponent) getContentPane()).setPreferredSize(new Dimension(500,500));
082            pack();
083            Dimension dlgSize = getPreferredSize();
084            if(f != null){
085                Dimension frmSize = f.getSize();
086                Dimension screen = f.getToolkit().getScreenSize();
087                Insets screenInsets = f.getToolkit().getScreenInsets(f.getGraphicsConfiguration());
088                screen.width -= (screenInsets.left + screenInsets.right);
089                screen.height -= (screenInsets.top + screenInsets.bottom);
090                Point loc = f.getLocation();
091                int x = (frmSize.width - dlgSize.width) / 2 +  loc.x;
092                if(dlgSize.width > screen.width){
093                    dlgSize.width = screen.width;
094                    setSize(dlgSize);
095                }
096                if(x + dlgSize.width > screen.width){
097                    x = screen.width - dlgSize.width;
098                }
099                if(x < screenInsets.left){
100                    x = screenInsets.left;
101                }
102                int y = (frmSize.height - dlgSize.height) / 2 + loc.y;
103                if(dlgSize.height > screen.height){
104                    dlgSize.height = screen.height;
105                    setSize(dlgSize);
106                }
107                if(y + dlgSize.height > screen.height){
108                    y = screen.height - dlgSize.height;
109                }
110                if(y < screenInsets.top){
111                    y = screenInsets.top;
112                }
113                setLocation(x, y);
114            }
115        }
116    
117        /** Gets the current value of the multi-line string that is or has been
118        * edited by this input dialog.
119        * @return the current value of the string being edited, or <code>null</code>
120        * if the user cancelled the dialog
121        */
122        private String getValue(){
123            return result;
124        }
125    
126        /** Displays a new FullTextStringInputDialog that is initialized with
127        * the specified FullTextString.
128        * @param owner the parent frame for this dialog, may be <code>null</code>
129        * @param message the title for this dialog
130        * @param x a FullTextString to initialize the editor with,
131        * may be <code>null</code>
132        * @return the user input as a FullTextString , or <code>null</code>
133        * if the user cancelled the dialog
134        */
135        static public FullTextString showDialog(Frame owner,
136                String message,
137                FullTextString x){
138            String v = (x == null)? null : x.toString();
139            v = showDialog(owner, message, v);
140            return (v == null)? null: new FullTextString(v);
141        }
142    
143        /** Displays a new FullTextStringInputDialog that is initialized with
144        * the specified string.
145        * @param owner the parent frame for this dialog, may be <code>null</code>
146        * @param message the title for this dialog
147        * @param x a string to initialize the editor with,
148        * may be <code>null</code>
149        * @return the user input as a string, or <code>null</code>
150        * if the user cancelled the dialog
151        */
152        static public String showDialog(Frame owner,
153                String message,
154                String x){
155            String v = (x == null)? "" : x;
156            FullTextStringInputDialog ftsid = new FullTextStringInputDialog(owner, message, v);
157            ftsid.setVisible(true);
158            return ftsid.getValue();
159        }
160    }