1 19 20 package org.netbeans.modules.websvc.design.multiview; 21 22 import java.awt.EventQueue ; 23 import java.awt.event.ActionEvent ; 24 import java.io.IOException ; 25 import java.io.Serializable ; 26 import java.util.Collections ; 27 import javax.swing.Action ; 28 import org.netbeans.api.project.FileOwnerQuery; 29 import org.netbeans.api.project.Project; 30 import org.openide.text.DataEditorSupport; 31 import org.openide.loaders.DataObject; 32 import org.openide.windows.Mode; 33 import org.openide.windows.WindowManager; 34 import org.openide.windows.CloneableTopComponent; 35 import org.openide.windows.TopComponent; 36 import org.netbeans.core.api.multiview.MultiViewHandler; 37 import org.netbeans.core.api.multiview.MultiViewPerspective; 38 import org.netbeans.core.api.multiview.MultiViews; 39 import org.netbeans.core.spi.multiview.CloseOperationHandler; 40 import org.netbeans.core.spi.multiview.CloseOperationState; 41 import org.netbeans.core.spi.multiview.MultiViewDescription; 42 import org.netbeans.core.spi.multiview.MultiViewFactory; 43 import org.netbeans.modules.websvc.api.jaxws.project.config.JaxWsModel; 44 import org.netbeans.modules.websvc.api.jaxws.project.config.Service; 45 import org.openide.DialogDisplayer; 46 import org.openide.NotifyDescriptor; 47 import org.openide.util.NbBundle; 48 import org.netbeans.modules.websvc.core.MultiViewCookie; 49 50 54 public class MultiViewSupport implements MultiViewCookie, Serializable { 55 56 static final java.awt.Image SERVICE_BADGE = org.openide.util.Utilities.loadImage( 57 "org/netbeans/modules/websvc/core/webservices/ui/resources/XMLServiceDataIcon.gif" ); static final long serialVersionUID = 1L; 59 private DataObject dataObject; 60 private transient Service service; 61 62 public static String SOURCE_UNSAFE_CLOSE = "SOURCE_UNSAFE_CLOSE"; 63 private static String DESIGN_UNSAFE_CLOSE = "DESIGN_UNSAFE_CLOSE"; 64 65 68 public enum View { 69 72 SOURCE, 73 76 DESIGN, 77 } 78 79 82 public MultiViewSupport() { 83 } 84 85 90 MultiViewSupport(Service service, DataObject dataObject) { 91 this.dataObject = dataObject; 92 this.service = service; 93 } 94 95 public void open() { 96 view(View.DESIGN); 97 } 98 99 public void edit() { 100 view(View.SOURCE); 101 } 102 103 DataObject getDataObject() { 104 return dataObject; 105 } 106 107 Service getService() { 108 return service; 109 } 110 111 public boolean equals(Object obj) { 112 if(obj==this) { 113 return true; 114 } 115 if(obj instanceof MultiViewSupport) { 116 MultiViewSupport sup = (MultiViewSupport)obj; 117 return getDataObject().equals(sup.getDataObject()); 118 } 119 return false; 120 } 121 122 125 private TopComponent ensureMultiViewActive() { 126 Mode editorMode = WindowManager.getDefault().findMode( 127 DataEditorSupport.EDITOR_MODE); 128 if (editorMode != null && editorMode.getSelectedTopComponent() != null) { 129 TopComponent activeTC = editorMode.getSelectedTopComponent(); 130 if(equals(activeTC.getLookup().lookup(MultiViewSupport.class))) { 131 return activeTC; 132 } 133 for(TopComponent openedTC:editorMode.getTopComponents()) { 134 if(equals(openedTC.getLookup().lookup(MultiViewSupport.class))) { 135 return openedTC; 136 } 137 } 138 } 139 CloneableTopComponent tc = createMultiView(); 140 tc.requestActive(); 141 return tc; 142 } 143 144 148 private CloneableTopComponent createMultiView() { 149 MultiViewDescription views[] = new MultiViewDescription[2]; 150 151 views[0] = new SourceMultiViewDesc(this); 154 views[1] = new DesignMultiViewDesc(this); 155 156 157 CloneableTopComponent multiview = 159 MultiViewFactory.createCloneableMultiView( 160 views, 161 views[0] 162 , new CloseHandler(dataObject) 163 ); 164 165 String displayName = getServiceDisplayName(); 166 multiview.setDisplayName(displayName); 167 multiview.setName(displayName); 168 169 Mode editorMode = WindowManager.getDefault().findMode( 170 DataEditorSupport.EDITOR_MODE); 171 if (editorMode != null) { 172 editorMode.dockInto(multiview); 173 } 174 multiview.open(); 175 return multiview; 176 } 177 178 179 184 public void view(final View view, final Object ... param) { 185 if (!EventQueue.isDispatchThread()) { 186 EventQueue.invokeLater(new Runnable () { 187 public void run() { 188 viewInSwingThread(view,dataObject,param); 189 } 190 }); 191 } else { 192 viewInSwingThread(view,dataObject,param); 193 } 194 } 195 196 private void viewInSwingThread(View view, Object ... parameters) { 197 TopComponent activeTC = ensureMultiViewActive(); 198 switch(view) { 199 case SOURCE: 200 requestMultiviewActive(activeTC,SourceMultiViewDesc.PREFERRED_ID); 201 break; 202 case DESIGN: 203 requestMultiviewActive(activeTC,DesignMultiViewDesc.PREFERRED_ID); 204 break; 205 } 206 } 207 208 216 private static void requestMultiviewActive(TopComponent activeTC, String id) { 217 MultiViewHandler handler = MultiViews.findMultiViewHandler(activeTC); 218 if (handler != null) { 219 MultiViewPerspective[] perspectives = handler.getPerspectives(); 220 for (MultiViewPerspective perspective : perspectives) { 221 if (perspective.preferredID().equals(id)) { 222 handler.requestActive(perspective); 223 } 224 } 225 } 226 } 227 228 236 public static int getNumberOfClones(TopComponent tc) { 237 if (!(tc instanceof CloneableTopComponent)) { 238 return -1; 239 } 240 return Collections.list(((CloneableTopComponent)tc).getReference().getComponents()).size(); 241 } 242 243 246 private String getServiceDisplayName() { 247 if (service.getWsdlUrl()!=null) 248 return service.getServiceName()+"["+service.getPortName()+"]"; 249 return service.getName(); 250 } 251 252 private void writeObject(java.io.ObjectOutputStream out) 253 throws IOException { 254 out.writeObject(dataObject); 255 out.writeObject(service.getImplementationClass()); 256 } 257 258 private void readObject(java.io.ObjectInputStream in) 259 throws IOException , ClassNotFoundException { 260 Object firstObject = in.readObject(); 261 if(firstObject instanceof DataObject) { 262 dataObject = (DataObject) firstObject; 263 } 264 Object secondObject = in.readObject(); 265 if(secondObject instanceof String ) { 266 String implClass = (String )secondObject; 267 Project project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile()); 268 JaxWsModel model = (JaxWsModel) project.getLookup().lookup(JaxWsModel.class); 269 service = model.findServiceByImplementationClass(implClass); 270 } 271 } 272 273 279 public static class CloseHandler implements CloseOperationHandler, Serializable { 280 private static final long serialVersionUID = -3838395157610633251L; 281 private DataObject sourceDataObject; 282 283 private CloseHandler() { 284 super(); 285 } 286 287 public CloseHandler(DataObject sourceDataObject) { 288 this.sourceDataObject = sourceDataObject; 289 } 290 291 private DataEditorSupport getEditorSupport() { 292 return sourceDataObject == null ? null : 293 (DataEditorSupport) sourceDataObject.getLookup().lookup( 294 DataEditorSupport.class); 295 } 296 297 public boolean resolveCloseOperation(CloseOperationState[] elements) { 298 StringBuffer message = new StringBuffer (); 299 for(CloseOperationState state:elements) { 300 if(state.getCloseWarningID().equals(SOURCE_UNSAFE_CLOSE)) { 301 message.append(NbBundle.getMessage(DataObject.class, 302 "MSG_SaveFile", sourceDataObject.getPrimaryFile().getNameExt())); 304 message.append("\n"); 305 } 306 } 307 NotifyDescriptor desc = new NotifyDescriptor.Confirmation(message.toString().trim()); 308 Object retVal = DialogDisplayer.getDefault().notify(desc); 309 for(CloseOperationState state:elements) { 310 Action act = null; 311 if (retVal == NotifyDescriptor.YES_OPTION) { 312 act = state.getProceedAction(); 313 } else if (retVal == NotifyDescriptor.NO_OPTION) { 314 act = state.getDiscardAction(); 315 } else { 316 return false; 317 } 318 if (act != null) { 319 act.actionPerformed(new ActionEvent (this, ActionEvent.ACTION_PERFORMED, "")); 320 } 321 } 322 return true; 323 } 324 } 325 } 326 | Popular Tags |