Revision 1760

View differences:

org.gvsig.tools/library/tags/org.gvsig.tools-3.0.163/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultToolsSwingManager.java
1

  
2
package org.gvsig.tools.swing.impl;
3

  
4
import java.awt.BorderLayout;
5
import java.awt.Color;
6
import java.awt.Dimension;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.awt.image.BufferedImage;
10
import java.awt.image.WritableRaster;
11
import java.io.File;
12
import java.lang.reflect.InvocationTargetException;
13
import java.lang.reflect.Method;
14
import javax.swing.AbstractAction;
15
import javax.swing.AbstractButton;
16
import javax.swing.Action;
17
import javax.swing.ComboBoxModel;
18
import javax.swing.ImageIcon;
19
import javax.swing.JButton;
20
import javax.swing.JComboBox;
21
import javax.swing.JComponent;
22
import javax.swing.JLabel;
23
import javax.swing.JList;
24
import javax.swing.JPopupMenu;
25
import javax.swing.JScrollPane;
26
import javax.swing.JSlider;
27
import javax.swing.JTabbedPane;
28
import javax.swing.JTextField;
29
import javax.swing.JViewport;
30
import javax.swing.text.DefaultEditorKit;
31
import javax.swing.text.JTextComponent;
32
import javax.swing.tree.TreeModel;
33
import org.apache.commons.lang3.StringUtils;
34
import org.gvsig.tools.ToolsLocator;
35
import org.gvsig.tools.i18n.I18nManager;
36
import org.gvsig.tools.swing.api.ActionListenerSupport;
37
import org.gvsig.tools.swing.api.ChangeListenerHelper;
38
import org.gvsig.tools.swing.api.ColorChooserController;
39
import org.gvsig.tools.swing.api.JListWithCheckbox;
40
import org.gvsig.tools.swing.api.ToolsSwingLocator;
41
import org.gvsig.tools.swing.api.ToolsSwingManager;
42
import org.gvsig.tools.swing.api.pickercontroller.ColorPickerController;
43
import org.gvsig.tools.swing.api.pickercontroller.DatePickerController;
44
import org.gvsig.tools.swing.api.pickercontroller.FilePickerController;
45
import org.gvsig.tools.swing.api.pickercontroller.FolderPickerController;
46
import org.gvsig.tools.swing.icontheme.IconTheme;
47
import org.gvsig.tools.swing.icontheme.IconThemeManager;
48
import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper;
49
import org.gvsig.tools.swing.impl.bufferedImage.VirtualBufferedImageHelper.VirtualDataBuffer;
50
import org.gvsig.tools.swing.impl.pickercontroller.ColorPickerControllerImpl;
51
import org.gvsig.tools.swing.impl.pickercontroller.DatePickerControllerImpl;
52
import org.gvsig.tools.swing.impl.pickercontroller.FilePickerControllerImpl;
53
import org.gvsig.tools.swing.impl.pickercontroller.FolderPickerControllerImpl;
54

  
55

  
56
public class DefaultToolsSwingManager implements ToolsSwingManager {
57

  
58
    private Dimension maxPhysicalSizeOfBufferedImage;
59

  
60
    @Override
61
    public ActionListenerSupport createActionListenerSupport() {
62
        return new DefaultActionListenerSupport();
63
    }
64

  
65
    @Override
66
    public JListWithCheckbox createJListWithCheckbox(JList wrappedList) {
67
        return new DefaultJListWithCheckbox(wrappedList);
68
    }
69

  
70
    @Override
71
    public void setTreeModel(JComboBox comboBox, TreeModel aTreeModel) {
72
        TreeComboUtils.setTreeModel(comboBox, aTreeModel);
73
    }
74

  
75
    @Override
76
    public ComboBoxModel createComboBoxModel(TreeModel treeModel) {
77
        return TreeComboUtils.createComboBoxModel(treeModel);
78
    }
79

  
80

  
81
    @Override
82
    public BufferedImage createBufferedImage(int w, int h, int type) {
83
        if(getMaxPhysicalSizeOfBufferedImage().getWidth() < w || getMaxPhysicalSizeOfBufferedImage().getHeight() < h){
84
            return createVirtualBufferedImage(w,h,type);
85
        }
86
        return new BufferedImage(w,h,type);
87
    }
88

  
89
    @Override
90
    public BufferedImage createVirtualBufferedImage(int w, int h, int type) {
91
        return VirtualBufferedImageHelper.createVirtualBufferedImage(w, h, type);
92
    }
93

  
94
    @Override
95
    public BufferedImage copyBufferedImage(BufferedImage img) {
96
        WritableRaster sourceRaster = img.getRaster();
97

  
98
        BufferedImage newImage;
99
        if(sourceRaster.getDataBuffer() instanceof VirtualDataBuffer ){
100
            newImage = VirtualBufferedImageHelper.createVirtualBufferedImage(
101
                img.getWidth(),
102
                img.getHeight(),
103
                img.getSampleModel(),
104
                img.getColorModel()
105
            );
106
        } else {
107
            newImage = createBufferedImage(img.getWidth(), img.getHeight(), img.getType());
108
        }
109

  
110
        WritableRaster raster = newImage.getRaster();
111
        img.copyData(raster);
112
        return newImage;
113
    }
114

  
115
    @Override
116
    public void setMaxPhysicalSizeOfBufferedImage(Dimension dimension) {
117
        this.maxPhysicalSizeOfBufferedImage = dimension;
118
    }
119

  
120
    @Override
121
    public Dimension getMaxPhysicalSizeOfBufferedImage() {
122
        if(this.maxPhysicalSizeOfBufferedImage == null){
123
            this.maxPhysicalSizeOfBufferedImage = new Dimension(2000, 2000);
124
        }
125
        return this.maxPhysicalSizeOfBufferedImage;
126
    }
127

  
128
    @Override
129
    public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) {
130
        return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha, allowNull);
131
    }
132

  
133
    @Override
134
    public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog) {
135
        return new DefaultColorChooserController(txtLabel, btnShowDialog);
136
    }
137

  
138
    @Override
139
    public ColorChooserController createColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) {
140
        return new DefaultColorChooserController(txtLabel, btnShowDialog, sldAlpha);
141
    }
142

  
143
    @Override
144
    public Color alphaBlendingWithOpaqueBackground(Color bgColor, Color fgColor) {
145
        // https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
146
        Color src = fgColor;
147
        Color dst = bgColor;
148

  
149
        // double outa = 1;
150
        double srca = src.getAlpha() / 255.0;
151
        double srca_1 = (1 - srca);
152

  
153
        Color color = new Color(
154
            (int)(src.getRed()  * srca + dst.getRed()  * srca_1) & 0xff,
155
            (int)(src.getGreen()* srca + dst.getGreen()* srca_1) & 0xff,
156
            (int)(src.getBlue() * srca + dst.getBlue() * srca_1) & 0xff
157
        );
158
        return color;
159
    }
160

  
161
    @Override
162
    public Color opaqueColor(Color src) {
163
        // https://en.wikipedia.org/wiki/Alpha_compositing#Alpha_blending
164
        double srca = src.getAlpha() / 255.0;
165
        double srca_1 = (1 - srca);
166

  
167
        Color color = new Color(
168
            (int)(src.getRed()  * srca + 255  * srca_1) & 0xff,
169
            (int)(src.getGreen()* srca + 255 * srca_1) & 0xff,
170
            (int)(src.getBlue() * srca + 255 * srca_1) & 0xff
171
        );
172
        return color;
173
    }
