Statistics
| Revision:

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 @ 1696

History | View | Annotate | Download (6.82 KB)

1 1283 jjdelcerro
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 1675 jjdelcerro
import javax.swing.JButton;
8 1283 jjdelcerro
import javax.swing.JComponent;
9 1439 jjdelcerro
import org.gvsig.tools.ToolsLocator;
10
import org.gvsig.tools.i18n.I18nManager;
11 1283 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
12
import org.gvsig.tools.swing.api.windowmanager.Dialog;
13
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
14
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
15
import static org.gvsig.tools.swing.api.windowmanager.WindowManager_v2.BUTTON_APPLY;
16
import static org.gvsig.tools.swing.api.windowmanager.WindowManager_v2.BUTTON_CANCEL;
17
import static org.gvsig.tools.swing.api.windowmanager.WindowManager_v2.BUTTON_OK;
18
import org.gvsig.tools.swing.impl.DefaultActionListenerSupport;
19
20
public class DefaultDialog extends DefaultDialogView implements Dialog {
21
22 1405 jjdelcerro
    private static final long serialVersionUID = -8823659108133528353L;
23 1283 jjdelcerro
24 1405 jjdelcerro
    protected JComponent contents;
25
    protected String header1;
26
    protected String header2;
27
    protected String title;
28 1283 jjdelcerro
29 1405 jjdelcerro
    protected int buttons;
30
    protected int action;
31
    protected DefaultActionListenerSupport listeners;
32
    protected Image icon = null;
33
    protected Image image = null;
34 1439 jjdelcerro
    private WindowManager windowManager = null;
35 1405 jjdelcerro
36 1695 jjdelcerro
    @SuppressWarnings("OverridableMethodCallInConstructor")
37 1283 jjdelcerro
    public DefaultDialog(JComponent contents, String title, String header, int buttons) {
38
        this.contents = contents;
39
        this.title = title;
40
        this.buttons = buttons;
41
        this.action = BUTTON_CANCEL;
42
        this.listeners = new DefaultActionListenerSupport();
43
        this.initComponents();
44
        this.setButtons(buttons);
45
        this.setHeaderLabel(header);
46
    }
47
48 1439 jjdelcerro
    public void setWindowManager(WindowManager windowManager) {
49
        this.windowManager = windowManager;
50
    }
51
52 1696 jjdelcerro
    @Override
53 1695 jjdelcerro
    public JComponent getContents() {
54
        return this.contents;
55
    }
56
57 1283 jjdelcerro
    private void initComponents() {
58 1439 jjdelcerro
        I18nManager i18nManager = ToolsLocator.getI18nManager();
59
        this.btnAccept.setText(i18nManager.getTranslation("Accept"));
60
        this.btnCancel.setText(i18nManager.getTranslation("Cancel"));
61
        this.btnApply.setText(i18nManager.getTranslation("Apply"));
62
63 1283 jjdelcerro
        this.pnlContents.setLayout(new BorderLayout());
64
        this.pnlContents.add(this.contents, BorderLayout.CENTER);
65
66
        this.btnAccept.addActionListener(new ActionListener() {
67
            @Override
68
            public void actionPerformed(ActionEvent ae) {
69
                action = BUTTON_OK;
70
                setVisible(false);
71
                fireActionEvent(new ActionEvent(DefaultDialog.this, BUTTON_OK, "ok"));
72
            }
73
        });
74
        this.btnCancel.addActionListener(new ActionListener() {
75
            @Override
76
            public void actionPerformed(ActionEvent ae) {
77
                action = BUTTON_CANCEL;
78
                setVisible(false);
79
                fireActionEvent(new ActionEvent(DefaultDialog.this, BUTTON_CANCEL, "cancel"));
80
            }
81
        });
82
        this.btnApply.addActionListener(new ActionListener() {
83
            @Override
84
            public void actionPerformed(ActionEvent ae) {
85
                action = BUTTON_APPLY;
86
                fireActionEvent(new ActionEvent(DefaultDialog.this, BUTTON_APPLY, "apply"));
87
            }
88
        });
89
    }
90
91 1405 jjdelcerro
    @Override
92 1283 jjdelcerro
    public void setHeaderTitle(String header) {
93
        this.header1 = header;
94 1405 jjdelcerro
        if (this.header1 == null) {
95 1392 jjdelcerro
            this.lblHeader1.setText("");
96
            this.lblHeader1.setVisible(false);
97
        } else {
98
            this.lblHeader1.setText(this.header1);
99
            this.lblHeader1.setVisible(true);
100
        }
101 1283 jjdelcerro
    }
102
103 1405 jjdelcerro
    @Override
104 1283 jjdelcerro
    public void setHeaderLabel(String header) {
105
        this.header2 = header;
106 1405 jjdelcerro
        if (this.header2 == null) {
107 1392 jjdelcerro
            this.lblHeader2.setText("");
108
            this.lblHeader2.setVisible(false);
109
        } else {
110
            this.lblHeader2.setText(this.header2);
111
            this.lblHeader2.setVisible(true);
112
        }
113 1283 jjdelcerro
    }
114
115
    @Override
116
    public void setContents(JComponent contents) {
117
        this.contents = contents;
118
        this.pnlContents.removeAll();
119
        this.pnlContents.add(this.contents, BorderLayout.CENTER);
120
    }
121
122
    @Override
123
    public int getAction() {
124
        return this.action;
125
    }
126
127
    @Override
128
    public void addActionListener(ActionListener listener) {
129
        this.listeners.addActionListener(listener);
130
    }
131
132
    @Override
133
    public ActionListener[] getActionListeners() {
134
        return this.listeners.getActionListeners();
135
    }
136
137
    @Override
138
    public void removeActionListener(ActionListener listener) {
139
        this.listeners.removeActionListener(listener);
140
    }
141
142
    @Override
143
    public void removeAllActionListener() {
144
        this.listeners.removeAllActionListener();
145
    }
146
147 1645 jjdelcerro
    private void fireActionEvent(ActionEvent event) {
148 1283 jjdelcerro
        this.listeners.fireActionEvent(event);
149
    }
150
151 1405 jjdelcerro
    protected WindowManager getWindowManager() {
152 1439 jjdelcerro
        if( this.windowManager == null ) {
153
            this.windowManager = ToolsSwingLocator.getWindowManager();
154
        }
155
        return this.windowManager;
156 1405 jjdelcerro
    }
157
158 1283 jjdelcerro
    @Override
159
    public void show(WindowManager.MODE mode) {
160 1405 jjdelcerro
        WindowManager winmgr = this.getWindowManager();
161 1283 jjdelcerro
        if (icon != null && winmgr instanceof WindowManager_v2) {
162
            ((WindowManager_v2) winmgr).showWindow(this, title, mode, icon);
163
        } else {
164
            winmgr.showWindow(this, title, mode);
165
        }
166
    }
167
168
    @Override
169
    public void setIcon(Image icon) {
170
        this.icon = icon;
171
    }
172
173
    @Override
174
    public void setHeaderImage(Image image) {
175
        this.image = image;
176
177
    }
178
179
    @Override
180
    public void setButtons(int buttons) {
181
        this.buttons = buttons;
182
        this.btnAccept.setVisible((this.buttons & BUTTON_OK) == BUTTON_OK);
183
        this.btnCancel.setVisible((this.buttons & BUTTON_CANCEL) == BUTTON_CANCEL);
184
        this.btnApply.setVisible((this.buttons & BUTTON_APPLY) == BUTTON_APPLY);
185
    }
186
187 1675 jjdelcerro
    public JButton getButton(int button) {
188 1283 jjdelcerro
        switch (button) {
189
            case BUTTON_APPLY:
190 1675 jjdelcerro
                return this.btnApply;
191 1283 jjdelcerro
            case BUTTON_OK:
192 1675 jjdelcerro
                return this.btnAccept;
193 1283 jjdelcerro
            case BUTTON_CANCEL:
194 1675 jjdelcerro
                return this.btnCancel;
195 1283 jjdelcerro
        }
196 1675 jjdelcerro
        throw new IllegalArgumentException("Button number not allowed (button="+button+").");
197 1283 jjdelcerro
    }
198 1675 jjdelcerro
199
    @Override
200
    public void setButtonLabel(int button, String label) {
201
        this.getButton(button).setText(label);
202
    }
203 1283 jjdelcerro
204
    @Override
205
    public JComponent asJComponent() {
206
        return this;
207
    }
208 1675 jjdelcerro
209
    @Override
210
    public void setButtonEnabled(int button, boolean enabled) {
211
        this.getButton(button).setEnabled(enabled);
212
    }
213
214
    @Override
215
    public boolean isButtonEnabled(int button) {
216
        return this.getButton(button).isEnabled();
217
    }
218 1283 jjdelcerro
}