KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > displaycomponents > SearchDisplayComponent


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19
20 package org.openharmonise.him.displaycomponents;
21
22 import java.util.*;
23
24 import javax.swing.*;
25
26 import org.openharmonise.him.*;
27 import org.openharmonise.him.context.StateHandler;
28 import org.openharmonise.him.displaycomponents.search.*;
29 import org.openharmonise.him.displaycomponents.table.*;
30 import org.openharmonise.him.window.*;
31 import org.openharmonise.him.window.messages.*;
32 import org.openharmonise.him.window.messages.builders.*;
33 import org.openharmonise.vfs.*;
34 import org.openharmonise.vfs.context.*;
35 import org.openharmonise.vfs.event.*;
36 import org.openharmonise.vfs.search.*;
37 import org.openharmonise.vfs.servers.ServerList;
38 import org.openharmonise.vfs.status.*;
39
40
41 /**
42  * Display component for managing the search tab.
43  *
44  * @author Matthew Large
45  * @version $Revision: 1.1 $
46  *
47  */

48 public class SearchDisplayComponent extends AbstractTreeTableDisplayComponent implements ContextListener, VirtualFileListener {
49
50     /**
51      * Search form panel.
52      */

53     private SearchTabPanel m_panel = null;
54     
55     /**
56      * Search table view.
57      */

58     private TableView m_tableView = null;
59
60     /**
61      * Virtual file system for the selected file.
62      */

63     private AbstractVirtualFileSystem m_SelectedFileVFS = null;
64     
65     /**
66      * Full path to the selected file.
67      */

68     private String JavaDoc m_sSelectedFilePath = null;
69     
70     /**
71      * List of full paths to virtual files that are the result of a search.
72      */

73     private List m_aResults = null;
74
75     /**
76      * Constructs a new search display component.
77      *
78      * @param displayManager Display manager
79      */

80     public SearchDisplayComponent(DisplayManager displayManager) {
81         super(displayManager);
82         this.setup();
83     }
84     
85     /**
86      * Configures this component.
87      *
88      */

89     private void setup() {
90         ContextHandler.getInstance().addListener(ContextType.CONTEXT_TABS, this);
91         m_panel = new SearchTabPanel(this);
92         this.m_tableView = new TableView(this);
93     }
94
95     /* (non-Javadoc)
96      * @see com.simulacramedia.contentmanager.displaycomponents.AbstractDisplayComponent#getIcon()
97      */

98     public String JavaDoc getIcon() {
99         return "16-command-search.gif";
100     }
101
102     /* (non-Javadoc)
103      * @see com.simulacramedia.contentmanager.displaycomponents.AbstractDisplayComponent#getTitle()
104      */

105     public String JavaDoc getTitle() {
106         return "Search";
107     }
108     
109     /**
110      * Returns the search form panel.
111      *
112      * @return Search form
113      */

114     public JPanel getSearchTab() {
115         return this.m_panel;
116     }
117
118     /* (non-Javadoc)
119      * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
120      */

121     public void virtualFileChanged(VirtualFileEvent vfe) {
122         if(vfe.getEventType()==VirtualFileEvent.FILE_DELETED) {
123             if(this.m_aResults!=null) {
124                 this.m_aResults.remove(vfe.getPath());
125             }
126             AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
127             Iterator itor = this.m_aResults.iterator();
128             while (itor.hasNext()) {
129                 String JavaDoc sPath = (String JavaDoc) itor.next();
130                 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
131                 if(vfFile!=null) {
132                     vfFile.addVirtualFileListener(this);
133                     if(vfFile.isVersionable() && ((VersionedVirtualFile)vfFile).hasPendingVersion()) {
134                         String JavaDoc sPendingPath = ((VersionedVirtualFile)vfFile).getPendingVersionPath();
135                         VirtualFile vfPending = vfFile.getVFS().getVirtualFile(sPendingPath).getResource();
136                         if(vfPending!=null) {
137                             vfPending.addVirtualFileListener(this);
138                         }
139                     }
140                 }
141             }
142             this.m_tableView.setResources(ServerList.getInstance().getHarmoniseServer().getVFS(), this.m_aResults);
143         } else if(vfe.getEventType()==VirtualFileEvent.FILE_SYNCHED || vfe.getEventType()==VirtualFileEvent.FILE_CHECKED_IN) {
144             AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
145             Iterator itor = this.m_aResults.iterator();
146             while (itor.hasNext()) {
147                 String JavaDoc sPath = (String JavaDoc) itor.next();
148                 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
149                 if(vfFile!=null) {
150                     vfFile.addVirtualFileListener(this);
151                     if(vfFile.isVersionable() && ((VersionedVirtualFile)vfFile).hasPendingVersion()) {
152                         String JavaDoc sPendingPath = ((VersionedVirtualFile)vfFile).getPendingVersionPath();
153                         VirtualFile vfPending = vfFile.getVFS().getVirtualFile(sPendingPath).getResource();
154                         if(vfPending!=null) {
155                             vfPending.addVirtualFileListener(this);
156                         }
157                     }
158                 }
159             }
160             this.m_tableView.setResources(ServerList.getInstance().getHarmoniseServer().getVFS(), this.m_aResults);
161         }
162     }
163     
164     /**
165      * Preforms the search.
166      *
167      * @param query Search query
168      */

169     public void doSearch(Query query) {
170         this.m_displayManager.hideMetadata();
171         this.clearSearchResults();
172         
173         AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
174         StatusData status = new VFSStatus();
175         try {
176             ResourceListStatusWrapper statusWrapper = vfs.search(query);
177             status = statusWrapper.getStatus();
178             if(status.isOK()) {
179                 this.m_aResults = statusWrapper.getResources();
180             }
181         } catch (Exception JavaDoc e) {
182             MessageHandler.getInstance().fireMessageEvent("Search failed, there was a problem contacting the server.", MessageHandler.TYPE_ERROR);
183             e.printStackTrace();
184         }
185         
186         if(status.isOK()) {
187             Iterator itor = this.m_aResults.iterator();
188             while (itor.hasNext()) {
189                 String JavaDoc sPath = (String JavaDoc) itor.next();
190                 try {
191                     VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource();
192                     if(vfFile!=null) {
193                         vfFile.addVirtualFileListener(this);
194                         if(vfFile.isVersionable() && ((VersionedVirtualFile)vfFile).hasPendingVersion()) {
195                             String JavaDoc sPendingPath = ((VersionedVirtualFile)vfFile).getPendingVersionPath();
196                             VirtualFile vfPending = vfFile.getVFS().getVirtualFile(sPendingPath).getResource();
197                             if(vfPending!=null) {
198                                 vfPending.addVirtualFileListener(this);
199                             }
200                         }
201                     }
202                 } catch (Exception JavaDoc e) {
203                     e.printStackTrace();
204                 }
205             }
206             this.m_tableView.setResources(ServerList.getInstance().getHarmoniseServer().getVFS(), this.m_aResults);
207         }
208         
209         VFSMessageBuilder.getInstance().fireMessage("ACTION_SEARCH", status);
210     }
211     
212     /**
213      * Clears the search results.
214      *
215      */

216     public void clearSearchResults() {
217         this.m_aResults = null;
218         this.m_SelectedFileVFS = null;
219         this.m_sSelectedFilePath = null;
220         this.m_tableView.clearTable();
221         this.m_displayManager.clearMetadataPanel();
222     }
223     
224     /**
225      * Returns the resource table view.
226      *
227      */

228     public TableView getTableView() {
229         return this.m_tableView;
230     }
231
232     /* (non-Javadoc)
233      * @see com.simulacramedia.contentmanager.displaycomponents.AbstractTreeTableDisplayComponent#directorySelected(com.simulacramedia.vfs.AbstractVirtualFileSystem, java.lang.String)
234      */

235     public void virtualFileSelected(AbstractVirtualFileSystem vfs, String JavaDoc sPath) {
236
237     }
238
239     /* (non-Javadoc)
240      * @see com.simulacramedia.contentmanager.displaycomponents.AbstractTreeTableDisplayComponent#fileSelection(com.simulacramedia.contentmanager.displaycomponents.table.TableEntry)
241      */

242     public void fileSelection(TableEntry entry) {
243         StateHandler.getInstance().addWait("CONTENT-FILE-SELECTION");
244         try {
245             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILES, null, entry.getVFS(), entry.getPath());
246             ContextHandler.getInstance().fireContextEvent(ce);
247             this.m_SelectedFileVFS = entry.getVFS();
248             this.m_sSelectedFilePath = entry.getPath();
249             this.m_displayManager.openMetadata();
250             this.m_displayManager.scrollTableTo( entry.getLocation().x );
251         } catch (Exception JavaDoc e) {
252             e.printStackTrace(System.err);
253         } finally {
254             StateHandler.getInstance().removeWait("CONTENT-FILE-SELECTION");
255         }
256     }
257
258     /* (non-Javadoc)
259      * @see com.simulacramedia.contentmanager.displaycomponents.AbstractTreeTableDisplayComponent#fileSelection(com.simulacramedia.contentmanager.displaycomponents.table.VersionEntry)
260      */

