gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.swing / org.gvsig.tools.swing.impl / src / main / java / org / gvsig / tools / swing / impl / windowmanager / DefaultDialog.java @ 2415
History | View | Annotate | Download (8.26 KB)
1 |
package org.gvsig.tools.swing.impl.windowmanager; |
---|---|
2 |
|
3 |
import java.awt.BorderLayout; |
4 |
import java.awt.Image; |
5 |
import java.awt.event.ActionEvent; |
6 |
import java.awt.event.ActionListener; |
7 |
import java.awt.event.ComponentEvent; |
8 |
import java.awt.event.ComponentListener; |
9 |
import javax.swing.JButton; |
10 |
import javax.swing.JComponent; |
11 |
import org.gvsig.tools.ToolsLocator; |
12 |
import org.gvsig.tools.i18n.I18nManager; |
13 |
import org.gvsig.tools.swing.api.ToolsSwingLocator; |
14 |
import org.gvsig.tools.swing.api.windowmanager.Dialog; |
15 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager; |
16 |
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2; |
17 |
import static org.gvsig.tools.swing.api.windowmanager.WindowManager_v2.BUTTON_APPLY; |
18 |
import static org.gvsig.tools.swing.api.windowmanager.WindowManager_v2.BUTTON_CANCEL; |
19 |
import static org.gvsig.tools.swing.api.windowmanager.WindowManager_v2.BUTTON_OK; |
20 |
import org.gvsig.tools.swing.impl.DefaultActionListenerSupport; |
21 |
|
22 |
public class DefaultDialog extends DefaultDialogView implements Dialog { |
23 |
|
24 |
private static final long serialVersionUID = -8823659108133528353L; |
25 |
|
26 |
protected JComponent contents; |
27 |
protected String header1; |
28 |
protected String header2; |
29 |
protected String title; |
30 |
|
31 |
protected int buttons; |
32 |
protected int action; |
33 |
protected DefaultActionListenerSupport listeners;
|
34 |
protected Image icon = null; |
35 |
protected Image image = null; |
36 |
private WindowManager windowManager = null; |
37 |
private boolean autoclose; |
38 |
|
39 |
@SuppressWarnings("OverridableMethodCallInConstructor") |
40 |
public DefaultDialog(JComponent contents, String title, String header, int buttons) { |
41 |
this.contents = contents;
|
42 |
this.title = title;
|
43 |
this.buttons = buttons;
|
44 |
this.action = BUTTON_CANCEL;
|
45 |
this.listeners = new DefaultActionListenerSupport(); |
46 |
this.autoclose = true; |
47 |
this.initComponents();
|
48 |
this.setButtons(buttons);
|
49 |
this.setHeaderLabel(header);
|
50 |
} |
51 |
|
52 |
public void setWindowManager(WindowManager windowManager) { |
53 |
this.windowManager = windowManager;
|
54 |
} |
55 |
|
56 |
@Override
|
57 |
public JComponent getContents() { |
58 |
return this.contents; |
59 |
} |
60 |
|
61 |
private void initComponents() { |
62 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
63 |
this.btnAccept.setText(i18nManager.getTranslation("Accept")); |
64 |
this.btnCancel.setText(i18nManager.getTranslation("Cancel")); |
65 |
this.btnApply.setText(i18nManager.getTranslation("Apply")); |
66 |
|
67 |
this.pnlContents.setLayout(new BorderLayout()); |
68 |
this.pnlContents.add(this.contents, BorderLayout.CENTER); |
69 |
|
70 |
this.addComponentListener(new ComponentListener() { |
71 |
@Override
|
72 |
public void componentResized(ComponentEvent e) { |
73 |
for(ComponentListener l : contents.getComponentListeners()) { |
74 |
l.componentResized(e); |
75 |
} |
76 |
} |
77 |
|
78 |
@Override
|
79 |
public void componentMoved(ComponentEvent e) { |
80 |
for(ComponentListener l : contents.getComponentListeners()) { |
81 |
l.componentMoved(e); |
82 |
} |
83 |
} |
84 |
|
85 |
@Override
|
86 |
public void componentShown(ComponentEvent e) { |
87 |
for(ComponentListener l : contents.getComponentListeners()) { |
88 |
l.componentShown(e); |
89 |
} |
90 |
} |
91 |
|
92 |
@Override
|
93 |
public void componentHidden(ComponentEvent e) { |
94 |
for(ComponentListener l : contents.getComponentListeners()) { |
95 |
l.componentHidden(e); |
96 |
} |
97 |
} |
98 |
}); |
99 |
|
100 |
|
101 |
this.btnAccept.addActionListener(new ActionListener() { |
102 |
@Override
|
103 |
public void actionPerformed(ActionEvent ae) { |
104 |
action = BUTTON_OK; |
105 |
if (autoclose) {
|
106 |
setVisible(false);
|
107 |
} |
108 |
fireActionEvent(new ActionEvent(DefaultDialog.this, BUTTON_OK, "ok")); |
109 |
} |
110 |
}); |
111 |
this.btnCancel.addActionListener(new ActionListener() { |
112 |
@Override
|
113 |
public void actionPerformed(ActionEvent ae) { |
114 |
action = BUTTON_CANCEL; |
115 |
if (autoclose) {
|
116 |
setVisible(false);
|
117 |
} |
118 |
fireActionEvent(new ActionEvent(DefaultDialog.this, BUTTON_CANCEL, "cancel")); |
119 |
} |
120 |
}); |
121 |
this.btnApply.addActionListener(new ActionListener() { |
122 |
@Override
|
123 |
public void actionPerformed(ActionEvent ae) { |
124 |
action = BUTTON_APPLY; |
125 |
fireActionEvent(new ActionEvent(DefaultDialog.this, BUTTON_APPLY, "apply")); |
126 |
} |
127 |
}); |
128 |
} |
129 |
|
130 |
@Override
|
131 |
public void setHeaderTitle(String header) { |
132 |
this.header1 = header;
|
133 |
if (this.header1 == null) { |
134 |
this.lblHeader1.setText(""); |
135 |
this.lblHeader1.setVisible(false); |
136 |
} else {
|
137 |
this.lblHeader1.setText(this.header1); |
138 |
this.lblHeader1.setVisible(true); |
139 |
} |
140 |
} |
141 |
|
142 |
@Override
|
143 |
public void setHeaderLabel(String header) { |
144 |
this.header2 = header;
|
145 |
if (this.header2 == null) { |
146 |
this.lblHeader2.setText(""); |
147 |
this.lblHeader2.setVisible(false); |
148 |
} else {
|
149 |
this.lblHeader2.setText(this.header2); |
150 |
this.lblHeader2.setVisible(true); |
151 |
} |
152 |
} |
153 |
|
154 |
@Override
|
155 |
public void setContents(JComponent contents) { |
156 |
this.contents = contents;
|
157 |
this.pnlContents.removeAll();
|
158 |
this.pnlContents.add(this.contents, BorderLayout.CENTER); |
159 |
} |
160 |
|
161 |
@Override
|
162 |
public int getAction() { |
163 |
return this.action; |
164 |
} |
165 |
|
166 |
@Override
|
167 |
public void addActionListener(ActionListener listener) { |
168 |
this.listeners.addActionListener(listener);
|
169 |
} |
170 |
|
171 |
@Override
|
172 |
public ActionListener[] getActionListeners() { |
173 |
return this.listeners.getActionListeners(); |
174 |
} |
175 |
|
176 |
@Override
|
177 |
public void removeActionListener(ActionListener listener) { |
178 |
this.listeners.removeActionListener(listener);
|
179 |
} |
180 |
|
181 |
@Override
|
182 |
public void removeAllActionListener() { |
183 |
this.listeners.removeAllActionListener();
|
184 |
} |
185 |
|
186 |
private void fireActionEvent(ActionEvent event) { |
187 |
this.listeners.fireActionEvent(event);
|
188 |
} |
189 |
|
190 |
protected WindowManager getWindowManager() {
|
191 |
if( this.windowManager == null ) { |
192 |
this.windowManager = ToolsSwingLocator.getWindowManager();
|
193 |
} |
194 |
return this.windowManager; |
195 |
} |
196 |
|
197 |
@Override
|
198 |
public void show(WindowManager.MODE mode) { |
199 |
WindowManager winmgr = this.getWindowManager();
|
200 |
if (icon != null && winmgr instanceof WindowManager_v2) { |
201 |
((WindowManager_v2) winmgr).showWindow(this, title, mode, icon);
|
202 |
} else {
|
203 |
winmgr.showWindow(this, title, mode);
|
204 |
} |
205 |
} |
206 |
|
207 |
@Override
|
208 |
public void setIcon(Image icon) { |
209 |
this.icon = icon;
|
210 |
} |
211 |
|
212 |
@Override
|
213 |
public void setHeaderImage(Image image) { |
214 |
this.image = image;
|
215 |
|
216 |
} |
217 |
|
218 |
@Override
|
219 |
public void setButtons(int buttons) { |
220 |
this.buttons = buttons;
|
221 |
this.btnAccept.setVisible((this.buttons & BUTTON_OK) == BUTTON_OK); |
222 |
this.btnCancel.setVisible((this.buttons & BUTTON_CANCEL) == BUTTON_CANCEL); |
223 |
this.btnApply.setVisible((this.buttons & BUTTON_APPLY) == BUTTON_APPLY); |
224 |
} |
225 |
|
226 |
public JButton getButton(int button) { |
227 |
switch (button) {
|
228 |
case BUTTON_APPLY:
|
229 |
return this.btnApply; |
230 |
case BUTTON_OK:
|
231 |
return this.btnAccept; |
232 |
case BUTTON_CANCEL:
|
233 |
return this.btnCancel; |
234 |
} |
235 |
throw new IllegalArgumentException("Button number not allowed (button="+button+")."); |
236 |
} |
237 |
|
238 |
@Override
|
239 |
public void setButtonLabel(int button, String label) { |
240 |
this.getButton(button).setText(label);
|
241 |
} |
242 |
|
243 |
@Override
|
244 |
public JComponent asJComponent() { |
245 |
return this; |
246 |
} |
247 |
|
248 |
@Override
|
249 |
public void setButtonEnabled(int button, boolean enabled) { |
250 |
this.getButton(button).setEnabled(enabled);
|
251 |
} |
252 |
|
253 |
@Override
|
254 |
public boolean isButtonEnabled(int button) { |
255 |
return this.getButton(button).isEnabled(); |
256 |
} |
257 |
|
258 |
@Override
|
259 |
public void setAutoclose(boolean autoclose) { |
260 |
this.autoclose = autoclose;
|
261 |
} |
262 |
|
263 |
@Override
|
264 |
public boolean isAutoclose() { |
265 |
return this.autoclose; |
266 |
} |
267 |
} |