source: trunk/org.modelica.mdt.ui/src/org/modelica/mdt/ui/ModelicaElementContentProvider.java @ 365

Last change on this file since 365 was 365, checked in by boris, 19 years ago
  • added a new IStandardLibrary package that acts as grand parent of all packages defined in standard library
  • removed dummy object for the standard library node in the modelica project view tree
  • added test for ui aspects of modelica elements and standard library
  • accidently renamed system library to standard library
File size: 7.9 KB
Line 
1/*
2 * This file is part of Modelica Development Tooling.
3 *
4 * Copyright (c) 2005, Link�pings universitet, Department of
5 * Computer and Information Science, PELAB
6 *
7 * All rights reserved.
8 *
9 * (The new BSD license, see also
10 * http://www.opensource.org/licenses/bsd-license.php)
11 *
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are
15 * met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 *   notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 *   notice, this list of conditions and the following disclaimer in
22 *   the documentation and/or other materials provided with the
23 *   distribution.
24 *
25 * * Neither the name of Link�pings universitet nor the names of its
26 *   contributors may be used to endorse or promote products derived from
27 *   this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
41
42package org.modelica.mdt.ui;
43
44import java.util.Collection;
45
46import org.eclipse.core.resources.IContainer;
47import org.eclipse.core.resources.IFolder;
48import org.eclipse.core.resources.IProject;
49import org.eclipse.core.runtime.CoreException;
50import org.eclipse.jface.viewers.ITreeContentProvider;
51import org.eclipse.jface.viewers.TreeViewer;
52import org.eclipse.jface.viewers.Viewer;
53import org.eclipse.swt.widgets.Control;
54import org.modelica.mdt.core.IModelicaElementChange;
55import org.modelica.mdt.core.IModelicaElementChangeListener;
56import org.modelica.mdt.core.IModelicaProject;
57import org.modelica.mdt.core.IModelicaRoot;
58import org.modelica.mdt.core.IParent;
59import org.modelica.mdt.core.ModelicaCore;
60import org.modelica.mdt.core.compiler.CompilerException;
61import org.modelica.mdt.internal.core.ErrorManager;
62import org.modelica.mdt.internal.core.CorePlugin;
63
64/**
65 * Content provider for a tree viewer. This content provider works only
66 * whith TreeViewer:s to keep things simple. If you want to use it for
67 * other viewers some addditional hacking is required on this class.
68 * 
69 * @author Elmir Jagudin
70 */
71public class ModelicaElementContentProvider 
72    implements ITreeContentProvider, IModelicaElementChangeListener
73{
74    private TreeViewer viewer;
75   
76    public ModelicaElementContentProvider()
77    {
78        /* we are interested in changes to modelica elements tree */
79        ModelicaCore.getModelicaRoot().addModelicaElementChangeListener(this);
80    }
81   
82    public Object[] getElements(Object inputElement)
83    {
84        if (inputElement instanceof IModelicaRoot)
85        {
86            return ((IModelicaRoot)inputElement).getProjects();
87        }
88       
89        ErrorManager.logBug(UIPlugin.getSymbolicName(),
90                "Elements of an object of unexpected type " + 
91                inputElement.getClass().getName() + " requested.");
92        return new Object[] {};
93    }
94   
95    public void dispose()
96    {
97    }
98
99    public void inputChanged(Viewer viewer, Object oldInput, Object newInput)
100    {
101        this.viewer = (TreeViewer)viewer;
102    }
103
104    public Object[] getChildren(Object parent)
105    {
106        if (parent instanceof IContainer)
107        {
108            try
109            {
110                return ((IContainer)parent).members();
111            }
112            catch (CoreException e)
113            {
114                ErrorManager.logError(e);
115            }
116        }
117        else if (parent instanceof IModelicaProject)
118        {
119            IModelicaProject modelicaProj = 
120                (IModelicaProject)parent;
121            if ( !modelicaProj.getWrappedProject().isOpen())
122            {
123                /* we have no children if we are closed */
124                return new Object[0];
125            }
126
127            Collection<?> list = null;
128            boolean hasModelicaNature = false;
129            try
130            {
131                list = 
132                    modelicaProj.getRootFolder().getChildren();
133               
134                hasModelicaNature = modelicaProj.getWrappedProject().
135                    getDescription().hasNature(CorePlugin.MODELICA_NATURE);
136            }
137            catch (CompilerException e)
138            {
139                /* on OMC error, show message and return empty children array */
140                ErrorManager.showCompilerError(e);
141                return new Object[0];
142            }
143            catch (CoreException e)
144            {
145                /* on core error, show message and return empty children array */
146                ErrorManager.showCoreError(e);
147                return new Object[0];
148            }
149
150            if (!hasModelicaNature)
151            {
152                /* don't add system library to non modelica projects */
153                return list.toArray();
154            }
155           
156            Object[] children = new Object[list.size()+1];
157            /*
158             * add as last element system library
159             */
160            list.toArray(children);
161            children[children.length-1] = 
162                ModelicaCore.getModelicaRoot().getStandardLibrary();
163
164            return children;           
165        }
166        else if (parent instanceof IParent)
167        {
168            try
169            {
170                return ((IParent)parent).getChildren().toArray();
171            }
172            catch (CompilerException e)
173            {
174                /* on OMC error, show message and return empty children array */
175                ErrorManager.showCompilerError(e);
176                return new Object[0];
177            }
178            catch (CoreException e)
179            {
180                /* on core error, show message and return empty children array */
181                ErrorManager.showCoreError(e);
182                return new Object[0];
183            }
184        }
185        return null;
186    }
187
188    public Object getParent(Object element)
189    {
190        return null;
191    }
192
193    public boolean hasChildren(Object element)
194    {
195        if (element instanceof IProject)
196        {
197            return ((IProject)element).isOpen();
198        }
199        else if (element instanceof IFolder)
200        {
201            return true;
202        }
203        else if (element instanceof IModelicaProject)
204        {
205            return ((IModelicaProject)element).getWrappedProject().isOpen();
206        }
207        else if (element instanceof IParent)
208        {
209            try 
210            {
211                return ((IParent)element).hasChildren();
212            } 
213            catch (CoreException e) 
214            {
215                /*
216                 * on Core error, show message and return that
217                 * there are no children
218                 */
219                ErrorManager.showCoreError(e);
220                return false;
221            }
222            catch (CompilerException e)
223            {
224                /*
225                 * on OMC error, show message and return that
226                 * there are no children
227                 */
228                ErrorManager.showCompilerError(e);
229                return false;
230            }
231        }
232        return false;
233    }
234
235    /*
236     * IModelicaElementChangeListener interface method
237     */
238    public void elementsChanged(final Collection<IModelicaElementChange> changes)
239    {
240        Control ctrl = viewer.getControl();
241
242        if (ctrl == null || ctrl.isDisposed())
243        {
244            return;
245        }
246               
247        if (ctrl.getDisplay().getThread() == Thread.currentThread()) 
248        {
249            handleChanges(changes);
250        }
251        else
252        {           
253            ctrl.getDisplay().asyncExec(new Runnable()
254            {
255                public void run() 
256                {
257                    /* Abort if this happens after disposes */
258                    Control ctrl = viewer.getControl();
259                    if (ctrl == null || ctrl.isDisposed())
260                    {
261                        return;
262                    }
263                    handleChanges(changes);
264                }
265            });
266        }
267    }
268
269    protected void handleChanges(Collection<IModelicaElementChange> changes)
270    {
271          for (IModelicaElementChange change : changes)
272          {
273              Object element = change.getElement();
274
275              switch (change.getChangeType())
276              {
277              case ADDED:
278                  viewer.add(change.getParent(), element);
279                  break;
280              case MODIFIED:
281                  viewer.update(element, null);
282                  break;
283              case REMOVED:
284                  viewer.remove(element);
285                  break;
286              case OPENED:
287              case CLOSED:
288                  viewer.refresh(element);
289                  break;
290              }
291          }
292    }
293}
Note: See TracBrowser for help on using the repository browser.