View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   package com.hack23.cia.web.views.user;
6   
7   import thinwire.ui.Button;
8   import thinwire.ui.Component;
9   import thinwire.ui.Label;
10  import thinwire.ui.Panel;
11  import thinwire.ui.TextField;
12  import thinwire.ui.event.ActionEvent;
13  import thinwire.ui.event.ActionListener;
14  import thinwire.ui.event.KeyPressEvent;
15  import thinwire.ui.event.KeyPressListener;
16  import thinwire.ui.layout.TableLayout;
17  
18  import com.hack23.cia.model.application.dto.common.UserSessionDTO;
19  import com.hack23.cia.model.application.impl.common.Agency;
20  import com.hack23.cia.web.action.user.SearchAction;
21  import com.hack23.cia.web.common.BeanLocator;
22  import com.hack23.cia.web.common.ControllerActionListener;
23  
24  /***
25   * The Class SearchPanel.
26   */
27  public class SearchPanel extends Panel {
28  
29      /*** The application action listener. */
30      private final ControllerActionListener applicationActionListener;
31  
32      /*** The search text field. */
33      private final TextField searchTextField = new TextField();
34  
35      /***
36       * Instantiates a new search panel.
37       *
38       * @param userSessionDTO the user session dto
39       */
40      public SearchPanel(final UserSessionDTO userSessionDTO) {
41          super();
42          applicationActionListener = BeanLocator
43                  .getApplicationActionListener();
44  
45          setLayout(new TableLayout(new double[][] { { 0 }, // Column Widths
46                  { 0, 0, 0 } }, // Row Heights
47                  1, // Margin around edge of container
48                  5)); // Spacing between cells
49  
50          final Label searchDescription = new Label(userSessionDTO
51                  .getLanguageResource(Agency.LanguageContentKey.SEARCH_DESCRIPTION));
52          searchDescription.setWrapText(true);
53          searchDescription.setLimit("0,0,1,1"); //$NON-NLS-1$
54  
55          searchTextField.setLimit("0,1,1,1"); //$NON-NLS-1$
56  
57          final Button actionButton = new Button(userSessionDTO
58                  .getLanguageResource(Agency.LanguageContentKey.SEARCH));
59          actionButton.setLimit("0,2,1,1"); //$NON-NLS-1$
60          actionButton.addActionListener(Component.ACTION_CLICK,
61                  new ActionListener() {
62  
63                      @Override
64                      public void actionPerformed(final ActionEvent ev) {
65                          final Object source = ev.getSource();
66  
67                          final String searchString = searchTextField.getText();
68                          searchTextField.setText(""); //$NON-NLS-1$
69  
70                          ((Component) source).setUserObject(new SearchAction(
71                                  searchString));
72                          applicationActionListener.actionPerformed(ev);
73  
74                      }
75                  });
76          searchTextField.addKeyPressListener("Enter", new KeyPressListener() { //$NON-NLS-1$
77  
78                      @Override
79                      public void keyPress(final KeyPressEvent ev) {
80                          final Object source = ev.getSource();
81  
82                          final String searchString = searchTextField.getText();
83                          searchTextField.setText(""); //$NON-NLS-1$
84  
85                          ((Component) source).setUserObject(new SearchAction(
86                                  searchString));
87                          applicationActionListener.keyPress(ev);
88                      }
89                  });
90  
91          getChildren().add(searchDescription);
92          getChildren().add(actionButton);
93          getChildren().add(searchTextField);
94      }
95  
96  }