1 19 20 21 package org.netbeans.modules.image; 22 23 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyVetoException ; 27 import java.io.IOException ; 28 import java.text.MessageFormat ; 29 import java.util.Enumeration ; 30 import javax.swing.Icon ; 31 import javax.swing.SwingUtilities ; 32 33 import org.openide.cookies.CloseCookie; 34 import org.openide.cookies.OpenCookie; 35 import org.openide.filesystems.FileChangeListener; 36 import org.openide.filesystems.FileChangeAdapter; 37 import org.openide.filesystems.FileEvent; 38 import org.openide.loaders.DataObject; 39 import org.openide.loaders.MultiDataObject; 40 import org.openide.loaders.OpenSupport; 41 import org.openide.NotifyDescriptor; 42 import org.openide.DialogDisplayer; 43 import org.openide.windows.CloneableOpenSupport; 44 import org.openide.windows.CloneableTopComponent; 45 import org.openide.util.NbBundle; 46 import org.openide.util.RequestProcessor; 47 import org.openide.util.RequestProcessor.Task; 48 49 50 58 59 public class ImageOpenSupport extends OpenSupport implements OpenCookie, CloseCookie { 60 61 62 private long lastSaveTime; 63 64 65 private FileChangeListener fileChangeL; 66 67 68 private Task reloadTask; 69 70 71 72 public ImageOpenSupport(MultiDataObject.Entry entry) { 73 super(entry, new Environment(entry.getDataObject())); } 75 76 77 78 public CloneableTopComponent createCloneableTopComponent () { 79 prepareViewer(); 80 return new ImageViewer((ImageDataObject)entry.getDataObject()); 81 } 82 83 84 void prepareViewer() { 85 if(fileChangeL == null) { 87 fileChangeL = new FileChangeAdapter() { 88 public void fileChanged(final FileEvent evt) { 89 if(allEditors.isEmpty()) { 90 return; 91 } 92 93 if(evt.getFile().isVirtual()) { 94 entry.getFile().removeFileChangeListener(this); 95 ((Environment)ImageOpenSupport.this.env).fileRemoved(); 98 entry.getFile().addFileChangeListener(this); 99 return; 100 } 101 102 if (evt.getTime() > lastSaveTime) { 103 lastSaveTime = System.currentTimeMillis(); 104 105 if(reloadTask == null || reloadTask.isFinished()) { 107 108 reloadTask = RequestProcessor.postRequest( 109 new Runnable () { 110 public void run() { 111 reload(evt); 112 } 113 } 114 ); 115 } 116 } 117 } 118 }; 119 } 120 entry.getFile().addFileChangeListener(fileChangeL); 121 lastSaveTime = System.currentTimeMillis(); 122 } 123 124 125 private void reload(FileEvent evt) { 126 String msg = NbBundle.getMessage(ImageOpenSupport.class, "MSG_ExternalChange", entry.getFile() ); 130 NotifyDescriptor nd = new NotifyDescriptor.Confirmation(msg, NotifyDescriptor.YES_NO_OPTION); 131 Object ret = DialogDisplayer.getDefault().notify(nd); 132 133 if (NotifyDescriptor.YES_OPTION.equals(ret)) { 134 final ImageDataObject imageObj = (ImageDataObject)entry.getDataObject(); 136 final CloneableTopComponent.Ref editors = allEditors; 137 138 Enumeration e = editors.getComponents(); 139 while(e.hasMoreElements()) { 140 final Object pane = e.nextElement(); 141 SwingUtilities.invokeLater(new Runnable () { 142 public void run() { 143 ((ImageViewer)pane).updateView(imageObj); 144 } 145 }); 146 } 147 } 148 } 149 150 151 private static class Environment extends OpenSupport.Env { 152 153 static final long serialVersionUID = -1934890789745432254L; 154 155 156 public Environment(DataObject dataObject) { 157 super(dataObject); 158 } 159 160 161 162 public CloneableOpenSupport findCloneableOpenSupport() { 163 return (CloneableOpenSupport)getDataObject().getCookie(OpenCookie.class); 164 } 165 166 169 private void fileRemoved() { 170 try { 171 fireVetoableChange(PROP_VALID, Boolean.TRUE, Boolean.FALSE); 172 } catch(PropertyVetoException pve) { 173 } 175 176 firePropertyChange(PROP_VALID, Boolean.TRUE, Boolean.FALSE); 177 } 178 } } 180 181 | Popular Tags |