Statistics
| Revision:

gvsig-projects-pool / org.gvsig.winmgr / trunk / org.gvsig.winmgr.app / org.gvsig.winmgr.app.mainplugin / src / main / java / org / gvsig / coreplugin / mdiManager / FrameWindowSupport.java @ 682

History | View | Annotate | Download (10.8 KB)

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.coreplugin.mdiManager;
25

    
26
import java.awt.Component;
27
import java.awt.Dimension;
28
import java.awt.Image;
29
import java.awt.event.ComponentEvent;
30
import java.awt.event.ComponentListener;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.Map;
34
import java.util.Set;
35

    
36
import javax.swing.ImageIcon;
37
import javax.swing.JComponent;
38
import javax.swing.JDialog;
39
import javax.swing.JFrame;
40
import javax.swing.JInternalFrame;
41
import javax.swing.JPanel;
42
import org.gvsig.andami.PluginServices;
43
import org.gvsig.andami.ui.ToolsWindowManager;
44

    
45
import org.gvsig.andami.ui.mdiFrame.MDIFrame;
46
import org.gvsig.andami.ui.mdiManager.IWindow;
47
import org.gvsig.andami.ui.mdiManager.WindowInfo;
48
import org.gvsig.coreplugin.mdiManager.frames.ExternalFrame;
49
import org.gvsig.coreplugin.mdiManager.frames.IFrame;
50
import org.gvsig.coreplugin.mdiManager.frames.InternalFrame;
51
import org.gvsig.i18n.Messages;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55

    
56

    
57
/**
58
 *
59
 */
