Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / toc / TocItemBranch.java @ 40558

History | View | Annotate | Download (7.81 KB)

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

    
72
import java.awt.Dimension;
73
import java.awt.Graphics2D;
74
import java.awt.Image;
75
import java.awt.datatransfer.DataFlavor;
76
import java.awt.datatransfer.UnsupportedFlavorException;
77
import java.awt.image.BufferedImage;
78
import java.io.File;
79
import java.io.IOException;
80
import java.net.URL;
81

    
82
import javax.swing.Icon;
83
import javax.swing.ImageIcon;
84

    
85
import org.gvsig.andami.IconThemeHelper;
86
import org.gvsig.andami.PluginServices;
87
import org.gvsig.app.project.documents.view.IContextMenuAction;
88
import org.gvsig.app.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
89
import org.gvsig.fmap.mapcontext.layers.FLayer;
90
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
91

    
92

    
93
/**
94
 * @author FJP
95
 *
96
 * TODO To change the template for this generated type comment go to
97
 * Window - Preferences - Java - Code Generation - Code and Comments
98
 */
99
public class TocItemBranch implements ITocItem {
100

    
101
        private ImageIcon icolayer = null;
102

    
103
        private ImageIcon finalIcon = null;
104

    
105
        private BufferedImage unavailableImg = null;
106

    
107
        private final String defaultIcon = "images/icolayer.PNG";
108

    
109
        private boolean isAvailable = true;
110

    
111
        private FLayer lyr;
112

    
113
        private Dimension sz;
114

    
115
    final public static DataFlavor INFO_FLAVOR =
116
            new DataFlavor(TocItemBranch.class, "ItemBranch");
117
    static DataFlavor flavors[] = {INFO_FLAVOR };
118

    
119
        public TocItemBranch(FLayer lyr)
120
        {
121
                this.lyr = lyr;
122
        }
123
        /* (non-Javadoc)
124
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
125
         */
126
        public String getLabel() {
127
                /*if (lyr instanceof FLyrVect && ((FLyrVect)lyr).isBroken())
128
                {
129
                        return lyr.getName() + "(broken)";
130
                }
131
                else*/
132
                        return lyr.getName();
133
        }
134

    
135
        /* (non-Javadoc)
136
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
137
         */
138
        public Icon getIcon() {
139
                // TODO Pedirle el icono a la capa. Por defecto habr? un icono
140
                // para vectoriales y otro para raster, pero se podr?n cambiar.
141

    
142
                if (finalIcon == null) {
143
                        ImageIcon icon = (ImageIcon) PluginServices.getIconTheme().get(lyr.getTocImageIcon());
144
                        this.setIcon(icon);
145
                }
146
                updateStateIcon();
147

    
148
                return finalIcon;
149
        }
150

    
151
        private void setIcon(String path) {
152
                File f = new File(path);
153
                if (f.exists()) {
154
                        icolayer = new ImageIcon(f.getAbsolutePath());
155
                } else {
156
                        URL url =PluginServices.getPluginServices("org.gvsig.app").getClassLoader().getResource(path);
157
                        if (url!=null) {
158
                                icolayer = new ImageIcon(url);
159
                                return;
160
                        }
161
                }
162
                updateStateIcon();
163

    
164
        }
165
        private void updateStateIcon() {
166
                if (icolayer == null) return;
167
                if (lyr.getTocStatusImage() != null)
168
                {
169
                        BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
170
                        Graphics2D grp = newImage.createGraphics();
171
                        grp.drawImage(icolayer.getImage(),0,0,null);
172
                        Image img = lyr.getTocStatusImage();
173
                        grp.drawImage(
174
                                        img,
175
                                        0,
176
                                        icolayer.getIconHeight() -img.getHeight(null),
177
                                        null
178
                        );
179
                        this.finalIcon = new ImageIcon(newImage);
180
                }
181
                if (this.lyr.isAvailable() != this.isAvailable || finalIcon==null) {
182
                        if (!this.lyr.isAvailable()) {
183
                                BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
184
                                Graphics2D grp = newImage.createGraphics();
185
                                grp.drawImage(icolayer.getImage(),0,0,null);
186
                                /*grp.setComposite(AlphaComposite.getInstance(
187
                                 AlphaComposite.SRC_OVER, (float) 1));*/
188
                                BufferedImage img = this.getUnavailableImage();
189
                                grp.drawImage(
190
                                                img,
191
                                                0,
192
                                                icolayer.getIconHeight() -img.getHeight(),
193
                                                null
194
                                );
195
                                this.finalIcon = new ImageIcon(newImage);
196

    
197
                        } else {
198
                                this.finalIcon = new ImageIcon(icolayer.getImage());
199
                        }
200
                        this.isAvailable =(this.lyr.isAvailable());
201
                }
202

    
203
        }
204

    
205
        private void setIcon(ImageIcon icon) {
206
                if (icon!=null) {
207
                        icolayer = icon;
208
                } else {
209
                        this.setIcon(defaultIcon);
210

    
211
                }
212
                updateStateIcon();
213

    
214
        }
215

    
216
        public FLayer getLayer() {
217
                return lyr;
218
        }
219

    
220
        /* (non-Javadoc)
221
         * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
222
         */
223
        public DataFlavor[] getTransferDataFlavors() {
224
                return flavors;
225
        }
226

    
227
        /* (non-Javadoc)
228
         * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
229
         */
230
        public boolean isDataFlavorSupported(DataFlavor dF) {
231
                return dF.equals(INFO_FLAVOR);
232
        }
233

    
234
        /* (non-Javadoc)
235
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
236
         */
237
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
238
            if (dF.equals(INFO_FLAVOR)) {
239
                return this;
240
              }
241
              else throw new UnsupportedFlavorException(dF);
242
        }
243
        /* (non-Javadoc)
244
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
245
         */
246
        public Dimension getSize() {
247
                return sz;
248
        }
249
        /* (non-Javadoc)
250
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
251
         */
252
        public void setSize(Dimension sz) {
253
                this.sz = sz;
254
        }
255

    
256
        private BufferedImage getUnavailableImage() {
257
                if (this.unavailableImg == null) {
258
                        ImageIcon uIcon = IconThemeHelper.getImageIcon("layer-icon-unavailable");
259
                        if (uIcon != null) {
260
                                this.unavailableImg = new BufferedImage(uIcon.getIconWidth(), uIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
261
                                this.unavailableImg.getGraphics().drawImage(uIcon.getImage(), 0, 0, null);
262
                        }
263
                }
264
                return this.unavailableImg;
265
        }
266

    
267
        public IContextMenuAction getDoubleClickAction() {
268
                // TODO
269
                // THIS IS A PATCH; IT WILL BE REMOVED WHEN ALL THE PROPERTIES
270
                // FOR ALL LAYERS WILL BE MERGED IN THE SAME DIALOG
271
                if (!(getLayer() instanceof FLyrVect)) {
272
                        return null;
273
                }
274
                return new FLyrVectEditPropertiesTocMenuEntry();
275
        }
276
}