1 package tim.prune.function;
3 import java.awt.BorderLayout;
4 import java.awt.Component;
5 import java.awt.FlowLayout;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.awt.event.KeyAdapter;
9 import java.awt.event.KeyEvent;
10 import java.awt.event.MouseAdapter;
11 import java.awt.event.MouseEvent;
13 import javax.swing.BorderFactory;
14 import javax.swing.JButton;
15 import javax.swing.JDialog;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTextArea;
22 import tim.prune.GenericFunction;
23 import tim.prune.I18nManager;
24 import tim.prune.load.TextFileLoader;
27 * Class to provide the function to paste a list of coordinates
28 * and create points for them as if they were loaded from a text file
30 public class PasteCoordinateList extends GenericFunction
32 private JDialog _dialog = null;
33 private JTextArea _coordArea = null;
34 private JButton _okButton = null;
39 * @param inApp application object for callback
41 public PasteCoordinateList(App inApp)
46 /** Get the name key */
47 public String getNameKey() {
48 return "function.pastecoordinatelist";
59 _dialog = new JDialog(_parentFrame, I18nManager.getText(getNameKey()), true);
60 _dialog.setLocationRelativeTo(_parentFrame);
61 _dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
62 _dialog.getContentPane().add(makeDialogComponents());
65 // MAYBE: Paste clipboard into the edit area
66 _coordArea.setText("");
68 _dialog.setVisible(true);
73 * Create dialog components
74 * @return Panel containing all gui elements in dialog
76 private Component makeDialogComponents()
78 JPanel dialogPanel = new JPanel();
79 dialogPanel.setLayout(new BorderLayout(0, 10));
80 dialogPanel.add(new JLabel(I18nManager.getText("dialog.pastecoordinatelist.desc")), BorderLayout.NORTH);
81 _coordArea = new JTextArea(8, 35);
82 _coordArea.setLineWrap(true);
83 _coordArea.setWrapStyleWord(true);
84 JScrollPane coordsPane = new JScrollPane(_coordArea);
85 // Listeners to enable/disable ok button
86 KeyAdapter keyListener = new KeyAdapter() {
88 public void keyReleased(KeyEvent inE) {
90 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
95 MouseAdapter mouseListener = new MouseAdapter() {
96 public void mouseReleased(MouseEvent inE) {
100 _coordArea.addKeyListener(keyListener);
101 _coordArea.addMouseListener(mouseListener);
102 dialogPanel.add(coordsPane, BorderLayout.CENTER);
103 // button panel at bottom
104 JPanel buttonPanel = new JPanel();
105 buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
106 _okButton = new JButton(I18nManager.getText("button.ok"));
107 ActionListener okListener = new ActionListener() {
108 public void actionPerformed(ActionEvent e)
110 if (_okButton.isEnabled()) {finish();}
113 _okButton.addActionListener(okListener);
114 _okButton.setEnabled(false);
115 buttonPanel.add(_okButton);
116 JButton cancelButton = new JButton(I18nManager.getText("button.cancel"));
117 cancelButton.addActionListener(new ActionListener() {
118 public void actionPerformed(ActionEvent e)
123 buttonPanel.add(cancelButton);
124 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
125 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
130 * Enable or disable the OK button based on the contents of the text field
132 private void enableOK()
134 String text = _coordArea.getText();
135 _okButton.setEnabled(text != null && text.length() > 6
136 && (text.indexOf(' ') >= 0 || text.indexOf(',') >= 0));
140 * Finish the dialog when OK pressed
142 private void finish()
144 new TextFileLoader(_app, _parentFrame).loadText(_coordArea.getText());