svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.symbology / org.gvsig.symbology.swing / org.gvsig.symbology.swing.impl / src / main / java / org / gvsig / symbology / swing / impl / pickercontroller / SymbolPickerController.java @ 47375
History | View | Annotate | Download (5.79 KB)
1 | 47375 | fdiaz | package org.gvsig.symbology.swing.impl.pickercontroller; |
---|---|---|---|
2 | |||
3 | import java.awt.Dimension; |
||
4 | import java.awt.Graphics2D; |
||
5 | import java.awt.Rectangle; |
||
6 | import java.awt.event.ActionEvent; |
||
7 | import java.awt.event.ComponentAdapter; |
||
8 | import java.awt.event.ComponentEvent; |
||
9 | import java.awt.geom.AffineTransform; |
||
10 | import java.awt.image.BufferedImage; |
||
11 | import javax.swing.BorderFactory; |
||
12 | import javax.swing.ImageIcon; |
||
13 | import javax.swing.JButton; |
||
14 | import javax.swing.JPanel; |
||
15 | import javax.swing.JTextField; |
||
16 | import javax.swing.text.JTextComponent; |
||
17 | import org.gvsig.app.gui.styling.SymbolSelector; |
||
18 | import org.gvsig.app.project.documents.view.legend.gui.ISymbolSelector; |
||
19 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
20 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
21 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
||
22 | import org.gvsig.tools.swing.api.ToolsSwingLocator; |
||
23 | import org.gvsig.tools.swing.api.ToolsSwingUtils; |
||
24 | import org.gvsig.tools.swing.api.pickercontroller.AbstractPickerController; |
||
25 | import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
||
26 | |||
27 | /**
|
||
28 | *
|
||
29 | * @author gvSIG Team
|
||
30 | */
|
||
31 | @SuppressWarnings("UseSpecificCatch") |
||
32 | public class SymbolPickerController extends AbstractPickerController<ISymbol> { |
||
33 | |||
34 | private final JButton btnPreview; |
||
35 | private final JTextComponent txtDescription; |
||
36 | private int geomType; |
||
37 | private ISymbol symbol;
|
||
38 | |||
39 | /*
|
||
40 | El tama?o m?nimo del bot?n ser? 22x22
|
||
41 | Si se quiere otro tama?o, hay que asegurarse de que se fuerza ese tama?o en el gui,
|
||
42 | no delegando en el layout para calcularlo.
|
||
43 | En el Abeille se recomienda:
|
||
44 | - en la pesta?a Cell, fijar Alignment a Horizontal y Vertical a FILL
|
||
45 | - en la pesta?a Column, poner Constant en Column Size con un valor de 22 DLU
|
||
46 | - en la pesta?a Row, poner Constant en Row Size con un valor de 22 DLU
|
||
47 | El valor del Row/Col Constant depender? de los componentes que coexistan
|
||
48 | en la linea o columna del bot?n.
|
||
49 | */
|
||
50 | public SymbolPickerController(JButton btnPreview, JTextComponent txtDescription, int geomType, ISymbol symbol) { |
||
51 | this.btnPreview = btnPreview;
|
||
52 | if( txtDescription==null ) { |
||
53 | this.txtDescription = new JTextField(); |
||
54 | } else {
|
||
55 | this.txtDescription = txtDescription;
|
||
56 | } |
||
57 | |||
58 | this.symbol = symbol;
|
||
59 | if (this.symbol == null) { |
||
60 | this.symbol = MapContextLocator.getSymbolManager().createSymbol(geomType);
|
||
61 | } |
||
62 | this.btnPreview.setBorder(BorderFactory.createEmptyBorder(2,2,2,2)); |
||
63 | this.btnPreview.setText(""); |
||
64 | this.btnPreview.addActionListener((ActionEvent e) -> { |
||
65 | doSelectSymbol(); |
||
66 | }); |
||
67 | this.btnPreview.addComponentListener(new ComponentAdapter() { |
||
68 | @Override
|
||
69 | public void componentResized(ComponentEvent e) { |
||
70 | updatePreview(); |
||
71 | } |
||
72 | }); |
||
73 | this.set(this.symbol); |
||
74 | } |
||
75 | |||
76 | private void updatePreview() { |
||
77 | |||
78 | Dimension previewSize = btnPreview.getSize();
|
||
79 | if(previewSize.width < 22) { |
||
80 | previewSize.width = 22;
|
||
81 | } |
||
82 | if(previewSize.height < 22) { |
||
83 | previewSize.height = 22;
|
||
84 | } |
||
85 | Dimension size = new Dimension(previewSize.width-4,previewSize.height-4); |
||
86 | this.btnPreview.setIcon(
|
||
87 | new ImageIcon(this.createPreview(size, this.symbol)) |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | private BufferedImage createPreview(Dimension size, ISymbol symbol) { |
||
92 | BufferedImage previewImage = new BufferedImage( |
||
93 | size.width, |
||
94 | size.height, |
||
95 | BufferedImage.TYPE_INT_ARGB
|
||
96 | ); |
||
97 | |||
98 | Graphics2D g2 = (Graphics2D) previewImage.createGraphics(); |
||
99 | if (g2 != null) { |
||
100 | Rectangle bounds = new Rectangle(size.width, size.height); |
||
101 | ISymbol prevSymbol = symbol; |
||
102 | try {
|
||
103 | prevSymbol.drawInsideRectangle(g2, new AffineTransform(), bounds, null); |
||
104 | } catch (Exception e) { |
||
105 | try {
|
||
106 | prevSymbol = MapContextLocator.getSymbolManager().getWarningSymbol( |
||
107 | SymbolDrawingException.STR_UNSUPPORTED_SET_OF_SETTINGS, |
||
108 | symbol.getDescription(), |
||
109 | SymbolDrawingException.UNSUPPORTED_SET_OF_SETTINGS |
||
110 | ); |
||
111 | prevSymbol.drawInsideRectangle(g2,g2.getTransform(),bounds,null);
|
||
112 | } catch (Exception e1) { |
||
113 | LOG.debug("Can't create preview symbol",e1);
|
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | return previewImage;
|
||
118 | } |
||
119 | |||
120 | private void doSelectSymbol() { |
||
121 | WindowManager wm = ToolsSwingLocator.getWindowManager(); |
||
122 | ISymbolSelector sSelect = SymbolSelector.createSymbolSelector(this.symbol, this.geomType); |
||
123 | |||
124 | Dimension desktopPanelSize = ToolsSwingUtils.getDesktopPanelSize();
|
||
125 | ToolsSwingUtils.ensureHeightWitdh( |
||
126 | (JPanel) sSelect,
|
||
127 | Math.max(3*(desktopPanelSize.height/4), ToolsSwingUtils.rows2px(80)), |
||
128 | Math.max(2*(desktopPanelSize.width/4), ToolsSwingUtils.cols2px(35)), |
||
129 | 7*(desktopPanelSize.height/8), |
||
130 | 3*(desktopPanelSize.width/4) |
||
131 | ); |
||
132 | |||
133 | |||
134 | wm.showWindow((JPanel) sSelect, sSelect.getWindowInfo().getTitle(), WindowManager.MODE.DIALOG);
|
||
135 | if (sSelect.getSelectedObject() != null) { |
||
136 | this.symbol = (ISymbol) sSelect.getSelectedObject();
|
||
137 | this.updatePreview();
|
||
138 | this.fireChangeEvent();
|
||
139 | } |
||
140 | } |
||
141 | |||
142 | @Override
|
||
143 | public ISymbol get() {
|
||
144 | symbol.setDescription(this.txtDescription.getText());
|
||
145 | return symbol;
|
||
146 | } |
||
147 | |||
148 | @Override
|
||
149 | public final void set(ISymbol symbol) { |
||
150 | this.symbol = symbol;
|
||
151 | this.geomType = symbol.getSymbolType();
|
||
152 | this.txtDescription.setText(this.symbol.getDescription()); |
||
153 | this.updatePreview();
|
||
154 | } |
||
155 | |||
156 | @Override
|
||
157 | public void coerceAndSet(Object o) { |
||
158 | this.set((ISymbol) o);
|
||
159 | } |
||
160 | |||
161 | @Override
|
||
162 | public void setEnabled(boolean enabled) { |
||
163 | this.btnPreview.setEnabled(enabled);
|
||
164 | this.txtDescription.setEnabled(enabled);
|
||
165 | } |
||
166 | |||
167 | @Override
|
||
168 | public boolean isEnabled() { |
||
169 | return this.btnPreview.isEnabled(); |
||
170 | } |
||
171 | |||
172 | } |