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 | 1251 | jjdelcerro | |
---|---|---|---|
2 | package org.gvsig.tools.swing.impl; |
||
3 | |||
4 | 1628 | jjdelcerro | import java.awt.Color; |
5 | 1530 | fdiaz | import java.awt.Dimension; |
6 | 1698 | jjdelcerro | import java.awt.event.ActionEvent; |
7 | 1530 | fdiaz | import java.awt.image.BufferedImage; |
8 | import java.awt.image.WritableRaster; |
||
9 | 1698 | jjdelcerro | import javax.swing.AbstractAction; |
10 | 1640 | jjdelcerro | import javax.swing.AbstractButton; |
11 | 1698 | jjdelcerro | import javax.swing.Action; |
12 | 1424 | jjdelcerro | import javax.swing.ComboBoxModel; |
13 | 1610 | jjdelcerro | import javax.swing.JButton; |
14 | 1377 | jjdelcerro | import javax.swing.JComboBox; |
15 | 1640 | jjdelcerro | import javax.swing.JLabel; |
16 | 1377 | jjdelcerro | import javax.swing.JList; |
17 | 1698 | jjdelcerro | import javax.swing.JPopupMenu; |
18 | 1610 | jjdelcerro | import javax.swing.JSlider; |
19 | 1683 | jjdelcerro | import javax.swing.JTabbedPane; |
20 | 1610 | jjdelcerro | import javax.swing.JTextField; |
21 | 1698 | jjdelcerro | import javax.swing.text.DefaultEditorKit; |
22 | import javax.swing.text.JTextComponent; |
||
23 | 1377 | jjdelcerro | import javax.swing.tree.TreeModel; |
24 | 1644 | jjdelcerro | import org.apache.commons.lang3.StringUtils; |
25 | 1640 | jjdelcerro | import org.gvsig.tools.ToolsLocator; |
26 | import org.gvsig.tools.i18n.I18nManager; |
||
27 | 1251 | jjdelcerro | import org.gvsig.tools.swing.api.ActionListenerSupport; |
28 | 1610 | jjdelcerro | import org.gvsig.tools.swing.api.ColorChooserController; |
29 | 1377 | jjdelcerro | import org.gvsig.tools.swing.api.JListWithCheckbox; |
30 | 1251 | jjdelcerro | import org.gvsig.tools.swing.api.ToolsSwingManager; |
31 | 1538 | fdiaz | import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper; |
32 | import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper.VirtualDataBuffer; |
||
33 | 1251 | jjdelcerro | |
34 | |||
35 | public class DefaultToolsSwingManager implements ToolsSwingManager { |
||
36 | |||
37 | 1530 | fdiaz | private Dimension maxPhysicalSizeOfBufferedImage; |
38 | |||
39 | 1377 | jjdelcerro | @Override
|
40 | 1251 | jjdelcerro | public ActionListenerSupport createActionListenerSupport() {
|
41 | return new DefaultActionListenerSupport(); |
||
42 | } |
||
43 | 1377 | jjdelcerro | |
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 | 1530 | fdiaz | |
54 | 1538 | fdiaz | @Override
|
55 | 1424 | jjdelcerro | public ComboBoxModel createComboBoxModel(TreeModel treeModel) { |
56 | return TreeComboUtils.createComboBoxModel(treeModel);
|
||
57 | 1530 | fdiaz | } |
58 | |||
59 | |||
60 | 1538 | fdiaz | @Override
|
61 | 1530 | fdiaz | 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 | 1538 | fdiaz | @Override
|
69 | 1530 | fdiaz | public BufferedImage createVirtualBufferedImage(int w, int h, int type) { |
70 | 1538 | fdiaz | return VirtualBufferedImageHelper.createVirtualBufferedImage(w, h, type);
|
71 | 1530 | fdiaz | } |
72 | |||
73 | 1538 | fdiaz | @Override
|
74 | 1530 | fdiaz | public BufferedImage copyBufferedImage(BufferedImage img) { |
75 | WritableRaster sourceRaster = img.getRaster();
|
||
76 | |||
77 | BufferedImage newImage;
|
||
78 | 1538 | fdiaz | if(sourceRaster.getDataBuffer() instanceof VirtualDataBuffer ){ |
79 | newImage = VirtualBufferedImageHelper.createVirtualBufferedImage( |
||
80 | 1692 | fdiaz | img.getWidth(), |
81 | img.getHeight(), |
||
82 | img.getSampleModel(), |
||
83 | 1538 | fdiaz | img.getColorModel() |
84 | ); |
||
85 | 1530 | fdiaz | } 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 | 1610 | jjdelcerro | @Override
|
108 | public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) { |
||
109 | return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha, allowNull); |
||
110 | } |
||
111 | 1530 | fdiaz | |
112 | 1610 | jjdelcerro | @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 | 1628 | jjdelcerro | @Override
|
123 | public Color alphaBlendingWithOpaqueBackground(Color bgColor, Color fgColor) { |
||
124 | 1632 | jjdelcerro | // https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
|
125 | 1628 | jjdelcerro | 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 | 1692 | fdiaz | return color;
|
138 | 1628 | jjdelcerro | } |
139 | 1692 | fdiaz | |
140 | 1632 | jjdelcerro | @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 | 1640 | jjdelcerro | |
154 | @Override
|
||
155 | public void translate(AbstractButton component) { |
||
156 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
157 | 1644 | jjdelcerro | 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 | 1683 | jjdelcerro | component.setToolTipText(i18n.getTranslation(s)); |
164 | 1644 | jjdelcerro | } |
165 | 1640 | jjdelcerro | } |
166 | |||
167 | @Override
|
||
168 | public void translate(JLabel component) { |
||
169 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
170 | 1644 | jjdelcerro | 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 | 1683 | jjdelcerro | component.setToolTipText(i18n.getTranslation(s)); |
177 | 1644 | jjdelcerro | } |
178 | 1640 | jjdelcerro | } |
179 | 1692 | fdiaz | |
180 | 1683 | jjdelcerro | public void translate(JTabbedPane component) { |
181 | I18nManager i18n = ToolsLocator.getI18nManager(); |
||
182 | 1692 | fdiaz | |
183 | 1683 | jjdelcerro | 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 | 1692 | fdiaz | } |
194 | 1698 | jjdelcerro | |
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 | 1251 | jjdelcerro | } |