001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019:
020: package org.openharmonise.him.displaycomponents;
021:
022: import java.util.*;
023:
024: import javax.swing.*;
025:
026: import org.openharmonise.him.*;
027: import org.openharmonise.him.context.StateHandler;
028: import org.openharmonise.him.displaycomponents.search.*;
029: import org.openharmonise.him.displaycomponents.table.*;
030: import org.openharmonise.him.window.*;
031: import org.openharmonise.him.window.messages.*;
032: import org.openharmonise.him.window.messages.builders.*;
033: import org.openharmonise.vfs.*;
034: import org.openharmonise.vfs.context.*;
035: import org.openharmonise.vfs.event.*;
036: import org.openharmonise.vfs.search.*;
037: import org.openharmonise.vfs.servers.ServerList;
038: import org.openharmonise.vfs.status.*;
039:
040: /**
041: * Display component for managing the search tab.
042: *
043: * @author Matthew Large
044: * @version $Revision: 1.1 $
045: *
046: */
047: public class SearchDisplayComponent extends
048: AbstractTreeTableDisplayComponent implements ContextListener,
049: VirtualFileListener {
050:
051: /**
052: * Search form panel.
053: */
054: private SearchTabPanel m_panel = null;
055:
056: /**
057: * Search table view.
058: */
059: private TableView m_tableView = null;
060:
061: /**
062: * Virtual file system for the selected file.
063: */
064: private AbstractVirtualFileSystem m_SelectedFileVFS = null;
065:
066: /**
067: * Full path to the selected file.
068: */
069: private String m_sSelectedFilePath = null;
070:
071: /**
072: * List of full paths to virtual files that are the result of a search.
073: */
074: private List m_aResults = null;
075:
076: /**
077: * Constructs a new search display component.
078: *
079: * @param displayManager Display manager
080: */
081: public SearchDisplayComponent(DisplayManager displayManager) {
082: super (displayManager);
083: this .setup();
084: }
085:
086: /**
087: * Configures this component.
088: *
089: */
090: private void setup() {
091: ContextHandler.getInstance().addListener(
092: ContextType.CONTEXT_TABS, this );
093: m_panel = new SearchTabPanel(this );
094: this .m_tableView = new TableView(this );
095: }
096:
097: /* (non-Javadoc)
098: * @see com.simulacramedia.contentmanager.displaycomponents.AbstractDisplayComponent#getIcon()
099: */
100: public String getIcon() {
101: return "16-command-search.gif";
102: }
103:
104: /* (non-Javadoc)
105: * @see com.simulacramedia.contentmanager.displaycomponents.AbstractDisplayComponent#getTitle()
106: */
107: public String getTitle() {
108: return "Search";
109: }
110:
111: /**
112: * Returns the search form panel.
113: *
114: * @return Search form
115: */
116: public JPanel getSearchTab() {
117: return this .m_panel;
118: }
119:
120: /* (non-Javadoc)
121: * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
122: */
123: public void virtualFileChanged(VirtualFileEvent vfe) {
124: if (vfe.getEventType() == VirtualFileEvent.FILE_DELETED) {
125: if (this .m_aResults != null) {
126: this .m_aResults.remove(vfe.getPath());
127: }
128: AbstractVirtualFileSystem vfs = ServerList.getInstance()
129: .getHarmoniseServer().getVFS();
130: Iterator itor = this .m_aResults.iterator();
131: while (itor.hasNext()) {
132: String sPath = (String) itor.next();
133: VirtualFile vfFile = vfs.getVirtualFile(sPath)
134: .getResource();
135: if (vfFile != null) {
136: vfFile.addVirtualFileListener(this );
137: if (vfFile.isVersionable()
138: && ((VersionedVirtualFile) vfFile)
139: .hasPendingVersion()) {
140: String sPendingPath = ((VersionedVirtualFile) vfFile)
141: .getPendingVersionPath();
142: VirtualFile vfPending = vfFile.getVFS()
143: .getVirtualFile(sPendingPath)
144: .getResource();
145: if (vfPending != null) {
146: vfPending.addVirtualFileListener(this );
147: }
148: }
149: }
150: }
151: this .m_tableView.setResources(ServerList.getInstance()
152: .getHarmoniseServer().getVFS(), this .m_aResults);
153: } else if (vfe.getEventType() == VirtualFileEvent.FILE_SYNCHED
154: || vfe.getEventType() == VirtualFileEvent.FILE_CHECKED_IN) {
155: AbstractVirtualFileSystem vfs = ServerList.getInstance()
156: .getHarmoniseServer().getVFS();
157: Iterator itor = this .m_aResults.iterator();
158: while (itor.hasNext()) {
159: String sPath = (String) itor.next();
160: VirtualFile vfFile = vfs.getVirtualFile(sPath)
161: .getResource();
162: if (vfFile != null) {
163: vfFile.addVirtualFileListener(this );
164: if (vfFile.isVersionable()
165: && ((VersionedVirtualFile) vfFile)
166: .hasPendingVersion()) {
167: String sPendingPath = ((VersionedVirtualFile) vfFile)
168: .getPendingVersionPath();
169: VirtualFile vfPending = vfFile.getVFS()
170: .getVirtualFile(sPendingPath)
171: .getResource();
172: if (vfPending != null) {
173: vfPending.addVirtualFileListener(this );
174: }
175: }
176: }
177: }
178: this .m_tableView.setResources(ServerList.getInstance()
179: .getHarmoniseServer().getVFS(), this .m_aResults);
180: }
181: }
182:
183: /**
184: * Preforms the search.
185: *
186: * @param query Search query
187: */
188: public void doSearch(Query query) {
189: this .m_displayManager.hideMetadata();
190: this .clearSearchResults();
191:
192: AbstractVirtualFileSystem vfs = ServerList.getInstance()
193: .getHarmoniseServer().getVFS();
194: StatusData status = new VFSStatus();
195: try {
196: ResourceListStatusWrapper statusWrapper = vfs.search(query);
197: status = statusWrapper.getStatus();
198: if (status.isOK()) {
199: this .m_aResults = statusWrapper.getResources();
200: }
201: } catch (Exception e) {
202: MessageHandler
203: .getInstance()
204: .fireMessageEvent(
205: "Search failed, there was a problem contacting the server.",
206: MessageHandler.TYPE_ERROR);
207: e.printStackTrace();
208: }
209:
210: if (status.isOK()) {
211: Iterator itor = this .m_aResults.iterator();
212: while (itor.hasNext()) {
213: String sPath = (String) itor.next();
214: try {
215: VirtualFile vfFile = vfs.getVirtualFile(sPath)
216: .getResource();
217: if (vfFile != null) {
218: vfFile.addVirtualFileListener(this );
219: if (vfFile.isVersionable()
220: && ((VersionedVirtualFile) vfFile)
221: .hasPendingVersion()) {
222: String sPendingPath = ((VersionedVirtualFile) vfFile)
223: .getPendingVersionPath();
224: VirtualFile vfPending = vfFile.getVFS()
225: .getVirtualFile(sPendingPath)
226: .getResource();
227: if (vfPending != null) {
228: vfPending.addVirtualFileListener(this );
229: }
230: }
231: }
232: } catch (Exception e) {
233: e.printStackTrace();
234: }
235: }
236: this .m_tableView.setResources(ServerList.getInstance()
237: .getHarmoniseServer().getVFS(), this .m_aResults);
238: }
239:
240: VFSMessageBuilder.getInstance().fireMessage("ACTION_SEARCH",
241: status);
242: }
243:
244: /**
245: * Clears the search results.
246: *
247: */
248: public void clearSearchResults() {
249: this .m_aResults = null;
250: this .m_SelectedFileVFS = null;
251: this .m_sSelectedFilePath = null;
252: this .m_tableView.clearTable();
253: this .m_displayManager.clearMetadataPanel();
254: }
255:
256: /**
257: * Returns the resource table view.
258: *
259: */
260: public TableView getTableView() {
261: return this .m_tableView;
262: }
263:
264: /* (non-Javadoc)
265: * @see com.simulacramedia.contentmanager.displaycomponents.AbstractTreeTableDisplayComponent#directorySelected(com.simulacramedia.vfs.AbstractVirtualFileSystem, java.lang.String)
266: */
267: public void virtualFileSelected(AbstractVirtualFileSystem vfs,
268: String sPath) {
269:
270: }
271:
272: /* (non-Javadoc)
273: * @see com.simulacramedia.contentmanager.displaycomponents.AbstractTreeTableDisplayComponent#fileSelection(com.simulacramedia.contentmanager.displaycomponents.table.TableEntry)
274: */
275: public void fileSelection(TableEntry entry) {
276: StateHandler.getInstance().addWait("CONTENT-FILE-SELECTION");
277: try {
278: ContextEvent ce = new ContextEvent(
279: ContextType.CONTEXT_FILES, null, entry.getVFS(),
280: entry.getPath());
281: ContextHandler.getInstance().fireContextEvent(ce);
282: this .m_SelectedFileVFS = entry.getVFS();
283: this .m_sSelectedFilePath = entry.getPath();
284: this .m_displayManager.openMetadata();
285: this .m_displayManager.scrollTableTo(entry.getLocation().x);
286: } catch (Exception e) {
287: e.printStackTrace(System.err);
288: } finally {
289: StateHandler.getInstance().removeWait(
290: "CONTENT-FILE-SELECTION");
291: }
292: }
293:
294: /* (non-Javadoc)
295: * @see com.simulacramedia.contentmanager.displaycomponents.AbstractTreeTableDisplayComponent#fileSelection(com.simulacramedia.contentmanager.displaycomponents.table.VersionEntry)
296: */
297: public void fileSelection(VersionEntry entry) {
298: StateHandler.getInstance().addWait("CONTENT-FILE-SELECTION");
299: try {
300: ContextEvent ce = new ContextEvent(
301: ContextType.CONTEXT_FILES, null, entry.getVFS(),
302: entry.getPath());
303: ContextHandler.getInstance().fireContextEvent(ce);
304: this .m_SelectedFileVFS = entry.getVFS();
305: this .m_sSelectedFilePath = entry.getPath();
306: this .m_displayManager.openMetadata();
307: this .m_displayManager.scrollTableTo(entry
308: .getParentTableEntry().getLocation().x);
309: } catch (Exception e) {
310: e.printStackTrace(System.err);
311: } finally {
312: StateHandler.getInstance().removeWait(
313: "CONTENT-FILE-SELECTION");
314: }
315: }
316:
317: /* (non-Javadoc)
318: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
319: */
320: public void contextMessage(ContextEvent ce) {
321: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_TABS
322: && ce.getMessage().equalsIgnoreCase(this .getTitle())) {
323: if (this .m_SelectedFileVFS != null
324: && this .m_sSelectedFilePath != null) {
325: ContextEvent ce2 = new ContextEvent(
326: ContextType.CONTEXT_FILES, null,
327: this.m_SelectedFileVFS,
328: this.m_sSelectedFilePath);
329: ContextHandler.getInstance().fireContextEvent(ce2);
330: this.m_displayManager.openMetadata();
331: } else {
332: this.m_displayManager.hideMetadata();
333: }
334: }
335: }
336:
337: }
|