1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.io;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GridBagConstraints;
|
---|
8 | import java.awt.GridBagLayout;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.event.FocusAdapter;
|
---|
11 | import java.awt.event.FocusEvent;
|
---|
12 | import java.io.File;
|
---|
13 | import java.util.EventObject;
|
---|
14 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
15 |
|
---|
16 | import javax.swing.AbstractAction;
|
---|
17 | import javax.swing.JButton;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JTable;
|
---|
20 | import javax.swing.JTextField;
|
---|
21 | import javax.swing.event.CellEditorListener;
|
---|
22 | import javax.swing.event.ChangeEvent;
|
---|
23 | import javax.swing.table.TableCellEditor;
|
---|
24 |
|
---|
25 | import org.openstreetmap.josm.actions.SaveActionBase;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * This is a {@see TableCellEditor} for filenames. It provides a text input field and
|
---|
29 | * a button for launchinig a {@see JFileChooser}.
|
---|
30 | *
|
---|
31 | *
|
---|
32 | */
|
---|
33 | class FilenameCellEditor extends JPanel implements TableCellEditor {
|
---|
34 | private JTextField tfFileName;
|
---|
35 | private CopyOnWriteArrayList<CellEditorListener> listeners;
|
---|
36 | private File value;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * build the GUI
|
---|
40 | */
|
---|
41 | protected void build() {
|
---|
42 | setLayout(new GridBagLayout());
|
---|
43 | GridBagConstraints gc = new GridBagConstraints();
|
---|
44 | gc.gridx = 0;
|
---|
45 | gc.gridy = 0;
|
---|
46 | gc.fill = GridBagConstraints.BOTH;
|
---|
47 | gc.weightx = 1.0;
|
---|
48 | gc.weighty = 1.0;
|
---|
49 | add(tfFileName = new JTextField(), gc);
|
---|
50 |
|
---|
51 | gc.gridx = 1;
|
---|
52 | gc.gridy = 0;
|
---|
53 | gc.fill = GridBagConstraints.BOTH;
|
---|
54 | gc.weightx = 0.0;
|
---|
55 | gc.weighty = 1.0;
|
---|
56 | add(new JButton(new LaunchFileChooserAction()));
|
---|
57 |
|
---|
58 | tfFileName.addFocusListener(
|
---|
59 | new FocusAdapter() {
|
---|
60 | @Override
|
---|
61 | public void focusGained(FocusEvent e) {
|
---|
62 | tfFileName.selectAll();
|
---|
63 | }
|
---|
64 | }
|
---|
65 | );
|
---|
66 | }
|
---|
67 |
|
---|
68 | public FilenameCellEditor() {
|
---|
69 | listeners = new CopyOnWriteArrayList<CellEditorListener>();
|
---|
70 | build();
|
---|
71 | }
|
---|
72 |
|
---|
73 | public void addCellEditorListener(CellEditorListener l) {
|
---|
74 | if (l != null) {
|
---|
75 | listeners.addIfAbsent(l);
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | protected void fireEditingCanceled() {
|
---|
80 | for (CellEditorListener l: listeners) {
|
---|
81 | l.editingCanceled(new ChangeEvent(this));
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected void fireEditingStopped() {
|
---|
86 | for (CellEditorListener l: listeners) {
|
---|
87 | l.editingStopped(new ChangeEvent(this));
|
---|
88 | }
|
---|
89 | }
|
---|
90 |
|
---|
91 | public void cancelCellEditing() {
|
---|
92 | fireEditingCanceled();
|
---|
93 | }
|
---|
94 |
|
---|
95 | public Object getCellEditorValue() {
|
---|
96 | return value;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public boolean isCellEditable(EventObject anEvent) {
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void removeCellEditorListener(CellEditorListener l) {
|
---|
104 | listeners.remove(l);
|
---|
105 | }
|
---|
106 |
|
---|
107 | public boolean shouldSelectCell(EventObject anEvent) {
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | public boolean stopCellEditing() {
|
---|
112 | if (tfFileName.getText() == null || tfFileName.getText().trim().equals("")) {
|
---|
113 | value = null;
|
---|
114 | } else {
|
---|
115 | value = new File(tfFileName.getText());
|
---|
116 | }
|
---|
117 | fireEditingStopped();
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 | public void setInitialValue(File initialValue) {
|
---|
122 | this.value = initialValue;
|
---|
123 | if (initialValue == null) {
|
---|
124 | this.tfFileName.setText("");
|
---|
125 | } else {
|
---|
126 | this.tfFileName.setText(initialValue.toString());
|
---|
127 | }
|
---|
128 | }
|
---|
129 |
|
---|
130 | public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
---|
131 | SaveLayerInfo info = (SaveLayerInfo)value;
|
---|
132 | setInitialValue(info.getFile());
|
---|
133 | tfFileName.selectAll();
|
---|
134 | return this;
|
---|
135 | }
|
---|
136 |
|
---|
137 | class LaunchFileChooserAction extends AbstractAction {
|
---|
138 | public LaunchFileChooserAction() {
|
---|
139 | putValue(NAME, "...");
|
---|
140 | putValue(SHORT_DESCRIPTION, tr("Launch a file chooser to select a file"));
|
---|
141 | }
|
---|
142 |
|
---|
143 | public void actionPerformed(ActionEvent e) {
|
---|
144 | File f = SaveActionBase.createAndOpenSaveFileChooser(tr("Select filename"), "osm");
|
---|
145 | if (f != null) {
|
---|
146 | FilenameCellEditor.this.tfFileName.setText(f.toString());
|
---|
147 | FilenameCellEditor.this.tfFileName.selectAll();
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|