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