Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.api / src / main / java / org / gvsig / app / project / documents / view / legend / gui / JSymbolPreviewButton.java @ 47475

History | View | Annotate | Download (6.94 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
package org.gvsig.app.project.documents.view.legend.gui;
25

    
26
import java.awt.BasicStroke;
27
import java.awt.Color;
28
import java.awt.Dimension;
29
import java.awt.Graphics;
30
import java.awt.Graphics2D;
31
import java.awt.Rectangle;
32
import java.awt.event.ActionEvent;
33
import java.awt.event.ActionListener;
34
import java.awt.event.MouseEvent;
35
import java.awt.event.MouseListener;
36
import java.awt.geom.AffineTransform;
37
import java.awt.geom.Rectangle2D;
38
import java.util.ArrayList;
39
import javax.swing.BorderFactory;
40
import javax.swing.JComponent;
41
import org.gvsig.andami.PluginServices;
42
import org.gvsig.app.gui.styling.SymbolSelector;
43
import org.gvsig.fmap.mapcontext.MapContextLocator;
44
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
45
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
46
import org.gvsig.i18n.Messages;
47

    
48
/**
49
 * Just a Button that shows an ISymbol instead a text.
50
 *
51
 * @author gvSIG Team
52
 *
53
 */
54
public class JSymbolPreviewButton extends JComponent implements MouseListener {
55

    
56
    private static final long serialVersionUID = -7878718124556977288L;
57
    private ISymbol prev;
58
    private boolean pressed;
59
    private int shapeType;
60
    private ArrayList<ActionListener> listeners;  //  @jve:decl-index=0:
61
    private ActionEvent event;
62
    private String actionCommand;
63

    
64
    /**
65
     * @return the actionCommand
66
     */
67
    public String getActionCommand() {
68
        return actionCommand;
69
    }
70

    
71
    /**
72
     * @param actionCommand the actionCommand to set
73
     */
74
    public void setActionCommand(String actionCommand) {
75
        this.actionCommand = actionCommand;
76
    }
77

    
78
    public JSymbolPreviewButton(int shapeType) {
79
        this(null, shapeType);
80
    }
81

    
82
    public JSymbolPreviewButton(ISymbol sym,
83
            int shapeType) {
84
        super();
85
        addMouseListener(this);
86
        mouseExited(null);
87
        setPreferredSize(new Dimension(150, 20));
88
        this.shapeType = shapeType;
89
        this.prev = sym;
90
    }
91

    
92
    @Override
93
    public void paint(Graphics g) {
94
        Rectangle bounds = getBounds();
95
        Graphics2D g2 = (Graphics2D) g;
96
        if (g2 != null) {
97
            g2.setStroke(new BasicStroke(2));
98
            g2.setColor(pressed && isEnabled() ? Color.GRAY : Color.WHITE);
99
            g2.drawLine(0, 0, (int) bounds.getWidth(), 0);
100
            g2.drawLine(0, 0, 0, (int) bounds.getHeight());
101
            g2.setColor(pressed && isEnabled() ? Color.WHITE : Color.GRAY);
102
            g2.drawLine(2, (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight());
103
            g2.drawLine((int) bounds.getWidth(), 0, (int) bounds.getWidth(), (int) bounds.getHeight());
104

    
105
            int _margin = 5;
106

    
107
            Rectangle2D r = new Rectangle2D.Double(
108
                    _margin, _margin,
109
                    bounds.getWidth() - 2 * _margin,
110
                    bounds.getHeight() - 2 * _margin);
111

    
112
            if (prev != null) {
113
                try {
114
                    prev.drawInsideRectangle(g2,
115
                            new AffineTransform(), r.getBounds());
116
                } catch (SymbolDrawingException e) {
117
                    if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
118
                        try {
119
                            MapContextLocator.getSymbolManager()
120
                                    .getWarningSymbol(
121
                                            SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
122
                                            prev.getDescription(),
123
                                            SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
124
                                    .drawInsideRectangle(g2, g2.getTransform(),
125
                                            r.getBounds());
126
                        } catch (SymbolDrawingException e1) {
127
                            // IMPOSSIBLE TO REACH THIS
128
                        }
129
                    } else {
130
                        // should be unreachable code
131
                        throw new Error(Messages.getText("symbol_shapetype_mismatch"));
132
                    }
133
                }
134
            }
135
        }
136
    }
137

    
138
    public void setSymbol(ISymbol symbol) {
139
        this.prev = symbol;
140
        this.repaint();
141
        // paintImmediately(getBounds());
142
    }
143

    
144
    @Override
145
    public void mouseClicked(MouseEvent e) {
146
        if (e.getButton() == MouseEvent.BUTTON1) {
147
            ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(prev, shapeType);
148
            PluginServices.getMDIManager().addWindow(sSelect);
149
            if (sSelect.getSelectedObject() != null) {
150
                setSymbol((ISymbol) sSelect.getSelectedObject());
151
                fireActionListeners();
152
            }
153
        }
154
    }
155

    
156
    private void fireActionListeners() {
157
        if (!isEnabled()) {
158
            return;
159
        }
160
        if (listeners != null) {
161
            if (event == null) {
162
                event = new ActionEvent(this, 0, actionCommand);
163
            }
164
            for (int i = 0; i < listeners.size(); i++) {
165
                ((ActionListener) listeners.get(i)).actionPerformed(
166
                        event);
167
            }
168
        }
169

    
170
    }
171

    
172
    @Override
173
    public void mouseEntered(MouseEvent e) {
174
    }
175

    
176
    @Override
177
    public void mouseExited(MouseEvent e) {
178
        setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
179
        pressed = false;
180
        paintImmediately(getBounds());
181
    }
182

    
183
    @Override
184
    public void mousePressed(MouseEvent e) {
185

    
186
        setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
187
        pressed = true;
188
        paintImmediately(getBounds());
189
    }
190

    
191
    @Override
192
    public void mouseReleased(MouseEvent e) {
193
        if (pressed) {
194
            mouseClicked(e);
195
        }
196
        mouseExited(e);
197
    }
198

    
199
    public ISymbol getSymbol() {
200
        return prev;
201
    }
202

    
203
    public void addActionListener(ActionListener l) {
204
        if (listeners == null) {
205
            listeners = new ArrayList<>();
206
        }
207
        listeners.add(l);
208
    }
209

    
210
    public void setShapeType(int shapeType) {
211
        this.shapeType = shapeType;
212
    }
213

    
214
}