Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / ToolsWindowManager.java @ 43939

History | View | Annotate | Download (7.55 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.andami.ui;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Dimension;
28
import java.awt.GridBagConstraints;
29
import java.awt.Image;
30
import java.awt.event.ComponentEvent;
31
import java.awt.event.ComponentListener;
32
import java.lang.reflect.InvocationTargetException;
33

    
34
import javax.swing.JComponent;
35
import javax.swing.JPanel;
36
import javax.swing.SwingUtilities;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.ui.mdiManager.IWindow;
40
import org.gvsig.andami.ui.mdiManager.MDIManager;
41
import org.gvsig.andami.ui.mdiManager.WindowInfo;
42
import org.gvsig.tools.swing.api.windowmanager.Dialog;
43
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE;
44
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
45
import org.gvsig.tools.swing.impl.windowmanager.DefaultDialog;
46

    
47

    
48
/**
49
 * @author gvSIG Team
50
 * @version $Id$
51
 *
52
 */
53
public class ToolsWindowManager implements WindowManager_v2 {
54

    
55
    private int getAlign(MODE mode) {
56
        switch(mode) {
57
        case DIALOG:
58
            return GridBagConstraints.CENTER;
59
        case TOOL:
60
            return GridBagConstraints.FIRST_LINE_END;
61
        case WINDOW:
62
        default:
63
            return GridBagConstraints.FIRST_LINE_START;
64
        }
65
    }
66
    
67
    @Override
68
    public void showWindow(JComponent contents, String title, MODE mode) {
69
        showWindow(contents, title, mode, getAlign(mode), null, 0);
70
    }
71

    
72
    @Override
73
    public void showWindow(final JComponent contents, final String title, final MODE mode, final Image icon, final int flags) {
74
        showWindow(contents, title, mode, getAlign(mode), icon, 0);
75
    }
76
//
77
//    public void showWindow(final JComponent contents, final String title, final MODE mode, final int align) {
78
//        this.showWindow(contents, title, mode, align, null, 0);
79
//    }
80
    
81
    public void showWindow(final JComponent contents, final String title, final MODE mode, final int align, final Image icon, final int flags ) {
82
        if( !SwingUtilities.isEventDispatchThread() ) {
83
            switch(mode) {
84
            case DIALOG:
85
                {
86
                    try {
87
                        SwingUtilities.invokeAndWait(new Runnable() {
88
                            @Override
89
                            public void run() {
90
                                showWindow(contents, title, mode, align, null, flags);
91
                            }
92
                        });
93
                    } catch (InterruptedException | InvocationTargetException ex) {
94
                    }
95
                }
96
                break;
97
            case TOOL:
98
            case WINDOW:
99
            default:
100
                SwingUtilities.invokeLater(new Runnable() {
101
                    @Override
102
                    public void run() {
103
                        showWindow(contents, title, mode, align,null, flags);
104
                    }
105
                });
106
                break;
107
            }
108
            return;
109
        }
110
        MDIManager manager = PluginServices.getMDIManager();
111
        IWindow window = new Window(contents, title, mode, flags);
112
        manager.addWindow(window,align);
113
    }
114

    
115
    public class Window extends JPanel implements IWindow, ComponentListener {
116

    
117
        /**
118
         *
119
         */
120
        private static final long serialVersionUID = -6688594664366192449L;
121
        protected WindowInfo windowInfo;
122
        protected Object profile;
123
        protected JComponent contents;
124

    
125
        @SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"})
126
        public Window(JComponent contents, String title, MODE mode, int flags) {
127
            int code;
128
            if( flags == 0 ) {
129
                code = WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE | WindowInfo.ICONIFIABLE;
130
            } else {
131
                code = flags;
132
            }
133
            switch(mode) {
134
            case DIALOG:
135
                code |= WindowInfo.MODALDIALOG;
136
                profile = WindowInfo.DIALOG_PROFILE;
137
                break;
138
            case TOOL:
139
                code |= WindowInfo.MODELESSDIALOG | WindowInfo.PALETTE;
140
                profile = WindowInfo.TOOL_PROFILE;
141
                break;
142
            case WINDOW:
143
            default:
144
                profile = WindowInfo.EDITOR_PROFILE;
145
                break;
146
            }
147

    
148
            this.contents = contents;
149

    
150
            this.windowInfo = new WindowInfo(code);
151
            this.windowInfo.setTitle(title);
152
            this.windowInfo.setNeedPack(true);
153

    
154
            Dimension size = this.contents.getPreferredSize();
155
            this.windowInfo.setHeight(size.height);
156
            this.windowInfo.setWidth(size.width);
157

    
158
            this.setLayout(new BorderLayout());
159
            this.add(this.contents,BorderLayout.CENTER);
160

    
161
            this.contents.addComponentListener(this);
162
        }
163

    
164
        public void fireClosingWindow() {
165
            this.contents.removeComponentListener(this);
166
            ComponentListener[] l = this.contents.getComponentListeners();
167
            for( int i=0; i<l.length; i++) {
168
                l[i].componentHidden(new ComponentEvent(this, i));
169
            }
170
        }
171

    
172
        @Override
173
        public WindowInfo getWindowInfo() {
174
            return this.windowInfo;
175
        }
176

    
177
        @Override
178
        public Object getWindowProfile() {
179
            return this.profile;
180
        }
181

    
182
        @Override
183
        public void componentHidden(ComponentEvent arg0) {
184
            // Close window when hide contents panel.
185
            MDIManager manager = PluginServices.getMDIManager();
186
            manager.closeWindow(this);
187
        }
188

    
189
        @Override
190
        public void componentMoved(ComponentEvent arg0) {
191
            // Do nothing
192
        }
193

    
194
        @Override
195
        public void componentResized(ComponentEvent arg0) {
196
            // Do nothing
197
        }
198

    
199
        @Override
200
        public void componentShown(ComponentEvent arg0) {
201
            // Do nothing
202
        }
203

    
204
        public JComponent getContents() {
205
            return this.contents;
206
        }
207

    
208
    }
209
    
210
    
211
    @Override
212
    public Dialog createDialog(JComponent component, String title, String header, int buttons) {
213
        DefaultDialog dialog = new DefaultDialog(component, title, header, buttons);
214
        return dialog;
215
    }
216

    
217
    @Override
218
    public void showWindow(JComponent contents, String title, MODE mode, Image icon) {
219
        int align;
220
        switch(mode) {
221
        case DIALOG:
222
            align = GridBagConstraints.CENTER;
223
            break;
224
        case TOOL:
225
            align = GridBagConstraints.FIRST_LINE_END;
226
            break;
227
        case WINDOW:
228
        default:
229
            align = GridBagConstraints.FIRST_LINE_START;
230
            break;
231
        }
232
        showWindow(contents, title, mode, align, icon, 0);
233
    }
234

    
235
}