1 19 20 package org.netbeans.core.multiview; 21 22 import java.awt.BorderLayout ; 23 import java.io.*; 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.Map ; 29 import javax.swing.AbstractAction ; 30 import javax.swing.Action ; 31 import javax.swing.ActionMap ; 32 import javax.swing.InputMap ; 33 import javax.swing.JComponent ; 34 import javax.swing.JPanel ; 35 import javax.swing.JScrollPane ; 36 import javax.swing.KeyStroke ; 37 import javax.swing.SwingUtilities ; 38 import org.netbeans.core.api.multiview.MultiViewHandler; 39 import org.netbeans.core.api.multiview.MultiViewPerspective; 40 import org.netbeans.core.multiview.MultiViewModel.ActionRequestObserverFactory; 41 import org.netbeans.core.multiview.MultiViewModel.ElementSelectionListener; 42 import org.netbeans.core.spi.multiview.CloseOperationHandler; 43 import org.netbeans.core.spi.multiview.CloseOperationState; 44 import org.netbeans.core.spi.multiview.MultiViewDescription; 45 import org.netbeans.core.spi.multiview.MultiViewElement; 46 import org.netbeans.core.spi.multiview.MultiViewElementCallback; 47 import org.netbeans.core.spi.multiview.MultiViewFactory; 48 import org.openide.awt.UndoRedo; 49 import org.openide.text.CloneableEditorSupport; 50 import org.openide.util.HelpCtx; 51 import org.openide.util.Lookup; 52 import org.openide.util.lookup.Lookups; 53 import org.openide.util.lookup.ProxyLookup; 54 import org.openide.windows.CloneableTopComponent; 55 import org.openide.windows.TopComponent; 56 57 58 65 66 67 public final class MultiViewCloneableTopComponent extends CloneableTopComponent 68 implements ActionRequestObserverFactory, CloneableEditorSupport.Pane { 69 70 MultiViewPeer peer; 71 72 public MultiViewCloneableTopComponent() { 73 super(); 74 peer = new MultiViewPeer(this, this); 75 peer.initComponents(); 77 setFocusCycleRoot(false); 78 setName(""); 79 associateLookup(peer.getLookup()); 81 82 } 84 85 86 public void setMultiViewDescriptions(MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) { 87 peer.setMultiViewDescriptions(descriptions, defaultDesc); 88 } 89 90 public void setCloseOperationHandler(CloseOperationHandler handler) { 91 peer.setCloseOperationHandler(handler); 92 } 93 94 private void setDeserializedMultiViewDescriptions(MultiViewDescription[] descriptions, 95 MultiViewDescription defaultDesc, Map existingElements) { 96 peer.setDeserializedMultiViewDescriptions(descriptions, defaultDesc, existingElements); 97 } 98 99 MultiViewModel getModel() { 100 return peer.getModel(); 101 } 102 103 104 105 protected void componentClosed() { 106 super.componentClosed(); 107 peer.peerComponentClosed(); 108 } 109 110 protected void componentShowing() { 111 super.componentShowing(); 112 peer.peerComponentShowing(); 113 } 114 115 protected void componentHidden() { 116 super.componentHidden(); 117 peer.peerComponentHidden(); 118 } 119 120 protected void componentDeactivated() { 121 super.componentDeactivated(); 122 peer.peerComponentDeactivated(); 123 } 124 125 protected void componentActivated() { 126 super.componentActivated(); 127 peer.peerComponentActivated(); 128 } 129 130 protected void componentOpened() { 131 super.componentOpened(); 132 peer.peerComponentOpened(); 133 } 134 135 138 public boolean requestFocusInWindow() { 139 return peer.requestFocusInWindow(); 140 } 141 142 145 public void requestFocus() { 146 peer.requestFocus(); 147 } 148 149 153 public Action [] getActions() { 154 Action [] superActions = super.getActions(); 156 Action [] acts = peer.peerGetActions(superActions); 157 return acts; 158 } 160 161 public MultiViewHandlerDelegate getMultiViewHandlerDelegate() { 162 return peer.getMultiViewHandlerDelegate(); 164 } 165 166 169 public int getPersistenceType() { 170 return peer.getPersistenceType(); 171 } 172 173 protected String preferredID() { 174 return peer.preferredID(); 175 } 176 177 178 179 183 public void writeExternal (ObjectOutput out) throws IOException { 184 super.writeExternal(out); 185 peer.peerWriteExternal(out); 186 } 187 188 192 public void readExternal (ObjectInput in) throws IOException, ClassNotFoundException { 193 super.readExternal(in); 194 peer.peerReadExternal(in); 195 } 196 197 198 Action [] getDefaultTCActions() { 199 return super.getActions(); 200 } 201 202 public MultiViewElementCallback createElementCallback(MultiViewDescription desc) { 203 return SpiAccessor.DEFAULT.createCallback(new ActReqObserver(desc)); 204 } 205 206 public CloneableTopComponent getComponent() { 207 return this; 208 } 209 210 public javax.swing.JEditorPane getEditorPane() { 211 if (peer == null || peer.model == null) { 212 return null; 213 } 214 MultiViewElement paneEl = findPaneElement(); 215 if (paneEl != null) { 216 CloneableEditorSupport.Pane pane = (CloneableEditorSupport.Pane)paneEl.getVisualRepresentation(); 217 return pane.getEditorPane(); 218 } 219 return null; 222 } 223 224 private MultiViewElement findPaneElement() { 225 MultiViewElement el = peer.model.getActiveElement(false); 226 if (el != null && el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) { 227 return el; 228 } 229 Collection col = peer.model.getCreatedElements(); 232 Iterator it = col.iterator(); 233 while (it.hasNext()) { 234 el = (MultiViewElement)it.next(); 235 if (el.getVisualRepresentation() instanceof CloneableEditorSupport.Pane) { 236 return el; 239 } 240 } 241 return null; 244 } 245 246 public HelpCtx getHelpCtx() { 247 return peer.getHelpCtx(); 248 } 249 250 public String toString() { 251 return "MVCTC[name=" + getDisplayName() + ", peer=" + peer + "]"; } 253 254 261 public UndoRedo getUndoRedo() { 262 UndoRedo retValue; 263 retValue = peer.peerGetUndoRedo(); 264 if (retValue == null) { 265 retValue = super.getUndoRedo(); 266 } 267 return retValue; 268 } 269 270 274 public boolean canClose() { 275 return peer.canClose(); 276 } 277 278 public void updateName() { 280 if (peer != null) { 282 if (SwingUtilities.isEventDispatchThread() ) { 283 peer.updateName(); 284 } else { 285 SwingUtilities.invokeLater(new Runnable () { 286 public void run() { 287 peer.updateName(); 288 } 289 }); 290 } 291 } 292 } 293 294 297 public void ensureVisible() { 298 MultiViewElement paneEl = findPaneElement(); 299 if (paneEl != null) { 300 open(); 301 MultiViewElementCallback call = peer.getModel().getCallbackForElement(paneEl); 302 call.requestVisible(); 303 } 304 } 305 306 307 311 class ActReqObserver implements Serializable, MultiViewElementCallbackDelegate { 312 313 private static final long serialVersionUID =-3126744916624172415L; 314 private MultiViewDescription description; 315 316 ActReqObserver(MultiViewDescription desc) { 317 description = desc; 318 } 319 320 public void requestActive() { 321 boolean activated = peer.isActivated(); 322 if (!activated) { 323 MultiViewCloneableTopComponent.this.requestActive(); 324 } 325 if (peer.model.getActiveDescription() != description) { 326 if (activated) { 327 peer.model.getActiveElement().componentDeactivated(); 328 } 329 peer.tabs.changeActiveManually(description); 330 if (activated) { 331 peer.model.getActiveElement().componentActivated(); 332 } 333 } 334 335 } 336 337 public void requestVisible() { 338 peer.tabs.changeVisibleManually(description); 339 } 340 341 public Action [] createDefaultActions() { 342 return MultiViewCloneableTopComponent.this.getDefaultTCActions(); 343 } 344 345 public void updateTitle(String title) { 346 MultiViewCloneableTopComponent.this.setDisplayName(title); 347 } 348 349 350 public Object writeReplace() throws ObjectStreamException { 351 return null; 352 } 353 354 355 public Object readResolve() throws ObjectStreamException { 356 return null; 357 } 358 359 public boolean isSelectedElement() { 360 return (description.equals(peer.model.getActiveDescription())); 361 } 362 363 public TopComponent getTopComponent() { 364 return MultiViewCloneableTopComponent.this; 365 } 366 367 } 368 369 } 370 | Popular Tags |