]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/PasteCoordinateList.java
Version 20.4, May 2021
[GpsPrune.git] / src / tim / prune / function / PasteCoordinateList.java
1 package tim.prune.function;
2
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;
12
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;
20
21 import tim.prune.App;
22 import tim.prune.GenericFunction;
23 import tim.prune.I18nManager;
24 import tim.prune.load.TextFileLoader;
25
26 /**
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
29  */
30 public class PasteCoordinateList extends GenericFunction
31 {
32         private JDialog _dialog = null;
33         private JTextArea _coordArea = null;
34         private JButton _okButton = null;
35
36
37         /**
38          * Constructor
39          * @param inApp application object for callback
40          */
41         public PasteCoordinateList(App inApp)
42         {
43                 super(inApp);
44         }
45
46         /** Get the name key */
47         public String getNameKey() {
48                 return "function.pastecoordinatelist";
49         }
50
51         /**
52          * Begin the function
53          */
54         public void begin()
55         {
56                 // Make dialog window
57                 if (_dialog == null)
58                 {
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());
63                         _dialog.pack();
64                 }
65                 // MAYBE: Paste clipboard into the edit area
66                 _coordArea.setText("");
67                 enableOK();
68                 _dialog.setVisible(true);
69         }
70
71
72         /**
73          * Create dialog components
74          * @return Panel containing all gui elements in dialog
75          */
76         private Component makeDialogComponents()
77         {
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() {
87                         /** Key released */
88                         public void keyReleased(KeyEvent inE) {
89                                 enableOK();
90                                 if (inE.getKeyCode() == KeyEvent.VK_ESCAPE) {
91                                         _dialog.dispose();
92                                 }
93                         }
94                 };
95                 MouseAdapter mouseListener = new MouseAdapter() {
96                         public void mouseReleased(MouseEvent inE) {
97                                 enableOK();
98                         }
99                 };
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)
109                         {
110                                 if (_okButton.isEnabled()) {finish();}
111                         }
112                 };
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)
119                         {
120                                 _dialog.dispose();
121                         }
122                 });
123                 buttonPanel.add(cancelButton);
124                 dialogPanel.add(buttonPanel, BorderLayout.SOUTH);
125                 dialogPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 15));
126                 return dialogPanel;
127         }
128
129         /**
130          * Enable or disable the OK button based on the contents of the text field
131          */
132         private void enableOK()
133         {
134                 String text = _coordArea.getText();
135                 _okButton.setEnabled(text != null && text.length() > 6
136                         && (text.indexOf(' ') >= 0 || text.indexOf(',') >= 0));
137         }
138
139         /**
140          * Finish the dialog when OK pressed
141          */
142         private void finish()
143         {
144                 new TextFileLoader(_app, _parentFrame).loadText(_coordArea.getText());
145                 _dialog.dispose();
146         }
147 }