Revision 47475 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

View differences:

JSymbolPreviewButton.java
21 21
 * For any additional information, do not hesitate to contact us
22 22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23 23
 */
24
/* CVS MESSAGES:
25
*
26
* $Id: JSymbolPreviewButton.java 31301 2009-10-16 06:56:13Z vcaballero $
27
* $Log$
28
* Revision 1.4  2007-09-17 09:21:45  jaume
29
* refactored SymboSelector (added support for multishapedsymbol)
30
*
31
* Revision 1.3  2007/08/09 10:39:04  jaume
32
* first round of found bugs fixed
33
*
34
* Revision 1.2  2007/03/09 11:25:00  jaume
35
* Advanced symbology (start committing)
36
*
37
* Revision 1.1.2.4  2007/02/21 07:35:14  jaume
38
* *** empty log message ***
39
*
40
* Revision 1.1.2.3  2007/02/14 10:00:45  jaume
41
* *** empty log message ***
42
*
43
* Revision 1.1.2.2  2007/02/14 09:59:17  jaume
44
* *** empty log message ***
45
*
46
* Revision 1.1.2.1  2007/02/13 16:19:19  jaume
47
* graduated symbol legends (start commiting)
48
*
49
*
50
*/
51 24
package org.gvsig.app.project.documents.view.legend.gui;
52 25

  
53 26
import java.awt.BasicStroke;
......
63 36
import java.awt.geom.AffineTransform;
64 37
import java.awt.geom.Rectangle2D;
65 38
import java.util.ArrayList;
66

  
67 39
import javax.swing.BorderFactory;
68 40
import javax.swing.JComponent;
69

  
70 41
import org.gvsig.andami.PluginServices;
71 42
import org.gvsig.app.gui.styling.SymbolSelector;
72 43
import org.gvsig.fmap.mapcontext.MapContextLocator;
......
77 48
/**
78 49
 * Just a Button that shows an ISymbol instead a text.
79 50
 *
80
 * @author jaume dominguez faus - jaume.dominguez@iver.es
51
 * @author gvSIG Team
81 52
 *
82 53
 */
83 54
public class JSymbolPreviewButton extends JComponent implements MouseListener {
84
	private static final long serialVersionUID = -7878718124556977288L;
85
	private ISymbol prev;
86
	private boolean pressed;
87
	private int shapeType;
88
	private ArrayList<ActionListener> listeners;  //  @jve:decl-index=0:
89
	private ActionEvent event;
90
	private String actionCommand;
91 55

  
92
	/**
93
	 * @return the actionCommand
94
	 */
95
	public String getActionCommand() {
96
		return actionCommand;
97
	}
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;
98 63

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

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

  
101
	/**
102
	 * @param actionCommand the actionCommand to set
103
	 */
104
	public void setActionCommand(String actionCommand) {
105
		this.actionCommand = actionCommand;
106
	}
78
    public JSymbolPreviewButton(int shapeType) {
79
        this(null, shapeType);
80
    }
107 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
    }
108 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());
109 104

  
110
	public JSymbolPreviewButton(int shapeType) {
111
		this(null, shapeType);
112
	}
105
            int _margin = 5;
113 106

  
114
	public JSymbolPreviewButton(ISymbol sym,
115
			int shapeType) {
116
		super();
117
		addMouseListener(this);
118
		mouseExited(null);
119
		setPreferredSize(new Dimension(150, 20));
120
		this.shapeType = shapeType;
121
		this.prev = sym;
122
   }
107
            Rectangle2D r = new Rectangle2D.Double(
108
                    _margin, _margin,
109
                    bounds.getWidth() - 2 * _margin,
110
                    bounds.getHeight() - 2 * _margin);
123 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
    }
