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 @ 40596

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

    
27
import java.awt.Dimension;
28
import java.awt.Graphics2D;
29
import java.awt.Image;
30
import java.awt.datatransfer.DataFlavor;
31
import java.awt.datatransfer.UnsupportedFlavorException;
32
import java.awt.image.BufferedImage;
33
import java.io.File;
34
import java.io.IOException;
35
import java.net.URL;
36

    
37
import javax.swing.Icon;
38
import javax.swing.ImageIcon;
39

    
40
import org.gvsig.andami.IconThemeHelper;
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.app.project.documents.view.IContextMenuAction;
43
import org.gvsig.app.project.documents.view.toc.actions.FLyrVectEditPropertiesTocMenuEntry;
44
import org.gvsig.fmap.mapcontext.layers.FLayer;
45
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
46

    
47

    
48
/**
49
 * @author FJP
50
 *
51
 * TODO To change the template for this generated type comment go to
52
 * Window - Preferences - Java - Code Generation - Code and Comments
53
 */
54
public class TocItemBranch implements ITocItem {
55

    
56
        private ImageIcon icolayer = null;
57

    
58
        private ImageIcon finalIcon = null;
59

    
60
        private BufferedImage unavailableImg = null;
61

    
62
        private final String defaultIcon = "images/icolayer.PNG";
63

    
64
        private boolean isAvailable = true;
65

    
66
        private FLayer lyr;
67

    
68
        private Dimension sz;
69

    
70
    final public static DataFlavor INFO_FLAVOR =
71
            new DataFlavor(TocItemBranch.class, "ItemBranch");
72
    static DataFlavor flavors[] = {INFO_FLAVOR };
73

    
74
        public TocItemBranch(FLayer lyr)
75
        {
76
                this.lyr = lyr;
77
        }
78
        /* (non-Javadoc)
79
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getLabel()
80
         */
81
        public String getLabel() {
82
                /*if (lyr instanceof FLyrVect && ((FLyrVect)lyr).isBroken())
83
                {
84
                        return lyr.getName() + "(broken)";
85
                }
86
                else*/
87
                        return lyr.getName();
88
        }
89

    
90
        /* (non-Javadoc)
91
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getIcon()
92
         */
93
        public Icon getIcon() {
94
                // TODO Pedirle el icono a la capa. Por defecto habr? un icono
95
                // para vectoriales y otro para raster, pero se podr?n cambiar.
96

    
97
                if (finalIcon == null) {
98
                        ImageIcon icon = (ImageIcon) PluginServices.getIconTheme().get(lyr.getTocImageIcon());
99
                        this.setIcon(icon);
100
                }
101
                updateStateIcon();
102

    
103
                return finalIcon;
104
        }
105

    
106
        private void setIcon(String path) {
107
                File f = new File(path);
108
                if (f.exists()) {
109
                        icolayer = new ImageIcon(f.getAbsolutePath());
110
                } else {
111
                        URL url =PluginServices.getPluginServices("org.gvsig.app").getClassLoader().getResource(path);
112
                        if (url!=null) {
113
                                icolayer = new ImageIcon(url);
114
                                return;
115
                        }
116
                }
117
                updateStateIcon();
118

    
119
        }
120
        private void updateStateIcon() {
121
                if (icolayer == null) return;
122
                if (lyr.getTocStatusImage() != null)
123
                {
124
                        BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
125
                        Graphics2D grp = newImage.createGraphics();
126
                        grp.drawImage(icolayer.getImage(),0,0,null);
127
                        Image img = lyr.getTocStatusImage();
128
                        grp.drawImage(
129
                                        img,
130
                                        0,
131
                                        icolayer.getIconHeight() -img.getHeight(null),
132
                                        null
133
                        );
134
                        this.finalIcon = new ImageIcon(newImage);
135
                }
136
                if (this.lyr.isAvailable() != this.isAvailable || finalIcon==null) {
137
                        if (!this.lyr.isAvailable()) {
138
                                BufferedImage newImage = new BufferedImage(icolayer.getIconWidth(),icolayer.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
139
                                Graphics2D grp = newImage.createGraphics();
140
                                grp.drawImage(icolayer.getImage(),0,0,null);
141
                                /*grp.setComposite(AlphaComposite.getInstance(
142
                                 AlphaComposite.SRC_OVER, (float) 1));*/
143
                                BufferedImage img = this.getUnavailableImage();
144
                                grp.drawImage(
145
                                                img,
146
                                                0,
147
                                                icolayer.getIconHeight() -img.getHeight(),
148
                                                null
149
                                );
150
                                this.finalIcon = new ImageIcon(newImage);
151

    
152
                        } else {
153
                                this.finalIcon = new ImageIcon(icolayer.getImage());
154
                        }
155
                        this.isAvailable =(this.lyr.isAvailable());
156
                }
157

    
158
        }
159

    
160
        private void setIcon(ImageIcon icon) {
161
                if (icon!=null) {
162
                        icolayer = icon;
163
                } else {
164
                        this.setIcon(defaultIcon);
165

    
166
                }
167
                updateStateIcon();
168

    
169
        }
170

    
171
        public FLayer getLayer() {
172
                return lyr;
173
        }
174

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

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

    
189
        /* (non-Javadoc)
190
         * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
191
         */
192
        public Object getTransferData(DataFlavor dF) throws UnsupportedFlavorException, IOException {
193
            if (dF.equals(INFO_FLAVOR)) {
194
                return this;
195
              }
196
              else throw new UnsupportedFlavorException(dF);
197
        }
198
        /* (non-Javadoc)
199
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#getSize()
200
         */
201
        public Dimension getSize() {
202
                return sz;
203
        }
204
        /* (non-Javadoc)
205
         * @see com.iver.cit.gvsig.gui.toc.ITocItem#setSize(java.awt.Dimension)
206
         */
207
        public void setSize(Dimension sz) {
208
                this.sz = sz;
209
        }
210

    
211
        private BufferedImage getUnavailableImage() {
212
                if (this.unavailableImg == null) {
213
                        ImageIcon uIcon = IconThemeHelper.getImageIcon("layer-icon-unavailable");
214
                        if (uIcon != null) {
215
                                this.unavailableImg = new BufferedImage(uIcon.getIconWidth(), uIcon.getIconHeight(), BufferedImage.TYPE_INT_RGB);
216
                                this.unavailableImg.getGraphics().drawImage(uIcon.getImage(), 0, 0, null);
217
                        }
218
                }
219
                return this.unavailableImg;
220
        }
221

    
222
        public IContextMenuAction getDoubleClickAction() {
223
                // TODO
224
                // THIS IS A PATCH; IT WILL BE REMOVED WHEN ALL THE PROPERTIES
225
                // FOR ALL LAYERS WILL BE MERGED IN THE SAME DIALOG
226
                if (!(getLayer() instanceof FLyrVect)) {
227
                        return null;
228
                }
229
                return new FLyrVectEditPropertiesTocMenuEntry();
230
        }
231
}