aba36dd275349c45843199d901e3ac2b69cb3b08
[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.Menu;
27 import org.eclipse.swt.widgets.Text;
28 import org.eclipse.ui.IViewPart;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
32
33 import eu.etaxonomy.taxeditor.model.ImageResources;
34 import eu.etaxonomy.taxeditor.model.Resources;
35 import eu.etaxonomy.taxeditor.navigation.internal.TaxeditorNavigationPlugin;
36
37 /**
38 * @author n.hoffmann
39 * @created 15.04.2009
40 * @version 1.0
41 */
42 public class SearchBar extends WorkbenchWindowControlContribution{
43 private static final Logger logger = Logger.getLogger(SearchBar.class);
44 private Text text_search;
45 private Button button_search;
46 private String srv;
47
48 private String defaultText = "Use \"*\" for wildcard searching";
49 private Button button_configure;
50
51 @Override
52 protected Control createControl(Composite parent) {
53 //parent.setLayout(new FillLayout());
54
55 Composite composite = new Composite(parent, SWT.NONE);
56 final GridLayout gridLayout = new GridLayout();
57 gridLayout.numColumns = 3;
58 gridLayout.marginWidth = 0;
59 gridLayout.marginHeight = 0;
60 gridLayout.verticalSpacing = 0;
61 gridLayout.horizontalSpacing = 0;
62 composite.setLayout(gridLayout);
63 // FIXME we have this here for debugging purposes, remove color once the layout of the search bar is correct
64 composite.setBackground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
65
66 text_search = new Text(composite, SWT.SEARCH);
67 GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
68
69 text_search.setLayoutData(gridData);
70 text_search.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
71 text_search.setText(defaultText);
72
73 text_search.addFocusListener(new FocusListener() {
74
75 public void focusGained(FocusEvent e) {
76 text_search.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOCUS));
77 text_search.setText("");
78 }
79
80 public void focusLost(FocusEvent e) {
81 if (text_search.getText() == "") {
82 text_search.setForeground(Resources.getColor(Resources.SEARCH_VIEW_FOREGROUND));
83 text_search.setText(defaultText);
84 }
85 }
86 });
87 text_search.addKeyListener(new KeyAdapter() {
88 public void keyReleased(KeyEvent e) {
89 int key = e.keyCode;
90 if (key == SWT.CR) {
91 openSearchResultsView(text_search);
92 } else {
93 setSearchButtonEnabled();
94 }
95 }
96 });
97
98 button_configure = new Button(composite, SWT.DROP_DOWN);
99 button_configure.setImage(ImageResources.getImage(ImageResources.MOVE_ICON));
100
101 button_search = new Button(composite, SWT.PUSH);
102 button_search.setText("Search");
103 button_search.setEnabled(false);
104 button_search.addMouseListener(new MouseAdapter() {
105
106 // Populate search results resultsTable after clicking button
107 public void mouseUp(MouseEvent e) {
108 openSearchResultsView(text_search);
109 }
110 });
111 return composite;
112 }
113
114 protected void setSearchButtonEnabled() {
115 if (text_search.getText().length() == 0) {
116 button_search.setEnabled(false);
117 } else {
118 button_search.setEnabled(true);
119 }
120 }
121
122 private void openSearchResultsView(Text searchText) {
123 if(searchText.getText().length() > 0){
124 srv += "1";
125 logger.info("Opening search results window " + srv);
126 try {
127 IViewPart resultsView = TaxeditorNavigationPlugin.getDefault().getWorkbench()
128 .getActiveWorkbenchWindow().getActivePage().showView(SearchResultView.ID,
129 srv, IWorkbenchPage.VIEW_ACTIVATE);
130 ((SearchResultView) resultsView).performSearch(searchText.getText());
131 } catch (PartInitException e) {
132 logger.error("Error opening search result.", e);
133 }
134 }
135 }
136 }