1 19 20 package org.netbeans.modules.xml.wsdl.ui.netbeans.module; 21 22 import java.io.IOException ; 23 import java.io.ObjectInput ; 24 import java.io.ObjectOutput ; 25 26 import javax.swing.JComponent ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.text.Document ; 29 import javax.swing.text.StyledDocument ; 30 31 import org.netbeans.core.spi.multiview.CloseOperationState; 32 import org.netbeans.core.spi.multiview.MultiViewElement; 33 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 34 import org.netbeans.core.spi.multiview.MultiViewFactory; 35 import org.netbeans.modules.xml.validation.ShowCookie; 36 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 37 import org.netbeans.modules.xml.xam.Component; 38 import org.netbeans.modules.xml.xam.dom.DocumentComponent; 39 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem; 40 import org.openide.awt.UndoRedo; 41 import org.openide.nodes.Node; 42 import org.openide.text.CloneableEditor; 43 import org.openide.text.NbDocument; 44 import org.openide.util.Lookup; 45 import org.openide.util.lookup.Lookups; 46 import org.openide.util.lookup.ProxyLookup; 47 48 52 53 public class WSDLSourceMultiViewElement extends CloneableEditor implements MultiViewElement { 54 55 static final long serialVersionUID = 4403502726950453345L; 56 57 transient private JComponent toolbar; 58 transient private MultiViewElementCallback multiViewObserver; 59 private WSDLDataObject wsdlDataObject; 60 61 62 public WSDLSourceMultiViewElement() { 64 super(); 65 } 66 67 public WSDLSourceMultiViewElement(WSDLDataObject wsdlDataObject) { 69 super(wsdlDataObject.getWSDLEditorSupport()); 70 this.wsdlDataObject = wsdlDataObject; 71 72 setActivatedNodes(new Node[] {wsdlDataObject.getNodeDelegate()}); 74 75 wsdlDataObject.getWSDLEditorSupport().initializeCloneableEditor(this); 86 initialize(); 87 } 88 89 public JComponent getToolbarRepresentation() { 90 Document doc = getEditorPane().getDocument(); 91 if (doc instanceof NbDocument.CustomToolbar) { 92 if (toolbar == null) { 93 toolbar = ((NbDocument.CustomToolbar) doc).createToolbar(getEditorPane()); 94 } 95 return toolbar; 96 } 97 return null; 98 } 99 100 public JComponent getVisualRepresentation() { 101 return this; 102 } 103 104 public void setMultiViewCallback(MultiViewElementCallback callback) { 105 multiViewObserver = callback; 106 } 107 108 @Override 109 public void requestVisible() { 110 if (multiViewObserver != null) 111 multiViewObserver.requestVisible(); 112 else 113 super.requestVisible(); 114 } 115 116 @Override 117 public void requestActive() { 118 if (multiViewObserver != null) 119 multiViewObserver.requestActive(); 120 else 121 super.requestActive(); 122 } 123 124 @Override 125 protected String preferredID() { 126 127 return "WSDLSourceMultiViewElementTC"; } 129 130 131 @Override 132 public UndoRedo getUndoRedo() { 133 return wsdlDataObject.getWSDLEditorSupport().getUndoManager(); 134 } 135 136 145 @Override 146 protected boolean closeLast() { 147 WSDLEditorSupport support = wsdlDataObject.getWSDLEditorSupport(); 148 JEditorPane [] editors = support.getOpenedPanes(); 149 if (editors == null || editors.length == 0) { 150 return support.silentClose(); 151 } 152 return false; 153 } 154 155 public CloseOperationState canCloseElement() { 156 if (!WSDLEditorSupport.isLastView(multiViewObserver.getTopComponent())) { 158 return CloseOperationState.STATE_OK; 159 } 160 return MultiViewFactory.createUnsafeCloseState( 162 "ID_TEXT_CLOSING", MultiViewFactory.NOOP_CLOSE_ACTION, 164 MultiViewFactory.NOOP_CLOSE_ACTION); 165 } 166 167 @Override 168 public void componentDeactivated() { 169 super.componentDeactivated(); 170 WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport(); 171 editor.syncModel(); 174 editor.removeUndoManagerFromDocument(); 175 } 176 177 @Override 178 public void componentActivated() { 179 super.componentActivated(); 180 WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport(); 181 editor.addUndoManagerToDocument(); 182 } 183 184 @Override 185 public void componentClosed() { 186 super.componentClosed(); 187 } 188 189 @Override 190 public void componentShowing() { 191 super.componentShowing(); 192 WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport(); 193 editor.addUndoManagerToDocument(); 194 } 195 196 @Override 197 public void componentHidden() { 198 super.componentHidden(); 199 WSDLEditorSupport editor = wsdlDataObject.getWSDLEditorSupport(); 200 editor.syncModel(); 203 editor.removeUndoManagerFromDocument(); 204 } 205 206 @Override 207 public void componentOpened() { 208 super.componentOpened(); 209 } 210 211 @Override 212 public void writeExternal(ObjectOutput out) throws IOException { 213 super.writeExternal(out); 214 out.writeObject(wsdlDataObject); 215 } 216 217 @Override 218 public void readExternal(ObjectInput in) 219 throws IOException , ClassNotFoundException { 220 super.readExternal(in); 221 Object firstObject = in.readObject(); 222 if (firstObject instanceof WSDLDataObject) { 223 wsdlDataObject = (WSDLDataObject) firstObject; 224 initialize(); 225 } 226 } 227 228 private void initialize() 229 { 230 ShowCookie showCookie = new ShowCookie() 231 { 232 233 public void show(ResultItem resultItem) { 234 if(isActiveTC()) { 235 Component component = resultItem.getComponents(); 236 if (component.getModel() == null) return; 238 UIUtilities.annotateSourceView(wsdlDataObject, (DocumentComponent) component, 239 resultItem.getDescription(), true); 240 if(component instanceof WSDLComponent) { 241 int position = ((WSDLComponent)component).findPosition(); 242 getEditorPane().setCaretPosition(position); 243 } else { 244 int line = resultItem.getLineNumber(); 245 try { 246 int position = NbDocument.findLineOffset( 247 (StyledDocument )getEditorPane().getDocument(),line); 248 getEditorPane().setCaretPosition(position); 249 } catch (IndexOutOfBoundsException iob) { 250 } 252 } 253 } 254 } 255 }; 256 257 ProxyLookup lookup = new ProxyLookup(new Lookup[] { 259 Lookups.fixed(new Object [] { 260 getActionMap(), 262 wsdlDataObject, 266 showCookie, 268 }), 269 wsdlDataObject.getNodeDelegate().getLookup(), 270 }); 271 associateLookup(lookup); 272 } 273 274 protected boolean isActiveTC() 275 { 276 return getRegistry().getActivated() == multiViewObserver.getTopComponent(); 277 } 278 } 279 | Popular Tags |