Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toc / TocItemBranch.java @ 11548

History | View | Annotate | Download (6 KB)

1
/*
2
 * Created on 03-dic-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Generation - Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.cit.gvsig.project.documents.view.toc;
48

    
49
import java.awt.Dimension;
50
import java.awt.Graphics2D;
51
import java.awt.datatransfer.DataFlavor;
52
import java.awt.datatransfer.UnsupportedFlavorException;
53
import java.awt.image.BufferedImage;
54
import java.io.File;
55
import java.io.IOException;
56
import java.net.URL;
57

    
58
import javax.swing.Icon;
59
import javax.swing.ImageIcon;
60

    
61
import com.iver.andami.PluginServices;
62
import com.iver.cit.gvsig.fmap.layers.FLayer;
63

    
64
/**
65
 * @author FJP
66
 *
67
 * TODO To change the template for this generated type comment go to
68
 * Window - Preferences - Java - Code Generation - Code and Comments
69
 */
70
public class TocItemBranch implements ITocItem {
71

    
72
        private ImageIcon icolayer = null;
73
        
74
        private ImageIcon finalIcon = null;
75
        
76
        private BufferedImage unavailableImg = null;
77
        
78
        private final String defaultIcon = "images/icolayer.PNG";
79
        
80
        private final String unavailableImgPath = "images/unavailable.png";
81
        
82
        private boolean isAvailable = true;
83
                
84
        private FLayer lyr;
85
        
86
        private Dimension sz;
87
        
88
    final public static DataFlavor INFO_FLAVOR =
89
            new DataFlavor(TocItemBranch.class, "ItemBranch");
90
    static DataFlavor flavors[] = {INFO_FLAVOR };
91
        
92
        public TocItemBranch(FLayer lyr)
93
        {
94
                this.lyr = lyr;
95
        }
96
        /* (non-Javadoc)
97
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
98
         */
99
        public String getLabel() {
100
                /*if (lyr instanceof FLyrVect && ((FLyrVect)lyr).isBroken())
101
                {
102
                        return lyr.getName() + "(broken)";
103
                }
104
                else*/
105
                        return lyr.getName();
106
        }
107

    
108
        /* (non-Javadoc)
109
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
110
         */
111
        public Icon getIcon() {
112
                // TODO Pedirle el icono a la capa. Por defecto habr? un icono
113
                // para vectoriales y otro para raster, pero se podr?n cambiar.
114
                
115
                if (finalIcon == null) {
116
                        this.setIcon(this.lyr.getTocImageIcon());                        
117
                }
118
                updateStateIcon();
119
                
120
                return finalIcon;
121
        }
122
        
123
        private void setIcon(String path) {
124
                File f = new File(path);
125
                if (f.exists()) {
126
                        icolayer = new ImageIcon(f.getAbsolutePath());                        
127
                } else {
128
                        URL url =PluginServices.getPluginServices("com.iver.cit.gvsig").getClassLoader().getResource(path);
129
                        if (url!=null) {                
130
                                icolayer = new ImageIcon(url);
131
                                return;
132
                        }
133
                }
134
                updateStateIcon();
135
                
136
        }
137
        private void updateStateIcon() {
138
                if (icolayer == null) return;
139
                if (this.lyr.isAvailable() != this.isAvailable || finalIcon==null) { 
140
                        if (!this.lyr.isAvailable()) {
141
                                BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);                        
142
                                Graphics2D grp = newImage.createGraphics();                        
143
                                grp.drawImage(icolayer.getImage(),0,0,null);
144
                                /*grp.setComposite(AlphaComposite.getInstance(
145
                                 AlphaComposite.SRC_OVER, (float) 1));*/
146
                                BufferedImage img = this.getUnavailableImage();
147
                                grp.drawImage(
148
                                                img,
149
                                                0,
150
                                                icolayer.getIconHeight() -img.getHeight(),
151
                                                null
152
                                );
153
                                this.finalIcon = new ImageIcon(newImage);
154
                                
155
                        } else {
156
                                this.finalIcon = new ImageIcon(icolayer.getImage());
157
                        }
158
                        this.isAvailable =(this.lyr.isAvailable());
159
                }                
160
                
161
        }
162
        
163
        private void setIcon(ImageIcon icon) {
164
                if (icon!=null) {                
165
                        icolayer = icon;                        
166
                } else {
167
                        this.setIcon(defaultIcon);
168
                
169
                }
170
                updateStateIcon();
171
                
172
        }
173

    
174
        public FLayer getLayer() {
175
                return lyr;
176
        }
177
        
178
        /* (non-Javadoc)
179
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
180
         */
181
        public DataFlavor[] getTransferDataFlavors() {
182
                return flavors;
183
        }
184

    
185
        /* (non-Javadoc)
186
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
187
         */
188
        public boolean isDataFlavorSupported(DataFlavor dF) {
189
                return dF.equals(INFO_FLAVOR);
190
        }
191

    
192
        /* (non-Javadoc)
193
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
194
         */
195
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
196
            if (dF.equals(INFO_FLAVOR)) {
197
                return this;
198
              }
199
              else throw new UnsupportedFlavorException(dF);
200
        }
201
        /* (non-Javadoc)
202
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
203
         */
204
        public Dimension getSize() {
205
                return sz;
206
        }
207
        /* (non-Javadoc)
208
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
209
         */
210
        public void setSize(Dimension sz) {
211
                this.sz = sz;
212
        }
213
        
214
        private BufferedImage getUnavailableImage() {
215
                if (this.unavailableImg == null) {
216
                        URL url =PluginServices.getPluginServices("com.iver.cit.gvsig").getClassLoader().getResource(this.unavailableImgPath);
217
                        if (url!=null) {
218
                                ImageIcon uIcon = new ImageIcon(url);                                
219
                                this.unavailableImg = new BufferedImage(uIcon.getIconWidth(),uIcon.getIconHeight(),BufferedImage.TYPE_INT_RGB);
220
                                this.unavailableImg.getGraphics().drawImage(uIcon.getImage(),0,0,null);
221
                        }
222
                         
223
                }
224
                return this.unavailableImg;
225
        }
226
        
227
}