1 |
alfonx |
244 |
/******************************************************************************* |
2 |
|
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
|
* |
4 |
|
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
|
|
* classes based on Java 1.6, focussing (not only) on Java Swing |
6 |
|
|
* and the Geotools library. |
7 |
|
|
* |
8 |
|
|
* The SCHMITZM project is hosted at: |
9 |
|
|
* http://wald.intevation.org/projects/schmitzm/ |
10 |
|
|
* |
11 |
|
|
* This program is free software; you can redistribute it and/or |
12 |
|
|
* modify it under the terms of the GNU Lesser General Public License |
13 |
|
|
* as published by the Free Software Foundation; either version 3 |
14 |
|
|
* of the License, or (at your option) any later version. |
15 |
|
|
* |
16 |
|
|
* This program is distributed in the hope that it will be useful, |
17 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
|
|
* GNU General Public License for more details. |
20 |
|
|
* |
21 |
|
|
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
|
|
* along with this program; if not, write to the Free Software |
23 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
|
|
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
|
|
* |
26 |
|
|
* Contributors: |
27 |
|
|
* Martin O. J. Schmitz - initial API and implementation |
28 |
|
|
* Stefan A. Krüger - additional utility classes |
29 |
|
|
******************************************************************************/ |
30 |
alfonx |
139 |
/* |
31 |
|
|
* SearchMapDialog.java |
32 |
|
|
* |
33 |
|
|
* Created on __DATE__, __TIME__ |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
package skrueger.geotools.labelsearch; |
37 |
|
|
|
38 |
|
|
import java.awt.BorderLayout; |
39 |
|
|
import java.awt.Cursor; |
40 |
|
|
import java.awt.FlowLayout; |
41 |
|
|
import java.awt.Point; |
42 |
|
|
import java.awt.Window; |
43 |
|
|
import java.awt.event.KeyAdapter; |
44 |
|
|
import java.awt.event.KeyEvent; |
45 |
|
|
import java.awt.event.MouseAdapter; |
46 |
|
|
import java.awt.event.MouseEvent; |
47 |
|
|
import java.util.List; |
48 |
|
|
import java.util.concurrent.ExecutionException; |
49 |
|
|
|
50 |
|
|
import javax.swing.BorderFactory; |
51 |
|
|
import javax.swing.JLabel; |
52 |
|
|
import javax.swing.JPanel; |
53 |
|
|
import javax.swing.JScrollPane; |
54 |
|
|
import javax.swing.JTextField; |
55 |
|
|
import javax.swing.ListSelectionModel; |
56 |
|
|
import javax.swing.SwingWorker; |
57 |
|
|
|
58 |
|
|
import org.apache.log4j.Logger; |
59 |
|
|
import org.geotools.gui.swing.ExceptionMonitor; |
60 |
|
|
import org.geotools.gui.swing.JMapPane; |
61 |
|
|
|
62 |
|
|
import schmitzm.swing.SortableJTable; |
63 |
|
|
import schmitzm.swing.SwingUtil; |
64 |
|
|
|
65 |
|
|
/** |
66 |
|
|
* @author Stefan A. Krueger |
67 |
|
|
*/ |
68 |
|
|
public class SearchMapDialog extends javax.swing.JDialog { |
69 |
|
|
final static private Logger LOGGER = Logger |
70 |
|
|
.getLogger(SearchMapDialog.class); |
71 |
|
|
|
72 |
|
|
private final LabelSearch labelSearch; |
73 |
|
|
/** This JPanel shows the results to the user if there is more than one **/ |
74 |
|
|
final private JTextField searchTextField = new JTextField(25); |
75 |
|
|
final private JScrollPane scrollPane = new JScrollPane(); |
76 |
|
|
|
77 |
|
|
public String getSearchText() { |
78 |
|
|
return searchTextField.getText(); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
public void setSearchText(String text) { |
82 |
|
|
searchTextField.setText(text); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
private final JMapPane mapPane; |
86 |
|
|
|
87 |
|
|
/** |
88 |
|
|
* The dialog will be relative to the {@link JMapPane}s parent |
89 |
|
|
* {@link Window}. |
90 |
|
|
* |
91 |
|
|
* @param parent |
92 |
|
|
* @param labelSearch |
93 |
|
|
* @param mapPane |
94 |
|
|
* @param windowTitle |
95 |
|
|
* A title to be used for the dialog. |
96 |
|
|
*/ |
97 |
|
|
public SearchMapDialog(LabelSearch labelSearch, JMapPane mapPane, |
98 |
|
|
final String windowTitle) { |
99 |
|
|
super(SwingUtil.getParentWindow(mapPane), windowTitle); |
100 |
|
|
this.labelSearch = labelSearch; |
101 |
|
|
this.mapPane = mapPane; |
102 |
|
|
initialize(); |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
// Pressing ESC disposes the Dialog |
106 |
|
|
KeyAdapter keyEscDispose = new KeyAdapter() { |
107 |
|
|
public void keyPressed(KeyEvent e) { |
108 |
|
|
int key = e.getKeyCode(); |
109 |
|
|
if (key == KeyEvent.VK_ESCAPE) { |
110 |
|
|
dispose(); |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
}; |
114 |
|
|
|
115 |
|
|
// Pressing ENTER in the JTextfield automatically starts the search |
116 |
|
|
KeyAdapter keyListenerStartSearch = new KeyAdapter() { |
117 |
|
|
public void keyPressed(KeyEvent e) { |
118 |
|
|
int key = e.getKeyCode(); |
119 |
|
|
if (key == KeyEvent.VK_ENTER) { |
120 |
|
|
if (searchTextField.getText().trim().length() > 0) |
121 |
|
|
search(searchTextField.getText()); |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
}; |
125 |
|
|
|
126 |
|
|
private void initialize() { |
127 |
|
|
|
128 |
|
|
JPanel cp = new JPanel(new BorderLayout()); |
129 |
|
|
|
130 |
|
|
JLabel label = new JLabel(LabelSearch.R("SearchMapDialog.searchString.Label")); |
131 |
|
|
label.setLabelFor(searchTextField); |
132 |
|
|
searchTextField.setToolTipText(LabelSearch.R("SearchMapDialog.searchString.tt")); |
133 |
|
|
|
134 |
|
|
searchTextField.addKeyListener(keyListenerStartSearch); |
135 |
|
|
|
136 |
|
|
// Pressing ESC disposes the Dialog |
137 |
|
|
searchTextField.addKeyListener(keyEscDispose); |
138 |
|
|
|
139 |
|
|
JPanel upper = new JPanel(new FlowLayout(FlowLayout.LEFT)); |
140 |
|
|
upper.add(label); |
141 |
|
|
upper.add(searchTextField); |
142 |
|
|
|
143 |
|
|
cp.add(upper, BorderLayout.NORTH); |
144 |
|
|
|
145 |
|
|
SwingUtil.setPreferredHeight(scrollPane, 0); |
146 |
|
|
SwingUtil.setMaximumHeight(scrollPane, 0); |
147 |
|
|
cp.add(scrollPane, BorderLayout.SOUTH); |
148 |
|
|
|
149 |
|
|
cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); |
150 |
|
|
|
151 |
|
|
setContentPane(cp); |
152 |
|
|
pack(); |
153 |
|
|
|
154 |
|
|
Point mapPanelocationOnScreen = mapPane.getLocationOnScreen(); |
155 |
|
|
mapPanelocationOnScreen.x -= mapPane.getLocation().x; |
156 |
|
|
mapPanelocationOnScreen.y -= mapPane.getLocation().y; |
157 |
|
|
setLocation(mapPanelocationOnScreen); |
158 |
|
|
|
159 |
|
|
// SwingUtil.centerFrameOnScreenRandom(this); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
/** |
163 |
|
|
* Performes a search on the {@link JMapPane}'s labels and outputs the |
164 |
|
|
* possible numerouse results. |
165 |
|
|
* |
166 |
|
|
* @param text |
167 |
|
|
* Text to search |
168 |
|
|
*/ |
169 |
|
|
public void search(final String text) { |
170 |
|
|
|
171 |
|
|
final Cursor backupCursor = getContentPane().getCursor(); |
172 |
|
|
getContentPane().setCursor( |
173 |
|
|
Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
174 |
|
|
searchTextField.setEnabled(false); |
175 |
|
|
|
176 |
|
|
SwingWorker<List<SearchResult>, String> swingWorker = new SwingWorker<List<SearchResult>, String>() { |
177 |
|
|
@Override |
178 |
|
|
protected List<SearchResult> doInBackground() throws Exception { |
179 |
|
|
return labelSearch.search(text); |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
@Override |
183 |
|
|
protected void done() { |
184 |
|
|
getContentPane().setCursor(backupCursor); |
185 |
|
|
searchTextField.setEnabled(true); |
186 |
|
|
super.done(); |
187 |
|
|
try { |
188 |
|
|
showMultipleResults(get()); |
189 |
|
|
} catch (InterruptedException e) { |
190 |
|
|
LOGGER.error(e); |
191 |
|
|
ExceptionMonitor.show(SearchMapDialog.this, e); |
192 |
|
|
} catch (ExecutionException e) { |
193 |
|
|
LOGGER.error(e); |
194 |
|
|
ExceptionMonitor.show(SearchMapDialog.this, e); |
195 |
|
|
} |
196 |
|
|
} |
197 |
|
|
}; |
198 |
|
|
swingWorker.execute(); |
199 |
|
|
|
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
private void showMultipleResults(final List<SearchResult> searchResultsList) { |
203 |
|
|
|
204 |
|
|
final SortableJTable resultsTable = new SortableJTable(); |
205 |
|
|
resultsTable.setModel(new SearchResultTableModel(searchResultsList)); |
206 |
|
|
|
207 |
|
|
resultsTable.addKeyListener(keyEscDispose); |
208 |
|
|
|
209 |
|
|
// set up the table |
210 |
|
|
resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); |
211 |
|
|
|
212 |
|
|
// resultsTable.setRowSorter( new TableRowSorter<SearchResultTableModel>((SearchResultTableModel)resultsTable.getModel())); |
213 |
|
|
// |
214 |
|
|
resultsTable.setToolTipText(LabelSearch.R("SearchMapDialog.resultsTable.tt")); |
215 |
|
|
|
216 |
|
|
// // The first column should be small |
217 |
|
|
// resultsTable.getColumnModel().getColumn(0).setMinWidth(20); |
218 |
|
|
// resultsTable.getColumnModel().getColumn(0).setMaxWidth(25); |
219 |
|
|
|
220 |
|
|
resultsTable.addMouseListener(new MouseAdapter() { |
221 |
|
|
|
222 |
|
|
@Override |
223 |
|
|
public void mouseClicked(MouseEvent e) { |
224 |
|
|
int index = resultsTable.getSelectedModelRow(); |
225 |
|
|
SearchResult searchResult = searchResultsList.get(index); |
226 |
|
|
Cursor backupCursor = scrollPane.getCursor(); |
227 |
|
|
scrollPane.setCursor(Cursor |
228 |
|
|
.getPredefinedCursor(Cursor.WAIT_CURSOR)); |
229 |
|
|
searchResult.open(); |
230 |
|
|
scrollPane.setCursor(backupCursor); |
231 |
|
|
} |
232 |
|
|
}); |
233 |
|
|
|
234 |
|
|
scrollPane.setViewportView(resultsTable); |
235 |
|
|
SwingUtil.setPreferredHeight(scrollPane, 140); |
236 |
|
|
SwingUtil.setMaximumHeight(scrollPane, 140); |
237 |
|
|
|
238 |
|
|
pack(); |
239 |
|
|
} |
240 |
|
|
|
241 |
alfonx |
244 |
} |