source: josm/trunk/src/org/openstreetmap/josm/gui/io/LayerNameAndFilePathTableCell.java@ 11457

Last change on this file since 11457 was 10873, checked in by Don-vip, 8 years ago

see #13412 - replace character by ' in comments, cause encoding problems when applying UTF-8 patches with Eclipse on Windows, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=214085

  • Property svn:eol-style set to native
File size: 9.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.Color;
7import java.awt.Component;
8import java.awt.Dimension;
9import java.awt.Font;
10import java.awt.GridBagLayout;
11import java.awt.event.ActionEvent;
12import java.awt.event.FocusAdapter;
13import java.awt.event.FocusEvent;
14import java.io.File;
15import java.util.EventObject;
16
17import javax.swing.AbstractAction;
18import javax.swing.BorderFactory;
19import javax.swing.JButton;
20import javax.swing.JLabel;
21import javax.swing.JPanel;
22import javax.swing.JTable;
23import javax.swing.event.CellEditorListener;
24import javax.swing.table.TableCellEditor;
25import javax.swing.table.TableCellRenderer;
26
27import org.openstreetmap.josm.actions.SaveActionBase;
28import org.openstreetmap.josm.gui.util.CellEditorSupport;
29import org.openstreetmap.josm.gui.widgets.JosmTextField;
30import org.openstreetmap.josm.tools.GBC;
31
32/**
33 * Display and edit layer name and file path in a <code>JTable</code>.
34 *
35 * Note: Do not use the same object both as <code>TableCellRenderer</code> and
36 * <code>TableCellEditor</code> - this can mess up the current editor component
37 * by subsequent calls to the renderer (#12462).
38 */
39class LayerNameAndFilePathTableCell extends JPanel implements TableCellRenderer, TableCellEditor {
40 private static final Color colorError = new Color(255, 197, 197);
41 private static final String ELLIPSIS = '…' + File.separator;
42
43 private final JLabel lblLayerName = new JLabel();
44 private final JLabel lblFilename = new JLabel("");
45 private final JosmTextField tfFilename = new JosmTextField();
46 private final JButton btnFileChooser = new JButton(new LaunchFileChooserAction());
47
48 private static final GBC defaultCellStyle = GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 0);
49
50 private final transient CellEditorSupport cellEditorSupport = new CellEditorSupport(this);
51 private File value;
52
53 /** constructor that sets the default on each element **/
54 LayerNameAndFilePathTableCell() {
55 setLayout(new GridBagLayout());
56
57 lblLayerName.setPreferredSize(new Dimension(lblLayerName.getPreferredSize().width, 19));
58 lblLayerName.setFont(lblLayerName.getFont().deriveFont(Font.BOLD));
59
60 lblFilename.setPreferredSize(new Dimension(lblFilename.getPreferredSize().width, 19));
61 lblFilename.setOpaque(true);
62 lblFilename.setLabelFor(btnFileChooser);
63
64 tfFilename.setToolTipText(tr("Either edit the path manually in the text field or click the \"...\" button to open a file chooser."));
65 tfFilename.setPreferredSize(new Dimension(tfFilename.getPreferredSize().width, 19));
66 tfFilename.addFocusListener(
67 new FocusAdapter() {
68 @Override
69 public void focusGained(FocusEvent e) {
70 tfFilename.selectAll();
71 }
72 }
73 );
74 // hide border
75 tfFilename.setBorder(BorderFactory.createLineBorder(getBackground()));
76
77 btnFileChooser.setPreferredSize(new Dimension(20, 19));
78 btnFileChooser.setOpaque(true);
79 }
80
81 /** renderer used while not editing the file path **/
82 @Override
83 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
84 boolean hasFocus, int row, int column) {
85 removeAll();
86 if (value == null) return this;
87 SaveLayerInfo info = (SaveLayerInfo) value;
88 StringBuilder sb = new StringBuilder();
89 sb.append("<html>")
90 .append(addLblLayerName(info));
91 if (info.isSavable()) {
92 add(btnFileChooser, GBC.std());
93 sb.append("<br>")
94 .append(addLblFilename(info));
95 }
96 sb.append("</html>");
97 setToolTipText(sb.toString());
98 return this;
99 }
100
101 @Override
102 public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
103 removeAll();
104 SaveLayerInfo info = (SaveLayerInfo) value;
105 value = info.getFile();
106 tfFilename.setText(value == null ? "" : value.toString());
107
108 StringBuilder sb = new StringBuilder();
109 sb.append("<html>")
110 .append(addLblLayerName(info));
111
112 if (info.isSavable()) {
113 add(btnFileChooser, GBC.std());
114 add(tfFilename, GBC.eol().fill(GBC.HORIZONTAL).insets(1, 0, 0, 0));
115 tfFilename.selectAll();
116
117 sb.append("<br>")
118 .append(tfFilename.getToolTipText());
119 }
120 sb.append("</html>");
121 setToolTipText(sb.toString());
122 return this;
123 }
124
125 private static boolean canWrite(File f) {
126 if (f == null) return false;
127 if (f.isDirectory()) return false;
128 if (f.exists() && f.canWrite()) return true;
129 if (!f.exists() && f.getParentFile() != null && f.getParentFile().canWrite())
130 return true;
131 return false;
132 }
133
134 /**
135 * Adds layer name label to (this) using the given info. Returns tooltip that should be added to the panel
136 * @param info information, user preferences and save/upload states of the layer
137 * @return tooltip that should be added to the panel
138 */
139 private String addLblLayerName(SaveLayerInfo info) {
140 lblLayerName.setIcon(info.getLayer().getIcon());
141 lblLayerName.setText(info.getName());
142 add(lblLayerName, defaultCellStyle);
143 return tr("The bold text is the name of the layer.");
144 }
145
146 /**
147 * Adds filename label to (this) using the given info. Returns tooltip that should be added to the panel
148 * @param info information, user preferences and save/upload states of the layer
149 * @return tooltip that should be added to the panel
150 */
151 private String addLblFilename(SaveLayerInfo info) {
152 String tooltip;
153 boolean error = false;
154 if (info.getFile() == null) {
155 error = info.isDoSaveToFile();
156 lblFilename.setText(tr("Click here to choose save path"));
157 lblFilename.setFont(lblFilename.getFont().deriveFont(Font.ITALIC));
158 tooltip = tr("Layer ''{0}'' is not backed by a file", info.getName());
159 } else {
160 String t = info.getFile().getPath();
161 lblFilename.setText(makePathFit(t));
162 tooltip = info.getFile().getAbsolutePath();
163 if (info.isDoSaveToFile() && !canWrite(info.getFile())) {
164 error = true;
165 tooltip = tr("File ''{0}'' is not writable. Please enter another file name.", info.getFile().getPath());
166 }
167 }
168
169 lblFilename.setBackground(error ? colorError : getBackground());
170 btnFileChooser.setBackground(error ? colorError : getBackground());
171
172 add(lblFilename, defaultCellStyle);
173 return tr("Click cell to change the file path.") + "<br/>" + tooltip;
174 }
175
176 /**
177 * Makes the given path fit lblFilename, appends ellipsis on the left if it doesn't fit.
178 * Idea: /home/user/josm → …/user/josm → …/josm; and take the first one that fits
179 * @param t complete path
180 * @return shorter path
181 */
182 private String makePathFit(String t) {
183 boolean hasEllipsis = false;
184 while (t != null && !t.isEmpty()) {
185 int txtwidth = lblFilename.getFontMetrics(lblFilename.getFont()).stringWidth(t);
186 if (txtwidth < lblFilename.getWidth() || t.lastIndexOf(File.separator) < ELLIPSIS.length()) {
187 break;
188 }
189 // remove ellipsis, if present
190 t = hasEllipsis ? t.substring(ELLIPSIS.length()) : t;
191 // cut next block, and re-add ellipsis
192 t = ELLIPSIS + t.substring(t.indexOf(File.separator) + 1);
193 hasEllipsis = true;
194 }
195 return t;
196 }
197
198 @Override
199 public void addCellEditorListener(CellEditorListener l) {
200 cellEditorSupport.addCellEditorListener(l);
201 }
202
203 @Override
204 public void cancelCellEditing() {
205 cellEditorSupport.fireEditingCanceled();
206 }
207
208 @Override
209 public Object getCellEditorValue() {
210 return value;
211 }
212
213 @Override
214 public boolean isCellEditable(EventObject anEvent) {
215 return true;
216 }
217
218 @Override
219 public void removeCellEditorListener(CellEditorListener l) {
220 cellEditorSupport.removeCellEditorListener(l);
221 }
222
223 @Override
224 public boolean shouldSelectCell(EventObject anEvent) {
225 return true;
226 }
227
228 @Override
229 public boolean stopCellEditing() {
230 if (tfFilename.getText() == null || tfFilename.getText().trim().isEmpty()) {
231 value = null;
232 } else {
233 value = new File(tfFilename.getText());
234 }
235 cellEditorSupport.fireEditingStopped();
236 return true;
237 }
238
239 private class LaunchFileChooserAction extends AbstractAction {
240 LaunchFileChooserAction() {
241 putValue(NAME, "...");
242 putValue(SHORT_DESCRIPTION, tr("Launch a file chooser to select a file"));
243 }
244
245 @Override
246 public void actionPerformed(ActionEvent e) {
247 File f = SaveActionBase.createAndOpenSaveFileChooser(tr("Select filename"), "osm");
248 if (f != null) {
249 tfFilename.setText(f.toString());
250 stopCellEditing();
251 }
252 }
253 }
254}
Note: See TracBrowser for help on using the repository browser.