174

  
175
    private Object call(Object obj, String name, Object... params) {
176
        Class[] types = null;
177
        if( params != null ) {
178
            types = new Class[params.length];
179
            for (int i = 0; i < params.length; i++) {
180
                types[i] = params[i].getClass();
181
            }
182
        }
183
        Method method;
184
        try {
185
            method = obj.getClass().getMethod(name, types);
186
            Object r = method.invoke(obj, params);
187
            return r;
188
        } catch (NoSuchMethodException | 
189
                SecurityException | 
190
                IllegalAccessException | 
191
                IllegalArgumentException | 
192
                InvocationTargetException ex) {
193
        }
194
        return  null;
195
    }
196
    
197
    @Override
198
    public void translate(JComponent component) {
199
        I18nManager i18n = ToolsLocator.getI18nManager();
200
        String s = (String) call(component, "getText", (Object[]) null);
201
        if( !StringUtils.isEmpty(s) ) {
202
            call(component,"setText",i18n.getTranslation(s));
203
        }
204
        s = (String) call(component, "getToolTipText", (Object[]) null);
205
        if( !StringUtils.isEmpty(s) ) {
206
            call(component,"setToolTipText",i18n.getTranslation(s));
207
        }
208
    }
209
    
210
    @Override
211
    public void translate(AbstractButton component) {
212
        I18nManager i18n = ToolsLocator.getI18nManager();
213
        String s = component.getText();
214
        if( !StringUtils.isEmpty(s) ) {
215
            component.setText(i18n.getTranslation(s));
216
        }
217
        s = component.getToolTipText();
218
        if( !StringUtils.isEmpty(s) ) {
219
            component.setToolTipText(i18n.getTranslation(s));
220
        }
221
    }
222

  
223
    @Override
224
    public void translate(JLabel component) {
225
        I18nManager i18n = ToolsLocator.getI18nManager();
226
        String s = component.getText();
227
        if( !StringUtils.isEmpty(s) ) {
228
            component.setText(i18n.getTranslation(s));
229
        }
230
        s = component.getToolTipText();
231
        if( !StringUtils.isEmpty(s) ) {
232
            component.setToolTipText(i18n.getTranslation(s));
233
        }
234
    }
235

  
236
    @Override
237
    public void translate(JTabbedPane component) {
238
        I18nManager i18n = ToolsLocator.getI18nManager();
239

  
240
        for( int i=0; i<component.getTabCount(); i++) {
241
            String text = component.getTitleAt(i);
242
            if( !StringUtils.isEmpty(text) ) {
243
                component.setTitleAt(i, i18n.getTranslation(text));
244
            }
245
            text = component.getToolTipTextAt(i);
246
            if( !StringUtils.isEmpty(text) ) {
247
                component.setToolTipTextAt(i, i18n.getTranslation(text));
248
            }
249
        }
250
    }
251
    
252
    @Override
253
    public void setDefaultPopupMenu(final JTextComponent component) {
254
        this.setDefaultPopupMenu(component, null);
255
    }
256

  
257
    @Override
258
    public void setDefaultPopupMenu(final JTextComponent component, String title) {
259
        JPopupMenu popupMenu = new JPopupMenu();
260
        I18nManager i18nManager = ToolsLocator.getI18nManager();
261

  
262
        Action copyAction = component.getActionMap().get(DefaultEditorKit.copyAction);
263
        Action cutAction = component.getActionMap().get(DefaultEditorKit.cutAction);
264
        Action pasteAction = component.getActionMap().get(DefaultEditorKit.pasteAction);
265
        Action selectAllAction = component.getActionMap().get(DefaultEditorKit.selectAllAction);
266

  
267
        if (copyAction == null
268
                && cutAction == null
269
                && pasteAction == null
270
                && selectAllAction == null) {
271
            copyAction = component.getActionMap().get(i18nManager.getTranslation("copy"));
272
            cutAction = component.getActionMap().get(i18nManager.getTranslation("cut"));
273
            pasteAction = component.getActionMap().get(i18nManager.getTranslation("paste"));
274
            selectAllAction = component.getActionMap().get(i18nManager.getTranslation("SelectAll"));
275
        } else {
276
            copyAction.putValue(Action.NAME, i18nManager.getTranslation("copy"));
277
            cutAction.putValue(Action.NAME, i18nManager.getTranslation("cut"));
278
            pasteAction.putValue(Action.NAME, i18nManager.getTranslation("paste"));
279
            selectAllAction.putValue(Action.NAME, i18nManager.getTranslation("SelectAll"));
280
        }
281

  
282
        final String title2;
283
        if( title == null ) {
284
            title2 = i18nManager.getTranslation("text_editor");
285
        } else {
286
            title2 = title;
287
        }
288
        Action textEditorAction = new AbstractAction(i18nManager.getTranslation("text_editor")) {
289
            @Override
290
            public void actionPerformed(ActionEvent e) {
291
                DefaultZoomDialog dialog = new DefaultZoomDialog(title2, component.getText());
292
                dialog.setEditable(component.isEditable());
293
                dialog.setAlwaysOnTop(true);
294
                dialog.setVisible(true);
295
                if (component.isEditable() && component.isEnabled()) {
296
                    component.setText(dialog.getText());
297
                }
298
            }
299
        };
300
        popupMenu.add(textEditorAction);
301
        popupMenu.addSeparator();
302

  
303
        popupMenu.add(cutAction);
304
        popupMenu.add(copyAction);
305
        popupMenu.add(pasteAction);
306
        popupMenu.add(selectAllAction);
307

  
308
        component.setComponentPopupMenu(popupMenu);
309
    }
310

  
311
    @Override
312
    public void setDefaultPopupMenu(JComboBox component) {
313
        this.setDefaultPopupMenu((JTextComponent) component.getEditor().getEditorComponent(), null);
314
    }
315

  
316

  
317
    @Override
318
    public void setDefaultPopupMenu(JComboBox component, String title) {
319
        this.setDefaultPopupMenu((JTextComponent) component.getEditor().getEditorComponent(), title);
320
    }
321

  
322
    @Override
323
    public ChangeListenerHelper createChangeListenerHelper() {
324
        DefaultChangeListenerHelper x = new DefaultChangeListenerHelper();
325
        return x;
326
    }
327

  
328
    @Override
329
    public ColorPickerController createColorPickerController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) {
330
        return new ColorPickerControllerImpl(txtLabel, btnShowDialog, sldAlpha, allowNull);
331
    }
332

  
333
    @Override
334
    public ColorPickerController createColorPickerController(JTextField txtLabel, JButton btnShowDialog) {
335
        return new ColorPickerControllerImpl(txtLabel, btnShowDialog);
336
    } 
337

  
338
    @Override
339
    public ColorPickerController createColorPickerController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) {
340
        return new ColorPickerControllerImpl(txtLabel, btnShowDialog, sldAlpha);
341
    }
342

  
343
    @Override
344
    public DatePickerController createDatePickerController(JTextField txtDate, JButton btnDate) {
345
        return new DatePickerControllerImpl(txtDate, btnDate);
346
    }
347

  
348
    @Override
349
    public FilePickerController createFilePickerController(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) {
350
        return new FilePickerControllerImpl(txtFile, btnFile, dialogTitle, fileChooserID, initialPath, seticon);
351
    }
352

  
353
    @Override
354
    public FilePickerController createFilePickerController(JTextField txtFile, JButton btnFile) {
355
        return new FilePickerControllerImpl(txtFile, btnFile);
356
    }
357

  
358
    @Override
359
    public FilePickerController createFilePickerController(JTextField txtFile, JButton btnFile, String dialogTitle) {
360
        return new FilePickerControllerImpl(txtFile, btnFile, dialogTitle);
361
    }