60
public class FrameWindowSupport {
61
    private static Logger logger = LoggerFactory.getLogger(FrameWindowSupport.class);
62
            
63
    private Map frameView = new HashMap();
64
    private Map<IWindow,Object> viewFrame = new HashMap();
65
    private Image icon;
66
    private WindowInfoSupport vis;
67
    private JFrame mainFrame;
68

    
69
    /**
70
     * Creates a new FrameViewSupport object.
71
     *
72
     * @param i DOCUMENT ME!
73
     */
74
    public FrameWindowSupport(MDIFrame mainFrame) {
75
            this.mainFrame = mainFrame;
76
        icon = mainFrame.getIconImage();
77
    }
78

    
79
    public Iterator getWindowIterator(){
80
            return viewFrame.keySet().iterator();
81
    }
82

    
83
    public boolean contains(IWindow v){
84
            return viewFrame.containsKey(v);
85
    }
86

    
87
        /**
88
         * @param wnd
89
         * @return
90
         */
91
        public boolean contains(JInternalFrame wnd) {
92
                return frameView.containsValue(wnd);
93
        }
94

    
95
    /**
96
     * DOCUMENT ME!
97
     *
98
     * @param p DOCUMENT ME!
99
     *
100
     * @return DOCUMENT ME!
101
     */
102
    public JDialog getJDialog(IWindow p) {
103
        JDialog dlg = (JDialog) viewFrame.get(p);
104

    
105
        if (dlg == null) {
106
            ExternalFrame nuevo = new ExternalFrame(mainFrame);
107

    
108
            nuevo.getContentPane().add((JPanel) p);
109
            
110
            WindowInfo vi = vis.getWindowInfo(p);
111
            nuevo.setSize(getWidth(p, vi), getHeight(p, vi) + 30);
112
            nuevo.setTitle(vi.getTitle());
113
            nuevo.setResizable(vi.isResizable());
114
            nuevo.setMinimumSize(vi.getMinimumSize());
115
            if( vi.needPack() ) {
116
                nuevo.pack();
117
            }
118
            viewFrame.put(p, nuevo);
119
            frameView.put(nuevo, p);
120

    
121
            nuevo.setModal(vi.isModal());
122
            closeOnHide(p);
123
            return nuevo;
124
        } else {
125
            return dlg;
126
        }
127
    }
128

    
129
    /**
130
     * DOCUMENT ME!
131
     *
132
     * @param p DOCUMENT ME!
133
     *
134
     * @return DOCUMENT ME!
135
     */
136
    public JInternalFrame getJInternalFrame(IWindow p) {
137
        JInternalFrame frame = null;
138
        try {
139
            for( Map.Entry<IWindow,Object> entry : viewFrame.entrySet()) {
140
                if( entry.getKey() instanceof ToolsWindowManager.Window )  {
141
                    JComponent contents = ((ToolsWindowManager.Window)entry.getKey()).getContents();
142
                    if( p == contents ) {
143
                        frame = (JInternalFrame) entry.getValue();
144
                    }
145
                }
146
            }
147
        } catch(Exception ex) {
148
            logger.warn("Can't get JInternalFrame from IWindow ("+p.getWindowInfo().getTitle()+").",ex);
149
        }
150
            if( frame == null ) {
151
            frame = (JInternalFrame) viewFrame.get(p);
152
        }
153

    
154
        if (frame == null) {
155
                //ViewInfo vi = vis.getViewInfo(p);
156
            JInternalFrame nuevo = createJInternalFrame(p);
157
            viewFrame.put(p, nuevo);
158
            frameView.put(nuevo, p);
159

    
160
            return nuevo;
161
        } else {
162
            return frame;
163
        }
164
    }
165
    
166
    /**
167
     * Gets the frame associated to the provided IWindow panel.
168
     * The frame will usually be a JInternalFrame or a JDialog.
169
     *
170
     * @param panel The IWindow panel whose frame wants to be retrieved.
171
     *
172
     * @return The associated frame, it will usually be a JInternalFrame or
173
     * a JDialog.
174
     */
175
    public Component getFrame(IWindow panel) {
176
            Object object = viewFrame.get(panel);
177
            if (object!=null && object instanceof Component) {
178
                    return (Component) object;
179
            }
180
            else {
181
            throw new RuntimeException(Messages.getText("window_not_found")
182
                + ": " + panel.getWindowInfo().getTitle());
183
            }
184
    }
185

    
186
    public JInternalFrame createJInternalFrame(IWindow p)
187
    {
188
        WindowInfo wi = vis.getWindowInfo(p);
189
        JInternalFrame nuevo = new InternalFrame();
190
        if (icon != null){
191
            nuevo.setFrameIcon(new ImageIcon(icon));
192
        }
193
        
194
        nuevo.getContentPane().add((JPanel) p);
195
        nuevo.setClosable(!wi.isNotClosable());
196
        nuevo.setSize(getWidth(p, wi), getHeight(p, wi));
197
        nuevo.setTitle(wi.getTitle());
198
        nuevo.setVisible(wi.isVisible());
199
        nuevo.setResizable(wi.isResizable());
200
        nuevo.setIconifiable(wi.isIconifiable());
201
        nuevo.setMaximizable(wi.isMaximizable());
202
        nuevo.setLocation(wi.getX(), wi.getY());
203
        nuevo.setMinimumSize(wi.getMinimumSize());
204
        closeOnHide(p);
205
        nuevo.setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
206
        return nuevo;
207
    }
208

    
209
    private void closeOnHide(final IWindow p) {
210
        ((JPanel)p).addComponentListener(new ComponentListener() {
211

    
212
            @Override
213
            public void componentResized(ComponentEvent ce) {
214
            }
215

    
216
            @Override
217
            public void componentMoved(ComponentEvent ce) {
218
            }
219

    
220
            @Override
221
            public void componentShown(ComponentEvent ce) {
222
            }
223

    
224
            @Override
225
            public void componentHidden(ComponentEvent ce) {
226
                PluginServices.getMDIManager().closeWindow(p);
227
            }
228
        });
229
        
230
    }
231
    
232
    public IWindow getWindow(Component dlg){
233
            return (IWindow) frameView.get(dlg);
234
    }
235

    
236
    public void closeWindow(IWindow v){
237
            Object c = viewFrame.remove(v);
238
            frameView.remove(c);
239
    }
240

    
241
    /**
242
     * DOCUMENT ME!
243
     *
244
     * @param v DOCUMENT ME!
245
     * @param x DOCUMENT ME!
246
     */
247
    public void setX(IWindow win, int x) {
248
            IFrame frame = (IFrame) viewFrame.get(win);
249
            if (frame != null) {
250
                frame.setX(x);
251
            }
252
            
253
    }
254

    
255
    /**
256
     * DOCUMENT ME!
257
     *
258
     * @param v DOCUMENT ME!
259
     * @param y DOCUMENT ME!
260
     */
261
    public void setY(IWindow win, int y) {
262
            IFrame frame = (IFrame) viewFrame.get(win);
263
        if (frame != null) {
264
            frame.setY(y);
265
        }
266
    }
267

    
268
    /**
269
     * DOCUMENT ME!
270
     *
271
     * @param v DOCUMENT ME!
272
     * @param height DOCUMENT ME!
273
     */
274
    public void setHeight(IWindow win, int height) {
275
            IFrame frame = (IFrame) viewFrame.get(win);
276
            frame.setHeight(height);
277
    }
278

    
279
    /**
280
     * DOCUMENT ME!
281
     *
282
     * @param v DOCUMENT ME!
283
     * @param width DOCUMENT ME!
284
     */
285
    public void setWidth(IWindow win, int width) {
286
            IFrame frame = (IFrame) viewFrame.get(win);
287
            frame.setWidth(width);
288
    }
289

    
290
    /**
291
     * DOCUMENT ME!
292
     *
293
     * @param v DOCUMENT ME!
294
     * @param title DOCUMENT ME!
295
     */
296
    public void setTitle(IWindow win, String title) {
297
            IFrame frame = (IFrame) viewFrame.get(win);
298
            frame.setTitle(title);
299
    }
300
    
301
        /**
302
         * Sets the minimum allowed size for the provided window.
303
         * 
304
         * @param sw
305
         * @param minSize
306
         */
307
        public void setMinimumSize(IWindow win, Dimension minSize) {
308
            IFrame frame = (IFrame) viewFrame.get(win);
309
            frame.setMinimumSize(minSize);
310
        }
311

    
312
    /**
313
     * DOCUMENT ME!
314
     *
315
     * @param vis The vis to set.
316
     */
317
    public void setVis(WindowInfoSupport vis) {
318
        this.vis = vis;
319
    }
320

    
321
    /**
322
     * DOCUMENT ME!
323
     *
324
     * @param v DOCUMENT ME!
325
     *
326
     * @return DOCUMENT ME!
327
     */
328
    private int getWidth(IWindow v) {
329
        WindowInfo vi = vis.getWindowInfo(v);
330

    
331
        if (vi.getWidth() == -1) {
332
            JPanel p = (JPanel) v;
333

    
334
            return p.getSize().width;
335
        } else {
336
            return vi.getWidth();
337
        }
338
    }
339

    
340
    /**
341
     * DOCUMENT ME!
342
     *
343
     * @param v DOCUMENT ME!
344
     *
345
     * @return DOCUMENT ME!
346
     */
347
    private int getWidth(IWindow v, WindowInfo wi) {
348
        if (wi.getWidth() == -1) {
349
            JPanel p = (JPanel) v;
350

    
351
            return p.getSize().width;
352
        } else {
353
            return wi.getWidth();
354
        }
355
    }
356

    
357
    /**
358
     * DOCUMENT ME!
359
     *
360
     * @param v DOCUMENT ME!
361
     *
362
     * @return DOCUMENT ME!
363
     */
364
    private int getHeight(IWindow v) {
365
        WindowInfo vi = vis.getWindowInfo(v);
366

    
367
        if (vi.getHeight() == -1) {
368
            JPanel p = (JPanel) v;
369

    
370
            return p.getSize().height;
371
        } else {
372
            return vi.getHeight();
373
        }
374
    }
375

    
376
    /**
377
     * DOCUMENT ME!
378
     *
379
     * @param v DOCUMENT ME!
380
     *
381
     * @return DOCUMENT ME!
382
     */
383
    private int getHeight(IWindow v, WindowInfo wi) {
384
        if (wi.getHeight() == -1) {
385
            JPanel p = (JPanel) v;
386

    
387
            return p.getSize().height;
388
        } else {
389
            return wi.getHeight();
390
        }
391
    }
392

    
393
    public void updateWindowInfo(IWindow win, WindowInfo windowInfo) {
394
            Object o = viewFrame.get(win);
395
            if (windowInfo!=null && o!=null) {
396
                    if (o instanceof JComponent) {
397
                        JComponent component = (JComponent) o;
398
                        windowInfo.updateWidth(component.getWidth());
399
                                windowInfo.updateHeight(component.getHeight());
400
                                windowInfo.updateX(component.getX());
401
                                windowInfo.updateY(component.getY());
402
                                windowInfo.updateClosed(!component.isShowing());
403
                                if (component instanceof JInternalFrame) {
404
                                        JInternalFrame iframe = (JInternalFrame) component;
405
                                        windowInfo.updateNormalBounds(iframe.getNormalBounds());
406
                                        windowInfo.updateMaximized(iframe.isMaximum());
407
                                }
408
                    }
409
            }
410
    }
411

    
412
}