Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / TocItemBranch.java @ 31496

History | View | Annotate | Download (7.01 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 org.gvsig.app.project.documents.view.toc;
48

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

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

    
62
import org.gvsig.andami.PluginServices;
63
import org.gvsig.app.project.documents.view.IContextMenuAction;
64
import org.gvsig.app.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
65
import org.gvsig.fmap.mapcontext.MapContext;
66
import org.gvsig.fmap.mapcontext.layers.FLayer;
67
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
68

    
69

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

    
78
        private ImageIcon icolayer = null;
79

    
80
        private ImageIcon finalIcon = null;
81

    
82
        private BufferedImage unavailableImg = null;
83

    
84
        private final String defaultIcon = "images/icolayer.PNG";
85

    
86
        private final String unavailableImgPath = "images/unavailable.png";
87

    
88
        private boolean isAvailable = true;
89

    
90
        private FLayer lyr;
91

    
92
        private Dimension sz;
93

    
94
    final public static DataFlavor INFO_FLAVOR =
95
            new DataFlavor(TocItemBranch.class, "ItemBranch");
96
    static DataFlavor flavors[] = {INFO_FLAVOR };
97

    
98
        public TocItemBranch(FLayer lyr)
99
        {
100
                this.lyr = lyr;
101
        }
102
        /* (non-Javadoc)
103
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
104
         */
105
        public String getLabel() {
106
                /*if (lyr instanceof FLyrVect && ((FLyrVect)lyr).isBroken())
107
                {
108
                        return lyr.getName() + "(broken)";
109
                }
110
                else*/
111
                        return lyr.getName();
112
        }
113

    
114
        /* (non-Javadoc)
115
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
116
         */
117
        public Icon getIcon() {
118
                // TODO Pedirle el icono a la capa. Por defecto habr? un icono
119
                // para vectoriales y otro para raster, pero se podr?n cambiar.
120

    
121
                if (finalIcon == null) {
122
                        ImageIcon icon = (ImageIcon) PluginServices.getIconTheme().get(lyr.getTocImageIcon());
123
                        this.setIcon(icon);
124
                }
125
                updateStateIcon();
126

    
127
                return finalIcon;
128
        }
129

    
130
        private void setIcon(String path) {
131
                File f = new File(path);
132
                if (f.exists()) {
133
                        icolayer = new ImageIcon(f.getAbsolutePath());
134
                } else {
135
                        URL url =PluginServices.getPluginServices("org.gvsig.app").getClassLoader().getResource(path);
136
                        if (url!=null) {
137
                                icolayer = new ImageIcon(url);
138
                                return;
139
                        }
140
                }
141
                updateStateIcon();
142

    
143
        }
144
        private void updateStateIcon() {
145
                if (icolayer == null) return;
146
                if (lyr.getTocStatusImage() != null)
147
                {
148
                        BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
149
                        Graphics2D grp = newImage.createGraphics();
150
                        grp.drawImage(icolayer.getImage(),0,0,null);
151
                        Image img = lyr.getTocStatusImage();
152
                        grp.drawImage(
153
                                        img,
154
                                        0,
155
                                        icolayer.getIconHeight() -img.getHeight(null),
156
                                        null
157
                        );
158
                        this.finalIcon = new ImageIcon(newImage);
159
                }
160
                if (this.lyr.isAvailable() != this.isAvailable || finalIcon==null) {
161
                        if (!this.lyr.isAvailable()) {
162
                                BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
163
                                Graphics2D grp = newImage.createGraphics();
164
                                grp.drawImage(icolayer.getImage(),0,0,null);
165
                                /*grp.setComposite(AlphaComposite.getInstance(
166
                                 AlphaComposite.SRC_OVER, (float) 1));*/
167
                                BufferedImage img = this.getUnavailableImage();
168
                                grp.drawImage(
169
                                                img,
170
                                                0,
171
                                                icolayer.getIconHeight() -img.getHeight(),
172
                                                null
173
                                );
174
                                this.finalIcon = new ImageIcon(newImage);
175

    
176
                        } else {
177
                                this.finalIcon = new ImageIcon(icolayer.getImage());
178
                        }
179
                        this.isAvailable =(this.lyr.isAvailable());
180
                }
181

    
182
        }
183

    
184
        private void setIcon(ImageIcon icon) {
185
                if (icon!=null) {
186
                        icolayer = icon;
187
                } else {
188
                        this.setIcon(defaultIcon);
189

    
190
                }
191
                updateStateIcon();
192

    
193
        }
194

    
195
        public FLayer getLayer() {
196
                return lyr;
197
        }
198

    
199
        /* (non-Javadoc)
200
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
201
         */
202
        public DataFlavor[] getTransferDataFlavors() {
203
                return flavors;
204
        }
205

    
206
        /* (non-Javadoc)
207
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
208
         */
209
        public boolean isDataFlavorSupported(DataFlavor dF) {
210
                return dF.equals(INFO_FLAVOR);
211
        }
212

    
213
        /* (non-Javadoc)
214
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
215
         */
216
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
217
            if (dF.equals(INFO_FLAVOR)) {
218
                return this;
219
              }
220
              else throw new UnsupportedFlavorException(dF);
221
        }
222
        /* (non-Javadoc)
223
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
224
         */
225
        public Dimension getSize() {
226
                return sz;
227
        }
228
        /* (non-Javadoc)
229
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
230
         */
231
        public void setSize(Dimension sz) {
232
                this.sz = sz;
233
        }
234

    
235
        private BufferedImage getUnavailableImage() {
236
                if (this.unavailableImg == null) {
237
                        URL url =PluginServices.getPluginServices("org.gvsig.app").getClassLoader().getResource(this.unavailableImgPath);
238
                        if (url!=null) {
239
                                ImageIcon uIcon = new ImageIcon(url);
240
                                this.unavailableImg = new BufferedImage(uIcon.getIconWidth(),uIcon.getIconHeight(),BufferedImage.TYPE_INT_RGB);
241
                                this.unavailableImg.getGraphics().drawImage(uIcon.getImage(),0,0,null);
242
                        }
243

    
244
                }
245
                return this.unavailableImg;
246
        }
247

    
248
        public IContextMenuAction getDoubleClickAction() {
249
                // TODO
250
                // THIS IS A PATCH; IT WILL BE REMOVED WHEN ALL THE PROPERTIES
251
                // FOR ALL LAYERS WILL BE MERGED IN THE SAME DIALOG
252
                if (!(getLayer() instanceof FLyrVect)) {
253
                        return null;
254
                }
255
                return new FLyrVectEditPropertiesTocMenuEntry();
256
        }
257
}