Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toc / TocItemBranch.java @ 38989

History | View | Annotate | Download (6.87 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.IconThemeHelper;
63
import org.gvsig.andami.PluginServices;
64
import org.gvsig.app.project.documents.view.IContextMenuAction;
65
import org.gvsig.app.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
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 boolean isAvailable = true;
87

    
88
        private FLayer lyr;
89

    
90
        private Dimension sz;
91

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

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

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

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

    
125
                return finalIcon;
126
        }
127

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

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

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

    
180
        }
181

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

    
188
                }
189
                updateStateIcon();
190

    
191
        }
192

    
193
        public FLayer getLayer() {
194
                return lyr;
195
        }
196

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

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

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

    
233
        private BufferedImage getUnavailableImage() {
234
                if (this.unavailableImg == null) {
235
                        ImageIcon uIcon = IconThemeHelper.getImageIcon("layer-icon-unavailable");
236
                        if (uIcon != null) {
237
                                this.unavailableImg = new BufferedImage(uIcon.getIconWidth(), uIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
238
                                this.unavailableImg.getGraphics().drawImage(uIcon.getImage(), 0, 0, null);
239
                        }
240
                }
241
                return this.unavailableImg;
242
        }
243

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