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