1 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 ; 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 45 public class TableView extends AbstractTableComponent implements Runnable , VirtualFileListener, MouseListener, ContextListener, KeyListener { 46 47 50 public static String ORDER_NAME = "ORDER_NAME"; 51 52 55 public static String ORDER_DATE = "ORDER_DATE"; 56 57 60 public static String ORDER_SERVER = "ORDER_SERVER"; 61 62 65 private String m_sOrderType = ORDER_NAME; 66 67 70 private String m_sPath = null; 71 72 75 private AbstractVirtualFileSystem m_vfs = null; 76 77 80 private String m_sSelectedPath = null; 81 82 85 private ArrayList m_order = new ArrayList(); 86 87 90 private HashMap m_entries = new HashMap(); 91 92 protected List m_colRowMap = new ArrayList(); 93 94 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 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 136 public void setPath(AbstractVirtualFileSystem vfs, String 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 163 public void setResources(AbstractVirtualFileSystem vfs, List aResources) { 164 this.m_vfs = vfs; 165 166 this.clearTable(); 167 168 Iterator itor = aResources.iterator(); 169 while(itor.hasNext()) { 170 String sChildPath = (String )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 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 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 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 230 protected void entryHeightChange(TableEntry entry) { 231 ((TableLayout)this.getLayout()).reflowColumn(this, entry); 232 this.getParent().validate(); 233 } 234 235 238 public void run() { 239 this.validate(); 240 this.repaint(); 241 } 242 243 public void setSelectedPath(String sSelectedPath) { 244 this.m_sSelectedPath = sSelectedPath; 245 this.grabFocus(); 246 } 247 248 251 public void virtualFileChanged(VirtualFileEvent vfe) { 252 String 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 () { 275 public void run() { 276 runSelection(); 277 } 278 }); 279 } 280 281 public void runSelection() { 282 String 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 )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 316 protected List getOrderedList() { 317 return (List ) this.m_order.clone(); 318 } 319 320 326 protected TableEntry getEntry(String sPath) { 327 return (TableEntry) this.m_entries.get(sPath); 328 } 329 330 335 public String getOrderType() { 336 return m_sOrderType; 337 } 338 339 344 public synchronized void setOrderType(String 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 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 element = (String ) 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 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 element = (String ) 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 children = vfDir.getChildren(); 390 391 for (int i = 0; i < children.size(); i++) { 392 String array_element = (String ) 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 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 419 public void mouseEntered(MouseEvent me) { 420 } 421 422 425 public void mouseExited(MouseEvent arg0) { 426 } 427 428 431 public void mousePressed(MouseEvent arg0) { 432 } 433 434 437 public void mouseReleased(MouseEvent arg0) { 438 } 439 440 446 public void openPathInTree(String sPath) { 447 if(this.m_displayComponent instanceof AbstractTreeTableDisplayComponent) { 448 ((AbstractTreeTableDisplayComponent)this.m_displayComponent).openPathInTree(sPath); 449 } 450 } 451 452 455 public void contextMessage(ContextEvent ce) { 456 457 } 458 459 462 public void keyPressed(KeyEvent arg0) { 463 } 465 466 469 public void keyReleased(KeyEvent ke) { 470 if(this.m_sSelectedPath!=null) { 471 if(ke.getKeyCode()==KeyEvent.VK_UP) { 472 List column = this.getColumn(this.m_sSelectedPath); 473 if(column!=null && column.indexOf(this.m_sSelectedPath)>0) { 474 String sNewPath = (String ) 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 column = this.getColumn(this.m_sSelectedPath); 481 if(column!=null && column.indexOf(this.m_sSelectedPath)<column.size()-1) { 482 String sNewPath = (String ) 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 column = this.getColumn(this.m_sSelectedPath); 489 if(column!=null && this.m_colRowMap.indexOf(column)>0) { 490 List newCol = (List ) this.m_colRowMap.get(this.m_colRowMap.indexOf(column)-1); 491 if(newCol.size()>=column.indexOf(this.m_sSelectedPath)+1) { 492 String sNewPath = (String ) 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 column = this.getColumn(this.m_sSelectedPath); 500 if(column!=null && this.m_colRowMap.indexOf(column)<this.m_colRowMap.size()-1) { 501 List newCol = (List ) this.m_colRowMap.get(this.m_colRowMap.indexOf(column)+1); 502 if(newCol.size()>=column.indexOf(this.m_sSelectedPath)+1) { 503 String sNewPath = (String ) 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 getColumn(String sPath) { 514 List retn = null; 515 516 Iterator itor = this.m_colRowMap.iterator(); 517 while (itor.hasNext()) { 518 List column = (List ) itor.next(); 519 if(column.contains(sPath)) { 520 retn = column; 521 } 522 } 523 524 return retn; 525 } 526 527 530 public void keyTyped(KeyEvent arg0) { 531 } 533 534 protected void setColRowMap(List colRowMap) { 535 this.m_colRowMap = colRowMap; 536 } 537 } 538 | Popular Tags |