source: josm/trunk/src/org/openstreetmap/josm/gui/io/FilenameCellEditor.java@ 5003

Last change on this file since 5003 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.io;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagConstraints;
8import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.FocusAdapter;
11import java.awt.event.FocusEvent;
12import java.io.File;
13import java.util.EventObject;
14import java.util.concurrent.CopyOnWriteArrayList;
15
16import javax.swing.AbstractAction;
17import javax.swing.JButton;
18import javax.swing.JPanel;
19import javax.swing.JTable;
20import javax.swing.JTextField;
21import javax.swing.event.CellEditorListener;
22import javax.swing.event.ChangeEvent;
23import javax.swing.table.TableCellEditor;
24
25import 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 */
33class 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}
Note: See TracBrowser for help on using the repository browser.