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