1 19 20 package org.netbeans.modules.websvc.design.multiview; 21 22 import java.awt.event.ActionEvent ; 23 import java.io.IOException ; 24 import java.io.ObjectInput ; 25 import java.io.ObjectOutput ; 26 import javax.swing.AbstractAction ; 27 import javax.swing.JComponent ; 28 import javax.swing.text.Document ; 29 import org.netbeans.core.spi.multiview.CloseOperationState; 30 import org.netbeans.core.spi.multiview.MultiViewElement; 31 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 32 import org.netbeans.core.spi.multiview.MultiViewFactory; 33 import org.openide.awt.UndoRedo; 34 import org.openide.nodes.Node; 35 import org.openide.text.CloneableEditor; 36 import org.openide.text.DataEditorSupport; 37 import org.openide.text.NbDocument; 38 import org.openide.util.lookup.Lookups; 39 40 45 public class SourceMultiViewElement extends CloneableEditor 46 implements MultiViewElement { 47 private static final long serialVersionUID = 4403502726950453345L; 48 private transient JComponent toolbar; 49 private transient MultiViewElementCallback multiViewCallback; 50 51 54 public SourceMultiViewElement() { 55 super(null); 57 } 58 59 64 public SourceMultiViewElement(MultiViewSupport mvSupport, DataEditorSupport support) { 65 super(support); 66 initialize(mvSupport); 67 } 68 69 private void initialize(MultiViewSupport mvSupport) { 70 if (mvSupport==null || mvSupport.getDataObject() != getEditorSupport().getDataObject()) { 71 throw new IllegalStateException ( 72 "The multiviewsupport object represents incorrect data object"); } 74 associateLookup(Lookups.fixed( 75 mvSupport, 76 getActionMap(), 77 mvSupport.getDataObject(), 78 mvSupport.getDataObject().getNodeDelegate() 79 )); 80 } 81 82 public JComponent getToolbarRepresentation() { 83 Document doc = getEditorPane().getDocument(); 84 if (doc instanceof NbDocument.CustomToolbar) { 85 if (toolbar == null) { 86 toolbar = ((NbDocument.CustomToolbar) doc).createToolbar(getEditorPane()); 87 } 88 return toolbar; 89 } 90 return null; 91 } 92 93 public JComponent getVisualRepresentation() { 94 return this; 95 } 96 97 public void setMultiViewCallback(final MultiViewElementCallback callback) { 98 multiViewCallback = callback; 99 } 100 101 public void requestVisible() { 102 if (multiViewCallback != null) 103 multiViewCallback.requestVisible(); 104 else 105 super.requestVisible(); 106 } 107 108 public void requestActive() { 109 if (multiViewCallback != null) 110 multiViewCallback.requestActive(); 111 else 112 super.requestActive(); 113 } 114 115 protected String preferredID() { 116 return getClass().getName(); 117 } 118 119 120 public UndoRedo getUndoRedo() { 121 return super.getUndoRedo(); 122 } 123 124 protected boolean closeLast() { 125 if(MultiViewSupport.getNumberOfClones(multiViewCallback.getTopComponent()) == 0) { 126 return super.closeLast(); 128 } 129 return true; 130 } 131 132 public CloseOperationState canCloseElement() { 133 if(!getEditorSupport().isModified() || 135 MultiViewSupport.getNumberOfClones(multiViewCallback.getTopComponent()) > 1) { 136 return CloseOperationState.STATE_OK; 137 } 138 return MultiViewFactory.createUnsafeCloseState( 140 MultiViewSupport.SOURCE_UNSAFE_CLOSE, 141 new AbstractAction () { 142 public void actionPerformed(ActionEvent arg0) { 143 try { 145 getEditorSupport().saveDocument(); 146 getEditorSupport().getDataObject().setModified(false); 147 } catch (IOException ex) { 148 } 149 } 150 }, 151 new AbstractAction () { 152 public void actionPerformed(ActionEvent arg0) { 153 } 155 }); 156 } 157 158 public void componentActivated() { 159 super.componentActivated(); 160 setActivatedNodes(new Node[] {getEditorSupport().getDataObject().getNodeDelegate()}); 161 } 162 163 public void componentDeactivated() { 164 super.componentDeactivated(); 165 setActivatedNodes(new Node[] {}); 166 } 167 168 public void componentOpened() { 169 super.componentOpened(); 170 } 171 172 public void componentClosed() { 173 super.componentClosed(); 174 } 175 176 public void componentShowing() { 177 super.componentShowing(); 178 } 179 180 public void componentHidden() { 181 super.componentHidden(); 182 } 183 184 public void writeExternal(ObjectOutput out) throws IOException { 185 super.writeExternal(out); 187 Object obj = getLookup().lookup(MultiViewSupport.class); 188 if(obj!=null) { 189 out.writeObject(obj); 190 } 191 } 192 193 public void readExternal(ObjectInput in) 194 throws IOException , ClassNotFoundException { 195 super.readExternal(in); 196 Object firstObject = in.readObject(); 197 if (firstObject instanceof MultiViewSupport ) { 198 initialize((MultiViewSupport)firstObject); 199 } 200 } 201 202 private DataEditorSupport getEditorSupport() { 203 return (DataEditorSupport) cloneableEditorSupport(); 204 } 205 } 206 | Popular Tags |