1 19 package org.openharmonise.him.editors.report.swing; 20 21 import java.awt.*; 22 import java.awt.event.*; 23 import java.util.*; 24 import java.util.List ; 25 26 import javax.swing.*; 27 import javax.swing.event.*; 28 import javax.swing.tree.*; 29 30 import org.openharmonise.him.*; 31 import org.openharmonise.him.swing.resourcetree.*; 32 import org.openharmonise.him.swing.resourcetree.formresourcetree.*; 33 import org.openharmonise.swing.FontManager; 34 import org.openharmonise.vfs.*; 35 import org.openharmonise.vfs.gui.*; 36 import org.openharmonise.vfs.servers.ServerList; 37 38 39 48 public class JComboTree 49 extends JPanel 50 implements ActionListener, LayoutManager, 51 FormResourceTreeListener, MouseListener, 52 WindowFocusListener, FocusListener { 53 54 59 private int m_nSelectionMode = -1; 60 61 64 protected JLabel m_value = null; 65 66 69 private JToggleButton m_button = null; 70 71 74 private int m_nScreenPosX = 0; 75 76 79 private int m_nScreenPosY = 0; 80 81 84 protected JWindow m_nWindow = null; 85 86 89 private ArrayList m_aCollectionPaths = new ArrayList(); 90 91 94 private String m_sActionCommand = null; 95 96 99 private ArrayList m_aActionListeners = new ArrayList(); 100 101 104 private boolean m_bShowLeafNodes = true; 105 106 109 private boolean m_bSelectedLeafOnly = false; 110 111 private boolean m_bShowApprovedOnly = false; 112 113 116 private Color m_selectedColor = new Color(173,169,143); 117 118 121 private List m_paths = null; 122 123 126 private boolean m_bAllowClear = false; 127 128 131 private boolean m_bIsTreePopulated = false; 132 133 136 private FormResourceTree m_tree; 137 138 private AbstractResourceFilter m_filter = new BasicResourceFilter(); 139 140 private int m_nWidth = 150; 141 private int m_nHeight = 20; 142 143 private boolean m_bHideReentrantStop = false; 144 145 150 public JComboTree(String sPath) { 151 super(); 152 m_paths = new ArrayList(); 153 154 if(sPath != null) { 155 m_paths.add(sPath); 156 } 157 this.setup(); 158 } 159 160 163 public JComboTree() { 164 super(); 165 this.setup(); 166 } 167 168 172 protected void setup() { 173 this.setLayout(this); 174 this.setBackground(Color.WHITE); 175 176 this.m_value = new JLabel(); 177 this.m_value.setBackground(Color.WHITE); 178 this.m_value.setBorder( BorderFactory.createEtchedBorder()); 179 this.m_value.setFont( FontManager.getInstance().getFont(FontManager.FONT_RESOURCE_TITLE) ); 180 this.add(m_value); 181 182 this.m_button = new JToggleButton("V"); 183 this.m_button.addActionListener(this); 184 this.add(this.m_button); 185 } 186 187 192 public JLabel getLabel() { 193 return m_value; 194 } 195 196 202 public String getPath() { 203 String sPath = null; 204 205 if(m_paths != null && m_paths.size() > 0) { 206 sPath = (String ) m_paths.get(0); 207 } 208 209 return sPath; 210 } 211 212 217 public List getPaths() { 218 return new ArrayList(m_paths); 219 } 220 221 222 225 public void actionPerformed(ActionEvent arg0) { 226 if(this.m_nWindow==null) { 227 Component comp = this.getParent(); 228 while(comp!=null && !(comp instanceof JDialog || comp instanceof JFrame)) { 229 comp = comp.getParent(); 230 } 231 this.m_nWindow = new JWindow(((Window)comp)); 232 m_nWindow.addWindowFocusListener(this); 233 234 JPanel panel = new JPanel(); 235 panel.addFocusListener(this); 236 panel.setBorder( BorderFactory.createLineBorder(Color.BLACK)); 237 panel.setBackground(Color.WHITE); 238 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); 239 240 241 String fontName = "Dialog"; 242 int fontSize = 11; 243 Font font = new Font(fontName, Font.PLAIN, fontSize); 244 245 if(this.m_bAllowClear) { 246 JTextField clearLabel = new JTextField("Clear value"); 247 clearLabel.setEditable(false); 248 clearLabel.setBorder( BorderFactory.createLineBorder(Color.BLACK)); 249 clearLabel.setFont(font); 250 clearLabel.addMouseListener(this); 251 panel.add(clearLabel); 252 clearLabel.setPreferredSize(new Dimension(220,15)); 253 clearLabel.setSize(new Dimension(220,15)); 254 } 255 256 List aPaths = new ArrayList(); 257 if(m_paths != null && m_paths.size() > 0) { 258 aPaths = new ArrayList(m_paths); 259 } 260 m_tree = new FormResourceTree(aPaths); 261 m_tree.setResourceFilter(getResourceTreeFilter()); 262 m_tree.addFocusListener(this); 263 if(m_nSelectionMode >= 0) { 264 m_tree.setSelectionMode(m_nSelectionMode); 265 if(m_nSelectionMode>1) { 266 m_tree.setIsMultiSelect(true); 267 } else { 268 m_tree.setIsMultiSelect(false); 269 } 270 } else { 271 m_tree.setIsMultiSelect(false); 272 } 273 m_tree.setBorder( BorderFactory.createLineBorder(Color.BLACK)); 274 m_tree.setShowLeafNodes(m_bShowLeafNodes); 275 m_tree.addFormResourceTreeListener(this); 276 if(m_bShowApprovedOnly){ 277 m_tree.setShowApprovedOnly(true); 278 } 279 AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS(); 280 Iterator itor = this.m_aCollectionPaths.iterator(); 281 while (itor.hasNext()) { 282 String sColPath = (String ) itor.next(); 283 VirtualFile vfDir = vfs.getVirtualFile(sColPath).getResource(); 284 if(vfDir!=null) { 285 m_tree.addCollection(vfDir); 286 } 287 } 288 289 this.m_nWindow.getContentPane().setLayout(new BorderLayout()); 290 panel.add(m_tree); 291 this.m_nWindow.getContentPane().add(panel); 292 m_bIsTreePopulated = true; 293 294 addAncestorListener(new AncestorListener(){ 295 public void ancestorAdded(AncestorEvent event){ 296 if (event.getSource()!=this && event.getSource()!=getButton()) 297 hidePopup(); 298 } 299 public void ancestorRemoved(AncestorEvent event){ 300 if (event.getSource()!=this && event.getSource()!=getButton()) 301 hidePopup(); 302 } 303 public void ancestorMoved(AncestorEvent event){ 304 if (event.getSource()!=this && event.getSource()!=getButton()) 305 hidePopup(); 306 }}); 307 308 } 309 if(!this.m_bHideReentrantStop && this.m_button.isSelected() && !this.m_nWindow.isVisible()) { 310 this.m_nWindow.setSize(200, 300); 311 this.m_nScreenPosX = this.getLocationOnScreen().x; 312 this.m_nScreenPosY = this.getLocationOnScreen().y; 313 this.m_nWindow.setLocation(this.m_nScreenPosX, this.m_nScreenPosY+20); 314 this.m_nWindow.toFront(); 315 this.m_nWindow.setVisible(true); 316 317 } else { 318 this.m_nWindow.setVisible(false); 319 this.m_button.setSelected(false); 320 this.revalidate(); 321 this.repaint(); 322 } 323 this.m_bHideReentrantStop = false; 324 } 325 326 protected JToggleButton getButton() { 327 return this.m_button; 328 } 329 330 333 protected void hidePopup() { 334 if(this.m_nWindow!=null) { 335 this.m_bHideReentrantStop = true; 336 this.m_nWindow.setVisible(false); 337 this.m_button.setSelected(false); 338 this.invalidate(); 339 } 340 } 341 342 public void setShowApprovedOnly(boolean show){ 343 m_bShowApprovedOnly = show; 344 } 345 346 351 public void setShowLeafNodes(boolean bShowLeafNodes) { 352 this.m_bShowLeafNodes = bShowLeafNodes; 353 } 354 355 360 public void setSelectedLeafOnly(boolean bSelectLeafOnly) { 361 this.m_bSelectedLeafOnly = bSelectLeafOnly; 362 } 363 364 370 public void setAllowClear(boolean bAllowClear) { 371 } 372 373 376 public void paint(Graphics arg0) { 377 super.paint(arg0); 378 if(this.m_nWindow!=null && this.m_nWindow.isVisible() && (this.m_nScreenPosX!=this.getLocationOnScreen().x || this.m_nScreenPosY!=this.getLocationOnScreen().y) ) { 379 this.m_nScreenPosX = this.getLocationOnScreen().x; 380 this.m_nScreenPosY = this.getLocationOnScreen().y; 381 this.m_nWindow.setLocation(this.m_nScreenPosX, this.m_nScreenPosY+20); 382 this.m_nWindow.toFront(); 383 this.m_nWindow.repaint(); 384 } 385 } 386 387 390 public void doLayout() { 391 super.doLayout(); 392 if(this.m_nWindow!=null && this.m_nWindow.isVisible() && (this.m_nScreenPosX!=this.getLocationOnScreen().x || this.m_nScreenPosY!=this.getLocationOnScreen().y) ) { 393 this.m_nScreenPosX = this.getLocationOnScreen().x; 394 this.m_nScreenPosY = this.getLocationOnScreen().y; 395 this.m_nWindow.setLocation(this.m_nScreenPosX, this.m_nScreenPosY+20); 396 this.m_nWindow.toFront(); 397 this.m_nWindow.repaint(); 398 } 399 } 400 401 404 public void layoutContainer(Container arg0) { 405 this.m_value.setSize(m_nWidth-20, m_nHeight); 406 this.m_value.setLocation(0, 0); 407 408 this.m_button.setSize(20, m_nHeight); 409 this.m_button.setLocation(m_nWidth-20, 0); 410 } 411 412 415 public void setSize(Dimension dim) { 416 this.setSize(dim.width, dim.height); 417 } 418 419 422 public void setSize(int nWidth, int nHeight) { 423 super.setSize(nWidth, nHeight); 424 this.m_nWidth = nWidth; 425 this.m_nHeight = nHeight; 426 } 427 428 429 430 433 public Dimension getSize() { 434 return new Dimension(this.m_nWidth, this.m_nHeight); 435 } 436 441 public void addCollectionPaths(List paths) { 442 this.m_aCollectionPaths.addAll(paths); 443 } 444 445 450 public void addCollectionPath(String sPath) { 451 this.m_aCollectionPaths.add(sPath); 452 } 453 454 459 public void removeCollectionPath(String sPath) { 460 this.m_aCollectionPaths.remove(sPath); 461 } 462 463 468 public List getCollectionPaths() { 469 return (List ) this.m_aCollectionPaths.clone(); 470 } 471 472 475 public Dimension getPreferredSize() { 476 return new Dimension(170, 20); 477 } 478 479 482 private JComboTree(boolean arg0) { 483 super(arg0); 484 } 485 486 489 private JComboTree(LayoutManager arg0) { 490 super(arg0); 491 } 492 493 497 private JComboTree(LayoutManager arg0, boolean arg1) { 498 super(arg0, arg1); 499 } 500 501 504 public void removeLayoutComponent(Component arg0) { 505 } 506 507 510 public void addLayoutComponent(String arg0, Component arg1) { 511 } 512 513 516 public Dimension minimumLayoutSize(Container arg0) { 517 return this.getPreferredSize(); 518 } 519 520 523 public Dimension preferredLayoutSize(Container arg0) { 524 return this.getPreferredSize(); 525 } 526 527 532 public void addActionListener(ActionListener listener) { 533 this.m_aActionListeners.add(listener); 534 } 535 536 541 public void removeActionListener(ActionListener listener) { 542 this.m_aActionListeners.remove(listener); 543 } 544 545 549 private void fireActionEvent() { 550 ActionEvent ae = new ActionEvent(this, 0, this.m_sActionCommand); 551 Iterator itor = this.m_aActionListeners.iterator(); 552 while (itor.hasNext()) { 553 ActionListener listener = (ActionListener) itor.next(); 554 listener.actionPerformed(ae); 555 } 556 } 557 558 563 public void setActionCommand(String sActionCommand) { 564 this.m_sActionCommand = sActionCommand; 565 } 566 567 571 public void clear() { 572 if(this.m_paths!=null) { 573 this.m_paths.clear(); 574 } 575 this.m_value.setIcon(IconManager.getInstance().getIcon("16-blank.gif")); 576 this.m_value.setText(""); 577 this.revalidate(); 578 this.m_nWindow.setVisible(false); 579 this.m_tree.clearSelection(); 580 } 581 582 585 public void mouseClicked(MouseEvent me) { 586 clear(); 587 } 588 589 595 public void addPath(AbstractVirtualFileSystem vfs, String sPath) { 596 if(m_paths == null) { 597 m_paths = new ArrayList(); 598 } 599 600 m_paths.add(sPath); 601 602 setPaths(vfs,m_paths); 603 } 604 605 611 public void removePath(AbstractVirtualFileSystem vfs, String sPath) { 612 if(m_paths != null) { 613 if(m_paths.contains(sPath)) { 614 m_paths.remove(sPath); 615 setPaths(vfs, m_paths); 616 } 617 } 618 } 619 620 626 public void setPath(AbstractVirtualFileSystem vfs, String sPath) { 627 ArrayList list = new ArrayList(); 628 list.add(sPath); 629 630 setPaths(vfs,list); 631 } 632 633 639 public void setPaths(AbstractVirtualFileSystem vfs, List paths) { 640 VirtualFileSystemView vfsView = vfs.getVirtualFileSystemView(); 641 Iterator iter = paths.iterator(); 642 StringBuffer sbuf = new StringBuffer (); 643 Icon icon = null; 644 m_paths = new ArrayList(); 645 while (iter.hasNext()) { 646 String sPath = (String ) iter.next(); 647 648 VirtualFile vfFile = vfs.getVirtualFile(sPath).getResource(); 649 if(m_value != null 650 && (!this.m_bSelectedLeafOnly 651 || (this.m_bSelectedLeafOnly 652 && !vfFile.isDirectory()) )) { 653 m_paths.add(sPath); 654 655 icon = vfsView.getIcon(vfFile); 656 if(sbuf.length() > 0) { 657 sbuf.append(";"); 658 } 659 sbuf.append(vfsView.getDisplayName(vfFile)); 660 } 661 } 662 this.m_value.setIcon(icon); 663 this.m_value.setText( sbuf.toString() ); 664 } 665 666 669 public void mouseEntered(MouseEvent arg0) { 670 671 } 672 673 676 public void mouseExited(MouseEvent arg0) { 677 678 } 679 680 683 public void mousePressed(MouseEvent arg0) { 684 685 } 686 687 690 public void mouseReleased(MouseEvent arg0) { 691 692 } 693 694 699 public void setSelectionMode(int nMode) { 700 m_nSelectionMode = nMode; 701 } 702 703 706 public void pathValuesChanged(List pathValues) { 707 if(m_bIsTreePopulated == true) { 708 709 setPaths(ServerList.getInstance().getHarmoniseServer().getVFS(), m_tree.getPathValues()); 710 711 this.revalidate(); 712 if(this.m_nWindow!=null 713 && (m_nSelectionMode == -1 || m_nSelectionMode == TreeSelectionModel.SINGLE_TREE_SELECTION)) { 714 this.m_nWindow.setVisible(false); 715 } 716 this.fireActionEvent(); 717 } 718 } 719 720 723 public void focusGained(FocusEvent arg0) { 724 } 725 726 729 public void focusLost(FocusEvent arg0) { 730 this.hidePopup(); 731 } 732 733 736 public void windowGainedFocus(WindowEvent arg0) { 737 } 738 739 742 public void windowLostFocus(WindowEvent arg0) { 743 this.hidePopup(); 744 } 745 public void setResourceTreeFilter(AbstractResourceFilter filter) { 746 m_filter = filter; 747 } 748 protected AbstractResourceFilter getResourceTreeFilter() { 749 return m_filter; 750 } 751 public void setScopeEnabled(boolean isEnabled){ 752 this.m_value.setEnabled(isEnabled); 753 this.m_button.setEnabled(isEnabled); 754 } 755 } 756 | Popular Tags |