gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / DefaultToolsSwingManager.java @ 1644
History | View | Annotate | Download (5.83 KB)
1 |
|
---|---|
2 |
package org.gvsig.tools.swing.impl; |
3 |
|
4 |
import java.awt.Color; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.image.BufferedImage; |
7 |
import java.awt.image.WritableRaster; |
8 |
import javax.swing.AbstractButton; |
9 |
import javax.swing.ComboBoxModel; |
10 |
import javax.swing.JButton; |
11 |
import javax.swing.JComboBox; |
12 |
import javax.swing.JLabel; |
13 |
import javax.swing.JList; |
14 |
import javax.swing.JSlider; |
15 |
import javax.swing.JTextField; |
16 |
import javax.swing.tree.TreeModel; |
17 |
import org.apache.commons.lang3.StringUtils; |
18 |
import org.gvsig.tools.ToolsLocator; |
19 |
import org.gvsig.tools.i18n.I18nManager; |
20 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
21 |
import org.gvsig.tools.swing.api.ColorChooserController; |
22 |
import org.gvsig.tools.swing.api.JListWithCheckbox; |
23 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
24 |
import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper; |
25 |
import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper.VirtualDataBuffer; |
26 |
|
27 |
|
28 |
public class DefaultToolsSwingManager implements ToolsSwingManager { |
29 |
|
30 |
private Dimension maxPhysicalSizeOfBufferedImage; |
31 |
|
32 |
@Override
|
33 |
public ActionListenerSupport createActionListenerSupport() {
|
34 |
return new DefaultActionListenerSupport(); |
35 |
} |
36 |
|
37 |
@Override
|
38 |
public JListWithCheckbox createJListWithCheckbox(JList wrappedList) { |
39 |
return new DefaultJListWithCheckbox(wrappedList); |
40 |
} |
41 |
|
42 |
@Override
|
43 |
public void setTreeModel(JComboBox comboBox, TreeModel aTreeModel) { |
44 |
TreeComboUtils.setTreeModel(comboBox, aTreeModel); |
45 |
} |
46 |
|
47 |
@Override
|
48 |
public ComboBoxModel createComboBoxModel(TreeModel treeModel) { |
49 |
return TreeComboUtils.createComboBoxModel(treeModel);
|
50 |
} |
51 |
|
52 |
|
53 |
@Override
|
54 |
public BufferedImage createBufferedImage(int w, int h, int type) { |
55 |
if(getMaxPhysicalSizeOfBufferedImage().getWidth() < w || getMaxPhysicalSizeOfBufferedImage().getHeight() < h){
|
56 |
return createVirtualBufferedImage(w,h,type);
|
57 |
} |
58 |
return new BufferedImage(w,h,type); |
59 |
} |
60 |
|
61 |
@Override
|
62 |
public BufferedImage createVirtualBufferedImage(int w, int h, int type) { |
63 |
return VirtualBufferedImageHelper.createVirtualBufferedImage(w, h, type);
|
64 |
} |
65 |
|
66 |
@Override
|
67 |
public BufferedImage copyBufferedImage(BufferedImage img) { |
68 |
WritableRaster sourceRaster = img.getRaster();
|
69 |
|
70 |
BufferedImage newImage;
|
71 |
if(sourceRaster.getDataBuffer() instanceof VirtualDataBuffer ){ |
72 |
newImage = VirtualBufferedImageHelper.createVirtualBufferedImage( |
73 |
img.getWidth(), |
74 |
img.getHeight(), |
75 |
img.getSampleModel(), |
76 |
img.getColorModel() |
77 |
); |
78 |
} else {
|
79 |
newImage = createBufferedImage(img.getWidth(), img.getHeight(), img.getType()); |
80 |
} |
81 |
|
82 |
WritableRaster raster = newImage.getRaster();
|
83 |
img.copyData(raster); |
84 |
return newImage;
|
85 |
} |
86 |
|
87 |
@Override
|
88 |
public void setMaxPhysicalSizeOfBufferedImage(Dimension dimension) { |
89 |
this.maxPhysicalSizeOfBufferedImage = dimension;
|
90 |
} |
91 |
|
92 |
@Override
|
93 |
public Dimension getMaxPhysicalSizeOfBufferedImage() { |
94 |
if(this.maxPhysicalSizeOfBufferedImage == null){ |
95 |
this.maxPhysicalSizeOfBufferedImage = new Dimension(2000, 2000); |
96 |
} |
97 |
return this.maxPhysicalSizeOfBufferedImage; |
98 |
} |
99 |
|
100 |
@Override
|
101 |
public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
102 |
return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha, allowNull); |
103 |
} |
104 |
|
105 |
@Override
|
106 |
public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog) { |
107 |
return new DefaultColorChooserController(txtLabel, btnShowDialog); |
108 |
} |
109 |
|
110 |
@Override
|
111 |
public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
112 |
return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha); |
113 |
} |
114 |
|
115 |
@Override
|
116 |
public Color alphaBlendingWithOpaqueBackground(Color bgColor, Color fgColor) { |
117 |
// https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
118 |
Color src = fgColor;
|
119 |
Color dst = bgColor;
|
120 |
|
121 |
// double outa = 1;
|
122 |
double srca = src.getAlpha() / 255.0; |
123 |
double srca_1 = (1 - srca); |
124 |
|
125 |
Color color = new Color( |
126 |
(int)(src.getRed() * srca + dst.getRed() * srca_1) & 0xff, |
127 |
(int)(src.getGreen()* srca + dst.getGreen()* srca_1) & 0xff, |
128 |
(int)(src.getBlue() * srca + dst.getBlue() * srca_1) & 0xff |
129 |
); |
130 |
return color;
|
131 |
} |
132 |
|
133 |
@Override
|
134 |
public Color opaqueColor(Color src) { |
135 |
// https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
136 |
double srca = src.getAlpha() / 255.0; |
137 |
double srca_1 = (1 - srca); |
138 |
|
139 |
Color color = new Color( |
140 |
(int)(src.getRed() * srca + 255 * srca_1) & 0xff, |
141 |
(int)(src.getGreen()* srca + 255 * srca_1) & 0xff, |
142 |
(int)(src.getBlue() * srca + 255 * srca_1) & 0xff |
143 |
); |
144 |
return color;
|
145 |
} |
146 |
|
147 |
@Override
|
148 |
public void translate(AbstractButton component) { |
149 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
150 |
String s = component.getText();
|
151 |
if( !StringUtils.isEmpty(s) ) {
|
152 |
component.setText(i18n.getTranslation(s)); |
153 |
} |
154 |
s = component.getToolTipText(); |
155 |
if( !StringUtils.isEmpty(s) ) {
|
156 |
component.setText(i18n.getTranslation(s)); |
157 |
} |
158 |
} |
159 |
|
160 |
@Override
|
161 |
public void translate(JLabel component) { |
162 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
163 |
String s = component.getText();
|
164 |
if( !StringUtils.isEmpty(s) ) {
|
165 |
component.setText(i18n.getTranslation(s)); |
166 |
} |
167 |
s = component.getToolTipText(); |
168 |
if( !StringUtils.isEmpty(s) ) {
|
169 |
component.setText(i18n.getTranslation(s)); |
170 |
} |
171 |
} |
172 |
} |