~ubuntu-branches/ubuntu/vivid/jayatana/vivid

« back to all changes in this revision

Viewing changes to jayatana/src/main/java/com/jarego/jayatana/swing/SwingGTKFixed.java

  • Committer: Package Import Robot
  • Author(s): Jared González
  • Date: 2014-12-10 17:38:15 UTC
  • Revision ID: package-import@ubuntu.com-20141210173815-8k4hm3bd481qkt63
Tags: upstream-2.7
Import upstream version 2.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2014 Jared González
 
3
 *
 
4
 * Permission is hereby granted, free of charge, to any
 
5
 * person obtaining a copy of this software and associated
 
6
 * documentation files (the "Software"), to deal in the
 
7
 * Software without restriction, including without limitation
 
8
 * the rights to use, copy, modify, merge, publish,
 
9
 * distribute, sublicense, and/or sell copies of the
 
10
 * Software, and to permit persons to whom the Software is
 
11
 * furnished to do so, subject to the following conditions:
 
12
 *
 
13
 * The above copyright notice and this permission notice
 
14
 * shall be included in all copies or substantial portions of
 
15
 * the Software.
 
16
 *
 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 
18
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 
19
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 
20
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 
21
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 
22
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 
23
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 
24
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
25
 */
 
26
package com.jarego.jayatana.swing;
 
27
 
 
28
import com.jarego.jayatana.Feature;
 
29
import com.sun.java.swing.plaf.gtk.GTKLookAndFeel;
 
30
 
 
31
import java.beans.PropertyChangeEvent;
 
32
import java.beans.PropertyChangeListener;
 
33
import java.lang.reflect.Field;
 
34
import java.util.logging.Level;
 
35
import java.util.logging.Logger;
 
36
 
 
37
import javax.swing.JComponent;
 
38
import javax.swing.UIManager;
 
39
import javax.swing.plaf.synth.Region;
 
40
 
 
41
/**
 
42
 * Esta clase es una característica para corregir el borde los menus en GTK los cuales no
 
43
 * se muestran. Esta clase permite incorporar el dibujado de los bordes de estos menús.
 
44
 * 
 
45
 * @author Jared González
 
46
 */
 
47
@SuppressWarnings("restriction")
 
48
public class SwingGTKFixed implements Feature, PropertyChangeListener {
 
49
        /**
 
50
         * Se despliega la correción del borde de los menús en GTK.
 
51
         */
 
52
        @Override
 
53
        public void deploy() {
 
54
                UIManager.addPropertyChangeListener(new SwingGTKFixed());
 
55
        }
 
56
        
 
57
        /**
 
58
         * Identifica si el LookAndFeel cambia a GTK.
 
59
         */
 
60
        @Override
 
61
        public void propertyChange(PropertyChangeEvent evt) {
 
62
                if ("lookAndFeel".equals(evt.getPropertyName())) {
 
63
                        if (evt.getNewValue() != null ?
 
64
                                        evt.getNewValue().getClass().getName().contains("GTKLookAndFeel") : false)
 
65
                                try {
 
66
                                        installGtkThikcness();
 
67
                                } catch (Exception e) {
 
68
                                        Logger.getLogger(SwingGTKFixed.class.getName())
 
69
                                                .log(Level.WARNING, "can't install gtk border fixed", e);
 
70
                                }
 
71
                }
 
72
        }
 
73
        
 
74
        /**
 
75
         * Establece un borde de 1.
 
76
         * 
 
77
         * @throws Exception
 
78
         */
 
79
        private static void installGtkThikcness() throws Exception {
 
80
                JComponent comp = new JComponent() {};
 
81
                changeGtkYThikcness(GTKLookAndFeel.getStyle(comp , Region.POPUP_MENU), 1);
 
82
        changeGtkXThikcness(GTKLookAndFeel.getStyle(comp, Region.POPUP_MENU), 1);
 
83
        changeGtkYThikcness(GTKLookAndFeel.getStyle(comp, Region.POPUP_MENU_SEPARATOR), 1);
 
84
        }
 
85
        /**
 
86
         * Cambia el borde vertical de los menús en al menos 1 si es que el valor inicial es 0.
 
87
         * 
 
88
         * @param style controlador de estilo.
 
89
         * @param border tamaño del borde.
 
90
         * @throws Exception En caso de que falle al acceder al attributo.
 
91
         */
 
92
        private static void changeGtkYThikcness(Object style, int border)
 
93
                        throws Exception {
 
94
                Field field = style.getClass().getDeclaredField("yThickness");
 
95
                field.setAccessible(true);
 
96
                field.setInt(style, Math.max(border, field.getInt(style)));
 
97
                field.setAccessible(false);
 
98
        }
 
99
        /**
 
100
         * Cambia el borde horizontal de los menús en al menos 1 si es que el valor inicial es 0.
 
101
         * 
 
102
         * @param style controlador de estilo.
 
103
         * @param border tamaño del borde.
 
104
         * @throws Exception En caso de que falle al acceder al attributo.
 
105
         */
 
106
        private static void changeGtkXThikcness(Object style, int border)
 
107
                        throws Exception {
 
108
                Field field = style.getClass().getDeclaredField("xThickness");
 
109
                field.setAccessible(true);
 
110
                field.setInt(style, Math.max(border, field.getInt(style)));
 
111
                field.setAccessible(false);
 
112
        }
 
113
}