/plugins/ProjectViewer/tags/bp_projectviewer_1_0_3/projectviewer/tree/ProjectTreeModel.java

# · Java · 164 lines · 75 code · 20 blank · 69 comment · 7 complexity · 7c46ad3a071e535bc51e04a3166ec183 MD5 · raw file

  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License
  4. * as published by the Free Software Foundation; either version 2
  5. * of the License, or any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more detaProjectTreeSelectionListenerils.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. package projectviewer.tree;
  17. import java.util.*;
  18. import javax.swing.event.*;
  19. import javax.swing.tree.*;
  20. import org.gjt.sp.util.Log;
  21. import projectviewer.*;
  22. /**
  23. * A super class tree modeling project artifacts.
  24. */
  25. abstract class ProjectTreeModel
  26. implements TreeModel, Branch
  27. {
  28. protected Project project;
  29. private List listeners;
  30. private BranchOwner owner;
  31. /**
  32. * Create a new <code>ProjectFilesFlatTreeModel</code>.
  33. */
  34. public ProjectTreeModel( Project aProject ) {
  35. listeners = new ArrayList();
  36. project = aProject;
  37. }
  38. /**
  39. * Set the branch owner.
  40. */
  41. public void setBranchOwner( BranchOwner anOwner ) {
  42. owner = anOwner;
  43. }
  44. /**
  45. * Returns the project this tree is modeling.
  46. */
  47. public Project getProject() {
  48. return project;
  49. }
  50. /**
  51. * Adds a listener for the events posted after the tree changes.
  52. */
  53. public void addTreeModelListener(TreeModelListener l) {
  54. listeners.add( l );
  55. }
  56. /**
  57. * Removes a listener previously added with {@link #addTreeModelListener(TreeModelListener)}.
  58. */
  59. public void removeTreeModelListener(TreeModelListener l) {
  60. listeners.remove( l );
  61. }
  62. /**
  63. * Messaged when the user has altered the value for the item identified by
  64. * <code>path</code> to <code>newValue</code>.
  65. *
  66. * <p><i>Note:</i> Since this tree is immutable, this method does nothing.
  67. */
  68. public void valueForPathChanged(TreePath path, Object newValue) {}
  69. /**
  70. * Returns true if node is a leaf.
  71. */
  72. public boolean isLeaf(Object node) {
  73. return node instanceof ProjectFile;
  74. }
  75. /**
  76. * Returns the root of the tree.
  77. */
  78. public Object getRoot() {
  79. return project;
  80. }
  81. /**
  82. * Convert the given list to a <code>TreePath</code>, prepending
  83. * branch path is required.
  84. */
  85. protected TreePath toTreePath( List path ) {
  86. if ( owner != null ) {
  87. Object[] root = owner.getPathTo(this);
  88. for ( int i=root.length-1; i>=0; i-- )
  89. path.add( 0, root[i] );
  90. }
  91. return new TreePath( path.toArray() );
  92. }
  93. /**
  94. * Fired an event notifying listeners that the tree structure has changed.
  95. */
  96. protected void fireStructureChanged() {
  97. TreeModelEvent evt = new TreeModelEvent( this, new TreePath( getRoot() ) );
  98. for ( int i=0; i<listeners.size(); i++ )
  99. ( (TreeModelListener) listeners.get(i) ).treeStructureChanged( evt );
  100. }
  101. /**
  102. * Fired an event notifying listeners that a node was inserted.
  103. */
  104. protected void fireNodeInserted( TreePath path, Object node ) {
  105. //Log.log( Log.DEBUG, this, "Node Inserted: path(" + path + ") node(" + node + ")" );
  106. int idx = getIndexOfChild( path.getLastPathComponent(), node );
  107. TreeModelEvent evt = new TreeModelEvent( this, path, toArray( idx ), toArray( node ) );
  108. for ( int i=0; i<listeners.size(); i++ )
  109. ( (TreeModelListener) listeners.get(i) ).treeNodesInserted( evt );
  110. }
  111. /**
  112. * Fired an event notifying listeners that a node was removed.
  113. */
  114. protected void fireNodeRemoved( TreePath path, int index, Object node ) {
  115. //Log.log( Log.DEBUG, this, "Node Removed: path(" + path + ") node(" + node + ")" );
  116. TreeModelEvent evt = new TreeModelEvent( this, path, toArray( index ), toArray( node ) );
  117. for ( int i=0; i<listeners.size(); i++ )
  118. ( (TreeModelListener) listeners.get(i) ).treeNodesRemoved( evt );
  119. }
  120. /**
  121. * Fire node changed.
  122. */
  123. protected void fireNodeChanged( TreePath path, int index ) {
  124. TreeModelEvent evt = new TreeModelEvent( this, path, toArray( index ), null );
  125. for ( int i=0; i<listeners.size(); i++ )
  126. ( (TreeModelListener) listeners.get(i) ).treeNodesChanged( evt );
  127. }
  128. /**
  129. * Convert the given <code>Object</code> to an <code>Object</code> array.
  130. */
  131. private static Object[] toArray( Object obj ) {
  132. Object[] arr = new Object[1];
  133. arr[0] = obj;
  134. return arr;
  135. }
  136. /**
  137. * Convert the given <code>int</code> to an <code>int</code> array.
  138. */
  139. private static int[] toArray( int index ) {
  140. int[] arr = new int[1];
  141. arr[0] = index;
  142. return arr;
  143. }
  144. }