svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.framework / org.gvsig.andami / src / main / java / org / gvsig / andami / ui / ToolsWindowManager.java @ 46586
History | View | Annotate | Download (7.88 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 |
import javax.swing.JComponent; |
34 |
import javax.swing.SwingUtilities; |
35 |
import org.gvsig.andami.PluginServices; |
36 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
37 |
import org.gvsig.andami.ui.mdiManager.MDIManager; |
38 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
39 |
import org.gvsig.tools.swing.api.WindowPanel; |
40 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
41 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager.MODE; |
42 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
43 |
import org.gvsig.tools.swing.impl.windowmanager.DefaultDialog; |
44 |
import org.gvsig.tools.util.PropertiesSupport; |
45 |
|
46 |
|
47 |
/**
|
48 |
* @author gvSIG Team
|
49 |
* @version $Id$
|
50 |
*
|
51 |
*/
|
52 |
public class ToolsWindowManager implements WindowManager_v2 { |
53 |
|
54 |
private int getAlign(MODE mode) { |
55 |
switch(mode) {
|
56 |
case DIALOG:
|
57 |
return GridBagConstraints.CENTER; |
58 |
case TOOL:
|
59 |
return GridBagConstraints.FIRST_LINE_END; |
60 |
case WINDOW:
|
61 |
default:
|
62 |
return GridBagConstraints.FIRST_LINE_START; |
63 |
} |
64 |
} |
65 |
|
66 |
@Override
|
67 |
public void showWindow(JComponent contents, String title, MODE mode) { |
68 |
int flags = getFlags(contents);
|
69 |
showWindow(contents, title, mode, getAlign(mode), null, flags);
|
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, flags); |
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 WindowPanel implements IWindow, ComponentListener { |
116 |
|
117 |
/**
|
118 |
*
|
119 |
*/
|
120 |
private static final long serialVersionUID = -6688594664366192449L; |
121 |
protected WindowInfo windowInfo;
|
122 |
protected Object profile; |
123 |
|
124 |
@SuppressWarnings({"OverridableMethodCallInConstructor", "LeakingThisInConstructor"}) |
125 |
public Window(JComponent contents, String title, MODE mode, int flags) { |
126 |
int code;
|
127 |
if( flags == 0 ) { |
128 |
code = WindowInfo.RESIZABLE | WindowInfo.MAXIMIZABLE | WindowInfo.ICONIFIABLE; |
129 |
} else {
|
130 |
code = flags; |
131 |
} |
132 |
switch(mode) {
|
133 |
case DIALOG:
|
134 |
code |= WindowInfo.MODALDIALOG; |
135 |
profile = WindowInfo.DIALOG_PROFILE; |
136 |
break;
|
137 |
case TOOL:
|
138 |
code |= WindowInfo.MODELESSDIALOG | WindowInfo.PALETTE; |
139 |
profile = WindowInfo.TOOL_PROFILE; |
140 |
break;
|
141 |
case WINDOW:
|
142 |
default:
|
143 |
profile = WindowInfo.EDITOR_PROFILE; |
144 |
break;
|
145 |
} |
146 |
|
147 |
this.contents = contents;
|
148 |
|
149 |
this.windowInfo = new WindowInfo(code); |
150 |
this.windowInfo.setTitle(title);
|
151 |
this.windowInfo.setNeedPack(true); |
152 |
|
153 |
Dimension size = this.contents.getPreferredSize(); |
154 |
this.windowInfo.setHeight(size.height);
|
155 |
this.windowInfo.setWidth(size.width);
|
156 |
|
157 |
this.setLayout(new BorderLayout()); |
158 |
this.add(this.contents,BorderLayout.CENTER); |
159 |
|
160 |
this.contents.addComponentListener(this); |
161 |
} |
162 |
|
163 |
public void fireClosingWindow() { |
164 |
this.contents.removeComponentListener(this); |
165 |
ComponentListener[] l = this.contents.getComponentListeners(); |
166 |
for( int i=0; i<l.length; i++) { |
167 |
l[i].componentHidden(new ComponentEvent(this, i)); |
168 |
} |
169 |
} |
170 |
|
171 |
@Override
|
172 |
public WindowInfo getWindowInfo() {
|
173 |
return this.windowInfo; |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public Object getWindowProfile() { |
178 |
return this.profile; |
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void componentHidden(ComponentEvent arg0) { |
183 |
// Close window when hide contents panel.
|
184 |
MDIManager manager = PluginServices.getMDIManager(); |
185 |
manager.closeWindow(this);
|
186 |
} |
187 |
|
188 |
@Override
|
189 |
public void componentMoved(ComponentEvent arg0) { |
190 |
// Do nothing
|
191 |
} |
192 |
|
193 |
@Override
|
194 |
public void componentResized(ComponentEvent arg0) { |
195 |
// Do nothing
|
196 |
} |
197 |
|
198 |
@Override
|
199 |
public void componentShown(ComponentEvent arg0) { |
200 |
// Do nothing
|
201 |
} |
202 |
|
203 |
} |
204 |
|
205 |
|
206 |
@Override
|
207 |
public Dialog createDialog(JComponent component, String title, String header, int buttons) { |
208 |
DefaultDialog dialog = new DefaultDialog(component, title, header, buttons);
|
209 |
return dialog;
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void showWindow(JComponent contents, String title, MODE mode, Image icon) { |
214 |
int align;
|
215 |
switch(mode) {
|
216 |
case DIALOG:
|
217 |
align = GridBagConstraints.CENTER;
|
218 |
break;
|
219 |
case TOOL:
|
220 |
align = GridBagConstraints.FIRST_LINE_END;
|
221 |
break;
|
222 |
case WINDOW:
|
223 |
default:
|
224 |
align = GridBagConstraints.FIRST_LINE_START;
|
225 |
break;
|
226 |
} |
227 |
showWindow(contents, title, mode, align, icon, 0);
|
228 |
} |
229 |
|
230 |
private int getFlags(JComponent comp) { |
231 |
try {
|
232 |
if( !(comp instanceof PropertiesSupport) ) { |
233 |
return 0; |
234 |
} |
235 |
int flags = ((Number) ((PropertiesSupport)comp).getProperty("WindowInfo.Flags")).intValue(); |
236 |
return flags;
|
237 |
} catch(Exception ex) { |
238 |
return 0; |
239 |
} |
240 |
} |
241 |
} |