95a1c386f23cda5f6b0bc64884559ca5cf754ba7
[taxeditor.git] / taxeditor-navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / search / SearchBar.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.navigation.search;
12
13 import org.apache.log4j.Logger;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.FocusEvent;
16 import org.eclipse.swt.events.FocusListener;
17 import org.eclipse.swt.events.KeyAdapter;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.MouseAdapter;
20 import org.eclipse.swt.events.MouseEvent;
21 import org.eclipse.swt.layout.GridData;
22 import org.eclipse.swt.layout.GridLayout;
23 import org.eclipse.swt.widgets.Button;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Text;
27 import org.eclipse.ui.IViewPart;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.PartInitException;
30 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
31
32 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
33 import eu.etaxonomy.taxeditor.store.model.Resources;
34
35 /**
36 * @author n.hoffmann
37 * @created 15.04.2009
38 * @version 1.0
39 */
40 public class SearchBar extends WorkbenchWindowControlContribution{
41 private static final Logger logger = Logger.getLogger(SearchBar.class);
42 private Composite searchComposite;
43 private Text searchText;
44 private Button searchButton;
45 private String srv;
46
47 private String defaultText = "Use \"*\" for wildcard searching";
48
49 @Override
50 protected Control createControl(Composite parent) {
51 //parent.setLayout(new FillLayout());
52
53 searchComposite = new Composite(parent, SWT.NONE);
54 final GridLayout gridLayout = new GridLayout();
55 gridLayout.numColumns = 2;
56 gridLayout.marginWidth = 0;
57 gridLayout.marginHeight = 0;
58 gridLayout.verticalSpacing = 0;
59 gridLayout.horizontalSpacing = 0;
60 searchComposite.setLayout(gridLayout);
61 // FIXME we have this here for debugging purposes, remove color once the layout of the search bar is correct
62 searchComposite.setBackground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
63
64 searchText = new Text(searchComposite, SWT.SEARCH);
65 GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
66
67 searchText.setLayoutData(gridData);
68 searchText.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
69 searchText.setText(defaultText);
70
71 searchText.addFocusListener(new FocusListener() {
72
73 public void focusGained(FocusEvent e) {
74 searchText.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOCUS));
75 searchText.setText("");
76 }
77
78 public void focusLost(FocusEvent e) {
79 if (searchText.getText() == "") {
80 searchText.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
81 searchText.setText(defaultText);
82 }
83 }
84 });
85 searchText.addKeyListener(new KeyAdapter() {
86 public void keyReleased(KeyEvent e) {
87 int key = e.keyCode;
88 if (key == SWT.CR) {
89 openSearchResultsView(searchText);
90 } else {
91 setSearchButtonEnabled();
92 }
93 }
94 });
95
96 searchButton = new Button(searchComposite, SWT.NONE);
97 searchButton.setText("Search");
98 searchButton.setEnabled(false);
99 searchButton.addMouseListener(new MouseAdapter() {
100
101 // Populate search results resultsTable after clicking button
102 public void mouseUp(MouseEvent e) {
103 openSearchResultsView(searchText);
104 }
105 });
106 return searchComposite;
107 }
108
109 protected void setSearchButtonEnabled() {
110 if (searchText.getText().length() == 0) {
111 searchButton.setEnabled(false);
112 } else {
113 searchButton.setEnabled(true);
114 }
115 }
116
117 private void openSearchResultsView(Text searchText) {
118 if(searchText.getText().length() > 0){
119 srv += "1";
120 logger.info("Opening search results window " + srv);
121 try {
122 IViewPart resultsView = TaxeditorNavigationPlugin.getDefault().getWorkbench()
123 .getActiveWorkbenchWindow().getActivePage().showView(SearchResultView.ID,
124 srv, IWorkbenchPage.VIEW_ACTIVATE);
125 ((SearchResultView) resultsView).performSearch(searchText.getText());
126 } catch (PartInitException e) {
127 logger.error("Error opening search result.", e);
128 }
129 }
130 }
131 }