261     public void fileSelection(VersionEntry entry) {
262         StateHandler.getInstance().addWait("CONTENT-FILE-SELECTION");
263         try {
264             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILES, null, entry.getVFS(), entry.getPath());
265             ContextHandler.getInstance().fireContextEvent(ce);
266             this.m_SelectedFileVFS = entry.getVFS();
267             this.m_sSelectedFilePath = entry.getPath();
268             this.m_displayManager.openMetadata();
269             this.m_displayManager.scrollTableTo( entry.getParentTableEntry().getLocation().x );
270         } catch (Exception JavaDoc e) {
271             e.printStackTrace(System.err);
272         } finally {
273             StateHandler.getInstance().removeWait("CONTENT-FILE-SELECTION");
274         }
275     }
276
277     /* (non-Javadoc)
278      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
279      */

280     public void contextMessage(ContextEvent ce) {
281         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_TABS && ce.getMessage().equalsIgnoreCase(this.getTitle())) {
282             if(this.m_SelectedFileVFS!=null && this.m_sSelectedFilePath!=null) {
283                 ContextEvent ce2 = new ContextEvent(ContextType.CONTEXT_FILES, null, this.m_SelectedFileVFS, this.m_sSelectedFilePath);
284                 ContextHandler.getInstance().fireContextEvent(ce2);
285                 this.m_displayManager.openMetadata();
286             } else {
287                 this.m_displayManager.hideMetadata();
288             }
289         }
290     }
291
292 }
293
Popular Tags