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

Last change on this file since 8379 was 8379, checked in by Don-vip, 10 years ago

Consecutively calls to StringBuffer/StringBuilder .append should reuse the target object

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