1 package freemarker.eclipse.outline; 2 3 import java.util.Collections ; 4 import java.util.Comparator ; 5 import java.util.Enumeration ; 6 import java.util.Iterator ; 7 import java.util.Map ; 8 import java.util.Vector ; 9 10 import org.eclipse.jface.text.BadPositionCategoryException; 11 import org.eclipse.jface.text.DefaultPositionUpdater; 12 import org.eclipse.jface.text.IDocument; 13 import org.eclipse.jface.text.IPositionUpdater; 14 import org.eclipse.jface.text.reconciler.IReconcilingStrategy; 15 import org.eclipse.jface.viewers.ITreeContentProvider; 16 import org.eclipse.jface.viewers.Viewer; 17 18 import freemarker.core.Macro; 19 import freemarker.eclipse.editors.FreemarkerEditor; 20 import freemarker.eclipse.editors.ReconcilingStrategy; 21 import freemarker.template.Template; 22 23 27 public class OutlineContentProvider implements ITreeContentProvider { 28 29 public static final Comparator COMPARATOR = new Comparator () { 30 public int compare(Object arg0, Object arg1) { 31 return (arg0.toString().compareTo( 32 arg1.toString())); 33 } 34 }; 35 public static final String FREEMARKER_TEMPLATE = "__ftl_template"; 36 37 private FreemarkerEditor fEditor; 38 private IReconcilingStrategy fReconcilingStrategy; 39 private IPositionUpdater fPositionUpdater; 40 41 public OutlineContentProvider(FreemarkerEditor anEditor) { 42 fEditor = anEditor; 43 fPositionUpdater = new DefaultPositionUpdater(FREEMARKER_TEMPLATE); 44 fReconcilingStrategy = new ReconcilingStrategy(fEditor); 45 } 46 47 public void inputChanged( 48 Viewer aViewer, 49 Object anOldInput, 50 Object aNewInput) { 51 if (anOldInput != aNewInput) { 52 if (anOldInput != null) { 53 IDocument document = 54 fEditor.getDocumentProvider().getDocument(anOldInput); 55 if (document != null) { 56 try { 57 document.removePositionCategory(FREEMARKER_TEMPLATE); 58 } catch (BadPositionCategoryException e) { 59 } 60 document.removePositionUpdater(fPositionUpdater); 61 } 62 } 63 64 if (aNewInput != null) { 65 IDocument document = 66 fEditor.getDocumentProvider().getDocument(aNewInput); 67 if (document != null) { 68 document.addPositionCategory(FREEMARKER_TEMPLATE); 69 document.addPositionUpdater(fPositionUpdater); 70 } 71 } 72 } 73 } 74 75 public boolean isDeleted(Object anElement) { 76 return false; 77 } 78 79 public void dispose() { 80 } 81 82 public Object [] getElements(Object inputElement) { 83 Template t = fEditor.getReconcilingStrategy().getTemplate(); 84 if (null == t) { 85 return new Object [0]; 86 } else { 87 Vector temp = new Vector (); 88 89 Map macros = t.getMacros(); 90 Iterator iter = macros.keySet().iterator(); 91 Object key = null; 92 Macro m = null; 93 while (iter.hasNext()) { 94 key = iter.next(); 95 m = (Macro) macros.get(key); 96 temp.add(new MacroNode(m)); 97 } 98 Collections.sort(temp,OutlineContentProvider.COMPARATOR); 99 temp.add(0,new ImportCollectionNode(t)); 100 101 return temp.toArray(); 102 } 103 } 104 105 public Object [] getChildren(Object anElement) { 106 Enumeration e; 107 return (anElement instanceof ImportCollectionNode) 108 ? ((ImportCollectionNode) anElement).getChildren() 109 : new Object [0]; 110 } 111 112 public Object getParent(Object anElement) { 113 return (anElement instanceof ImportNode) 114 ? ((ImportNode) anElement).getParent() 115 : null; 116 } 117 118 public boolean hasChildren(Object anElement) { 119 return (anElement instanceof ImportCollectionNode) 120 ? ((ImportCollectionNode) anElement).hasChildren() 121 : false; 122 } 123 124 } 125 | Popular Tags |