362

  
363
    @Override
364
    public FolderPickerController createFolderPickerController(JTextField txtFile, JButton btnFile, String dialogTitle, String fileChooserID, File initialPath, boolean seticon) {
365
        return new FolderPickerControllerImpl(txtFile, btnFile, dialogTitle, fileChooserID, initialPath, seticon);
366
    }
367

  
368
    @Override
369
    public FolderPickerController createFolderPickerController(JTextField txtFile, JButton btnFile) {
370
        return new FolderPickerControllerImpl(txtFile, btnFile);
371
    }
372

  
373
    @Override
374
    public FolderPickerController createFolderPickerController(JTextField txtFile, JButton btnFile, String dialogTitle) {
375
        return new FolderPickerControllerImpl(txtFile, btnFile,dialogTitle);
376
    }
377

  
378
    @Override
379
    public void removeBorder(JComponent component) {
380
        try {
381
            component.setBorder(null);
382
            JComponent p1 = (JComponent) component.getParent();
383
            if( p1 instanceof JViewport ) {
384
                p1 = (JComponent) component.getParent();
385
            }
386
            if( p1 instanceof JScrollPane ) {
387
                JScrollPane scrollPanel = (JScrollPane) component.getParent();
388
                scrollPanel.setBorder(null);
389
            }
390
        } catch(Exception ex) {
391
            
392
        }
393
    }
394

  
395
    protected ImageIcon getIcon(String name) {
396
        IconThemeManager iconThemeManager = ToolsSwingLocator.getIconThemeManager();
397
        IconTheme theme = iconThemeManager.getCurrent();
398
        ImageIcon icon = theme.get(name);
399
        return icon;
400
    }
401

  
402
    @Override
403
    public void addClearButton(JTextField text) {
404
        this.addClearButton(text, null);
405
    }
406

  
407
    @Override
408
    public void addClearButton(final JTextField text,  final ActionListener action) {
409
        JButton btnClear = new JButton(getIcon("picker-cleartext"));
410
        btnClear.setBorderPainted( false );
411
        btnClear.setFocusPainted( false );
412
        btnClear.setContentAreaFilled( false );        
413
        btnClear.setOpaque(false);        
414
        if( action == null ) {
415
            btnClear.addActionListener(new ActionListener() {
416
                @Override
417
                public void actionPerformed(ActionEvent e) {
418
                    text.setText("");
419
                }
420
            });        
421
        } else {
422
            btnClear.addActionListener(action);
423
        }
424
        text.setLayout(new BorderLayout());
425
        text.add(btnClear, BorderLayout.EAST);
426
    }
