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

Last change on this file since 12452 was 11893, checked in by Don-vip, 7 years ago

sonar - squid:S1126 - Return of boolean expressions should not be wrapped into an "if-then-else" statement

  • Property svn:eol-style set to native
File size: 9.1 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 || f.isDirectory()) return false;
127 if (f.exists() && f.canWrite()) return true;
128 return !f.exists() && f.getParentFile() != null && f.getParentFile().canWrite();
129 }
130
131 /**
132 * Adds layer name label to (this) using the given info. Returns tooltip that should be added to the panel
133 * @param info information, user preferences and save/upload states of the layer
134 * @return tooltip that should be added to the panel
135 */
136 private String addLblLayerName(SaveLayerInfo info) {
137 lblLayerName.setIcon(info.getLayer().getIcon());
138 lblLayerName.setText(info.getName());
139 add(lblLayerName, defaultCellStyle);
140 return tr("The bold text is the name of the layer.");
141 }
142
143 /**
144 * Adds filename label to (this) using the given info. Returns tooltip that should be added to the panel
145 * @param info information, user preferences and save/upload states of the layer
146 * @return tooltip that should be added to the panel
147 */
148 private String addLblFilename(SaveLayerInfo info) {
149 String tooltip;
150 boolean error = false;
151 if (info.getFile() == null) {
152 error = info.isDoSaveToFile();
153 lblFilename.setText(tr("Click here to choose save path"));
154 lblFilename.setFont(lblFilename.getFont().deriveFont(Font.ITALIC));
155 tooltip = tr("Layer ''{0}'' is not backed by a file", info.getName());
156 } else {
157 String t = info.getFile().getPath();
158 lblFilename.setText(makePathFit(t));
159 tooltip = info.getFile().getAbsolutePath();
160 if (info.isDoSaveToFile() && !canWrite(info.getFile())) {
161 error = true;
162 tooltip = tr("File ''{0}'' is not writable. Please enter another file name.", info.getFile().getPath());
163 }
164 }
165
166 lblFilename.setBackground(error ? colorError : getBackground());
167 btnFileChooser.setBackground(error ? colorError : getBackground());
168
169 add(lblFilename, defaultCellStyle);
170 return tr("Click cell to change the file path.") + "<br/>" + tooltip;
171 }
172
173 /**
174 * Makes the given path fit lblFilename, appends ellipsis on the left if it doesn't fit.
175 * Idea: /home/user/josm → …/user/josm → …/josm; and take the first one that fits
176 * @param t complete path
177 * @return shorter path
178 */
179 private String makePathFit(String t) {
180 boolean hasEllipsis = false;
181 while (t != null && !t.isEmpty()) {
182 int txtwidth = lblFilename.getFontMetrics(lblFilename.getFont()).stringWidth(t);
183 if (txtwidth < lblFilename.getWidth() || t.lastIndexOf(File.separator) < ELLIPSIS.length()) {
184 break;
185 }
186 // remove ellipsis, if present
187 t = hasEllipsis ? t.substring(ELLIPSIS.length()) : t;
188 // cut next block, and re-add ellipsis
189 t = ELLIPSIS + t.substring(t.indexOf(File.separator) + 1);
190 hasEllipsis = true;
191 }
192 return t;
193 }
194
195 @Override
196 public void addCellEditorListener(CellEditorListener l) {
197 cellEditorSupport.addCellEditorListener(l);
198 }
199
200 @Override
201 public void cancelCellEditing() {
202 cellEditorSupport.fireEditingCanceled();
203 }
204
205 @Override
206 public Object getCellEditorValue() {
207 return value;
208 }
209
210 @Override
211 public boolean isCellEditable(EventObject anEvent) {
212 return true;
213 }
214
215 @Override
216 public void removeCellEditorListener(CellEditorListener l) {
217 cellEditorSupport.removeCellEditorListener(l);
218 }
219
220 @Override
221 public boolean shouldSelectCell(EventObject anEvent) {
222 return true;
223 }
224
225 @Override
226 public boolean stopCellEditing() {
227 if (tfFilename.getText() == null || tfFilename.getText().trim().isEmpty()) {
228 value = null;
229 } else {
230 value = new File(tfFilename.getText());
231 }
232 cellEditorSupport.fireEditingStopped();
233 return true;
234 }
235
236 private class LaunchFileChooserAction extends AbstractAction {
237 LaunchFileChooserAction() {
238 putValue(NAME, "...");
239 putValue(SHORT_DESCRIPTION, tr("Launch a file chooser to select a file"));
240 }
241
242 @Override
243 public void actionPerformed(ActionEvent e) {
244 File f = SaveActionBase.createAndOpenSaveFileChooser(tr("Select filename"), "osm");
245 if (f != null) {
246 tfFilename.setText(f.toString());
247 stopCellEditing();
248 }
249 }
250 }
251}
Note: See TracBrowser for help on using the repository browser.