Changeset 32395 in osm for applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/FindRelationAction.java
- Timestamp:
- 2016-06-24T09:10:57+02:00 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/reltoolbox/src/relcontext/actions/FindRelationAction.java
r30737 r32395 1 // License: GPL. For details, see LICENSE file. 1 2 package relcontext.actions; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 2 5 3 6 import java.awt.BorderLayout; 4 7 import java.awt.Dialog.ModalityType; 5 8 import java.awt.Dimension; 6 import java.awt.event.*; 7 import static org.openstreetmap.josm.tools.I18n.tr; 8 import java.util.*; 9 import java.awt.event.ActionEvent; 10 import java.awt.event.ActionListener; 11 import java.awt.event.KeyAdapter; 12 import java.awt.event.KeyEvent; 13 import java.awt.event.MouseAdapter; 14 import java.awt.event.MouseEvent; 9 15 import java.util.ArrayList; 10 import javax.swing.*; 16 import java.util.Collection; 17 import java.util.Collections; 18 import java.util.List; 19 20 import javax.swing.AbstractListModel; 21 import javax.swing.DefaultListSelectionModel; 22 import javax.swing.JDialog; 23 import javax.swing.JList; 24 import javax.swing.JOptionPane; 25 import javax.swing.JPanel; 26 import javax.swing.JScrollPane; 27 import javax.swing.JTextField; 28 import javax.swing.ListSelectionModel; 29 import javax.swing.SwingUtilities; 30 11 31 import org.openstreetmap.josm.Main; 12 32 import org.openstreetmap.josm.actions.JosmAction; … … 15 35 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 16 36 import org.openstreetmap.josm.tools.Shortcut; 37 17 38 import relcontext.ChosenRelation; 18 39 … … 25 46 protected ChosenRelation chRel; 26 47 27 public FindRelationAction( ChosenRelation chRel) {48 public FindRelationAction(ChosenRelation chRel) { 28 49 super(tr("Find"), "relcontext/find", tr("Find a relation"), 29 50 Shortcut.registerShortcut("reltoolbox:find", tr("Relation Toolbox: {0}", tr("Find a relation")), 30 KeyEvent.VK_F, Shortcut.ALT_CTRL), false); 51 KeyEvent.VK_F, Shortcut.ALT_CTRL), false); 31 52 this.chRel = chRel; 32 53 } 33 54 34 public void actionPerformed( ActionEvent e ) { 55 @Override 56 public void actionPerformed(ActionEvent e) { 35 57 JPanel panel = new JPanel(new BorderLayout()); 36 58 final JTextField searchField = new JTextField(); … … 55 77 relationsList.addMouseListener(new MouseAdapter() { 56 78 @Override 57 public void mouseClicked( MouseEvent e) {58 if (e.getClickCount() >= 2 && !relationsList.isSelectionEmpty()) {79 public void mouseClicked(MouseEvent e) { 80 if (e.getClickCount() >= 2 && !relationsList.isSelectionEmpty()) { 59 81 dlg.setVisible(false); 60 82 optionPane.setValue(JOptionPane.OK_OPTION); … … 64 86 65 87 searchField.addActionListener(new ActionListener() { 66 public void actionPerformed( ActionEvent e ) { 67 if( !relationsList.isSelectionEmpty() ) { 88 @Override 89 public void actionPerformed(ActionEvent e) { 90 if (!relationsList.isSelectionEmpty()) { 68 91 dlg.setVisible(false); 69 92 optionPane.setValue(JOptionPane.OK_OPTION); … … 74 97 searchField.addKeyListener(new KeyAdapter() { 75 98 @Override 76 public void keyTyped( KeyEvent e) {99 public void keyTyped(KeyEvent e) { 77 100 SwingUtilities.invokeLater(new Runnable() { 101 @Override 78 102 public void run() { 79 103 updateRelationData(relationsData, searchField.getText()); … … 83 107 84 108 @Override 85 public void keyPressed( KeyEvent e) {86 if (shouldForward(e) )109 public void keyPressed(KeyEvent e) { 110 if (shouldForward(e) ) { 87 111 relationsList.dispatchEvent(e); 88 } 89 90 @Override 91 public void keyReleased( KeyEvent e ) { 92 if( shouldForward(e) ) 112 } 113 } 114 115 @Override 116 public void keyReleased(KeyEvent e) { 117 if (shouldForward(e) ) { 93 118 relationsList.dispatchEvent(e); 94 } 95 96 private boolean shouldForward( KeyEvent e ) { 119 } 120 } 121 122 private boolean shouldForward(KeyEvent e) { 97 123 return e.getKeyCode() == KeyEvent.VK_UP || e.getKeyCode() == KeyEvent.VK_DOWN 98 124 || e.getKeyCode() == KeyEvent.VK_PAGE_DOWN || e.getKeyCode() == KeyEvent.VK_PAGE_UP … … 105 131 106 132 Object answer = optionPane.getValue(); 107 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE108 || (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION) ) {133 if (answer == null || answer == JOptionPane.UNINITIALIZED_VALUE 134 || (answer instanceof Integer && (Integer)answer != JOptionPane.OK_OPTION)) 109 135 return; 110 } 111 112 Relation r = (Relation)relationsList.getSelectedValue(); 113 if( r != null ) 136 137 Relation r = relationsList.getSelectedValue(); 138 if (r != null ) { 114 139 chRel.set(r); 140 } 115 141 } 116 142 … … 120 146 } 121 147 122 protected void updateRelationData( FindRelationListModel data, String filter) {148 protected void updateRelationData(FindRelationListModel data, String filter) { 123 149 String[] keywords = filter == null ? new String[0] : filter.split("\\s+"); 124 if (keywords.length > 0) {150 if (keywords.length > 0) { 125 151 List<String> filteredKeywords = new ArrayList<>(keywords.length); 126 for (String s : keywords )127 if (s.length() > 0 )152 for (String s : keywords ) 153 if (s.length() > 0 ) { 128 154 filteredKeywords.add(s.trim().toLowerCase()); 155 } 129 156 keywords = filteredKeywords.toArray(new String[0]); 130 157 } 131 158 132 159 System.out.println("keywords.length = " + keywords.length); 133 for (int i = 0; i < keywords.length; i++ )160 for (int i = 0; i < keywords.length; i++ ) { 134 161 System.out.println("keyword["+i+"] = " + keywords[i]); 162 } 135 163 136 164 List<Relation> relations = new ArrayList<>(); 137 if (getEditLayer() != null) {138 for (Relation r : getEditLayer().data.getRelations()) {139 if (!r.isDeleted() && r.isVisible() && !r.isIncomplete()) {165 if (getEditLayer() != null) { 166 for (Relation r : getEditLayer().data.getRelations()) { 167 if (!r.isDeleted() && r.isVisible() && !r.isIncomplete()) { 140 168 boolean add = true; 141 for (int i = 0; i < keywords.length && add; i++) {169 for (int i = 0; i < keywords.length && add; i++) { 142 170 boolean ok = false; 143 if (String.valueOf(r.getPrimitiveId().getUniqueId()).contains(keywords[i]) )171 if (String.valueOf(r.getPrimitiveId().getUniqueId()).contains(keywords[i]) ) { 144 172 ok = true; 145 else { 146 for (String key : r.keySet()) {147 if (key.contains(keywords[i]) || r.get(key).toLowerCase().contains(keywords[i])148 || tr(r.get(key)).toLowerCase().contains(keywords[i]) ) {173 } else { 174 for (String key : r.keySet()) { 175 if (key.contains(keywords[i]) || r.get(key).toLowerCase().contains(keywords[i]) 176 || tr(r.get(key)).toLowerCase().contains(keywords[i])) { 149 177 ok = true; 150 178 break; … … 152 180 } 153 181 } 154 if( !ok ) add = false; 182 if (!ok ) { 183 add = false; 184 } 155 185 } 156 if ( add )186 if (add ) { 157 187 relations.add(r); 188 } 158 189 } 159 190 } … … 171 202 private DefaultListSelectionModel selectionModel; 172 203 173 public FindRelationListModel( DefaultListSelectionModel selectionModel) {204 public FindRelationListModel(DefaultListSelectionModel selectionModel) { 174 205 super(); 175 206 this.selectionModel = selectionModel; … … 190 221 191 222 @Override 192 public Relation getElementAt( int index) {223 public Relation getElementAt(int index) { 193 224 return relations.get(index); 194 225 } … … 197 228 int selectedIndex = selectionModel.getMinSelectionIndex(); 198 229 Relation sel = selectedIndex < 0 ? null : getElementAt(selectedIndex); 199 230 200 231 this.relations.clear(); 201 232 selectionModel.clearSelection(); 202 if (relations != null )233 if (relations != null ) { 203 234 this.relations.addAll(relations); 235 } 204 236 fireIntervalAdded(this, 0, getSize()); 205 237 206 if (sel != null) {238 if (sel != null) { 207 239 selectedIndex = this.relations.indexOf(sel); 208 if (selectedIndex >= 0 )240 if (selectedIndex >= 0 ) { 209 241 selectionModel.addSelectionInterval(selectedIndex, selectedIndex); 210 } 211 if( selectionModel.isSelectionEmpty() && !this.relations.isEmpty() ) 242 } 243 } 244 if (selectionModel.isSelectionEmpty() && !this.relations.isEmpty() ) { 212 245 selectionModel.addSelectionInterval(0, 0); 246 } 213 247 } 214 248 }
Note:
See TracChangeset
for help on using the changeset viewer.