427
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.163/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultZoomDialog.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.swing.impl;
25

  
26
import java.awt.BorderLayout;
27
import java.awt.Dialog;
28
import java.awt.Dimension;
29
import java.awt.Toolkit;
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32

  
33
import javax.swing.BorderFactory;
34
import javax.swing.JButton;
35
import javax.swing.JDialog;
36
import javax.swing.JPanel;
37
import javax.swing.JScrollPane;
38
import javax.swing.JTextArea;
39

  
40
public class DefaultZoomDialog extends JDialog {
41

  
42
    /**
43
     *
44
     */
45
    private static final long serialVersionUID = 7913363575200612492L;
46

  
47
    private String value = null;
48
    private JTextArea text = null;
49

  
50
    public DefaultZoomDialog(String title) {
51
        super((Dialog) null, title, true);
52
    }
53

  
54
    public DefaultZoomDialog(String title, String value) {
55
        this(title);
56
        this.value = value;
57
        initComponents();
58

  
59
    }
60

  
61
    public void setEditable(boolean editable) {
62
        this.text.setEditable(editable);
63
    }
64

  
65
    private void initComponents() {
66

  
67
        JPanel panel = new JPanel();
68
        panel.setLayout(new BorderLayout());
69

  
70
        text = new JTextArea();
71
        text.setText(value);
72
        text.setLineWrap(true);
73
        JScrollPane scroll = new JScrollPane(text);
74
        panel.add(scroll, BorderLayout.CENTER);
75
        panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
76

  
77
        JPanel p = new JPanel();
78

  
79
        JButton close = new JButton("Close");
80
        close.addActionListener(new ActionListener() {
81
            @Override
82
            public void actionPerformed(ActionEvent arg0) {
83
                value = text.getText();
84
                setVisible(false);
85
            }
86
        });
87

  
88
        p.setLayout(new BorderLayout());
89
        p.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
90
        p.add(close, BorderLayout.LINE_END);
91

  
92
        panel.add(p, BorderLayout.PAGE_END);
93

  
94
        panel.setPreferredSize(new Dimension(600, 250));
95

  
96
        this.setContentPane(panel);
97

  
98
        this.center();
99
        this.pack();
100
    }
101

  
102
    public void center() {
103
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
104
        Dimension size = this.getPreferredSize();
105
        int x = (int) ((dimension.getWidth() - size.getWidth()) / 2);
106
        int y = (int) ((dimension.getHeight() - size.getHeight()) / 2);
107
        this.setLocation(x, y);
108
    }
109

  
110
    public String getText() {
111
        return this.value;
112
    }
113
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.163/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/DefaultColorChooserController.java
1

  
2
package org.gvsig.tools.swing.impl;
3

  
4
import java.awt.Color;
5
import java.awt.Font;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8

  
9
import javax.swing.JButton;
10
import javax.swing.JColorChooser;
11
import javax.swing.JSlider;
12
import javax.swing.JTextField;
13
import javax.swing.SwingUtilities;
14
import javax.swing.event.ChangeEvent;
15
import javax.swing.event.ChangeListener;
16

  
17
import org.gvsig.tools.ToolsLocator;
18
import org.gvsig.tools.i18n.I18nManager;
19
import org.gvsig.tools.swing.api.ColorChooserController;
20
import org.gvsig.tools.swing.api.ToolsSwingLocator;
21
import org.gvsig.tools.swing.api.ToolsSwingManager;
22

  
23
public class DefaultColorChooserController implements ColorChooserController {
24

  
25
    private final JTextField txtLabel;
26
    private final JButton btnShowDialog;
27
    private final JSlider sldAlpha;
28
    private Color color;
29
    private boolean allowNull;
30
    private ChangeListener changeListener;
31

  
32
    public DefaultColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha, boolean allowNull) {
33
        this.txtLabel = txtLabel;
34
        this.btnShowDialog = btnShowDialog;
35
        this.sldAlpha = sldAlpha;
36

  
37
        // Remove the listeners to prevent strange behavior when you call this
38
        // class repeatedly with the same button and slider.
39
        for(ActionListener l : btnShowDialog.getActionListeners()) {
40
            btnShowDialog.removeActionListener(l);
41
        }
42
        for( ChangeListener l : sldAlpha.getChangeListeners()) {
43
            sldAlpha.removeChangeListener(l);
44
        }
45
        this.btnShowDialog.addActionListener(new ActionListener() {
46
            @Override
47
            public void actionPerformed(ActionEvent e) {
48
                doShowDialog();
49
            }
50
        });
51
        if( this.sldAlpha != null ) {
52
            this.sldAlpha.setMinimum(0);
53
            this.sldAlpha.setMaximum(255);
54
            this.sldAlpha.addChangeListener(new ChangeListener() {
55
                @Override
56
                public void stateChanged(ChangeEvent e) {
57
                    doAlphaChanged();
58
                }
59
            });
60
        }
61
        Font font = this.txtLabel.getFont();
62
        try {
63
            font = new Font(Font.MONOSPACED, Font.PLAIN, font.getSize());
64
            this.txtLabel.setFont(font);
65
        } catch(Exception ex) {
66
            // If dont set a monospaced font ignore it.
67
            font = null;
68
        }
69
        this.allowNull = allowNull;
70
        if( !this.allowNull ) {
71
            this.color = Color.BLACK;
72
        }
73
    }
74

  
75
    public DefaultColorChooserController(JTextField txtLabel, JButton btnShowDialog) {
76
        this(txtLabel, btnShowDialog, null, false);
77
    }
78

  
79
    public DefaultColorChooserController(JTextField txtLabel, JButton btnShowDialog, JSlider sldAlpha) {
80
        this(txtLabel, btnShowDialog, sldAlpha, false);
81
    }
82

  
83
    private void doAlphaChanged() {
84
        if( this.color == null ) {
85
            set(Color.BLACK);
86
        }
87
        int alpha = 255;
88
        if( this.sldAlpha == null ) {
89
            set(new Color(
90
                color.getRed(),
91
                color.getGreen(),
92
                color.getBlue()
93
            ));
94
        } else {
95
            alpha = this.sldAlpha.getValue();
96
            set(new Color(
97
                color.getRed(),
98
                color.getGreen(),
99
                color.getBlue(),
100
                alpha
101
            ));
102
        }
103
    }
104

  
105
    @Override
106
    public void addChangeListener(ChangeListener changeListener) {
107
        this.changeListener = changeListener;
108
    }
109

  
110
    protected void fireChanged() {
111
        if( this.changeListener != null ) {
112
            this.changeListener.stateChanged(null);
113
        }
114
    }
115

  
116
    protected void doShowDialog() {
117
        I18nManager i18n = ToolsLocator.getI18nManager();
118
        Color c = JColorChooser.showDialog(
119
            SwingUtilities.windowForComponent(this.btnShowDialog),
120
            i18n.getTranslation("choose_color"),
121
            this.get()
122
        );
123
        if( c == null ) {
124
            return;
125
        }
126
        set(c);
127
    }
128

  
129
    @Override
130
    public void set(Color color) {
131
        if( color == null ) {
132
            if( allowNull ) {
133
                this.color = null;
134
                return;
135
            }
136
            color = Color.BLACK;
137
        }
138
        ToolsSwingManager manager = ToolsSwingLocator.getToolsSwingManager();
139
        this.color = color;
140
        String text = String.format("%02x %02x%02x%02x",color.getAlpha(),color.getRed(),color.getGreen(),color.getBlue());
141
        this.txtLabel.setBackground(manager.opaqueColor(this.color));
142
        this.txtLabel.setText(text);
143
        if( this.sldAlpha != null ) {
144
            this.sldAlpha.setValue(this.color.getAlpha());
145
        }
146
        this.txtLabel.invalidate();
147
    }
148

  
149
    @Override
150
    public Color get() {
151
        return this.color;
152
    }
153

  
154
    @Override
155
    public void setEnabled(boolean enabled) {
156
        this.txtLabel.setEditable(enabled);
157
        this.btnShowDialog.setEnabled(enabled);
158
        if( this.sldAlpha!=null ) {
159
            this.sldAlpha.setEnabled(enabled);
160
        }
161
    }
162

  
163
    @Override
164
    public boolean isEnabled() {
165
        return this.btnShowDialog.isEnabled();
166
    }
167

  
168

  
169
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.163/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/coerce/CoerceToColor.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.tools.swing.impl.coerce;
25

  
26
import java.awt.Color;
27
import java.util.HashMap;
28
import java.util.Map;
29
import org.apache.commons.lang3.StringUtils;
30
import org.apache.commons.lang3.math.NumberUtils;
31
import org.gvsig.tools.ToolsLocator;
32
import org.gvsig.tools.dataTypes.CoercionException;
33
import org.gvsig.tools.dataTypes.DataTypesManager;
34
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
35
import org.gvsig.tools.swing.api.DataTypes;
36

  
37
public class CoerceToColor implements Coercion {
38

  
39
    
40
    private static Map<String,Color> colorTable = null;
41
    
42
    @Override
43
    public Object coerce(Object value) throws CoercionException {
44
        try {
45
            if (value == null || value instanceof Color) {
46
                return value;
47
            }
48
            if( value instanceof Number ) {
49
                int rgb = ((Number)value).intValue();
50
                Color color = new Color(rgb); // Alpha??
51
                return color;
52
            }
53
            String s = value.toString().trim();
54
            if( s.startsWith("rgb(") ) {
55
                s = s.replace(")", "");
56
                String[] x = StringUtils.split(s, ",");
57
                if( x.length<3 || x.length>4 ) {
58
                    throw new CoercionException("Can't convert '"+value.toString()+"' to color.");
59
                }
60
                try {
61
                    Color color;
62
                    int r = NumberUtils.createInteger(x[0]);
63
                    int g = NumberUtils.createInteger(x[1]);
64
                    int b = NumberUtils.createInteger(x[2]);
65
                    if( x.length==3 ) {
66
                        color = new Color(r, g, b);
67
                    } else {
68
                        int a = NumberUtils.createInteger(x[3]);
69
                        color = new Color(r, g, b, a);
70
                    }
71
                    return color;
72
                } catch(NumberFormatException ex) {
73
                    throw new CoercionException("Can't convert '"+value.toString()+"' to color.");
74
                }
75
                
76
            }
77
            if( s.startsWith("#") ) {
78
                s = "0x" + s.substring(1); 
79
            }
80
            try {
81
                int rgb = NumberUtils.createInteger(s);
82
                Color color = new Color(rgb);
83
                return color;
84
            } catch(NumberFormatException ex) {
85
                // Pass, do nothing
86
            }
87
            Color color = this.getColor(s);
88
            if( color == null ) {
89
                throw new CoercionException("Can't convert '"+value.toString()+"' to color.");
90
            }
91
            return color;
92
        } catch (Exception e) {
93
            throw new CoercionException(e);
94
        }
95

  
96
    }
97

  
98
    private Color getColor(String name) {
99
        if( colorTable==null ) {
100
            //
101
            // HTML Color Names
102
            // from https://www.w3schools.com/colors/colors_names.asp
103
            //
104
            colorTable = new HashMap<>();
105
            colorTable.put("aliceblue",new Color(0xF0,0xF8,0xFF));
106
            colorTable.put("antiquewhite",new Color(0xFA,0xEB,0xD7));
107
            colorTable.put("aqua",new Color(0x00,0xFF,0xFF));
108
            colorTable.put("aquamarine",new Color(0x7F,0xFF,0xD4));
109
            colorTable.put("azure",new Color(0xF0,0xFF,0xFF));
110
            colorTable.put("beige",new Color(0xF5,0xF5,0xDC));
111
            colorTable.put("bisque",new Color(0xFF,0xE4,0xC4));
112
            colorTable.put("black",new Color(0x00,0x00,0x00));
113
            colorTable.put("blanchedalmond",new Color(0xFF,0xEB,0xCD));
114
            colorTable.put("blue",new Color(0x00,0x00,0xFF));
115
            colorTable.put("blueviolet",new Color(0x8A,0x2B,0xE2));
116
            colorTable.put("brown",new Color(0xA5,0x2A,0x2A));
117
            colorTable.put("burlywood",new Color(0xDE,0xB8,0x87));
118
            colorTable.put("cadetblue",new Color(0x5F,0x9E,0xA0));
119
            colorTable.put("chartreuse",new Color(0x7F,0xFF,0x00));
120
            colorTable.put("chocolate",new Color(0xD2,0x69,0x1E));
121
            colorTable.put("coral",new Color(0xFF,0x7F,0x50));
122
            colorTable.put("cornflowerblue",new Color(0x64,0x95,0xED));
123
            colorTable.put("cornsilk",new Color(0xFF,0xF8,0xDC));
124
            colorTable.put("crimson",new Color(0xDC,0x14,0x3C));
125
            colorTable.put("cyan",new Color(0x00,0xFF,0xFF));
126
            colorTable.put("darkblue",new Color(0x00,0x00,0x8B));
127
            colorTable.put("darkcyan",new Color(0x00,0x8B,0x8B));
128
            colorTable.put("darkgoldenRod",new Color(0xB8,0x86,0x0B));
129
            colorTable.put("darkgray",new Color(0xA9,0xA9,0xA9));
130
            colorTable.put("darkgrey",new Color(0xA9,0xA9,0xA9));
131
            colorTable.put("darkgreen",new Color(0x00,0x64,0x00));
132
            colorTable.put("darkkhaki",new Color(0xBD,0xB7,0x6B));
133
            colorTable.put("darkmagenta",new Color(0x8B,0x00,0x8B));
134
            colorTable.put("darkolivegreen",new Color(0x55,0x6B,0x2F));
135
            colorTable.put("darkorange",new Color(0xFF,0x8C,0x00));
136
            colorTable.put("darkorchid",new Color(0x99,0x32,0xCC));
137
            colorTable.put("darkred",new Color(0x8B,0x00,0x00));
138
            colorTable.put("darksalmon",new Color(0xE9,0x96,0x7A));
139
            colorTable.put("darkseagreen",new Color(0x8F,0xBC,0x8F));
140
            colorTable.put("darkslateblue",new Color(0x48,0x3D,0x8B));
141
            colorTable.put("darkslategray",new Color(0x2F,0x4F,0x4F));
142
            colorTable.put("darkslategrey",new Color(0x2F,0x4F,0x4F));
143
            colorTable.put("darkturquoise",new Color(0x00,0xCE,0xD1));
144
            colorTable.put("darkviolet",new Color(0x94,0x00,0xD3));
145
            colorTable.put("deeppink",new Color(0xFF,0x14,0x93));
146
            colorTable.put("deepskyblue",new Color(0x00,0xBF,0xFF));
147
            colorTable.put("dimgray",new Color(0x69,0x69,0x69));
148
            colorTable.put("dimgrey",new Color(0x69,0x69,0x69));
149
            colorTable.put("dodgerblue",new Color(0x1E,0x90,0xFF));
150
            colorTable.put("firebrick",new Color(0xB2,0x22,0x22));
151
            colorTable.put("floralwhite",new Color(0xFF,0xFA,0xF0));
152
            colorTable.put("forestgreen",new Color(0x22,0x8B,0x22));
153
            colorTable.put("fuchsia",new Color(0xFF,0x00,0xFF));
154
            colorTable.put("gainsboro",new Color(0xDC,0xDC,0xDC));
155
            colorTable.put("ghostwhite",new Color(0xF8,0xF8,0xFF));
156
            colorTable.put("gold",new Color(0xFF,0xD7,0x00));
157
            colorTable.put("goldenrod",new Color(0xDA,0xA5,0x20));
158
            colorTable.put("gray",new Color(0x80,0x80,0x80));
159
            colorTable.put("grey",new Color(0x80,0x80,0x80));
160
            colorTable.put("green",new Color(0x00,0x80,0x00));
161
            colorTable.put("greenyellow",new Color(0xAD,0xFF,0x2F));
162
            colorTable.put("honeydew",new Color(0xF0,0xFF,0xF0));
163
            colorTable.put("hotpink",new Color(0xFF,0x69,0xB4));
164
            colorTable.put("indianred",new Color(0xCD,0x5C,0x5C));
165
            colorTable.put("indigo",new Color(0x4B,0x00,0x82));
166
            colorTable.put("ivory",new Color(0xFF,0xFF,0xF0));
167
            colorTable.put("khaki",new Color(0xF0,0xE6,0x8C));
168
            colorTable.put("lavender",new Color(0xE6,0xE6,0xFA));
169
            colorTable.put("lavenderblush",new Color(0xFF,0xF0,0xF5));
170
            colorTable.put("lawnGreen",new Color(0x7C,0xFC,0x00));
171
            colorTable.put("lemonchiffon",new Color(0xFF,0xFA,0xCD));
172
            colorTable.put("lightblue",new Color(0xAD,0xD8,0xE6));
173
            colorTable.put("lightcoral",new Color(0xF0,0x80,0x80));
174
            colorTable.put("lightcyan",new Color(0xE0,0xFF,0xFF));
175
            colorTable.put("lightgoldenRodYellow",new Color(0xFA,0xFA,0xD2));
176
            colorTable.put("lightgray",new Color(0xD3,0xD3,0xD3));
177
            colorTable.put("lightgrey",new Color(0xD3,0xD3,0xD3));
178
            colorTable.put("lightgreen",new Color(0x90,0xEE,0x90));
179
            colorTable.put("lightpink",new Color(0xFF,0xB6,0xC1));
180
            colorTable.put("lightsalmon",new Color(0xFF,0xA0,0x7A));
181
            colorTable.put("lightseagreen",new Color(0x20,0xB2,0xAA));
182
            colorTable.put("lightskyblue",new Color(0x87,0xCE,0xFA));
183
            colorTable.put("lightslategray",new Color(0x77,0x88,0x99));
184
            colorTable.put("lightslategrey",new Color(0x77,0x88,0x99));
185
            colorTable.put("lightsteelblue",new Color(0xB0,0xC4,0xDE));
186
            colorTable.put("lightyellow",new Color(0xFF,0xFF,0xE0));
187
            colorTable.put("lime",new Color(0x00,0xFF,0x00));
188
            colorTable.put("limegreen",new Color(0x32,0xCD,0x32));
189
            colorTable.put("linen",new Color(0xFA,0xF0,0xE6));
190
            colorTable.put("magenta",new Color(0xFF,0x00,0xFF));
191
            colorTable.put("maroon",new Color(0x80,0x00,0x00));
192
            colorTable.put("mediumaquamarine",new Color(0x66,0xCD,0xAA));
193
            colorTable.put("mediumblue",new Color(0x00,0x00,0xCD));
194
            colorTable.put("mediumorchid",new Color(0xBA,0x55,0xD3));
195
            colorTable.put("mediumpurple",new Color(0x93,0x70,0xDB));
196
            colorTable.put("mediumseagreen",new Color(0x3C,0xB3,0x71));
197
            colorTable.put("mediumslateblue",new Color(0x7B,0x68,0xEE));
198
            colorTable.put("mediumspringgreen",new Color(0x00,0xFA,0x9A));
199
            colorTable.put("mediumturquoise",new Color(0x48,0xD1,0xCC));
200
            colorTable.put("mediumvioletred",new Color(0xC7,0x15,0x85));
201
            colorTable.put("midnightblue",new Color(0x19,0x19,0x70));
202
            colorTable.put("mintcream",new Color(0xF5,0xFF,0xFA));
203
            colorTable.put("mistyrose",new Color(0xFF,0xE4,0xE1));
204
            colorTable.put("moccasin",new Color(0xFF,0xE4,0xB5));
205
            colorTable.put("navajowhite",new Color(0xFF,0xDE,0xAD));
206
            colorTable.put("navy",new Color(0x00,0x00,0x80));
207
            colorTable.put("oldlace",new Color(0xFD,0xF5,0xE6));
208
            colorTable.put("olive",new Color(0x80,0x80,0x00));
209
            colorTable.put("olivedrab",new Color(0x6B,0x8E,0x23));
210
            colorTable.put("orange",new Color(0xFF,0xA5,0x00));
211
            colorTable.put("orangered",new Color(0xFF,0x45,0x00));
212
            colorTable.put("orchid",new Color(0xDA,0x70,0xD6));
213
            colorTable.put("palegoldenrod",new Color(0xEE,0xE8,0xAA));
214
            colorTable.put("palegreen",new Color(0x98,0xFB,0x98));
215
            colorTable.put("paleturquoise",new Color(0xAF,0xEE,0xEE));
216
            colorTable.put("palevioletred",new Color(0xDB,0x70,0x93));
217
            colorTable.put("papayawhip",new Color(0xFF,0xEF,0xD5));
218
            colorTable.put("peachpuff",new Color(0xFF,0xDA,0xB9));
219
            colorTable.put("peru",new Color(0xCD,0x85,0x3F));
220
            colorTable.put("pink",new Color(0xFF,0xC0,0xCB));
221
            colorTable.put("plum",new Color(0xDD,0xA0,0xDD));
222
            colorTable.put("powderblue",new Color(0xB0,0xE0,0xE6));
223
            colorTable.put("purple",new Color(0x80,0x00,0x80));
224
            colorTable.put("rebeccapurple",new Color(0x66,0x33,0x99));
225
            colorTable.put("red",new Color(0xFF,0x00,0x00));
226
            colorTable.put("rosybrown",new Color(0xBC,0x8F,0x8F));
227
            colorTable.put("royalblue",new Color(0x41,0x69,0xE1));
228
            colorTable.put("saddlebrown",new Color(0x8B,0x45,0x13));
229
            colorTable.put("salmon",new Color(0xFA,0x80,0x72));
230
            colorTable.put("sandybrown",new Color(0xF4,0xA4,0x60));
231
            colorTable.put("seagreen",new Color(0x2E,0x8B,0x57));
232
            colorTable.put("seashell",new Color(0xFF,0xF5,0xEE));
233
            colorTable.put("sienna",new Color(0xA0,0x52,0x2D));
234
            colorTable.put("silver",new Color(0xC0,0xC0,0xC0));
235
            colorTable.put("skyblue",new Color(0x87,0xCE,0xEB));
236
            colorTable.put("slateblue",new Color(0x6A,0x5A,0xCD));
237
            colorTable.put("Slategray",new Color(0x70,0x80,0x90));
238
            colorTable.put("slategrey",new Color(0x70,0x80,0x90));
239
            colorTable.put("snow",new Color(0xFF,0xFA,0xFA));
240
            colorTable.put("springgreen",new Color(0x00,0xFF,0x7F));
241
            colorTable.put("steelblue",new Color(0x46,0x82,0xB4));
242
            colorTable.put("tan",new Color(0xD2,0xB4,0x8C));
243
            colorTable.put("teal",new Color(0x00,0x80,0x80));
244
            colorTable.put("thistle",new Color(0xD8,0xBF,0xD8));
245
            colorTable.put("tomato",new Color(0xFF,0x63,0x47));
246
            colorTable.put("turquoise",new Color(0x40,0xE0,0xD0));
247
            colorTable.put("violet",new Color(0xEE,0x82,0xEE));
248
            colorTable.put("wheat",new Color(0xF5,0xDE,0xB3));
249
            colorTable.put("white",new Color(0xFF,0xFF,0xFF));
250
            colorTable.put("whitesmoke",new Color(0xF5,0xF5,0xF5));
251
            colorTable.put("yellow",new Color(0xFF,0xFF,0x00));
252
            colorTable.put("yellowgreen",new Color(0x9A,0xCD,0x32));
253
        }
254
        return colorTable.get(name.toLowerCase());
255
    }
256
    
257
    public static class CoerceColorToString implements Coercion {
258

  
259
        @Override
260
        public Object coerce(Object value) throws CoercionException {
261
            if( value == null ) {
262
                return null;
263
            }
264
            if( value instanceof Color ) {
265
                int rgb = ((Color) value).getRGB();
266
                int r = ((Color) value).getRed();
267
                int g = ((Color) value).getGreen();
268
                int b = ((Color) value).getBlue();
269
                int a = ((Color) value).getAlpha();
270
                for (Map.Entry<String, Color> entry : colorTable.entrySet()) {
271
                    String name = entry.getKey();
272
                    Color color = entry.getValue();
273
                    if( rgb==color.getRGB() && color.getAlpha()==a ) {
274
                        return name;
275
                    }
276
                }
277
                return String.format("rgb(0x%02x,0x%02x,0x%02x,0x%02x)", r,g,b,a);
278
            }
279
            throw new CoercionException("Can't convert '"+value.getClass().getName()+"' to string");
280
        }
281
        
282
    }
283
     
284
    public static void selfRegister() {
285
        DataTypesManager dataTypesManager = ToolsLocator.getDataTypesManager();
286
        dataTypesManager.addtype(DataTypes.COLOR, null, "Color", Color.class, new CoerceToColor());
287
        dataTypesManager.addCoercion(DataTypes.STRING, new CoerceColorToString());
288
    }
289
}
org.gvsig.tools/library/tags/org.gvsig.tools-3.0.163/org.gvsig.tools.swing/org.gvsig.tools.swing.impl/src/main/java/org/gvsig/tools/swing/impl/component/DefaultEvaluatorPanel.java
1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2013 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.tools.swing.impl.component;
24

  
25
import java.awt.BorderLayout;
26
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.Dimension;
29
import java.awt.Font;
30
import java.awt.GridBagConstraints;
31
import java.awt.GridBagLayout;
32
import java.awt.GridLayout;
33
import java.awt.Insets;
34
import java.awt.Rectangle;
35
import java.awt.event.ActionEvent;
36
import java.awt.event.ActionListener;
37
import java.awt.event.MouseEvent;
38
import java.awt.event.MouseListener;
39
import java.util.ArrayList;
40

  
41
import javax.swing.AbstractButton;
42
import javax.swing.BorderFactory;
43
import javax.swing.ButtonGroup;
44
import javax.swing.DefaultListModel;
45
import javax.swing.JButton;
46
import javax.swing.JLabel;
47
import javax.swing.JList;
48
import javax.swing.JPanel;
49
import javax.swing.JRadioButton;
50
import javax.swing.JScrollBar;
51
import javax.swing.JScrollPane;
52
import javax.swing.JTextArea;
53
import javax.swing.JTextPane;
54
import javax.swing.ListSelectionModel;
55
import javax.swing.ScrollPaneConstants;
56
import javax.swing.border.EmptyBorder;
57
import javax.swing.event.ListSelectionEvent;
58
import javax.swing.event.ListSelectionListener;
59
import javax.swing.text.SimpleAttributeSet;
60
import javax.swing.text.StyleConstants;
61
import javax.swing.text.StyledDocument;
62

  
63
import org.gvsig.tools.ToolsLocator;
64
import org.gvsig.tools.dynobject.DynClass;
65
import org.gvsig.tools.dynobject.DynField;
66
import org.gvsig.tools.dynobject.DynStruct;
67
import org.gvsig.tools.evaluator.EvaluatorWithDescriptions;
68
import org.gvsig.tools.evaluator.EvaluatorWithDescriptions.Description;
69
import org.gvsig.tools.i18n.I18nManager;
70
import org.gvsig.tools.swing.api.evaluator.EvaluatorPanel;
71
//import org.gvsig.tools.swing.impl.dynobject.dynfield.DynFieldListItem;
72

  
73

  
74
/**
75
 *
76
 * @author jldominguez
77
 *
78
 */
79
public class DefaultEvaluatorPanel extends EvaluatorPanel
80
implements ListSelectionListener, MouseListener, ActionListener {
81

  
82
    private DynClass dynclass = null;
83
    private EvaluatorWithDescriptions eval_wdesc = null;
84

  
85
    private JTextArea expressionArea = null;
86
    private JList fieldList = null;
87
    private JList opfuList = null;
88
    private JPanel categRadioPanel = null;
89

  
90
    private JTextArea fieldExplainLabel = null;
91
    private JTextArea opfuDescLabel = null;
92

  
93
    private JRadioButton num_CategRB = null;
94
    private JRadioButton str_CategRB = null;
95
    private JRadioButton dat_CategRB = null;
96
    private JRadioButton geo_CategRB = null;
97
    private JRadioButton boo_CategRB = null;
98
    private JRadioButton all_CategRB = null;
99

  
100
    private JPanel opfuDescPanel = null;
101
    private JScrollPane opfuDescScroll = null;
102
    private JPanel opfuPanel = null;
103
    private JPanel fieldsPanel = null;
104

  
105
    private JPanel topPanel = null;
106
    private JPanel expressionPanel = null;
107

  
108
    private JButton clearButton = null;
109
    private JButton validateButton = null;
110

  
111
    private static I18nManager im = ToolsLocator.getI18nManager();
112

  
113
    public DefaultEvaluatorPanel(
114
        DynClass dcla, EvaluatorWithDescriptions evaluator) {
115
        initialize(dcla, evaluator);
116
    }
117

  
118
    public String getExpression() {
119
        return getExpressionArea().getText();
120
    }
121

  
122
    public void initialize(DynClass dcla, EvaluatorWithDescriptions evaluator) {
123
        dynclass = dcla;
124
        eval_wdesc = evaluator;
125
        initComponents();
126
    }
127

  
128
    /**
129
     *
130
     */
131
    private void initComponents() {
132

  
133
        this.setLayout(new GridBagLayout());
134
        GridBagConstraints gbc = new GridBagConstraints();
135
        gbc.fill = GridBagConstraints.BOTH;
136
        gbc.weightx = 1;
137
        gbc.weighty = 0.8;
138
        gbc.gridheight = 2;
139
        gbc.gridx = 0;
140
        gbc.gridy = 0;
141
        this.add(getTopPanel(), gbc);
142
        gbc.weightx = 1;
143
        gbc.weighty = 0.2;
144
        gbc.gridheight = 1;
145
        gbc.gridy = 2;
146
        gbc.insets = new Insets(5, 0, 0, 0);
147
        this.add(getExpressionPanel(), gbc);
148

  
149
    }
150

  
151

  
152
    private JTextArea getExpressionArea() {
153
        if (expressionArea == null) {
154
            expressionArea = new JTextArea();
155
            expressionArea.setLineWrap(true);
156
            expressionArea.setWrapStyleWord(true);
157
            if (eval_wdesc != null) {
158
                expressionArea.setText(eval_wdesc.getSQL());
159
            }
160
        }
161
        return expressionArea;
162
    }
163

  
164
    private JPanel getExpressionPanel() {
165

  
166
        if (expressionPanel == null) {
167
            expressionPanel = new JPanel();
168
            expressionPanel.setLayout(new BorderLayout());
169
            expressionPanel.setBorder(BorderFactory.createTitledBorder(
170
                im.getTranslation("_User_expression")));
171

  
172
            JScrollPane scroll = new JScrollPane(getExpressionArea());
173
            scroll.setHorizontalScrollBarPolicy(
174
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
175
            scroll.setVerticalScrollBarPolicy(
176
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
177
            expressionPanel.add(scroll, BorderLayout.CENTER);
178

  
179
            JPanel buttonsp = new JPanel();
180
            buttonsp.setLayout(new GridLayout(1, 1));
181
            // ========================
182
            // JPanel aux = new JPanel();
183
            // aux.add(getValidateButton());
184
            // buttonsp.add(aux);
185
            // =====================
186
            JPanel aux = new JPanel();
187
            aux.add(getClearButton());
188
            buttonsp.add(aux);
189
            // ===========================
190
            expressionPanel.add(buttonsp, BorderLayout.EAST);
191
        }
192
        return expressionPanel;
193

  
194
    }
195

  
196

  
197
    /**
198
     * @return
199
     */
200
    private Component getClearButton() {
201
        if (clearButton == null) {
202
            clearButton = new JButton(im.getTranslation("clear_expression"));
203
            clearButton.addActionListener(this);
204
        }
205
        return clearButton;
206
    }
207

  
208
    /**
209
     * @return
210
     */
211
    private JButton getValidateButton() {
212
        if (validateButton == null) {
213
            validateButton = new JButton(im.getTranslation("_Validate"));
214
            validateButton.addActionListener(this);
215
        }
216
        return validateButton;
217
    }
218

  
219
    private JList getFieldList() {
220
        if (fieldList == null) {
221
            fieldList = new JList();
222
            if (dynclass != null) {
223

  
224
                fieldList.addListSelectionListener(this);
225
                DynField[] ff = dynclass.getDynFields();
226
                DefaultListModel resultList = new DefaultListModel();
227
                for (int i=0; i<ff.length; i++) {
228
                    resultList.addElement(ff[i].getName());
229
                }
230
                fieldList.setModel(resultList);
231
                fieldList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
232
                fieldList.addMouseListener(this);
233
            }
234
        }
235
        return fieldList;
236
    }
237

  
238

  
239
    private JList getOpfuList() {
240
        if (opfuList == null) {
241
            opfuList = new JList();
242
            reloadOpfuList(opfuList);
243
            opfuList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
244
            opfuList.addListSelectionListener(this);
245
            opfuList.addMouseListener(this);
246
        }
247
        return opfuList;
248
    }
249

  
250

  
251
    private void reloadOpfuList(JList list) {
252

  
253
        DefaultListModel dlm = new DefaultListModel();
254
        if (this.eval_wdesc != null) {
255
            Description[] descs = getCurrentDescriptions(eval_wdesc);
256
            for (int i=0; i<descs.length; i++) {
257
                dlm.addElement(new DescriptionListItem(descs[i]));
258
            }
259
        }
260
        list.setModel(dlm);
261

  
262
    }
263

  
264
    private Description[] getCurrentDescriptions(
265
        EvaluatorWithDescriptions eval) {
266

  
267
        int goodbits = 0;
268
        if (getAllCategRB().isSelected()) {
269
            goodbits =
270
                EvaluatorWithDescriptions.Description.DATATYPE_CATEGORY_ALL;
271
        } else {
272
            if (getNumCategRB().isSelected()) {
273
                goodbits = EvaluatorWithDescriptions.Description.DATATYPE_CATEGORY_NUMBER;
274
            } else {
275
                if (getStrCategRB().isSelected()) {
276
                    goodbits = EvaluatorWithDescriptions.Description.DATATYPE_CATEGORY_STRING;
277
                } else {
278
                    if (getDatCategRB().isSelected()) {
279
                        goodbits = EvaluatorWithDescriptions.Description.DATATYPE_CATEGORY_DATETIME;
280
                    } else {
281
                        if (getGeoCategRB().isSelected()) {
282
                            goodbits = EvaluatorWithDescriptions.Description.DATATYPE_CATEGORY_GEOMETRY;
283
                        } else {
284
                            if (getBooCategRB().isSelected()) {
285
                                goodbits = EvaluatorWithDescriptions.Description.DATATYPE_CATEGORY_BOOLEAN;
286
                            } else {
287

  
288
                            }
289
                        }
290
                    }
291
                }
292
            }
293
        }
294

  
295
        ArrayList list = new ArrayList();
296
        Description[] dd = eval.getAvailableOperators();
297
        int andop = 0;
298
        for (int i=0; i<dd.length; i++) {
299
            andop = (dd[i].getDataTypeCategories() & goodbits);
300
            if (goodbits == 0 /* all */ || andop != 0) {
301
                list.add(dd[i]);
302
            }
303
        }
304
        dd = eval.getAvailableFunctions();
305
        for (int i=0; i<dd.length; i++) {
306
            andop = (dd[i].getDataTypeCategories() & goodbits);
307
            if (goodbits == 0 /* all */ || andop != 0) {
308
                list.add(dd[i]);
309
            }
310
        }
311

  
312
        return (Description[]) list.toArray(new Description[0]);
313
    }
314

  
315
    private JRadioButton getAllCategRB() {
316
        if (all_CategRB == null) {
317
            this.all_CategRB = new JRadioButton(im.getTranslation("_All"));
318
            this.all_CategRB.addActionListener(this);
319
        }
320
        return all_CategRB;
321
    }
322

  
323

  
324
    private JRadioButton getNumCategRB() {
325
        if (num_CategRB == null) {
326
            this.num_CategRB = new JRadioButton(im.getTranslation("_Numeric"));
327
            this.num_CategRB.addActionListener(this);
328
        }
329
        return num_CategRB;
330
    }
331

  
332

  
333
    private JRadioButton getStrCategRB() {
334
        if (str_CategRB == null) {
335
            this.str_CategRB = new JRadioButton(im.getTranslation("_String"));
336
            this.str_CategRB.addActionListener(this);
337
        }
338
        return str_CategRB;
339
    }
340

  
341

  
342
    private JRadioButton getDatCategRB() {
343
        if (dat_CategRB == null) {
344
            this.dat_CategRB = new JRadioButton(
345
                im.getTranslation("_Date_or_time"));
346
            this.dat_CategRB.addActionListener(this);
347
        }
348
        return dat_CategRB;
349
    }
350

  
351

  
352
    private JRadioButton getGeoCategRB() {
353
        if (geo_CategRB == null) {
354
            this.geo_CategRB = new JRadioButton(im.getTranslation("_Geometry"));
355
            this.geo_CategRB.addActionListener(this);
356
        }
357
        return geo_CategRB;
358
    }
359

  
360

  
361
    private JPanel getCategRadioPanel() {
362
        if (categRadioPanel == null) {
363
            categRadioPanel = new JPanel();
364
            ButtonGroup bg = new ButtonGroup();
365
            bg.add(getAllCategRB());
366
            bg.add(getNumCategRB());
367
            bg.add(getStrCategRB());
368
            bg.add(getDatCategRB());
369
            bg.add(getBooCategRB());
370
            bg.add(getGeoCategRB());
371
            getAllCategRB().setSelected(true);
372
            categRadioPanel.setLayout(new GridBagLayout());
373
            GridBagConstraints gbc = new GridBagConstraints();
374
            gbc.anchor = GridBagConstraints.NORTHWEST;
375
            gbc.fill = GridBagConstraints.BOTH;
376
            gbc.weightx = 0.5;
377
            gbc.gridx = 0;
378
            gbc.gridy = 0;
379
            categRadioPanel.add(getAllCategRB(), gbc);
380
            gbc.gridy = 1;
381
            categRadioPanel.add(getNumCategRB(), gbc);
382
            gbc.gridy = 2;
383
            categRadioPanel.add(getStrCategRB(), gbc);
384
            gbc.gridy = 3;
385
            categRadioPanel.add(getDatCategRB(), gbc);
386
            gbc.gridy = 4;
387
            categRadioPanel.add(getBooCategRB(), gbc);
388
            gbc.gridy = 5;
389
            categRadioPanel.add(getGeoCategRB(), gbc);
390
            gbc.gridy = 6;
391
            gbc.weighty = 1;
392
            categRadioPanel.add(new JPanel(), gbc);
393
        }
394
        return categRadioPanel;
395
    }
396

  
397

  
398
    /**
399
     * @return
400
     */
401
    private JRadioButton getBooCategRB() {
402
        if (boo_CategRB == null) {
403
            this.boo_CategRB = new JRadioButton(im.getTranslation("_Boolean"));
404
            this.boo_CategRB.addActionListener(this);
405
        }
406
        return boo_CategRB;
407
    }
408

  
409
    private JTextArea getOpfuDescLabel(Color bgcolor) {
410
        if (opfuDescLabel == null) {
411
            opfuDescLabel = new JTextArea();
412
            opfuDescLabel.setEditable(false);
413
            opfuDescLabel.setBorder(new EmptyBorder(2,2,2,2));
414
            if (bgcolor != null) {
415
                opfuDescLabel.setBackground(bgcolor);
416
            }
417
            opfuDescLabel.setWrapStyleWord(true);
418
            opfuDescLabel.setLineWrap(true);
419
            opfuDescLabel.setFont(new JLabel().getFont());
420
            opfuDescLabel.setText(" \n \n \n ");
421
        }
422
        return opfuDescLabel;
423
    }
424

  
425
    private JPanel getOpfuListCategPanel() {
426

  
427
        if (opfuDescPanel == null) {
428
            opfuDescPanel = new JPanel();
429
            opfuDescPanel.setLayout(new GridBagLayout());
430

  
431
            GridBagConstraints gbc = new GridBagConstraints();
432
            gbc.fill = GridBagConstraints.BOTH;
433
            gbc.anchor = GridBagConstraints.NORTHWEST;
434
            gbc.weightx = 0.5;
435
            gbc.weighty = 1;
436
            gbc.gridx = 0;
437
            gbc.gridy = 0;
438

  
439
            JScrollPane scroll = new JScrollPane(getOpfuList());
440
            scroll.setHorizontalScrollBarPolicy(
441
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
442
            scroll.setVerticalScrollBarPolicy(
443
                ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
444
            // scroll.setMinimumSize(new Dimension(100, 100));
445
            opfuDescPanel.add(scroll, gbc);
446

  
447
            gbc.weightx = 0.5;
448
            gbc.weighty = 1;
449
            gbc.gridx = 1;
450
            gbc.insets = new Insets(0, 5, 0, 0);
451
            opfuDescPanel.add(getCategRadioPanel(), gbc);
452
        }
453
        return opfuDescPanel;
454
    }
455

  
456
    private JPanel getOpfuPanel() {
457
        if (opfuPanel == null) {
458
            opfuPanel = new JPanel();
459
            opfuPanel.setLayout(new GridBagLayout());
460

  
461
            GridBagConstraints gbc = new GridBagConstraints();
462
            gbc.anchor = GridBagConstraints.NORTHWEST;
463
            gbc.fill = GridBagConstraints.BOTH;
464
            gbc.gridx = 0;
465
            gbc.gridy = 0;
466
            gbc.gridheight = 2;
467
            gbc.weightx = 1;
468
            gbc.weighty = 0.5;
469

  
470
            opfuPanel.add(getOpfuListCategPanel(), gbc);
471

  
472
            gbc.gridx = 0;
473
            gbc.gridy = 2;
474
            gbc.weightx = 1;
475
            gbc.weighty = 0.5;
476
            gbc.gridheight = 1;
477
            gbc.insets = new Insets(5, 0, 0, 0);
478

  
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff