View Javadoc
1   /*
2    WSMO Studio - a Semantic Web Service Editor
3    Copyright (c) 2004-2006, OntoText Lab. / SIRMA Group
4    
5    This library is free software; you can redistribute it and/or modify it under
6    the terms of the GNU Lesser General Public License as published by the Free
7    Software Foundation; either version 2.1 of the License, or (at your option)
8    any later version.
9    This library is distributed in the hope that it will be useful, but WITHOUT
10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   details.
13   You should have received a copy of the GNU Lesser General Public License along
14   with this library; if not, write to the Free Software Foundation, Inc.,
15   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16   */
17  
18  /***
19   * <p>Title: WSMO Studio</p>
20   * <p>Description: Semantic Web Service Editor</p>
21   * <p>Copyright:  Copyright (c) 2004-2006</p>
22   * <p>Company: OntoText Lab. / SIRMA </p>
23   */
24  
25  package org.wsmostudio.ui.views.navigator;
26  
27  import org.eclipse.ui.*;
28  import org.wsmostudio.ui.Utils;
29  import org.wsmostudio.ui.editors.model.ObservableModel;
30  
31  /***
32   * A class which listens for certain events in the workbench and dynamically
33   * updates the content of the <i>WSMO Navigator</i> view if this is needed.
34   * Such events are: activating/closing an editor, modifications in the active 
35   * editor.
36   *
37   * @author not attributable
38   * @version $Revision: 1.9 $ $Date: 2006/01/09 12:51:14 $
39   */
40  
41  public class WSMONavigatorUpdater implements IPartListener, IPropertyListener {
42          
43      private WSMONavigator navigator;
44      
45      public WSMONavigatorUpdater(WSMONavigator navi) {
46          this.navigator = navi;
47      }
48      
49      public void registerInOpenedEditors() {
50          IWorkbenchPage page = PlatformUI.getWorkbench()
51                                    .getActiveWorkbenchWindow()
52                                        .getActivePage();
53          if (page == null) {
54              return;
55          }
56          IEditorReference[] editors = page.getEditorReferences();
57          if (editors == null) {
58              return;
59          }
60          for(int i = 0; i < editors.length; i++) {
61              IEditorPart editor = editors[i].getEditor(true);
62              if (editor != null) { 
63                  editor.addPropertyListener(this);
64              }
65          }
66      }
67      
68      public void unregisterFromOpenedEditors() {
69          IWorkbenchPage page = PlatformUI.getWorkbench()
70                                    .getActiveWorkbenchWindow()
71                                        .getActivePage();
72          if (page == null) {
73              return;
74          }
75          IEditorReference[] editors = page.getEditorReferences();
76          for(int i = 0; i < editors.length; i++) {
77              IEditorPart editor = editors[i].getEditor(true);
78              if (editor != null) { 
79                  editor.removePropertyListener(this);
80              }
81          }
82      }
83  
84      public void partActivated(IWorkbenchPart part) {
85          part.addPropertyListener(this);
86          notifyNavigator(part);
87      }
88      public void partBroughtToTop(IWorkbenchPart part) {
89          notifyNavigator(part);
90      }
91      public void partClosed(IWorkbenchPart part) {
92          part.removePropertyListener(this);
93          Utils.closeDependentEditors(part);
94          clearNavigator(part);
95      }
96      public void partDeactivated(IWorkbenchPart part) {
97      }
98      public void partOpened(IWorkbenchPart part) {
99          part.addPropertyListener(this);
100         notifyNavigator(part);
101     }
102     
103     public void propertyChanged(Object source, int propId) {
104         if (propId == IEditorPart.PROP_DIRTY) {
105             navigator.getTree().refresh(true);
106         }
107     }
108     
109 /*    private void closeDependentEditors(IWorkbenchPart part) {
110         if (false == part instanceof WSMOEditor) {
111             return;
112         }
113         IEditorInput input = ((WSMOEditor)part).getEditorInput();
114         if (EditorsDependencyManager.getMasterInput(input) != null) { //i.e. this editor is mastered
115             EditorsDependencyManager.unregisterEditorDependency(input);
116         }
117         else { // i.e. this editor masters others
118             Set dependentEInputs = EditorsDependencyManager.getDependentEditors(input);
119             IWorkbenchPage page = PlatformUI.getWorkbench()
120                                       .getActiveWorkbenchWindow()
121                                           .getActivePage();
122             if (page == null) {
123                 return;
124             }
125             for (Iterator it = dependentEInputs.iterator(); it.hasNext();) {
126                 IEditorPart dependentEditor = page.findEditor((IEditorInput)it.next());
127                 if (dependentEditor != null) {
128                     page.closeEditor(dependentEditor, false);
129                 }
130             }
131         }
132     }*/
133     
134     private void notifyNavigator(IWorkbenchPart part) {
135         if (false == part instanceof IEditorPart) {
136             return;
137         }
138         ObservableModel tmpModel = (ObservableModel)((IEditorPart)part)
139                                    .getEditorInput()
140                                        .getAdapter(ObservableModel.class);
141         if (tmpModel == null) {
142             return;
143         }
144         while (tmpModel.getMasterModel() != null) {
145             tmpModel = tmpModel.getMasterModel();
146         }
147         
148         IEditorInput newInput = Utils.findInputForModel(tmpModel);
149         if (newInput != null 
150                 && false == newInput.equals(this.navigator.getWsmoInput())) {
151             this.navigator.setWsmoInput(newInput);
152         }
153     }
154 
155     private void clearNavigator(IWorkbenchPart part) {
156         if (false == part instanceof IEditorPart) {
157             return;
158         }
159         IEditorInput input = ((IEditorPart)part).getEditorInput();
160         if (input != null 
161                 && input.equals(this.navigator.getWsmoInput())) {
162             this.navigator.setWsmoInput(null);
163         }
164     }
165 }
166 
167 /*
168  * $Log: WSMONavigatorUpdater.java,v $
169  * Revision 1.9  2006/01/09 12:51:14  alex_simov
170  * Copyright message in header updated
171  *
172  * Revision 1.8  2006/01/06 14:43:22  alex_simov
173  * fix: unused restriction removed
174  *
175  * Revision 1.7  2005/11/09 16:14:34  alex_simov
176  * UIModels notification improved
177  *
178  * Revision 1.6  2005/09/21 07:57:23  alex_simov
179  * fix in navigator's input reloading
180  *
181  * Revision 1.5  2005/08/04 15:00:55  alex_simov
182  * editor <-> subeditor dependency links established
183  *
184  * Revision 1.4  2005/07/29 15:08:03  alex_simov
185  * added javadoc: class description, footer
186  *
187  *
188  */