KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > displaycomponents > table > TableView


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.table;
21
22 import java.awt.*;
23 import java.awt.dnd.*;
24 import java.awt.event.*;
25 import java.util.*;
26 import java.util.List JavaDoc;
27
28 import javax.swing.*;
29
30 import org.openharmonise.him.displaycomponents.*;
31 import org.openharmonise.him.dnd.*;
32 import org.openharmonise.him.window.menus.*;
33 import org.openharmonise.vfs.*;
34 import org.openharmonise.vfs.context.*;
35 import org.openharmonise.vfs.event.*;
36
37
38 /**
39  * Table view of resources.
40  *
41  * @author Matthew Large
42  * @version $Revision: 1.2 $
43  *
44  */

45 public class TableView extends AbstractTableComponent implements Runnable JavaDoc, VirtualFileListener, MouseListener, ContextListener, KeyListener {
46
47     /**
48      * Name order type identifier.
49      */

50     public static String JavaDoc ORDER_NAME = "ORDER_NAME";
51
52     /**
53      * Date order type identifier.
54      */

55     public static String JavaDoc ORDER_DATE = "ORDER_DATE";
56
57     /**
58      * Server order type identifier.
59      */

60     public static String JavaDoc ORDER_SERVER = "ORDER_SERVER";
61     
62     /**
63      * Order type.
64      */

65     private String JavaDoc m_sOrderType = ORDER_NAME;
66
67     /**
68      * Full path to collection to be displayed in table.
69      */

70     private String JavaDoc m_sPath = null;
71     
72     /**
73      * Virtual file system for collection to be displayed in table.
74      */

75     private AbstractVirtualFileSystem m_vfs = null;
76     
77     /**
78      * Full path of selected resource.
79      */

80     private String JavaDoc m_sSelectedPath = null;
81     
82     /**
83      * List of ordered full paths to resources in this view.
84      */

85     private ArrayList m_order = new ArrayList();
86     
87     /**
88      * Map of full path to {@link TableEntry} objects.
89      */

90     private HashMap m_entries = new HashMap();
91     
92     protected List JavaDoc m_colRowMap = new ArrayList();
93
94     /**
95      * Constructs a new table view.
96      *
97      * @param displayComponent Display component
98      */

99     public TableView(AbstractDisplayComponent displayComponent) {
100         super(displayComponent);
101         this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
102         TableLayout fl = new TableLayout(this);
103
104         
105
106         this.setLayout(fl);
107         this.addMouseListener(this);
108         this.addKeyListener(this);
109     }
110     
111     /**
112      * Clears the table view.
113      *
114      */

115     public void clearTable() {
116         Component[] comps = this.getComponents();
117         for(int i=0; i<comps.length; i++) {
118             if(comps[i] instanceof TableEntry) {
119                 TableEntry comp = (TableEntry) comps[i];
120                 comp.clearToDestroy();
121             }
122         }
123         this.m_entries.clear();
124         this.m_order.clear();
125         
126         this.removeAll();
127     }
128     
129     /**
130      * Sets the virtual file system and full path for the collection
131      * that this table is to display.
132      *
133      * @param vfs Virtual file system
134      * @param sPath Full path
135      */

136     public void setPath(AbstractVirtualFileSystem vfs, String JavaDoc sPath) {
137
138         if(this.m_vfs!=null && this.m_sPath!=null) {
139             VirtualFile vfFile = m_vfs.getVirtualFile( sPath ).getResource();
140             if(vfFile!=null) {
141                 vfFile.removeVirtualFileListener(this);
142             }
143         }
144         
145         this.m_sPath = sPath;
146
147         this.m_vfs = vfs;
148         VirtualFile vfFile = m_vfs.getVirtualFile( sPath ).getResource();
149
150         vfFile.addVirtualFileListener(this);
151         
152         this.setResources(vfs, vfFile.getChildren());
153     }
154     
155     /**
156      * Sets the virtual file system and full paths for the resources.
157      * Used for display components, such as Search, which do not show
158      * collections but lists of resources.
159      *
160      * @param vfs Virtual file system
161      * @param aResources List of full paths
162      */

163     public void setResources(AbstractVirtualFileSystem vfs, List JavaDoc aResources) {
164         this.m_vfs = vfs;
165
166         this.clearTable();
167         
168         Iterator itor = aResources.iterator();
169         while(itor.hasNext()) {
170             String JavaDoc sChildPath = (String JavaDoc)itor.next();
171             TableEntry entry = new TableEntry(this.m_vfs, sChildPath, this);
172             this.m_order.add(sChildPath);
173             this.m_entries.put(sChildPath, entry);
174             TableDragSource source = new TableDragSource(entry, entry, DnDConstants.ACTION_COPY_OR_MOVE);
175             this.add(entry);
176         }
177         
178         this.setOrderType(this.m_sOrderType);
179         
180         ((TableLayout)this.getLayout()).reflowContainer(this);
181         this.getParent().validate();
182     }
183     
184     /**
185      * Called when a table entry is selected.
186      *
187      * @param entry Table entry
188      */

189     protected void entrySelected(TableEntry entry) {
190         System.out.println(entry.getPath());
191         m_sSelectedPath = entry.getPath();
192         ((AbstractTreeTableDisplayComponent)this.m_displayComponent).fileSelection(entry);
193         this.deselectOtherEntries(entry);
194         this.requestFocus();
195     }
196     
197     /**
198      * Called when a version entry is selected.
199      *
200      * @param entry Version entry
201      */

202     protected void entrySelected(VersionEntry entry) {
203         m_sSelectedPath = entry.getPath();
204         ((AbstractTreeTableDisplayComponent)this.m_displayComponent).fileSelection(entry);
205         this.deselectOtherEntries(entry.getParentTableEntry());
206         this.requestFocus();
207     }
208     
209     /**
210      * Deselects all table entries except the given one.
211      *
212      * @param entry Table entry
213      */

214     private void deselectOtherEntries(TableEntry entry) {
215         for(int i=0; i<this.getComponentCount(); i++) {
216             if(this.getComponent(i) instanceof TableEntry) {
217                 TableEntry currentEntry = (TableEntry)this.getComponent(i);
218                 if(currentEntry!=entry) {
219                     currentEntry.deselectEntry();
220                 }
221             }
222         }
223     }
224     
225     /**
226      * Called when a table entry is expanded or collapsed.
227      *
228      * @param entry Table entry
229      */

230     protected void entryHeightChange(TableEntry entry) {
231         ((TableLayout)this.getLayout()).reflowColumn(this, entry);
232         this.getParent().validate();
233     }
234
235     /* (non-Javadoc)
236      * @see java.lang.Runnable#run()
237      */

238     public void run() {
239         this.validate();
240         this.repaint();
241     }
242     
243     public void setSelectedPath(String JavaDoc sSelectedPath) {
244         this.m_sSelectedPath = sSelectedPath;
245         this.grabFocus();
246     }
247
248     /* (non-Javadoc)
249      * @see com.simulacramedia.vfs.event.VirtualFileListener#virtualFileChanged(com.simulacramedia.vfs.event.VirtualFileEvent)
250      */

251     public void virtualFileChanged(VirtualFileEvent vfe) {
252         String JavaDoc sSelectedPath = this.m_sSelectedPath;
253         if(this.m_vfs!=null) {
254             VirtualFile vfFile = this.m_vfs.getVirtualFile(sSelectedPath).getResource();
255             if(vfFile !=null && vfFile.isVersionable()) {
256                 VersionedVirtualFile vfVersion = (VersionedVirtualFile) vfFile;
257                 if(vfVersion.hasPendingVersion()) {
258                     this.m_sSelectedPath = vfVersion.getPendingVersionPath();
259                 }
260             }
261         }
262         if(vfe.getEventType()==VirtualFileEvent.FILE_MEMBERS_CHANGED) {
263             if(vfe.getSubType()==VirtualFile.EVENT_ADDITION && vfe.getChildPath()!=null) {
264                 System.out.println("Setting child added path to " + vfe.getChildPath());
265                 this.m_sSelectedPath = vfe.getChildPath();
266             }
267             
268             this.setPath(this.m_vfs, this.m_sPath);
269             
270             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_CLEAR_METADATA);
271             ContextHandler.getInstance().fireContextEvent(ce);
272         }
273         SwingUtilities.invokeLater(
274                 new Runnable JavaDoc() {
275                     public void run() {
276                         runSelection();
277                     }
278                 });
279     }
280     
281     public void runSelection() {
282         String JavaDoc sSelectedPath = this.m_sSelectedPath;
283         boolean bFound = false;
284         if(sSelectedPath!=null && sSelectedPath.length()>0) {
285             for(int i=0;i<this.getComponentCount();i++) {
286                 Component comp = this.getComponent(i);
287                 if(comp instanceof TableEntry) {
288                     TableEntry entry = (TableEntry) comp;
289                     System.out.println("RunSelection Comparing: [" + sSelectedPath + "] with entry [" + entry.getPath() + "]");
290                     if(entry.getPath().equals(sSelectedPath)) {
291                         this.entrySelected(entry);
292                         entry.selectEntry();
293                         bFound = true;
294                     }
295                 }
296             }
297         }
298         if(!bFound && this.getOrderedList().size()>0) {
299             TableEntry entry = this.getEntry((String JavaDoc)this.getOrderedList().get(0));
300             this.m_sSelectedPath = entry.getPath();
301             this.entrySelected(entry);
302             entry.selectEntry();
303             ContextEvent ce = new ContextEvent(ContextType.CONTEXT_FILES, null, this.m_vfs, this.m_sSelectedPath);
304             ContextHandler.getInstance().fireContextEvent(ce);
305         }
306         
307         this.validate();
308     }
309     
310     /**
311      * Returns the ordered list of full paths to resources in this
312      * table view.
313      *
314      * @return List of full paths
315      */

