Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1009 / libraries / libCq CMS for java.old / src / org / cresques / io / BookmarkList.java @ 12649

History | View | Annotate | Download (7.09 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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 2
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io;
25

    
26
import org.cresques.cts.IProjection;
27

    
28
import org.cresques.ui.CQApp;
29

    
30
import java.awt.event.ActionEvent;
31
import java.awt.event.ActionListener;
32

    
33
import java.util.Iterator;
34
import java.util.Map;
35
import java.util.TreeMap;
36
import java.util.Vector;
37

    
38
import javax.swing.ImageIcon;
39
import javax.swing.JMenu;
40
import javax.swing.JMenuItem;
41

    
42

    
43
/**
44
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
45
 */
46
class Bookmark {
47
    String folder;
48
    String name;
49
    String[] fName;
50
    String projName;
51
    IProjection proj;
52

    
53
    public Bookmark(String fld, String n, String f, IProjection p) {
54
        projName = null;
55
        fName = new String[1];
56
        folder = fld;
57
        name = n;
58
        fName[0] = f;
59
        proj = p;
60
    }
61

    
62
    public Bookmark(String fld, String n, String[] f, IProjection p) {
63
        projName = null;
64
        fName = new String[f.length];
65
        folder = fld;
66
        name = n;
67
        proj = p;
68

    
69
        for (int i = 0; i < f.length; i++)
70
            fName[i] = f[i];
71
    }
72
}
73

    
74

    
75
public class BookmarkList extends TreeMap implements ActionListener {
76
    final private static long serialVersionUID = -3370601314380922368L;
77
    CQApp app = null;
78

    
79
    public BookmarkList(CQApp mainApp) {
80
        app = mainApp;
81
        loadList();
82
    }
83

    
84
    public void loadList() {
85
    }
86

    
87
    public void addEntry(String fld, String n, String f, IProjection p) {
88
        addEntry(new Bookmark(fld, n, f, p));
89
    }
90

    
91
    public void addEntry(String fld, String n, String[] f, IProjection p) {
92
        addEntry(new Bookmark(fld, n, f, p));
93
    }
94

    
95
    public void addEntry(Bookmark bookmark) {
96
        Vector vector;
97

    
98
        if (containsKey(bookmark.folder)) {
99
            vector = (Vector) get(bookmark.folder);
100
        } else {
101
            vector = new Vector();
102
        }
103

    
104
        vector.add(bookmark);
105
        put(bookmark.folder, vector);
106
    }
107

    
108
    public JMenu getJMenu() {
109
        JMenuItem it = null;
110
        JMenu menu = null;
111
        JMenu menu2 = null;
112
        ClassLoader loader = this.getClass().getClassLoader();
113

    
114
        //loader.getResource("images/"+"folderXP.gif");
115
        //System.err.println("URL="+loader.getResource("images/"+"folderXP.gif"));
116
        ImageIcon folderIcon = new ImageIcon(loader.getResource("images/" +
117
                                                                "folderXP.gif"));
118
        ImageIcon dxfIcon = new ImageIcon(loader.getResource("images/" +
119
                                                             "fileXPdxf.gif"));
120
        ImageIcon gmlIcon = new ImageIcon(loader.getResource("images/" +
121
                                                             "fileXPgml.gif"));
122
        ImageIcon ecwIcon = new ImageIcon(loader.getResource("images/" +
123
                                                             "fileXPecw.gif"));
124
        ImageIcon tifIcon = new ImageIcon(loader.getResource("images/" +
125
                                                             "fileXPtif.gif"));
126
        ImageIcon fileIcon = new ImageIcon(loader.getResource("images/" +
127
                                                              "fileXP.gif"));
128

    
129
        menu = new JMenu("Favoritos");
130
        menu.setMnemonic('F');
131

    
132
        // Arbol de carpetas
133
        TreeMap menuMap = new TreeMap();
134
        String[] menuNames;
135
        JMenu parentMenu = null;
136
        String mName = null;
137
        String key = null;
138
        ;
139

    
140
        Iterator mIter = keySet().iterator();
141

    
142
        while (mIter.hasNext()) {
143
            key = (String) mIter.next();
144
            System.out.println("Key:" + key);
145
            menuNames = key.split("\\|");
146

    
147
            parentMenu = menu;
148
            mName = "";
149

    
150
            for (int i = 0; i < menuNames.length; i++) {
151
                if (i > 0) {
152
                    mName += "|";
153
                }
154

    
155
                mName += menuNames[i];
156

    
157
                if (menuMap.containsKey(mName)) {
158
                    //System.out.println(" mName:"+mName+", parent:"+parentMenu.getText());
159
                    parentMenu = (JMenu) menuMap.get(mName);
160
                } else {
161
                    //System.out.println("+mName:"+mName+", parent:"+parentMenu.getText());
162
                    menu2 = new JMenu(menuNames[i]);
163
                    menu2.setIcon(folderIcon);
164
                    parentMenu.add(menu2);
165
                    menuMap.put(mName, menu2);
166
                    parentMenu = menu2;
167
                }
168
            }
169
        }
170

    
171
        // Entradas (Bookmarks)
172
        mIter = entrySet().iterator();
173

    
174
        while (mIter.hasNext()) {
175
            Map.Entry entry = (Map.Entry) mIter.next();
176
            String folder = (String) entry.getKey();
177

    
178
            /*menu2 = new JMenu(folder);
179
            menu2.setIcon(folderIcon);
180
            menu.add(menu2);*/
181
            menu2 = (JMenu) menuMap.get(folder);
182

    
183
            Vector vector = ((Vector) entry.getValue());
184
            Bookmark bookmark;
185

    
186
            for (int i = 0; i < vector.size(); i++) {
187
                bookmark = (Bookmark) vector.get(i);
188
                it = new JMenuItem(bookmark.name);
189
                it.setActionCommand(i + "|" + folder);
190
                it.addActionListener(this);
191

    
192
                if (bookmark.fName[0].endsWith("tif")) {
193
                    it.setIcon(tifIcon);
194
                } else if (bookmark.fName[0].endsWith("ecw")) {
195
                    it.setIcon(ecwIcon);
196
                } else if (bookmark.fName[0].endsWith("gml")) {
197
                    it.setIcon(gmlIcon);
198
                } else if (bookmark.fName[0].endsWith("dxf") ||
199
                               bookmark.fName[0].endsWith("DXF")) {
200
                    it.setIcon(dxfIcon);
201
                } else {
202
                    it.setIcon(fileIcon);
203
                }
204

    
205
                menu2.add(it);
206
            }
207
        }
208

    
209
        //menu.add(menu2);
210
        return menu;
211
    }
212

    
213
    public void actionPerformed(ActionEvent event) {
214
        IProjection proj = null;
215
        String cmd = event.getActionCommand();
216
        int mark = cmd.indexOf('|');
217
        int vectorPos = Integer.parseInt(cmd.substring(0, mark));
218
        cmd = cmd.substring(mark + 1);
219

    
220
        Bookmark bookmark = (Bookmark) ((Vector) get(cmd)).get(vectorPos);
221

    
222
        if (bookmark.fName.length == 1) {
223
            app.loadFile(bookmark.fName[0], bookmark.proj);
224
        } else {
225
            app.loadFile(bookmark.fName, bookmark.proj);
226
        }
227
    }
228
}