View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics
3    * and Informatics, Comenius University, Bratislava. This file is protected by
4    * the Mozilla Public License version 1.1 (the License); you may not use this
5    * file except in compliance with the License. You may obtain a copy of the
6    * License at http://euromath2.sourceforge.net/license.html Unless required by
7    * applicable law or agreed to in writing, software distributed under the
8    * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
9    * OF ANY KIND, either express or implied. See the License for the specific
10   * language governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.plugin.views.outline;
13  
14  import java.io.InputStream;
15  
16  import org.eclipse.jface.resource.ImageRegistry;
17  import org.eclipse.jface.viewers.LabelProvider;
18  import org.eclipse.swt.graphics.Image;
19  import org.eclipse.swt.graphics.ImageData;
20  
21  import sk.uniba.euromath.editor.EditorSite;
22  import sk.uniba.euromath.plugin.views.outline.items.OutlineItem;
23  
24  /***
25   * Provides images and labels for outline items. Every node type has different
26   * image and label
27   * 
28   * @author Tomáš Studva 2.11.2005
29   */
30  
31  class OutlineItemsLabelProvider extends LabelProvider {
32  
33      /***
34       * Image registry for image labels.
35       */
36      protected ImageRegistry registry;
37      
38      /***
39       * Editor site instance.
40       */
41      private EditorSite editorSite;
42  
43      /***
44       * Inverted images flag.
45       */
46      protected boolean inverted = true;
47  
48      /***
49       * Constructor.
50       * @param site 
51       * 
52       * @param xmlAccess
53       */
54      public OutlineItemsLabelProvider(EditorSite site) {
55          super();
56          this.editorSite = site;
57          this.registry = new ImageRegistry();
58          createImages();
59      }
60  
61      /***
62       * Adds image to registry.
63       * 
64       * @param file
65       * @param key
66       *            under which will be registered
67       */
68      private void addImage(String file, String key) {
69          InputStream stream = OutlineItemsLabelProvider.class
70                  .getResourceAsStream(file);
71          ImageData data = new ImageData(stream);
72          this.registry.put(key,
73                  new Image(null, data, data.getTransparencyMask()));
74  
75      }
76  
77      /***
78       * Creates needed images by this provider.
79       */
80      private void createImages() {
81          addImage(OutlineConsts.ATTR_FILE, OutlineConsts.ATTR_KEY);
82          addImage(OutlineConsts.ATTR_FILE_INV, OutlineConsts.ATTR_KEY_INV);
83          addImage(OutlineConsts.CDAT_FILE, OutlineConsts.CDAT_KEY);
84          addImage(OutlineConsts.CDAT_FILE_INV, OutlineConsts.CDAT_KEY_INV);
85          addImage(OutlineConsts.COMM_FILE, OutlineConsts.COMM_KEY);
86          addImage(OutlineConsts.COMM_FILE_INV, OutlineConsts.COMM_KEY_INV);
87          addImage(OutlineConsts.ELEM_FILE, OutlineConsts.ELEM_KEY);
88          addImage(OutlineConsts.ELEM_FILE_INV, OutlineConsts.ELEM_KEY_INV);
89          addImage(OutlineConsts.ENTI_FILE, OutlineConsts.ENTI_KEY);
90          addImage(OutlineConsts.ENTI_FILE_INV, OutlineConsts.ENTI_KEY_INV);
91          addImage(OutlineConsts.INST_FILE, OutlineConsts.INST_KEY);
92          addImage(OutlineConsts.INST_FILE_INV, OutlineConsts.INST_KEY_INV);
93          addImage(OutlineConsts.TEXT_FILE, OutlineConsts.TEXT_KEY);
94          addImage(OutlineConsts.TEXT_FILE_INV, OutlineConsts.TEXT_KEY_INV);
95      }
96  
97      /*
98       * (non-Javadoc)
99       * 
100      * @see org.eclipse.jface.viewers.IBaseLabelProvider#dispose()
101      */
102     @Override
103     public void dispose() {
104         this.registry = null;
105         super.dispose();
106     }
107 
108     /***
109      * Returns image for obj. Depends on image key and inverted flag. Images of
110      * not visualized nodes have inverted image.
111      * 
112      * @param obj
113      *            must be instance of outline item
114      */
115     @Override
116     public Image getImage(Object obj) {
117         assert (obj instanceof OutlineItem);
118 
119         OutlineItem item = (OutlineItem) obj;
120         String imageKey = item.getImageKey();
121         if (imageKey == null)
122             return null;
123 
124         if (this.inverted) {
125             String id = this.editorSite.getXMLAccess().getIDManager().getIDNull(item.getModel());
126             // not visualized id(node) in any editor, has inverted image
127             if (!this.editorSite.getVisualizedIds().contains(id)) {
128                 imageKey += OutlineConsts.KEY_INVERTED;
129             }
130         }
131         return this.registry.get(imageKey);
132     }
133 
134     /***
135      * Getter for image registry.
136      * 
137      * @return image registry holding images registered under keys from
138      *         OutlineConsts.
139      */
140     public ImageRegistry getImageRegistry() {
141         return this.registry;
142     }
143 
144     /***
145      * Returns text, label for object.
146      */
147     @Override
148     public String getText(Object obj) {
149         return obj.toString();
150     }
151 
152     /***
153      * @param b
154      */
155     public void setWithInvertedImages(boolean b) {
156         this.inverted = b;
157     }
158 
159 }