316     protected List JavaDoc getOrderedList() {
317         return (List JavaDoc) this.m_order.clone();
318     }
319     
320     /**
321      * Returns the table entry for a given full path.
322      *
323      * @param sPath Full path
324      * @return Table entry
325      */

326     protected TableEntry getEntry(String JavaDoc sPath) {
327         return (TableEntry) this.m_entries.get(sPath);
328     }
329
330     /**
331      * Returns the order type.
332      *
333      * @return Order type
334      */

335     public String JavaDoc getOrderType() {
336         return m_sOrderType;
337     }
338
339     /**
340      * Sets the order type and re-orders the table view accordingly.
341      *
342      * @param string Order type
343      */

344     public synchronized void setOrderType(String JavaDoc string) {
345         m_sOrderType = string;
346         
347         this.m_order.clear();
348         
349         if(this.m_sOrderType.equals(TableView.ORDER_DATE)) {
350             TreeMap treeMap = new TreeMap();
351             Iterator itor = this.m_entries.values().iterator();
352             while (itor.hasNext()) {
353                 TableEntry element = (TableEntry) itor.next();
354                 String JavaDoc sDate = element.getDate();
355                 while(treeMap.keySet().contains(sDate)) {
356                     sDate = sDate+element.getName();
357                 }
358                 treeMap.put(sDate, element.getDragVirtualFilePath());
359             }
360         
361             itor = treeMap.values().iterator();
362             while (itor.hasNext()) {
363                 String JavaDoc element = (String JavaDoc) itor.next();
364                 this.m_order.add(element);
365             }
366         } else if(this.m_sOrderType.equals(TableView.ORDER_NAME)) {
367             TreeMap treeMap = new TreeMap();
368             Iterator itor = this.m_entries.values().iterator();
369             while (itor.hasNext()) {
370                 TableEntry element = (TableEntry) itor.next();
371                 String JavaDoc sName = element.getName();
372                 int nCount=1;
373                 while(treeMap.keySet().contains(sName)) {
374                     sName = sName+nCount;
375                     nCount++;
376                 }
377                 treeMap.put(sName, element.getDragVirtualFilePath());
378             }
379             
380             itor = treeMap.values().iterator();
381             while (itor.hasNext()) {
382                 String JavaDoc element = (String JavaDoc) itor.next();
383                 this.m_order.add(element);
384             }
385         } else if(this.m_sOrderType.equals(TableView.ORDER_SERVER)) {
386
387             VirtualFile vfDir = this.m_vfs.getVirtualFile(this.m_sPath).getResource();
388     
389             List JavaDoc children = vfDir.getChildren();
390         
391             for (int i = 0; i < children.size(); i++) {
392                 String JavaDoc array_element = (String JavaDoc) children.get(i);
393                 this.m_order.add(array_element);
394             }
395         }
396         
397         if(this.getParent()!=null) {
398             ((TableLayout)this.getLayout()).reflowContainer(this);
399             this.getParent().validate();
400         }
401     }
402
403     /* (non-Javadoc)
404      * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
405      */

