Statistics
| Revision:

svn-gvsig-desktop / tags / v10_RC2c / applications / appgvSIG / src / com / iver / cit / gvsig / gui / toc / TocItemBranch.java @ 8745

History | View | Annotate | Download (6.09 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.gui.toc;
48

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

    
60
import javax.swing.Icon;
61
import javax.swing.ImageIcon;
62

    
63
import com.iver.andami.PluginServices;
64
import com.iver.cit.gvsig.fmap.layers.FLayer;
65
import com.iver.cit.gvsig.fmap.layers.FLyrDefault;
66

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

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

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

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

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

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