/[schmitzm]/trunk/src/skrueger/geotools/labelsearch/SearchMapDialog.java
ViewVC logotype

Annotation of /trunk/src/skrueger/geotools/labelsearch/SearchMapDialog.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 567 - (hide annotations)
Tue Nov 24 15:56:20 2009 UTC (15 years, 3 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/SearchMapDialog.java
File MIME type: text/plain
File size: 7368 byte(s)
* Switched ExceptionMonitor to schmitzm ExceptionDialog
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 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * 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.Point;
41     import java.awt.Window;
42     import java.awt.event.KeyAdapter;
43     import java.awt.event.KeyEvent;
44     import java.awt.event.MouseAdapter;
45     import java.awt.event.MouseEvent;
46     import java.util.List;
47     import java.util.concurrent.ExecutionException;
48    
49     import javax.swing.BorderFactory;
50     import javax.swing.JLabel;
51     import javax.swing.JPanel;
52     import javax.swing.JScrollPane;
53     import javax.swing.JTextField;
54     import javax.swing.ListSelectionModel;
55     import javax.swing.SwingWorker;
56    
57 alfonx 418 import net.miginfocom.swing.MigLayout;
58    
59 alfonx 139 import org.apache.log4j.Logger;
60    
61 alfonx 509 import schmitzm.geotools.gui.SelectableXMapPane;
62 alfonx 567 import schmitzm.swing.ExceptionDialog;
63 alfonx 139 import schmitzm.swing.SortableJTable;
64     import schmitzm.swing.SwingUtil;
65    
66     /**
67     * @author Stefan A. Krueger
68     */
69     public class SearchMapDialog extends javax.swing.JDialog {
70     final static private Logger LOGGER = Logger
71     .getLogger(SearchMapDialog.class);
72    
73     private final LabelSearch labelSearch;
74     /** This JPanel shows the results to the user if there is more than one **/
75     final private JTextField searchTextField = new JTextField(25);
76     final private JScrollPane scrollPane = new JScrollPane();
77    
78     public String getSearchText() {
79     return searchTextField.getText();
80     }
81    
82     public void setSearchText(String text) {
83     searchTextField.setText(text);
84     }
85    
86 alfonx 509 private final SelectableXMapPane mapPane;
87 alfonx 139
88     /**
89 alfonx 509 * The dialog will be relative to the {@link SelectableXMapPane}s parent
90 alfonx 139 * {@link Window}.
91     *
92     * @param parent
93     * @param labelSearch
94     * @param mapPane
95     * @param windowTitle
96     * A title to be used for the dialog.
97     */
98 alfonx 509 public SearchMapDialog(LabelSearch labelSearch, SelectableXMapPane mapPane,
99 alfonx 139 final String windowTitle) {
100     super(SwingUtil.getParentWindow(mapPane), windowTitle);
101     this.labelSearch = labelSearch;
102     this.mapPane = mapPane;
103     initialize();
104     }
105    
106     // Pressing ESC disposes the Dialog
107     KeyAdapter keyEscDispose = new KeyAdapter() {
108     public void keyPressed(KeyEvent e) {
109     int key = e.getKeyCode();
110     if (key == KeyEvent.VK_ESCAPE) {
111     dispose();
112     }
113     }
114     };
115    
116     // Pressing ENTER in the JTextfield automatically starts the search
117     KeyAdapter keyListenerStartSearch = new KeyAdapter() {
118     public void keyPressed(KeyEvent e) {
119     int key = e.getKeyCode();
120     if (key == KeyEvent.VK_ENTER) {
121     if (searchTextField.getText().trim().length() > 0)
122     search(searchTextField.getText());
123     }
124     }
125     };
126    
127     private void initialize() {
128    
129 alfonx 418 JPanel cp = new JPanel(new MigLayout("wrap 1","[grow]","[][][grow]"));
130 alfonx 139
131     JLabel label = new JLabel(LabelSearch.R("SearchMapDialog.searchString.Label"));
132     label.setLabelFor(searchTextField);
133     searchTextField.setToolTipText(LabelSearch.R("SearchMapDialog.searchString.tt"));
134     searchTextField.addKeyListener(keyListenerStartSearch);
135     // Pressing ESC disposes the Dialog
136     searchTextField.addKeyListener(keyEscDispose);
137 alfonx 418 cp.add(label, "split 2");
138     cp.add(searchTextField);
139    
140     cp.add(new JLabel(LabelSearch.R("SearchMapDialog.Explanation")));
141    
142     cp.add(scrollPane);
143 alfonx 139
144     cp.add(scrollPane, BorderLayout.SOUTH);
145    
146     cp.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
147    
148     setContentPane(cp);
149     pack();
150    
151     Point mapPanelocationOnScreen = mapPane.getLocationOnScreen();
152     mapPanelocationOnScreen.x -= mapPane.getLocation().x;
153     mapPanelocationOnScreen.y -= mapPane.getLocation().y;
154     setLocation(mapPanelocationOnScreen);
155    
156     // SwingUtil.centerFrameOnScreenRandom(this);
157     }
158    
159     /**
160 alfonx 509 * Performes a search on the {@link SelectableXMapPane}'s labels and outputs the
161 alfonx 139 * possible numerouse results.
162     *
163     * @param text
164     * Text to search
165     */
166     public void search(final String text) {
167    
168     final Cursor backupCursor = getContentPane().getCursor();
169     getContentPane().setCursor(
170     Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
171     searchTextField.setEnabled(false);
172    
173     SwingWorker<List<SearchResult>, String> swingWorker = new SwingWorker<List<SearchResult>, String>() {
174     @Override
175     protected List<SearchResult> doInBackground() throws Exception {
176     return labelSearch.search(text);
177     }
178    
179     @Override
180     protected void done() {
181     getContentPane().setCursor(backupCursor);
182     searchTextField.setEnabled(true);
183     super.done();
184     try {
185     showMultipleResults(get());
186     } catch (InterruptedException e) {
187     LOGGER.error(e);
188 alfonx 567 ExceptionDialog.show(SearchMapDialog.this, e);
189 alfonx 139 } catch (ExecutionException e) {
190     LOGGER.error(e);
191 alfonx 567 ExceptionDialog.show(SearchMapDialog.this, e);
192 alfonx 139 }
193     }
194     };
195     swingWorker.execute();
196    
197     }
198    
199     private void showMultipleResults(final List<SearchResult> searchResultsList) {
200    
201     final SortableJTable resultsTable = new SortableJTable();
202     resultsTable.setModel(new SearchResultTableModel(searchResultsList));
203    
204     resultsTable.addKeyListener(keyEscDispose);
205    
206     // set up the table
207     resultsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
208    
209     // resultsTable.setRowSorter( new TableRowSorter<SearchResultTableModel>((SearchResultTableModel)resultsTable.getModel()));
210     //
211     resultsTable.setToolTipText(LabelSearch.R("SearchMapDialog.resultsTable.tt"));
212    
213     // // The first column should be small
214     // resultsTable.getColumnModel().getColumn(0).setMinWidth(20);
215     // resultsTable.getColumnModel().getColumn(0).setMaxWidth(25);
216    
217     resultsTable.addMouseListener(new MouseAdapter() {
218    
219     @Override
220     public void mouseClicked(MouseEvent e) {
221     int index = resultsTable.getSelectedModelRow();
222     SearchResult searchResult = searchResultsList.get(index);
223     Cursor backupCursor = scrollPane.getCursor();
224     scrollPane.setCursor(Cursor
225     .getPredefinedCursor(Cursor.WAIT_CURSOR));
226     searchResult.open();
227     scrollPane.setCursor(backupCursor);
228     }
229     });
230    
231     scrollPane.setViewportView(resultsTable);
232     SwingUtil.setPreferredHeight(scrollPane, 140);
233     SwingUtil.setMaximumHeight(scrollPane, 140);
234    
235     pack();
236     }
237    
238 alfonx 244 }

Properties

Name Value
svn:eol-style native
svn:keywords Id
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26