1 |
alfonx |
139 |
/* |
2 |
|
|
* SearchMapDialog.java |
3 |
|
|
* |
4 |
|
|
* Created on __DATE__, __TIME__ |
5 |
|
|
*/ |
6 |
|
|
|
7 |
|
|
package skrueger.geotools.labelsearch; |
8 |
|
|
|
9 |
|
|
import java.awt.BorderLayout; |
10 |
|
|
import java.awt.Cursor; |
11 |
|
|
import java.awt.FlowLayout; |
12 |
|
|
import java.awt.Point; |
13 |
|
|
import java.awt.Window; |
14 |
|
|
import java.awt.event.KeyAdapter; |
15 |
|
|
import java.awt.event.KeyEvent; |
16 |
|
|
import java.awt.event.MouseAdapter; |
17 |
|
|
import java.awt.event.MouseEvent; |
18 |
|
|
import java.util.List; |
19 |
|
|
import java.util.concurrent.ExecutionException; |
20 |
|
|
|
21 |
|
|
import javax.swing.BorderFactory; |
22 |
|
|
import javax.swing.JLabel; |
23 |
|
|
import javax.swing.JPanel; |
24 |
|
|
import javax.swing.JScrollPane; |
25 |
|
|
import javax.swing.JTextField; |
26 |
|
|
import javax.swing.ListSelectionModel; |
27 |
|
|
import javax.swing.SwingWorker; |
28 |
|
|
|
29 |
|
|
import org.apache.log4j.Logger; |
30 |
|
|
import org.geotools.gui.swing.ExceptionMonitor; |
31 |
|
|
import org.geotools.gui.swing.JMapPane; |
32 |
|
|
|
33 |
|
|
import schmitzm.swing.SortableJTable; |
34 |
|
|
import schmitzm.swing.SwingUtil; |
35 |
|
|
import skrueger.geotools.labelsearch.LabelSearch; |
36 |
|
|
import skrueger.geotools.labelsearch.SearchResult; |
37 |
|
|
|
38 |
|
|
/** |
39 |
|
|
* @author Stefan A. Krueger |
40 |
|
|
*/ |
41 |
|
|
public class SearchMapDialog extends javax.swing.JDialog { |
42 |
|
|
final static private Logger LOGGER = Logger |
43 |
|
|
.getLogger(SearchMapDialog.class); |
44 |
|
|
|
45 |
|
|
private final LabelSearch labelSearch; |
46 |
|
|
/** This JPanel shows the results to the user if there is more than one **/ |
47 |
|
|
final private JTextField searchTextField = new JTextField(25); |
48 |
|
|
final private JScrollPane scrollPane = new JScrollPane(); |
49 |
|
|
|
50 |
|
|
public String getSearchText() { |
51 |
|
|
return searchTextField.getText(); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
public void setSearchText(String text) { |
55 |
|
|
searchTextField.setText(text); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
private final JMapPane mapPane; |
59 |
|
|
|
60 |
|
|
/** |
61 |
|
|
* The dialog will be relative to the {@link JMapPane}s parent |
62 |
|
|
* {@link Window}. |
63 |
|
|
* |
64 |
|
|
* @param parent |
65 |
|
|
* @param labelSearch |
66 |
|
|
* @param mapPane |
67 |
|
|
* @param windowTitle |
68 |
|
|
* A title to be used for the dialog. |
69 |
|
|
*/ |
70 |
|
|
public SearchMapDialog(LabelSearch labelSearch, JMapPane mapPane, |
71 |
|
|
final String windowTitle) { |
72 |
|
|
super(SwingUtil.getParentWindow(mapPane), windowTitle); |
73 |
|
|
this.labelSearch = labelSearch; |
74 |
|
|
this.mapPane = mapPane; |
75 |
|
|
initialize(); |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
// Pressing ESC disposes the Dialog |
79 |
|
|
KeyAdapter keyEscDispose = new KeyAdapter() { |
80 |
|
|
public void keyPressed(KeyEvent e) { |
81 |
|
|
int key = e.getKeyCode(); |
82 |
|
|
if (key == KeyEvent.VK_ESCAPE) { |
83 |
|
|
dispose(); |
84 |
|
|
} |
85 |
|
|
} |
86 |
|
|
}; |
87 |
|
|
|
88 |
|
|
// Pressing ENTER in the JTextfield automatically starts the search |
89 |
|
|
KeyAdapter keyListenerStartSearch = new KeyAdapter() { |
90 |
|
|
public void keyPressed(KeyEvent e) { |
91 |
|
|
int key = e.getKeyCode(); |
92 |
|
|
if (key == KeyEvent.VK_ENTER) { |
93 |
|
|
if (searchTextField.getText().trim().length() > 0) |
94 |
|
|
search(searchTextField.getText()); |
95 |
|
|
} |
96 |
|
|
} |
97 |
|
|
}; |
98 |
|
|
|
99 |
|
|
private void initialize() { |
100 |
|
|
|
101 |
|
|
JPanel cp = new JPanel(new BorderLayout()); |
102 |
|
|
|
103 |
|
|
JLabel label = new JLabel(LabelSearch.R("SearchMapDialog.searchString.Label")); |
104 |
|
|
label.setLabelFor(searchTextField); |
105 |
|
|
searchTextField.setToolTipText(LabelSearch.R("SearchMapDialog.searchString.tt")); |
106 |
|
|
|
107 |
|
|
searchTextField.addKeyListener(keyListenerStartSearch); |
108 |
|
|
|
109 |
|
|
// Pressing ESC disposes the Dialog |
110 |
|
|
searchTextField.addKeyListener(keyEscDispose); |
111 |
|
|
|
112 |
|
|
JPanel upper = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
113 |
|
|
upper.add(label); |
114 |
|
|
upper.add(searchTextField); |
115 |
|
|
|
116 |
|
|
cp.add(upper, BorderLayout.NORTH); |
117 |
|
|
|
118 |
|
|
SwingUtil.setPreferredHeight(scrollPane, 0); |
119 |
|
|
SwingUtil.setMaximumHeight(scrollPane, 0); |
120 |
|
|
cp.add(scrollPane, BorderLayout.SOUTH); |
121 |
|
|
|
122 |
|
|
cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
123 |
|
|
|
124 |
|
|
setContentPane(cp); |
125 |
|
|
pack(); |
126 |
|
|
|
127 |
|
|
Point mapPanelocationOnScreen = mapPane.getLocationOnScreen(); |
128 |
|
|
mapPanelocationOnScreen.x -= mapPane.getLocation().x; |
129 |
|
|
mapPanelocationOnScreen.y -= mapPane.getLocation().y; |
130 |
|
|
setLocation(mapPanelocationOnScreen); |
131 |
|
|
|
132 |
|
|
// SwingUtil.centerFrameOnScreenRandom(this); |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/** |
136 |
|
|
* Performes a search on the {@link JMapPane}'s labels and outputs the |
137 |
|
|
* possible numerouse results. |
138 |
|
|
* |
139 |
|
|
* @param text |
140 |
|
|
* Text to search |
141 |
|
|
*/ |
142 |
|
|
public void search(final String text) { |
143 |
|
|
|
144 |
|
|
final Cursor backupCursor = getContentPane().getCursor(); |
145 |
|
|
getContentPane().setCursor( |
146 |
|
|
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
147 |
|
|
searchTextField.setEnabled(false); |
148 |
|
|
|
149 |
|
|
SwingWorker<List<SearchResult>, String> swingWorker = new SwingWorker<List<SearchResult>, String>() { |
150 |
|
|
@Override |
151 |
|
|
protected List<SearchResult> doInBackground() throws Exception { |
152 |
|
|
return labelSearch.search(text); |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
@Override |
156 |
|
|
protected void done() { |
157 |
|
|
getContentPane().setCursor(backupCursor); |
158 |
|
|
searchTextField.setEnabled(true); |
159 |
|
|
super.done(); |
160 |
|
|
try { |
161 |
|
|
showMultipleResults(get()); |
162 |
|
|
} catch (InterruptedException e) { |
163 |
|
|
LOGGER.error(e); |
164 |
|
|
ExceptionMonitor.show(SearchMapDialog.this, e); |
165 |
|
|
} catch (ExecutionException e) { |
166 |
|
|
LOGGER.error(e); |
167 |
|
|
ExceptionMonitor.show(SearchMapDialog.this, e); |
168 |
|
|
} |
169 |
|
|
} |
170 |
|
|
}; |
171 |
|
|
swingWorker.execute(); |
172 |
|
|
|
173 |
|
|
} |
174 |
|
|
|
175 |
|
|
private void showMultipleResults(final List<SearchResult> searchResultsList) { |
176 |
|
|
|
177 |
|
|
final SortableJTable resultsTable = new SortableJTable(); |
178 |
|
|
resultsTable.setModel(new SearchResultTableModel(searchResultsList)); |
179 |
|
|
|
180 |
|
|
resultsTable.addKeyListener(keyEscDispose); |
181 |
|
|
|
182 |
|
|
// set up the table |
183 |
|
|
resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
184 |
|
|
|
185 |
|
|
// resultsTable.setRowSorter( new TableRowSorter<SearchResultTableModel>((SearchResultTableModel)resultsTable.getModel())); |
186 |
|
|
// |
187 |
|
|
resultsTable.setToolTipText(LabelSearch.R("SearchMapDialog.resultsTable.tt")); |
188 |
|
|
|
189 |
|
|
// // The first column should be small |
190 |
|
|
// resultsTable.getColumnModel().getColumn(0).setMinWidth(20); |
191 |
|
|
// resultsTable.getColumnModel().getColumn(0).setMaxWidth(25); |
192 |
|
|
|
193 |
|
|
resultsTable.addMouseListener(new MouseAdapter() { |
194 |
|
|
|
195 |
|
|
@Override |
196 |
|
|
public void mouseClicked(MouseEvent e) { |
197 |
|
|
int index = resultsTable.getSelectedModelRow(); |
198 |
|
|
SearchResult searchResult = searchResultsList.get(index); |
199 |
|
|
Cursor backupCursor = scrollPane.getCursor(); |
200 |
|
|
scrollPane.setCursor(Cursor |
201 |
|
|
.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
202 |
|
|
searchResult.open(); |
203 |
|
|
scrollPane.setCursor(backupCursor); |
204 |
|
|
} |
205 |
|
|
}); |
206 |
|
|
|
207 |
|
|
scrollPane.setViewportView(resultsTable); |
208 |
|
|
SwingUtil.setPreferredHeight(scrollPane, 140); |
209 |
|
|
SwingUtil.setMaximumHeight(scrollPane, 140); |
210 |
|
|
|
211 |
|
|
pack(); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
} |