1 19 package org.netbeans.modules.xml.core.sync; 20 21 import java.io.IOException ; 22 import java.util.Iterator ; 23 import java.util.Vector ; 24 25 import javax.swing.text.Document ; 26 27 import org.openide.cookies.EditorCookie; 28 import org.openide.filesystems.FileObject; 29 import org.openide.loaders.DataObject; 30 31 import org.netbeans.modules.xml.core.*; 32 import org.netbeans.modules.xml.core.cookies.*; 33 34 46 public class DataObjectSyncSupport extends SyncSupport implements Synchronizator { 47 48 private final Vector reps; 49 50 private final CookieManagerCookie cookieMgr; 51 52 53 54 public DataObjectSyncSupport(XMLDataObjectLook dobj) { 55 super((DataObject)dobj); 56 reps = new Vector (3); 57 cookieMgr = dobj.getCookieManager(); 58 59 Representation basic = new FileRepresentation (getDO(), this); 60 reps.add(basic); 61 } 62 63 64 public void representationChanged(Class type) { 65 super.representationChanged(type); 66 } 67 68 72 protected Representation[] getRepresentations() { 73 return (Representation[]) reps.toArray(new Representation[0]); 74 } 75 76 79 public Representation getPrimaryRepresentation() { 80 81 final Class priority[] = new Class [] { Document .class, 83 FileObject.class 85 }; 86 87 Representation all[] = getRepresentations(); 88 89 for (int i = 0; i<priority.length; i++) { 90 for (int r = 0; r<all.length; r++) { 91 Representation rep = all[r]; 92 if (rep.isValid() == false) 93 continue; 94 if (rep.represents(priority[i])) { 95 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Primary rep = " + rep); 97 return rep; 98 } 99 } 100 } 101 102 throw new IllegalStateException ("No primary representation found: " + reps); } 104 105 109 public void addRepresentation(Representation rep) { 110 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Sync addRepresentation " + rep); 112 if (rep.represents(Document .class)) { 113 for (Iterator it = reps.iterator(); it.hasNext();) { 114 Representation next = (Representation) it.next(); 115 if (next.represents(FileObject.class)) { 116 it.remove(); 117 } 118 } 119 } else if (rep.level() > 1) { 120 121 123 loadTextRepresentation(); 124 } 125 reps.add(rep); 126 } 127 128 129 130 134 public void removeRepresentation(Representation rep) { 135 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Sync removeRepresentation " + rep); 137 boolean modelLoaded = false; 138 139 if (rep.represents(Document .class)) { 140 141 143 144 for (Iterator it = reps.iterator(); it.hasNext();) { 145 Representation next = (Representation) it.next(); 146 if (next.level() > 1) { 147 modelLoaded = true; 148 } 149 } 150 151 if (modelLoaded == false) { 152 153 Representation basic = new FileRepresentation (getDO(), this); 154 reps.add(basic); 155 156 } else { 157 158 160 loadTextRepresentation(); 161 } 162 } 163 reps.remove(rep); 164 165 if ( modelLoaded ) { representationChanged (Document .class); 167 } 168 } 169 170 171 private void loadTextRepresentation() { 172 if ( getDO().isValid() ) { try { 174 EditorCookie editor = (EditorCookie) getDO().getCookie(EditorCookie.class); 175 editor.openDocument(); 176 } catch (IOException ex) { 177 } 179 } 180 } 181 } 182 | Popular Tags |