406     public void mouseClicked(MouseEvent me) {
407         if(me.getButton()==MouseEvent.BUTTON3) {
408             CollectionContextMenu menu = new CollectionContextMenu(this.m_sPath, this.m_vfs);
409             Point pt = me.getPoint();
410             menu.show(this, pt.x, pt.y);
411         } else if(me.getButton()==MouseEvent.BUTTON1) {
412             this.requestFocus();
413         }
414     }
415
416     /* (non-Javadoc)
417      * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
418      */

419     public void mouseEntered(MouseEvent me) {
420     }
421
422     /* (non-Javadoc)
423      * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
424      */

425     public void mouseExited(MouseEvent arg0) {
426     }
427
428     /* (non-Javadoc)
429      * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
430      */

431     public void mousePressed(MouseEvent arg0) {
432     }
433
434     /* (non-Javadoc)
435      * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
436      */

437     public void mouseReleased(MouseEvent arg0) {
438     }
439     
440     /**
441      * Opens the specified path in the tree view, if the display component
442      * for this table has a tree.
443      *
444      * @param sPath Full path
445      */

446     public void openPathInTree(String JavaDoc sPath) {
447         if(this.m_displayComponent instanceof AbstractTreeTableDisplayComponent) {
448             ((AbstractTreeTableDisplayComponent)this.m_displayComponent).openPathInTree(sPath);
449         }
450     }
451
452     /* (non-Javadoc)
453      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
454      */

