KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > multiview > MultiViewPeer


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.multiview;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInput JavaDoc;
26 import java.io.ObjectOutput JavaDoc;
27 import java.io.Serializable JavaDoc;
28 import java.lang.reflect.InvocationHandler JavaDoc;
29 import java.lang.reflect.InvocationTargetException JavaDoc;
30 import java.lang.reflect.Method JavaDoc;
31 import java.lang.reflect.Proxy JavaDoc;
32 import java.util.*;
33 import java.util.logging.Level JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35 import javax.swing.*;
36 import javax.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38 import javax.swing.undo.CannotRedoException JavaDoc;
39 import javax.swing.undo.CannotUndoException JavaDoc;
40 import org.netbeans.core.api.multiview.MultiViewPerspective;
41 import org.netbeans.core.multiview.MultiViewModel.ActionRequestObserverFactory;
42 import org.netbeans.core.multiview.MultiViewModel.ElementSelectionListener;
43 import org.netbeans.core.spi.multiview.CloseOperationHandler;
44 import org.netbeans.core.spi.multiview.CloseOperationState;
45 import org.netbeans.core.spi.multiview.MultiViewDescription;
46 import org.netbeans.core.spi.multiview.MultiViewElement;
47 import org.openide.awt.UndoRedo;
48 import org.openide.text.CloneableEditorSupport.Pane;
49 import org.openide.util.HelpCtx;
50 import org.openide.util.Lookup;
51 import org.openide.util.SharedClassObject;
52 import org.openide.windows.TopComponent;
53
54 /** Special subclass of TopComponent which shows and handles set of
55  * MultiViewElements, shows them in switchable toggle buttons style, along
56  * with toolbsrs af actions asociated with individual view elements.
57  *
58  *
59  * @author Dafe Simonek, Milos Kleint
60  */