124 137

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

  
126
	public void paint(Graphics g) {
127
		Rectangle bounds = getBounds();
128
		Graphics2D g2 = (Graphics2D) g;
129
		if (g2 != null) {
130
			g2.setStroke(new BasicStroke(2));
131
			g2.setColor(pressed && isEnabled() ? Color.GRAY : Color.WHITE);
132
			g2.drawLine(0, 0, (int) bounds.getWidth(), 0);
133
			g2.drawLine(0, 0, 0, (int) bounds.getHeight());
134
			g2.setColor(pressed && isEnabled() ? Color.WHITE : Color.GRAY);
135
			g2.drawLine(2, (int) bounds.getHeight(), (int) bounds.getWidth(), (int) bounds.getHeight());
136
			g2.drawLine((int) bounds.getWidth(), 0, (int) bounds.getWidth(), (int) bounds.getHeight());
137
			
138
			int _margin = 5;
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
    }
139 155

  
140
			Rectangle2D r = new Rectangle2D.Double(
141
			    _margin, _margin,
142
			    bounds.getWidth() - 2 * _margin,
143
			    bounds.getHeight() - 2 * _margin);
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
        }
144 169

  
145
			if (prev!=null) {
146
				try {
147
					prev.drawInsideRectangle(g2,
148
					    new AffineTransform(), r.getBounds(),null);
149
				} catch (SymbolDrawingException e) {
150
					if (e.getType() == SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS) {
151
						try {
152
							MapContextLocator.getSymbolManager()
153
									.getWarningSymbol(
154
											SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS,
155
											prev.getDescription(),
156
											SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS)
157
									.drawInsideRectangle(g2, g2.getTransform(),
158
											r.getBounds(), null);
159
						} catch (SymbolDrawingException e1) {
160
							// IMPOSSIBLE TO REACH THIS
161
						}
162
					} else {
163
						// should be unreachable code
164
						throw new Error(Messages.getText("symbol_shapetype_mismatch"));
165
					}
166
				}
167
			}
168
		}
169
	}
170
    }
170 171

  
171
	public void setSymbol(ISymbol symbol) {
172
		this.prev = symbol;
173
		this.repaint();
174
		// paintImmediately(getBounds());
175
	}
172
    @Override
173
    public void mouseEntered(MouseEvent e) {
174
    }
176 175

  
177
	public void mouseClicked(MouseEvent e) {
178
		if (e.getButton() == MouseEvent.BUTTON1) {
179
			ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(prev, shapeType);
180
			PluginServices.getMDIManager().addWindow(sSelect);
181
			if (sSelect.getSelectedObject()!=null){
182
				setSymbol((ISymbol) sSelect.getSelectedObject());
183
				fireActionListeners();
184
			}
185
		}
186
	}
176
    @Override
177
    public void mouseExited(MouseEvent e) {
178
        setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
179
        pressed = false;
180
        paintImmediately(getBounds());
181
    }
187 182

  
188
	private void fireActionListeners() {
189
		if (!isEnabled()) return;
190
		if (listeners != null) {
191
			if (event == null) {
192
				event = new ActionEvent(this, 0, actionCommand);
193
			}
194
			for (int i = 0; i < listeners.size(); i++) {
195
				((ActionListener) listeners.get(i)).actionPerformed(
196
						event);
197
			}
198
		}
183
    @Override
184
    public void mousePressed(MouseEvent e) {
199 185

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

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

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

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

  
206
	public void mouseExited(MouseEvent e) {
207
		setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
208
		pressed = false;
209
		paintImmediately(getBounds());
210
	}
210
    public void setShapeType(int shapeType) {
211
        this.shapeType = shapeType;
212
    }
211 213

  
212
	public void mousePressed(MouseEvent e) {
213

  
214
		setBorder(BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
215
		pressed = true;
216
		paintImmediately(getBounds());
217
	}
218

  
219
	public void mouseReleased(MouseEvent e) {
220
		if (pressed)
221
			mouseClicked(e);
222
		mouseExited(e);
223
	}
224

  
225

  
226

  
227
	public ISymbol getSymbol() {
228
		return prev;
229
	}
230

  
231

  
232

  
233
	public void addActionListener(ActionListener l) {
234
		if (listeners == null)
235
			listeners = new ArrayList<ActionListener>();
236
		listeners.add(l);
237
	}
238

  
239

  
240

  
241
	public void setShapeType(int shapeType) {
242
		this.shapeType = shapeType;
243
	}
244

  
245

  
246 214
}

Also available in: Unified diff