455     public void contextMessage(ContextEvent ce) {
456
457     }
458
459     /* (non-Javadoc)
460      * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
461      */

462     public void keyPressed(KeyEvent arg0) {
463         // NO-OP
464
}
465
466     /* (non-Javadoc)
467      * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
468      */

469     public void keyReleased(KeyEvent ke) {
470         if(this.m_sSelectedPath!=null) {
471             if(ke.getKeyCode()==KeyEvent.VK_UP) {
472                 List JavaDoc column = this.getColumn(this.m_sSelectedPath);
473                 if(column!=null && column.indexOf(this.m_sSelectedPath)>0) {
474                     String JavaDoc sNewPath = (String JavaDoc) column.get(column.indexOf(this.m_sSelectedPath)-1);
475                     TableEntry entry = this.getEntry(sNewPath);
476                     entry.selectEntry();
477                     this.entrySelected(entry);
478                 }
479             } else if(ke.getKeyCode()==KeyEvent.VK_DOWN) {
480                 List JavaDoc column = this.getColumn(this.m_sSelectedPath);
481                 if(column!=null && column.indexOf(this.m_sSelectedPath)<column.size()-1) {
482                     String JavaDoc sNewPath = (String JavaDoc) column.get(column.indexOf(this.m_sSelectedPath)+1);
483                     TableEntry entry = this.getEntry(sNewPath);
484                     entry.selectEntry();
485                     this.entrySelected(entry);
486                 }
487             } else if(ke.getKeyCode()==KeyEvent.VK_LEFT) {
488                 List JavaDoc column = this.getColumn(this.m_sSelectedPath);
489                 if(column!=null && this.m_colRowMap.indexOf(column)>0) {
490                     List JavaDoc newCol = (List JavaDoc) this.m_colRowMap.get(this.m_colRowMap.indexOf(column)-1);
491                     if(newCol.size()>=column.indexOf(this.m_sSelectedPath)+1) {
492                         String JavaDoc sNewPath = (String JavaDoc) newCol.get(column.indexOf(this.m_sSelectedPath));
493                         TableEntry entry = this.getEntry(sNewPath);
494                         entry.selectEntry();
495                         this.entrySelected(entry);
496                     }
497                 }
498             } else if(ke.getKeyCode()==KeyEvent.VK_RIGHT) {
499                 List JavaDoc column = this.getColumn(this.m_sSelectedPath);
500                 if(column!=null && this.m_colRowMap.indexOf(column)<this.m_colRowMap.size()-1) {
501                     List JavaDoc newCol = (List JavaDoc) this.m_colRowMap.get(this.m_colRowMap.indexOf(column)+1);
502                     if(newCol.size()>=column.indexOf(this.m_sSelectedPath)+1) {
503                         String JavaDoc sNewPath = (String JavaDoc) newCol.get(column.indexOf(this.m_sSelectedPath));
504                         TableEntry entry = this.getEntry(sNewPath);
505                         entry.selectEntry();
506                         this.entrySelected(entry);
507                     }
508                 }
509             }
510         }
511     }
512     
513     private List JavaDoc getColumn(String JavaDoc sPath) {
514         List JavaDoc retn = null;
515         
516         Iterator itor = this.m_colRowMap.iterator();
517         while (itor.hasNext()) {
518             List JavaDoc column = (List JavaDoc) itor.next();
519             if(column.contains(sPath)) {
520                 retn = column;
521             }
522         }
523         
524         return retn;
525     }
526
527     /* (non-Javadoc)
528      * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
529      */

530     public void keyTyped(KeyEvent arg0) {
531         // NO-OP
532
}
533
534     protected void setColRowMap(List JavaDoc colRowMap) {
535         this.m_colRowMap = colRowMap;
536     }
537 }
538
Popular Tags