Changeset 35982 in osm for applications/editors/josm
- Timestamp:
- 2022-06-16T20:09:45+02:00 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/DirectDownload/src/org/openstreetmap/josm/plugins/directdownload/DownloadDataGui.java
r35944 r35982 5 5 6 6 import java.awt.BorderLayout; 7 import java.awt.Component;8 7 import java.awt.Dimension; 9 8 import java.util.ArrayList; 10 9 import java.util.List; 11 10 12 import javax.swing.BorderFactory;13 11 import javax.swing.DefaultListSelectionModel; 14 import javax.swing.JLabel;15 12 import javax.swing.JPanel; 16 13 import javax.swing.JScrollPane; 17 14 import javax.swing.JTable; 18 15 import javax.swing.ListSelectionModel; 19 import javax.swing.UIManager;20 import javax.swing.table.DefaultTableColumnModel;21 16 import javax.swing.table.DefaultTableModel; 22 import javax.swing.table.TableCellRenderer;23 import javax.swing.table.TableColumn;24 17 25 18 import org.openstreetmap.josm.gui.ExtendedDialog; … … 28 21 public class DownloadDataGui extends ExtendedDialog { 29 22 30 private NamedResultTableModel model; 31 private NamedResultTableColumnModel columnmodel; 32 private JTable tblSearchResults; 23 private final NamedResultTableModel model; 24 private final JTable tblSearchResults; 33 25 34 26 public DownloadDataGui() { … … 44 36 45 37 DefaultListSelectionModel selectionModel = new DefaultListSelectionModel(); 46 model = new NamedResultTableModel( selectionModel);47 columnmodel= newNamedResultTableColumnModel();48 tblSearchResults = new JTable(model, columnmodel);38 model = new NamedResultTableModel(); 39 tblSearchResults = new JTable(model); 40 tblSearchResults.setAutoCreateRowSorter(true); 49 41 tblSearchResults.setSelectionModel(selectionModel); 50 42 tblSearchResults.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); … … 53 45 panel.add(scrollPane, BorderLayout.CENTER); 54 46 55 model.setData(new UserTrackReader().getTrackList()); 47 model.setData(new UserTrackReader().getTrackList()); 56 48 57 setContent(panel); 58 setupDialog(); 49 setContent(panel); 50 setupDialog(); 59 51 } 60 52 61 53 static class NamedResultTableModel extends DefaultTableModel { 62 54 private ArrayList<UserTrack> data; 63 private ListSelectionModel selectionModel;64 55 65 NamedResultTableModel( ListSelectionModel selectionModel) {56 NamedResultTableModel() { 66 57 data = new ArrayList<>(); 67 this.selectionModel = selectionModel;68 58 } 69 59 … … 77 67 public Object getValueAt(int row, int column) { 78 68 if (data == null) return null; 79 return data.get(row); 69 UserTrack track = data.get(row); 70 switch (column) { 71 case 0: 72 return track.datetime; 73 case 1: 74 return track.filename; 75 case 2: 76 return track.description; 77 case 3: 78 return String.join(";", track.tags); 79 default: 80 return track; 81 } 80 82 } 81 83 … … 89 91 } 90 92 91 public ArrayList<UserTrack> getDataArrayList() {93 public List<UserTrack> getDataArrayList() { 92 94 return data; 93 95 } … … 98 100 } 99 101 100 public UserTrack getSelectedUserTrack() { 101 if (selectionModel.getMinSelectionIndex() < 0) 102 return null; 103 return data.get(selectionModel.getMinSelectionIndex()); 102 @Override 103 public int getColumnCount() { 104 return 4; 105 } 106 107 @Override 108 public String getColumnName(int column) { 109 switch (column) { 110 case 0: 111 return tr("Date"); 112 case 1: 113 return tr("Filename"); 114 case 2: 115 return tr("Description"); 116 case 3: 117 return tr("Tags"); 118 default: 119 throw new IllegalArgumentException("Unknown column"); 120 } 121 } 122 123 @Override 124 public Class<?> getColumnClass(int columnIndex) { 125 switch (columnIndex) { 126 case 0: 127 case 1: 128 case 2: 129 case 3: 130 return String.class; 131 default: 132 throw new IllegalArgumentException("Unknown column"); 133 } 104 134 } 105 135 } … … 111 141 112 142 for (int i : selected) { 113 selectedTracks.add(dataArray.get( i));143 selectedTracks.add(dataArray.get(tblSearchResults.convertRowIndexToModel(i))); 114 144 } 115 145 … … 118 148 119 149 public UserTrack getSelectedUserTrack() { 120 return model.getSelectedUserTrack(); 121 } 122 123 static class NamedResultTableColumnModel extends DefaultTableColumnModel { 124 protected void createColumns() { 125 TableColumn col; 126 NamedResultCellRenderer renderer = new NamedResultCellRenderer(); 127 128 // column 0 - DateTime 129 col = new TableColumn(0); 130 col.setHeaderValue(tr("Date")); 131 col.setResizable(true); 132 col.setPreferredWidth(150); 133 col.setCellRenderer(renderer); 134 addColumn(col); 135 136 // column 1 - Filename 137 col = new TableColumn(1); 138 col.setHeaderValue(tr("Filename")); 139 col.setResizable(true); 140 col.setPreferredWidth(200); 141 col.setCellRenderer(renderer); 142 addColumn(col); 143 144 // column 2 - Description 145 col = new TableColumn(2); 146 col.setHeaderValue(tr("Description")); 147 col.setResizable(true); 148 col.setPreferredWidth(450); 149 col.setCellRenderer(renderer); 150 addColumn(col); 151 152 // column 3 - tags 153 154 col = new TableColumn(3); 155 col.setHeaderValue(tr("Tags")); 156 col.setResizable(true); 157 col.setPreferredWidth(100); 158 col.setCellRenderer(renderer); 159 addColumn(col); 160 161 } 162 163 NamedResultTableColumnModel() { 164 createColumns(); 165 } 166 } 167 168 static class NamedResultCellRenderer extends JLabel implements TableCellRenderer { 169 170 NamedResultCellRenderer() { 171 setOpaque(true); 172 setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2)); 173 } 174 175 protected void reset() { 176 setText(""); 177 setIcon(null); 178 } 179 180 protected void renderColor(boolean selected) { 181 if (selected) { 182 setForeground(UIManager.getColor("Table.selectionForeground")); 183 setBackground(UIManager.getColor("Table.selectionBackground")); 184 } else { 185 setForeground(UIManager.getColor("Table.foreground")); 186 setBackground(UIManager.getColor("Table.background")); 187 } 188 } 189 190 @Override 191 public Component getTableCellRendererComponent(JTable table, Object value, 192 boolean isSelected, boolean hasFocus, int row, int column) { 193 194 reset(); 195 renderColor(isSelected); 196 197 if (value == null) return this; 198 UserTrack sr = (UserTrack) value; 199 switch(column) { 200 case 0: 201 setText(sr.datetime); 202 break; 203 case 1: 204 setText(sr.filename); 205 break; 206 case 2: 207 setText(sr.description); 208 break; 209 210 case 3: 211 setText(String.join(";", sr.tags)); 212 break; 213 214 } 215 return this; 216 } 150 return model.getDataArrayList().get(tblSearchResults.convertRowIndexToModel(tblSearchResults.getSelectedRow())); 217 151 } 218 152 }
Note:
See TracChangeset
for help on using the changeset viewer.