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 @ 1698
History | View | Annotate | Download (9.57 KB)
1 |
|
---|---|
2 |
package org.gvsig.tools.swing.impl; |
3 |
|
4 |
import java.awt.Color; |
5 |
import java.awt.Dimension; |
6 |
import java.awt.event.ActionEvent; |
7 |
import java.awt.image.BufferedImage; |
8 |
import java.awt.image.WritableRaster; |
9 |
import javax.swing.AbstractAction; |
10 |
import javax.swing.AbstractButton; |
11 |
import javax.swing.Action; |
12 |
import javax.swing.ComboBoxModel; |
13 |
import javax.swing.JButton; |
14 |
import javax.swing.JComboBox; |
15 |
import javax.swing.JLabel; |
16 |
import javax.swing.JList; |
17 |
import javax.swing.JPopupMenu; |
18 |
import javax.swing.JSlider; |
19 |
import javax.swing.JTabbedPane; |
20 |
import javax.swing.JTextField; |
21 |
import javax.swing.text.DefaultEditorKit; |
22 |
import javax.swing.text.JTextComponent; |
23 |
import javax.swing.tree.TreeModel; |
24 |
import org.apache.commons.lang3.StringUtils; |
25 |
import org.gvsig.tools.ToolsLocator; |
26 |
import org.gvsig.tools.i18n.I18nManager; |
27 |
import org.gvsig.tools.swing.api.ActionListenerSupport; |
28 |
import org.gvsig.tools.swing.api.ColorChooserController; |
29 |
import org.gvsig.tools.swing.api.JListWithCheckbox; |
30 |
import org.gvsig.tools.swing.api.ToolsSwingManager; |
31 |
import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper; |
32 |
import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper.VirtualDataBuffer; |
33 |
|
34 |
|
35 |
public class DefaultToolsSwingManager implements ToolsSwingManager { |
36 |
|
37 |
private Dimension maxPhysicalSizeOfBufferedImage; |
38 |
|
39 |
@Override
|
40 |
public ActionListenerSupport createActionListenerSupport() {
|
41 |
return new DefaultActionListenerSupport(); |
42 |
} |
43 |
|
44 |
@Override
|
45 |
public JListWithCheckbox createJListWithCheckbox(JList wrappedList) { |
46 |
return new DefaultJListWithCheckbox(wrappedList); |
47 |
} |
48 |
|
49 |
@Override
|
50 |
public void setTreeModel(JComboBox comboBox, TreeModel aTreeModel) { |
51 |
TreeComboUtils.setTreeModel(comboBox, aTreeModel); |
52 |
} |
53 |
|
54 |
@Override
|
55 |
public ComboBoxModel createComboBoxModel(TreeModel treeModel) { |
56 |
return TreeComboUtils.createComboBoxModel(treeModel);
|
57 |
} |
58 |
|
59 |
|
60 |
@Override
|
61 |
public BufferedImage createBufferedImage(int w, int h, int type) { |
62 |
if(getMaxPhysicalSizeOfBufferedImage().getWidth() < w || getMaxPhysicalSizeOfBufferedImage().getHeight() < h){
|
63 |
return createVirtualBufferedImage(w,h,type);
|
64 |
} |
65 |
return new BufferedImage(w,h,type); |
66 |
} |
67 |
|
68 |
@Override
|
69 |
public BufferedImage createVirtualBufferedImage(int w, int h, int type) { |
70 |
return VirtualBufferedImageHelper.createVirtualBufferedImage(w, h, type);
|
71 |
} |
72 |
|
73 |
@Override
|
74 |
public BufferedImage copyBufferedImage(BufferedImage img) { |
75 |
WritableRaster sourceRaster = img.getRaster();
|
76 |
|
77 |
BufferedImage newImage;
|
78 |
if(sourceRaster.getDataBuffer() instanceof VirtualDataBuffer ){ |
79 |
newImage = VirtualBufferedImageHelper.createVirtualBufferedImage( |
80 |
img.getWidth(), |
81 |
img.getHeight(), |
82 |
img.getSampleModel(), |
83 |
img.getColorModel() |
84 |
); |
85 |
} else {
|
86 |
newImage = createBufferedImage(img.getWidth(), img.getHeight(), img.getType()); |
87 |
} |
88 |
|
89 |
WritableRaster raster = newImage.getRaster();
|
90 |
img.copyData(raster); |
91 |
return newImage;
|
92 |
} |
93 |
|
94 |
@Override
|
95 |
public void setMaxPhysicalSizeOfBufferedImage(Dimension dimension) { |
96 |
this.maxPhysicalSizeOfBufferedImage = dimension;
|
97 |
} |
98 |
|
99 |
@Override
|
100 |
public Dimension getMaxPhysicalSizeOfBufferedImage() { |
101 |
if(this.maxPhysicalSizeOfBufferedImage == null){ |
102 |
this.maxPhysicalSizeOfBufferedImage = new Dimension(2000, 2000); |
103 |
} |
104 |
return this.maxPhysicalSizeOfBufferedImage; |
105 |
} |
106 |
|
107 |
@Override
|
108 |
public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
109 |
return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha, allowNull); |
110 |
} |
111 |
|
112 |
@Override
|
113 |
public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog) { |
114 |
return new DefaultColorChooserController(txtLabel, btnShowDialog); |
115 |
} |
116 |
|
117 |
@Override
|
118 |
public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) { |
119 |
return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha); |
120 |
} |
121 |
|
122 |
@Override
|
123 |
public Color alphaBlendingWithOpaqueBackground(Color bgColor, Color fgColor) { |
124 |
// https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
125 |
Color src = fgColor;
|
126 |
Color dst = bgColor;
|
127 |
|
128 |
// double outa = 1;
|
129 |
double srca = src.getAlpha() / 255.0; |
130 |
double srca_1 = (1 - srca); |
131 |
|
132 |
Color color = new Color( |
133 |
(int)(src.getRed() * srca + dst.getRed() * srca_1) & 0xff, |
134 |
(int)(src.getGreen()* srca + dst.getGreen()* srca_1) & 0xff, |
135 |
(int)(src.getBlue() * srca + dst.getBlue() * srca_1) & 0xff |
136 |
); |
137 |
return color;
|
138 |
} |
139 |
|
140 |
@Override
|
141 |
public Color opaqueColor(Color src) { |
142 |
// https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
143 |
double srca = src.getAlpha() / 255.0; |
144 |
double srca_1 = (1 - srca); |
145 |
|
146 |
Color color = new Color( |
147 |
(int)(src.getRed() * srca + 255 * srca_1) & 0xff, |
148 |
(int)(src.getGreen()* srca + 255 * srca_1) & 0xff, |
149 |
(int)(src.getBlue() * srca + 255 * srca_1) & 0xff |
150 |
); |
151 |
return color;
|
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void translate(AbstractButton component) { |
156 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
157 |
String s = component.getText();
|
158 |
if( !StringUtils.isEmpty(s) ) {
|
159 |
component.setText(i18n.getTranslation(s)); |
160 |
} |
161 |
s = component.getToolTipText(); |
162 |
if( !StringUtils.isEmpty(s) ) {
|
163 |
component.setToolTipText(i18n.getTranslation(s)); |
164 |
} |
165 |
} |
166 |
|
167 |
@Override
|
168 |
public void translate(JLabel component) { |
169 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
170 |
String s = component.getText();
|
171 |
if( !StringUtils.isEmpty(s) ) {
|
172 |
component.setText(i18n.getTranslation(s)); |
173 |
} |
174 |
s = component.getToolTipText(); |
175 |
if( !StringUtils.isEmpty(s) ) {
|
176 |
component.setToolTipText(i18n.getTranslation(s)); |
177 |
} |
178 |
} |
179 |
|
180 |
public void translate(JTabbedPane component) { |
181 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
182 |
|
183 |
for( int i=0; i<component.getTabCount(); i++) { |
184 |
String text = component.getTitleAt(i);
|
185 |
if( !StringUtils.isEmpty(text) ) {
|
186 |
component.setTitleAt(i, i18n.getTranslation(text)); |
187 |
} |
188 |
text = component.getToolTipTextAt(i); |
189 |
if( !StringUtils.isEmpty(text) ) {
|
190 |
component.setToolTipTextAt(i, i18n.getTranslation(text)); |
191 |
} |
192 |
} |
193 |
} |
194 |
|
195 |
@Override
|
196 |
public void setDefaultPopupMenu(final JTextComponent component) { |
197 |
this.setDefaultPopupMenu(component, null); |
198 |
} |
199 |
|
200 |
@Override
|
201 |
public void setDefaultPopupMenu(final JTextComponent component, String title) { |
202 |
JPopupMenu popupMenu = new JPopupMenu(); |
203 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
204 |
|
205 |
Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction); |
206 |
Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction); |
207 |
Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction); |
208 |
Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction); |
209 |
|
210 |
if (copyAction == null |
211 |
&& cutAction == null
|
212 |
&& pasteAction == null
|
213 |
&& selectAllAction == null) {
|
214 |
copyAction = component.getActionMap().get(i18nManager.getTranslation("copy"));
|
215 |
cutAction = component.getActionMap().get(i18nManager.getTranslation("cut"));
|
216 |
pasteAction = component.getActionMap().get(i18nManager.getTranslation("paste"));
|
217 |
selectAllAction = component.getActionMap().get(i18nManager.getTranslation("SelectAll"));
|
218 |
} else {
|
219 |
copyAction.putValue(Action.NAME, i18nManager.getTranslation("copy")); |
220 |
cutAction.putValue(Action.NAME, i18nManager.getTranslation("cut")); |
221 |
pasteAction.putValue(Action.NAME, i18nManager.getTranslation("paste")); |
222 |
selectAllAction.putValue(Action.NAME, i18nManager.getTranslation("SelectAll")); |
223 |
} |
224 |
|
225 |
final String title2; |
226 |
if( title == null ) { |
227 |
title2 = i18nManager.getTranslation("text_editor");
|
228 |
} else {
|
229 |
title2 = title; |
230 |
} |
231 |
Action textEditorAction = new AbstractAction(i18nManager.getTranslation("text_editor")) { |
232 |
@Override
|
233 |
public void actionPerformed(ActionEvent e) { |
234 |
DefaultZoomDialog dialog = new DefaultZoomDialog(title2, component.getText());
|
235 |
dialog.setEditable(component.isEditable()); |
236 |
dialog.setAlwaysOnTop(true);
|
237 |
dialog.setVisible(true);
|
238 |
if (component.isEditable() && component.isEnabled()) {
|
239 |
component.setText(dialog.getText()); |
240 |
} |
241 |
} |
242 |
}; |
243 |
popupMenu.add(textEditorAction); |
244 |
popupMenu.addSeparator(); |
245 |
|
246 |
popupMenu.add(cutAction); |
247 |
popupMenu.add(copyAction); |
248 |
popupMenu.add(pasteAction); |
249 |
popupMenu.add(selectAllAction); |
250 |
|
251 |
component.setComponentPopupMenu(popupMenu); |
252 |
} |
253 |
|
254 |
@Override
|
255 |
public void setDefaultPopupMenu(JComboBox component) { |
256 |
this.setDefaultPopupMenu((JTextComponent) component.getEditor().getEditorComponent(), null); |
257 |
} |
258 |
|
259 |
|
260 |
@Override
|
261 |
public void setDefaultPopupMenu(JComboBox component, String title) { |
262 |
this.setDefaultPopupMenu((JTextComponent) component.getEditor().getEditorComponent(), title); |
263 |
} |
264 |
|
265 |
} |