1 19 20 package org.netbeans.modules.xml.schema.ui.nodes; 21 22 import java.util.ArrayList ; 23 import java.util.List ; 24 import javax.swing.event.ChangeEvent ; 25 import javax.swing.event.ChangeListener ; 26 import org.netbeans.modules.xml.schema.model.SchemaComponent; 27 import org.netbeans.modules.xml.schema.ui.nodes.categorized.CategoryNode; 28 import org.netbeans.modules.xml.xam.Model; 29 import org.openide.nodes.Children; 30 import org.openide.nodes.Index; 31 import org.openide.nodes.Node; 32 33 41 public abstract class RefreshableChildren extends Children.Keys implements Index { 42 43 private Index indexSupport; 44 45 48 public RefreshableChildren() { 49 super(); 50 indexSupport = new IndexSupport(); 51 } 52 53 56 public abstract void refreshChildren(); 57 58 public void addChangeListener(ChangeListener l) { 59 indexSupport.addChangeListener(l); 60 } 61 62 public void removeChangeListener(ChangeListener l) { 63 indexSupport.removeChangeListener(l); 64 } 65 66 public void exchange(int x, int y) { 67 indexSupport.exchange(x, y); 68 } 69 70 public int indexOf(Node node) { 71 return indexSupport.indexOf(node); 72 } 73 74 public void moveUp(int i) { 75 indexSupport.moveUp(i); 76 } 77 78 public void moveDown(int i) { 79 indexSupport.moveDown(i); 80 } 81 82 public void move(int x, int y) { 83 indexSupport.move(x, y); 84 } 85 86 public void reorder() { 87 indexSupport.reorder(); 88 } 89 90 public void reorder(int[] i) { 91 indexSupport.reorder(i); 92 } 93 94 97 private class IndexSupport extends Index.Support { 98 99 public void reorder(int[] perm) { 100 106 Node[] nodes = getNodes(); 112 SchemaComponentNode scn = (SchemaComponentNode) nodes[0]. 113 getCookie(SchemaComponentNode.class); 114 SchemaComponent parent = null; 115 if (scn != null) { 116 parent = scn.getReference().get().getParent(); 117 } else { 118 CategoryNode cn = (CategoryNode) getNode(). 120 getCookie(CategoryNode.class); 121 if (cn != null) { 122 parent = cn.getReference().get(); 123 } 124 } 126 if (parent != null) { 127 Model model = parent.getModel(); 130 try { 131 model.startTransaction(); 132 List <SchemaComponent> children = parent.getChildren(); 133 children = new ArrayList <SchemaComponent>(children); 136 SchemaComponent[] arr = children.toArray( 137 new SchemaComponent[children.size()]); 138 for (int i = 0; i < arr.length; i++) { 139 children.set(perm[i], arr[i]); 140 } 141 List <SchemaComponent> copies = new ArrayList <SchemaComponent>(); 145 for (SchemaComponent child : children) { 146 copies.add((SchemaComponent)child.copy(parent)); 147 } 148 for (SchemaComponent child : children) { 150 model.removeChildComponent(child); 151 } 152 for (SchemaComponent copy : copies) { 154 model.addChildComponent(parent, copy, -1); 155 } 156 } catch (IndexOutOfBoundsException ioobe) { 157 return; 160 } finally { 161 model.endTransaction(); 162 } 163 fireChangeEvent(new ChangeEvent (this)); 165 } 166 } 168 169 public int getNodesCount() { 170 return RefreshableChildren.this.getNodesCount(); 171 } 172 173 public Node[] getNodes() { 174 return RefreshableChildren.this.getNodes(); 175 } 176 } 177 } 178 | Popular Tags |