d06e991212f335952ee095ac6891331e00e1d8c3
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / navigation / SearchView.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.navigation;
11
12 import java.util.List;
13 import java.util.Set;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.action.IToolBarManager;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.FocusEvent;
20 import org.eclipse.swt.events.FocusListener;
21 import org.eclipse.swt.events.KeyAdapter;
22 import org.eclipse.swt.events.KeyEvent;
23 import org.eclipse.swt.events.MouseAdapter;
24 import org.eclipse.swt.events.MouseEvent;
25 import org.eclipse.swt.layout.FillLayout;
26 import org.eclipse.swt.layout.GridData;
27 import org.eclipse.swt.layout.GridLayout;
28 import org.eclipse.swt.widgets.Button;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Display;
31 import org.eclipse.swt.widgets.Label;
32 import org.eclipse.swt.widgets.Text;
33 import org.eclipse.ui.part.ViewPart;
34
35 import com.swtdesigner.SWTResourceManager;
36
37 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38 import eu.etaxonomy.taxeditor.UiUtil;
39 import eu.etaxonomy.taxeditor.model.CdmUtil;
40
41 /**
42 * The left navigation pane.
43 *
44 * @author p.ciardelli
45 * @created 27.05.2008
46 * @version 1.0
47 */
48 public class SearchView extends ViewPart {
49 private static final Logger logger = Logger.getLogger(SearchView.class);
50
51 private Text searchText;
52 private Composite searchComposite = null;
53 public static final String ID = "eu.etaxonomy.taxeditor.navigation.searchview"; //$NON-NLS-1$
54
55 private Label noResultsLabel;
56
57 private Button searchButton;
58
59 /**
60 * Create contents of the view part
61 * @param parent
62 */
63 @Override
64 public void createPartControl(Composite parent) {
65
66 parent.setLayout(new FillLayout());
67
68 searchComposite = new Composite(parent, SWT.NONE);
69 searchComposite.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
70 final GridLayout gridLayout = new GridLayout();
71 gridLayout.numColumns = 2;
72 searchComposite.setLayout(gridLayout);
73
74 searchText = new Text(searchComposite, SWT.BORDER);
75 searchText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
76 searchText.setForeground(SWTResourceManager.getColor(192, 192, 192));
77 searchText.setText("Use \"*\" for wildcard searching");
78 final GridData gd_useForText = new GridData(SWT.FILL, SWT.CENTER, true, false);
79 searchText.setLayoutData(gd_useForText);
80 searchText.addFocusListener(new FocusListener() {
81
82 public void focusGained(FocusEvent e) {
83 searchText.setForeground(SWTResourceManager.getColor(0,0,0));
84 searchText.setText("");
85 }
86
87 public void focusLost(FocusEvent e) {
88 if (searchText.getText() == "") {
89 searchText.setForeground(SWTResourceManager.getColor(192, 192, 192));
90 searchText.setText("Use \"*\" for wildcard searching");
91 }
92 }
93 });
94 searchText.addKeyListener(new KeyAdapter() {
95 public void keyReleased(KeyEvent e) {
96 int key = e.keyCode;
97 if (key == SWT.CR) {
98 populateSearchResults();
99 } else {
100 setSearchButtonEnabled();
101 }
102 }
103 });
104
105 searchButton = new Button(searchComposite, SWT.NONE);
106 searchButton.setText("Search");
107 searchButton.setEnabled(false);
108 searchButton.addMouseListener(new MouseAdapter() {
109
110 // Populate search results resultsTable after clicking button
111 public void mouseUp(MouseEvent e) {
112 populateSearchResults();
113 }
114 });
115
116 noResultsLabel = new Label(searchComposite, SWT.NONE);
117 noResultsLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
118 noResultsLabel.setText("0 results found.");
119 noResultsLabel.setVisible(false);
120 noResultsLabel.setBackground(Display.getCurrent().getSystemColor(SWT.COLOR_WHITE));
121
122 // TEMPORARY PENDING IMPLEMENTATION - Disable visible fields
123 // searchButton.setEnabled(false);
124
125 createActions();
126 initializeToolBar();
127 initializeMenu();
128 }
129
130 protected void setSearchButtonEnabled() {
131 if (searchText.getText().length() == 0) {
132 searchButton.setEnabled(false);
133 } else {
134 searchButton.setEnabled(true);
135 }
136 }
137
138 protected void populateSearchResults() {
139 // UiUtil.openSearchResultsView(null);
140
141 noResultsLabel.setVisible(false);
142 if (searchText.getText().length() > 0) {
143
144 // Query CDM with search text
145 SearchResult searchResult = CdmUtil.searchTaxaByName(searchText.getText());
146 searchResult.setSearchString(searchText.getText());
147 // List<TaxonNameBase> results = CdmUtil.searchNameString(searchText.getText());
148
149 // If there are results, open a view in the center pane
150 if (searchResult.size() > 0) {
151 UiUtil.openSearchResultsView(searchResult);
152 } else {
153
154 // Display "no results found" message
155 logger.warn("no results found");
156 // noResultsLabel.setText("No results found for '" + searchText.getText() + "'");
157 noResultsLabel.setVisible(true);
158 }
159 }
160 }
161
162 private void createActions() {
163 // Create the actions
164 }
165
166 /**
167 * Initialize the toolbar
168 */
169 private void initializeToolBar() {
170 IToolBarManager toolbarManager = getViewSite().getActionBars()
171 .getToolBarManager();
172 }
173
174 /**
175 * Initialize the menu
176 */
177 private void initializeMenu() {
178 IMenuManager menuManager = getViewSite().getActionBars()
179 .getMenuManager();
180 }
181
182 @Override
183 public void setFocus() {
184 // Set the focus
185 }
186 }