61
62
63 public final class MultiViewPeer {
64
65     static final String JavaDoc MULTIVIEW_ID = "MultiView-"; //NOI18N
66

67     MultiViewModel model;
68     TabsComponent tabs;
69     SelectionListener selListener;
70     CloseOperationHandler closeHandler;
71     transient MultiViewTopComponentLookup lookup;
72     TopComponent peer;
73     private ActionRequestObserverFactory factory;
74     private MultiViewActionMap delegatingMap;
75     private boolean activated = false;
76     private Object JavaDoc editorSettingsListener;
77     private DelegateUndoRedo delegateUndoRedo;
78     
79     public MultiViewPeer(TopComponent pr, ActionRequestObserverFactory fact) {
80         selListener = new SelectionListener();
81         peer = pr;
82         factory = fact;
83         editorSettingsListener = createEditorListener();
84         delegateUndoRedo = new DelegateUndoRedo();
85     }
86     
87  
88     
89     
90     public void setMultiViewDescriptions(MultiViewDescription[] descriptions, MultiViewDescription defaultDesc) {
91         if (model != null) {
92             model.removeElementSelectionListener(selListener);
93         }
94         model = new MultiViewModel(descriptions, defaultDesc, factory);
95         model.addElementSelectionListener(selListener);
96         tabs.setModel(model);
97     }
98     
99     public void setCloseOperationHandler(CloseOperationHandler handler) {
100         closeHandler = handler;
101     }
102     
103     void setDeserializedMultiViewDescriptions(MultiViewDescription[] descriptions,
104                                                       MultiViewDescription defaultDesc, Map existingElements) {
105         if (model != null) {
106             model.removeElementSelectionListener(selListener);
107         }
108         model = new MultiViewModel(descriptions, defaultDesc, factory, existingElements);
109         model.addElementSelectionListener(selListener);
110         tabs.setModel(model);
111     }
112     
113     /**
114      * for use in tests only!!!!!
115      */

116     MultiViewModel getModel() {
117         return model;
118     }
119     
120     
121     void initComponents() {
122         initActionMap();
123         peer.setLayout(new BorderLayout JavaDoc());
124         tabs = new TabsComponent(isToolbarVisible());
125         peer.add(tabs);
126         ActionMap map = peer.getActionMap();
127         Action act = new AccessTogglesAction();
128         map.put("NextViewAction", new GetRightEditorAction()); //NOI18N
129
map.put("PreviousViewAction", new GetLeftEditorAction()); //NOI18N
130
map.put("accesstoggles", act); //NOI18N
131
InputMap input = peer.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
132         KeyStroke stroke = KeyStroke.getKeyStroke("control F10"); //NOI18N
133
input.put(stroke, "accesstoggles"); //NOI18N
134
// stroke = (KeyStroke)new GetLeftEditorAction().getValue(Action.ACCELERATOR_KEY);
135
// input.put(stroke, "getLeftEditor");
136
input = peer.getInputMap(JComponent.WHEN_FOCUSED);
137         input.put(stroke, "accesstoggles"); //NOI18N
138

139         peer.putClientProperty("MultiViewBorderHack.topOffset", new Integer JavaDoc(tabs.getPreferredSize().height - 1));
140     }
141     
142   // It is necessary so the old actions (clone and close from org.openide.actions package) remain working.
143
// cannot use the
144
private void initActionMap() {
145         delegatingMap = new MultiViewActionMap(peer, new ActionMap ());
146         if(peer instanceof TopComponent.Cloneable) {
147             delegatingMap.put("cloneWindow", new javax.swing.AbstractAction JavaDoc() { // NOI18N
148
public void actionPerformed(ActionEvent JavaDoc evt) {
149                     TopComponent cloned = ((TopComponent.Cloneable)
150                         peer).cloneComponent();
151                     cloned.open();
152                     cloned.requestActive();
153                 }
154             });
155         }
156         delegatingMap.put("closeWindow", new javax.swing.AbstractAction JavaDoc() { // NOI18N
157
public void actionPerformed(ActionEvent JavaDoc evt) {
158                peer.close();
159            }
160         });
161         peer.setActionMap(delegatingMap);
162     }
163     
164     void peerComponentClosed() {
165         Iterator it = model.getCreatedElements().iterator();
166         while (it.hasNext()) {
167             MultiViewElement el = (MultiViewElement)it.next();
168             model.markAsHidden(el);
169             el.componentClosed();
170         }
171
172     }
173     
174     void peerComponentShowing() {
175         MultiViewElement el = model.getActiveElement();
176         el.componentShowing();
177         delegatingMap.setDelegateMap(el.getVisualRepresentation().getActionMap());
178         ((MultiViewTopComponentLookup)peer.getLookup()).setElementLookup(el.getLookup());
179         JComponent jc = el.getToolbarRepresentation();
180         jc.setOpaque(false);
181         tabs.setInnerToolBar(jc);
182         tabs.setToolbarBarVisible(isToolbarVisible());
183         if (editorSettingsListener != null) {
184             addEditorListener(editorSettingsListener);
185         }
186     }
187     
188     void peerComponentHidden() {
189         model.getActiveElement().componentHidden();
190         if (editorSettingsListener != null) {
191             removeEditorListener(editorSettingsListener);
192         }
193     }
194     
195     void peerComponentDeactivated() {
196         activated = false;
197         model.getActiveElement().componentDeactivated();
198     }
199     
200     boolean isActivated() {
201         return activated;
202     }
203     
204     void peerComponentActivated() {
205         activated = true;
206         model.getActiveElement().componentActivated();
207     }
208     
209     void peerComponentOpened() {
210         showCurrentElement(true);
211         tabs.setToolbarBarVisible(isToolbarVisible());
212     }
213     
214     boolean requestFocusInWindow() {
215         return model.getActiveElement().getVisualRepresentation().requestFocusInWindow();
216     }
217     
218     void requestFocus() {
219         model.getActiveElement().getVisualRepresentation().requestFocus();
220     }
221     
222     /**
223      * hides the old element when switching elements.
224      */

225     void hideElement(MultiViewDescription desc) {
226         if (desc != null) {
227             MultiViewElement el = model.getElementForDescription(desc);
228             el.componentHidden();
229         }
230     }
231
232     
233     void showCurrentElement() {
234         showCurrentElement(false);
235     }
236     
237     /**
238      * shows the new element after switching elements.
239      */

240     
241     private void showCurrentElement(boolean calledFromComponentOpened) {
242         MultiViewElement el = model.getActiveElement();
243         MultiViewDescription desc = model.getActiveDescription();
244
245         // TODO display name is not a good unique id..
246
// also consider a usecase where multiple elements point to a single visual component.
247
//. eg. property sheet uses same component and only changes model.
248
// in this case we probably should not remove and add the component from awt hierarchy
249
tabs.switchToCard(el, desc.getDisplayName());
250         peer.setIcon(desc.getIcon());
251         // the first time the component is shown, we need to call componentOpened() on it to be in synch with current
252
// TopComponent behaviour?
253
if (peer.isOpened() || calledFromComponentOpened) {
254             if (!model.wasShownBefore(el)) {
255                 el.componentOpened();
256                 model.markAsShown(el);
257             }
258         }
259         if (!calledFromComponentOpened) {
260             //#68199
261
// replace isOpened() with isVisible() because some multiview don't have to be directly referenced form the
262
// winsys codebase
263
if (peer.isVisible()) {
264                 el.componentShowing();
265             }
266         // should we really set the stuff only when not called from componentOpened()? maybe it's better to call it twice sometimes.
267
// if we don't call it here for opened but not showing component, then the map, lookup and nodes will not be initialized properly.
268
// is it a problem?
269
delegatingMap.setDelegateMap(el.getVisualRepresentation().getActionMap());
270             ((MultiViewTopComponentLookup)peer.getLookup()).setElementLookup(el.getLookup());
271             
272             if (peer.isVisible()) {
273                 tabs.setInnerToolBar(el.getToolbarRepresentation());
274                 tabs.setToolbarBarVisible(isToolbarVisible());
275             }
276             
277         }
278     }
279     
280     
281     
282     /**
283      * merge action for the topcomponent and the enclosed MultiViewElement..
284      *
285      */

286     Action[] peerGetActions(Action[] superActions) {
287         //TEMP don't delegate to element's actions..
288
Action[] acts = model.getActiveElement().getActions();
289         for (int i = 0; i < acts.length; i++) {
290             Action act = acts[i];
291             for (int j = 0 ; j < superActions.length; j++) {
292                 Action superact = superActions[j];
293                 if (superact != null && act != null && superact.getClass().equals(act.getClass())) {
294                     // these are the default topcomponent actions.. we need to replace them
295
// in order to have the correct context.
296
acts[i] = superActions[j];
297                     break;
298                 }
299             }
300         }
301         return acts;
302     }
303     
304     MultiViewHandlerDelegate getMultiViewHandlerDelegate() {
305         // TODO have one handler only or create a new one each time?
306
return new MVTCHandler();
307     }
308     
309     /**
310      * Delegates the value to the element descriptions.
311      */

312     int getPersistenceType() {
313         
314         // should also take the opened/created elements into account.
315
// no need to serialize the tc when the element that want to be serialized, was not
316
// even opened?!? but maybe handle this during the serialization proceess, avoid creating
317
// the element when serializing.
318
MultiViewDescription[] descs = model.getDescriptions();
319         int type = TopComponent.PERSISTENCE_NEVER;
320         for (int i = 0; i < descs.length; i++) {
321             if (!(descs[i] instanceof Serializable JavaDoc)) {
322                 Logger.getLogger(MultiViewTopComponent.class.getName()).warning(
323                         "The MultiviewDescription instance " + descs[i].getClass() + " is not serializable. Cannot persist TopComponent.");
324                 type = TopComponent.PERSISTENCE_NEVER;
325                 break;
326             }
327             if (descs[i].getPersistenceType() == TopComponent.PERSISTENCE_ALWAYS) {
328                 type = descs[i].getPersistenceType();
329                 // cannot ge any better than that.
330
}
331             if (descs[i].getPersistenceType() == TopComponent.PERSISTENCE_ONLY_OPENED &&
332                  type != TopComponent.PERSISTENCE_ALWAYS) {
333                 type = descs[i].getPersistenceType();
334                 // go on searching..
335
}
336         
337         }
338         return type;
339     }
340     
341     String JavaDoc preferredID() {
342         StringBuffer JavaDoc retValue = new StringBuffer JavaDoc(MULTIVIEW_ID);
343         MultiViewDescription[] descs = model.getDescriptions();
344         for (int i = 0; i < descs.length; i++) {
345             retValue.append(descs[i].preferredID());
346             retValue.append("|"); //NOI18N
347
}
348         return retValue.toString();
349     }
350     
351     
352     
353     /** Serialize this top component.
354     * Subclasses wishing to store state must call the super method, then write to the stream.
355     * @param out the stream to serialize to
356     */

357     void peerWriteExternal (ObjectOutput JavaDoc out) throws IOException JavaDoc {
358         if (closeHandler != null) {
359             if (closeHandler instanceof Serializable JavaDoc) {
360                 out.writeObject(closeHandler);
361             } else {
362                 //TODO some warning to the SPI programmer
363
Logger.getAnonymousLogger().info(
364                        "The CloseOperationHandler isn not serializable. MultiView component id=" + preferredID());
365             }
366         }
367         MultiViewDescription[] descs = model.getDescriptions();
368         MultiViewDescription curr = model.getActiveDescription();
369         int currIndex = 0;
370         for (int i = 0; i < descs.length; i++) {
371             out.writeObject(descs[i]);
372             if (descs[i].getPersistenceType() != TopComponent.PERSISTENCE_NEVER) {
373                 // only those requeTopsted and previously created elements are serialized.
374
MultiViewElement elem = model.getElementForDescription(descs[i], false);
375                 if (elem != null && elem instanceof Serializable JavaDoc) {
376                     out.writeObject(elem);
377                 }
378             }
379             if (descs[i] == curr) {
380                 currIndex = i;
381             }
382         }
383         out.writeObject(new Integer JavaDoc(currIndex));
384         
385     }
386
387     /** Deserialize this top component.
388     * Subclasses wishing to store state must call the super method, then read from the stream.
389     * @param in the stream to deserialize from
390     */

391     void peerReadExternal (ObjectInput JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
392         ArrayList descList = new ArrayList();
393         HashMap map = new HashMap();
394         int current = 0;
395         CloseOperationHandler close = null;
396         while (true) {
397             Object JavaDoc obj = in.readObject();
398             if (obj instanceof MultiViewDescription) {
399                 descList.add(obj);
400             }
401             else if (obj instanceof MultiViewElement) {
402                 map.put(descList.get(descList.size() - 1), obj);
403             }
404             else if (obj instanceof Integer JavaDoc) {
405                 Integer JavaDoc integ = (Integer JavaDoc)obj;
406                 current = integ.intValue();
407                 break;
408             }
409             if (obj instanceof CloseOperationHandler) {
410                 close = (CloseOperationHandler)obj;
411             }
412         }
413         if (close == null) {
414                 //TODO some warning to the SPI programmer
415
close = SpiAccessor.DEFAULT.createDefaultCloseHandler();
416         }
417         setCloseOperationHandler(close);
418         // now that we've read everything, we should set it correctly.
419
MultiViewDescription[] descs = new MultiViewDescription[descList.size()];
420         descs = (MultiViewDescription[])descList.toArray(descs);
421         MultiViewDescription currDesc = descs[current];
422         setDeserializedMultiViewDescriptions(descs, currDesc, map);
423     }
424     
425     
426     private Action[] getDefaultTCActions() {
427         //TODO for each suppoerted peer have one entry..
428
if (peer instanceof MultiViewTopComponent) {
429             return ((MultiViewTopComponent)peer).getDefaultTCActions();
430         }
431         return new Action[0];
432     }
433     
434
435     
436     JEditorPane getEditorPane() {
437         if (model != null) {
438             MultiViewElement el = model.getActiveElement();
439             if (el != null && el.getVisualRepresentation() instanceof Pane) {
440                 Pane pane = (Pane)el.getVisualRepresentation();
441                 return pane.getEditorPane();
442             }
443         }
444         return null;
445     }
446     
447     HelpCtx getHelpCtx() {
448         return model.getActiveDescription().getHelpCtx();
449     }
450     
451     /**
452      * Get the undo/redo support for this component.
453      * The default implementation returns a dummy support that cannot
454      * undo anything.
455      *
456      * @return undoable edit for this component
457      */

458     UndoRedo peerGetUndoRedo() {
459         return delegateUndoRedo;
460     }
461     
462     private UndoRedo privateGetUndoRedo() {
463         return model.getActiveElement().getUndoRedo() != null ? model.getActiveElement().getUndoRedo() : UndoRedo.NONE;
464     }
465     
466     /**
467      * This method is called when this <code>TopComponent</code> is about to close.
468      * Delegates to CloseOperationHandler.
469      */

470     boolean canClose() {
471         Collection col = model.getCreatedElements();
472         Iterator it = col.iterator();
473         Collection badOnes = new ArrayList();
474         while (it.hasNext()) {
475            MultiViewElement el = (MultiViewElement)it.next();
476            CloseOperationState state = el.canCloseElement();
477            if (!state.canClose()) {
478                badOnes.add(state);
479            }
480         }
481         if (badOnes.size() > 0) {
482             CloseOperationState[] states = new CloseOperationState[badOnes.size()];
483             states = (CloseOperationState[])badOnes.toArray(states);
484             return closeHandler.resolveCloseOperation(states);
485         }
486         return true;
487     }
488     
489     // from CloneableEditor.Pane
490
public void updateName() {
491         // is called before setMultiViewDescriptions() need to check for null.
492
if (model != null) {
493             MultiViewElement el = model.getActiveElement();
494             if (el.getVisualRepresentation() instanceof Pane) {
495                 Pane pane = (Pane)el.getVisualRepresentation();
496                 pane.updateName();
497                 peer.setDisplayName(pane.getComponent().getDisplayName());
498             }
499         }
500         //TODO
501
}
502     
503     public Lookup getLookup() {
504         if (lookup == null) {
505             lookup = new MultiViewTopComponentLookup(delegatingMap);
506         }
507         return lookup;
508     }
509     
510     
511 //-------------------------------------------------------------------------------
512
//---------------------------------------------------------------------------
513
//--------------- editor reflection stuff to retrieve the toolbar visibility setting
514
//----------------------------------------------------------------------------------
515
void addEditorListener(Object JavaDoc listener) {
516         try {
517             final ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
518             Class JavaDoc settingsClass = Class.forName(
519                     "org.netbeans.editor.Settings", false, loader); //NOI18N
520
Class JavaDoc listenerClass = Class.forName(
521                     "org.netbeans.editor.SettingsChangeListener", false, loader); //NOI18N
522
Method JavaDoc addSettingsListener = settingsClass.getMethod(
523                     "addSettingsChangeListener",new Class JavaDoc[ ] { listenerClass });//NOI18N
524
addSettingsListener.invoke(settingsClass, new Object JavaDoc[] { listener });
525         } catch (Throwable JavaDoc t) {
526             t.printStackTrace();
527         }
528     }
529     
530     void removeEditorListener(Object JavaDoc listener) {
531         try {
532             final ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
533             Class JavaDoc settingsClass = Class.forName(
534                     "org.netbeans.editor.Settings", false, loader); //NOI18N
535
Class JavaDoc listenerClass = Class.forName(
536                     "org.netbeans.editor.SettingsChangeListener", false, loader); //NOI18N
537
Method JavaDoc addSettingsListener = settingsClass.getMethod(
538                     "removeSettingsChangeListener",new Class JavaDoc[ ] { listenerClass });//NOI18N
539
addSettingsListener.invoke(settingsClass, new Object JavaDoc[] { listener });
540         } catch (Throwable JavaDoc t) {
541             t.printStackTrace();
542         }
543     }
544     
545     Object JavaDoc createEditorListener() {
546         try {
547             final ClassLoader JavaDoc loader = (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
548             Class JavaDoc listenerClass;
549             try {
550                 listenerClass = Class.forName("org.netbeans.editor.SettingsChangeListener", false, loader); //NOI18N
551
} catch (ClassNotFoundException JavaDoc ex) {
552                 Logger.getLogger(MultiViewPeer.class.getName()).log(Level.CONFIG, "Disabling interaction with editor/lib", ex); // NOI18N
553
return null;
554             }
555             InvocationHandler JavaDoc ih = new InvocationHandler JavaDoc() {
556                 public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args) {
557                     SwingUtilities.invokeLater(new Runnable JavaDoc() {
558                         public void run() {
559                             tabs.setToolbarBarVisible(isToolbarVisible());
560                         }
561                     });
562                     return null;
563                 }
564             };
565             return Proxy.newProxyInstance(loader,
566                     new Class JavaDoc[] { listenerClass }, ih);
567         } catch (Throwable JavaDoc t) {
568             Logger.getLogger(MultiViewPeer.class.getName()).log(Level.WARNING, null, t);
569         }
570         return null;
571     }
572     
573     boolean isToolbarVisible() {
574         //TODO need some way to restrict the validity of this swicth only to multiviews that contain
575
// sources in some form..
576
JEditorPane pane = getEditorPane();
577         if (pane != null) {
578               Object JavaDoc obj = pane.getActionMap().get("toggle-toolbar");
579               if (obj == null) {
580                   return true;
581               }
582         } else {
583             return true;
584         }
585         SharedClassObject option = null;
586         ClassLoader JavaDoc loader = (ClassLoader JavaDoc) Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
587         if (loader == null) {
588             loader = MultiViewPeer.class.getClassLoader().getSystemClassLoader();
589         }
590         try {
591             Class JavaDoc editorBaseOption = Class.forName("org.netbeans.modules.editor.options.BaseOptions", true,
592                     loader);
593             option = SharedClassObject.findObject(editorBaseOption);
594         } catch (ClassNotFoundException JavaDoc ex) {
595             ex.printStackTrace();
596         }
597         if (option != null) {
598             try {
599                 Method JavaDoc is = option.getClass().getMethod("isToolbarVisible", new Class JavaDoc[0]);
600                 Object JavaDoc ret;
601                 ret = is.invoke(option, new Object JavaDoc[0]);
602                 if (ret instanceof Boolean JavaDoc) {
603                     return ((Boolean JavaDoc)ret).booleanValue();
604                 }
605             } catch (IllegalArgumentException JavaDoc ex) {
606                 ex.printStackTrace();
607             } catch (SecurityException JavaDoc ex) {
608                 ex.printStackTrace();
609             } catch (InvocationTargetException JavaDoc ex) {
610                 ex.printStackTrace();
611             } catch (NoSuchMethodException JavaDoc ex) {
612                 ex.printStackTrace();
613             } catch (IllegalAccessException JavaDoc ex) {
614                 ex.printStackTrace();
615             }
616         }
617         return true;
618     }
619
620     
621     public String JavaDoc toString() {
622         return "[model=" + model + "]"; // NOI18N
623
}
624     /**
625      * notification from the model that the selection changed.
626      */

627     private class SelectionListener implements ElementSelectionListener {
628         
629         public void selectionChanged(MultiViewDescription oldOne, MultiViewDescription newOne) {
630             hideElement(oldOne);
631             showCurrentElement();
632             delegateUndoRedo.updateListeners(model.getElementForDescription(oldOne),
633                                              model.getElementForDescription(newOne));
634         }
635         
636         public void selectionActivatedByButton() {
637             MultiViewElement elem = model.getActiveElement();
638             elem.getVisualRepresentation().requestFocus();
639             elem.componentActivated();
640         }
641         
642     }
643     
644     private class MVTCHandler implements MultiViewHandlerDelegate {
645         private MultiViewPerspective[] perspectives = null;
646         
647         public MultiViewPerspective[] getDescriptions() {
648             return model.getPerspectives();
649         }
650         
651         public MultiViewPerspective getSelectedDescription() {
652             return model.getSelectedPerspective();
653         }
654         
655         public void requestActive(MultiViewPerspective pers) {
656             MultiViewDescription desc = Accessor.DEFAULT.extractDescription(pers);
657             if (model.getActiveDescription() != desc) {
658                 model.getActiveElement().componentDeactivated();
659                 tabs.changeActiveManually(desc);
660                 model.getActiveElement().componentActivated();
661             }
662         }
663         
664         public void requestVisible(MultiViewPerspective pers) {
665             MultiViewDescription desc = Accessor.DEFAULT.extractDescription(pers);
666             tabs.changeVisibleManually(desc);
667         }
668         
669 // public MultiViewPerspectiveComponent getElementForDescription(MultiViewPerspective pers) {
670
// MultiViewDescription desc = Accessor.DEFAULT.extractDescription(pers);
671
// return model.getMVComponentForDescription(desc);
672
// }
673

674         
675     }
676     
677
678     private class AccessTogglesAction extends AbstractAction {
679         
680         AccessTogglesAction() {
681 // putValue(Action.NAME, "AccessToggleMenu");
682
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("control F10")); //NOI18N
683
}
684         
685         public void actionPerformed(ActionEvent JavaDoc e) {
686             tabs.requestFocusForSelectedButton();
687             
688         }
689     }
690     
691     private class DelegateUndoRedo implements UndoRedo {
692         
693         private List listeners = new ArrayList();
694         
695         public boolean canUndo() {
696             return privateGetUndoRedo().canUndo();
697         }
698
699         public boolean canRedo() {
700             return privateGetUndoRedo().canRedo();
701         }
702
703         public void undo() throws CannotUndoException JavaDoc {
704             privateGetUndoRedo().undo();
705         }
706
707         public void redo() throws CannotRedoException JavaDoc {
708             privateGetUndoRedo().redo();
709         }
710
711         public void addChangeListener(ChangeListener JavaDoc l) {
712             listeners.add(l);
713             privateGetUndoRedo().addChangeListener(l);
714         }
715
716         public void removeChangeListener(ChangeListener JavaDoc l) {
717             listeners.remove(l);
718             privateGetUndoRedo().removeChangeListener(l);
719         }
720
721         public String JavaDoc getUndoPresentationName() {
722             return privateGetUndoRedo().getUndoPresentationName();
723         }
724
725         public String JavaDoc getRedoPresentationName() {
726             return privateGetUndoRedo().getRedoPresentationName();
727         }
728         
729         private void fireElementChange() {
730             Iterator it = new ArrayList(listeners).iterator();
731             while (it.hasNext()) {
732                 ChangeListener JavaDoc elem = (ChangeListener JavaDoc) it.next();
733                 ChangeEvent JavaDoc event = new ChangeEvent JavaDoc(this);
734                 elem.stateChanged(event);
735             }
736             
737         }
738         
739         void updateListeners(MultiViewElement old, MultiViewElement fresh) {
740             Iterator it = listeners.iterator();
741             while (it.hasNext()) {
742                 ChangeListener JavaDoc elem = (ChangeListener JavaDoc) it.next();
743                 if (old.getUndoRedo() != null) {
744                     old.getUndoRedo().removeChangeListener(elem);
745                 }
746                 if (fresh.getUndoRedo() != null) {
747                     fresh.getUndoRedo().addChangeListener(elem);
748                 }
749             }
750             fireElementChange();
751         }
752         
753     }
754     
755 }
Popular Tags