Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / styling / StyleSelector.java @ 11009

History | View | Annotate | Download (5.57 KB)

1
package com.iver.cit.gvsig.gui.styling;
2

    
3
import java.awt.Color;
4
import java.awt.Component;
5
import java.awt.Dimension;
6
import java.io.File;
7
import java.io.FileFilter;
8
import java.util.prefs.Preferences;
9

    
10
import javax.swing.BoxLayout;
11
import javax.swing.JLabel;
12
import javax.swing.JList;
13
import javax.swing.JPanel;
14
import javax.swing.ListCellRenderer;
15
import javax.swing.event.ListSelectionEvent;
16
import javax.swing.event.ListSelectionListener;
17
import javax.swing.event.TreeModelListener;
18
import javax.swing.tree.TreeModel;
19
import javax.swing.tree.TreePath;
20

    
21
import org.gvsig.gui.beans.swing.GridBagLayoutPanel;
22

    
23
import com.iver.andami.PluginServices;
24
import com.iver.cit.gvsig.fmap.core.styles.IStyle;
25
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
26

    
27
/**
28
 *
29
 * @author jaume dominguez faus - jaume.dominguez@iver.es
30
 *
31
 */
32
public class StyleSelector extends SymbolSelector {
33

    
34
        public StyleSelector(IStyle style, int shapeType) {
35
                this(style, shapeType, new SelectorFilter() {
36
                        public boolean accepts(Object obj) {
37
                                return obj instanceof IStyle;
38
                        }
39
                });
40
        }
41

    
42
        public StyleSelector(IStyle style, int shapeType, SelectorFilter filter) {
43
                super(null, shapeType, filter);
44
                selectedElement = style;
45
            Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
46
                rootDir = new File(prefs.get("SymbolStylesFolder", System.getProperty("user.home")+"/gvSIG/Styles"));
47
                if (!rootDir.exists())
48
                        rootDir.mkdir();
49
                lblTitle.setText(PluginServices.getText(this, "label_styles"));
50
                treeRootName = PluginServices.getText(this, "style_library");
51
                treeModel = new TreeModel() {
52
                    final class MyFile extends File {
53
                            public MyFile(String pathname) {
54
                                    super(pathname);
55
                            }
56

    
57
                            public String toString() {
58
                                    if (this.equals(root))
59
                                            return treeRootName;
60
                                    String path = getAbsolutePath();
61
                                    String prefixToRemove = rootDir.getAbsolutePath();
62
                                    path = path.substring(prefixToRemove.length()+1, path.length());
63
                                    return path;
64
                            }
65
                    };
66
                    MyFile root = new MyFile(rootDir.getAbsolutePath());
67

    
68
                    private FileFilter ff = new FileFilter() {
69
                            public boolean accept(File pathname) {
70
                                    return pathname.isDirectory();
71
                            }
72
                    };
73

    
74
                    public Object getRoot() {
75
                            return root;
76
                    }
77

    
78
                    public int getChildCount(Object parent) {
79
                            return ((File) parent).listFiles(ff).length;
80
                    }
81

    
82
                    public boolean isLeaf(Object node) {
83
                            return getChildCount(node)==0;
84
                    }
85

    
86
                    public void addTreeModelListener(TreeModelListener l) {
87
                            // TODO Necessite?
88

    
89
                    }
90

    
91
                    public void removeTreeModelListener(TreeModelListener l) {
92
                            // TODO Necessite?
93
                    }
94

    
95
                    public Object getChild(Object parent, int index) {
96
                            return new MyFile(((File) parent).listFiles(ff)[index].getAbsolutePath());
97
                    }
98

    
99
                    public int getIndexOfChild(Object parent, Object child) {
100
                            if (parent == null)
101
                                    return -1;
102
                            File[] files = ((File) parent).listFiles(ff);
103
                            for (int i = 0; i < files.length; i++) {
104
                                    if (files[i].equals((File) child))
105
                                            return i;
106
                            }
107
                            return -1;
108
                    }
109

    
110
                    public void valueForPathChanged(TreePath path, Object newValue) {
111
                            // TODO Auto-generated method stub
112
                    }
113

    
114
            };
115
                getTreeFav().setModel(treeModel);
116
        }
117

    
118
        protected SymbolSelectorListModel newListModel() {
119
                StyleSelectorListModel listModel = new StyleSelectorListModel(
120
                                dir,
121
                                selectedElement,
122
                                sFilter,
123
                                StyleSelectorListModel.STYLE_FILE_EXTENSION);
124
                return listModel;
125

    
126
        }
127

    
128
        protected JPanel getJPanelOptions() {
129
                if (jPanelOptions == null) {
130
                        jPanelOptions = new GridBagLayoutPanel();
131
                        // nothing in the panel
132

    
133
                }
134
            return jPanelOptions;
135
    }
136

    
137
    /**
138
     * This method initializes jList
139
     *
140
     * @return javax.swing.JList
141
     */
142
    protected JList getJListSymbols() {
143
            if (jListSymbols == null) {
144
                    jListSymbols = new JList();
145
                    jListSymbols.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
146
            jListSymbols.setLayoutOrientation(JList.HORIZONTAL_WRAP);
147
            jListSymbols.setVisibleRowCount(-1);
148
            jListSymbols.addListSelectionListener(new ListSelectionListener() {
149
                    public void valueChanged(ListSelectionEvent e) {
150
                            setStyle(jListSymbols.getSelectedValue());
151
                            updateOptionsPanel();
152
                    }
153
            });
154
            ListCellRenderer renderer = new ListCellRenderer() {
155
                        private Color mySelectedBGColor = new Color(255,145,100,255);
156
                            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
157
                                    IStyle sty = (IStyle) value;
158
                                    JPanel pnl = new JPanel();
159
                                    BoxLayout layout = new BoxLayout(pnl, BoxLayout.Y_AXIS);
160
                                    pnl.setLayout(layout);
161
                                    Color bgColor = (isSelected) ? mySelectedBGColor
162
                                                             : getJListSymbols().getBackground();
163

    
164
                                    pnl.setBackground(bgColor);
165
                                    StylePreviewer sp = new StylePreviewer();
166
                                    sp.setAlignmentX(Component.CENTER_ALIGNMENT);
167
                                    sp.setPreferredSize(new Dimension(50, 50));
168
                                    sp.setStyle(sty);
169
                                    sp.setBackground(bgColor);
170
                                    pnl.add(sp);
171
                                    JLabel lbl = new JLabel(sty.getDescription());
172
                                    lbl.setBackground(bgColor);
173
                                    lbl.setAlignmentX(Component.CENTER_ALIGNMENT);
174
                                    pnl.add(lbl);
175

    
176
                                    return pnl;
177
                            }
178

    
179
                };
180
                jListSymbols.setCellRenderer(renderer);
181
            }
182
            return jListSymbols;
183
    }
184

    
185
        protected void setStyle(Object selectedValue) {
186
                // TODO missing change SymbolPreviewer by a StylePrevier and apply it
187
                // the new style
188
                selectedElement = selectedValue;
189
        }
190

    
191
}