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