Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.util / org.gvsig.tools.util.api / src / main / java / org / gvsig / propertypage / BasePropertiesPageDialog.java @ 1746

History | View | Annotate | Download (7.88 KB)

1

    
2
package org.gvsig.propertypage;
3

    
4
import java.awt.BorderLayout;
5
import java.awt.Component;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.util.List;
9
import javax.swing.JComponent;
10
import javax.swing.JTabbedPane;
11
import org.gvsig.propertypage.PropertiesPage.SetPageEnabledEvent;
12
import org.gvsig.tools.ToolsLocator;
13
import org.gvsig.tools.i18n.I18nManager;
14
import org.gvsig.tools.swing.api.ActionListenerSupport;
15
import org.gvsig.tools.swing.api.windowmanager.WindowManager_v2;
16
import org.gvsig.tools.util.Invocable;
17
import org.gvsig.tools.util.ToolsUtilLocator;
18

    
19

    
20
public class BasePropertiesPageDialog extends BasePropertiesPagePanelLayout implements org.gvsig.tools.swing.api.Component {
21
    public static final int ACTION_CANCEL = 0;
22
    public static final int ACTION_ACCEPT = 1;
23
    public static final int ACTION_APPLY = 2;
24

    
25
    private Object obj;
26
    private String groupID;
27
    private List<PropertiesPage> pages = null;
28
    private int userAction = ACTION_ACCEPT;
29
    private JTabbedPane tabbedPane = null;
30
    
31
    public BasePropertiesPageDialog() {
32
    }
33

    
34
    public BasePropertiesPageDialog(Object obj, String groupID) {
35
        init(groupID, obj);
36
    }
37

    
38
    protected void init(String groupID, Object obj) {
39
        this.obj = obj;
40
        this.groupID = groupID;
41
        PropertiesPageManager manager = ToolsUtilLocator.getPropertiesPageManager();
42
        this.pages = manager.getPages(this.groupID, this, this.obj);        
43
        initComponents();
44
    }
45
    
46
    protected void initComponents() {
47
        I18nManager i18nManager = ToolsLocator.getI18nManager(); 
48

    
49
        this.content.setLayout(new BorderLayout());
50
        Component contents = createPropertiesPagesPanel();
51
        if( contents instanceof JTabbedPane ) {
52
            this.tabbedPane = (JTabbedPane) contents;
53
        }
54
        this.content.add(contents, BorderLayout.CENTER );
55

    
56
        this.titleLabel.setText("");
57
        
58
        this.acceptButton.setText(i18nManager.getTranslation("accept"));
59
        this.acceptButton.addActionListener( new ActionListener() {
60
            @Override
61
            public void actionPerformed(ActionEvent ae) {
62
                whenAccept();
63
            }
64
        });
65
        this.applyButton.setText(i18nManager.getTranslation("apply"));
66
        this.applyButton.addActionListener( new ActionListener() {
67
            @Override
68
            public void actionPerformed(ActionEvent ae) {
69
                whenApply();
70
            }
71
        });
72
        this.cancelButton.setText(i18nManager.getTranslation("cancel"));
73
        this.cancelButton.addActionListener( new ActionListener() {
74
            @Override
75
            public void actionPerformed(ActionEvent ae) {
76
                whenCancel();
77
            }
78
        });
79

    
80
    }
81

    
82
    protected List<PropertiesPage> getPages() {
83
        return pages;
84
    }
85
    
86
    protected PropertiesPage getPage(Class<? extends PropertiesPage> theClass) {
87
        for (PropertiesPage page : pages) {
88
            if( theClass.isInstance(page) ) {
89
                return page;
90
            }
91
        }
92
        return null;
93
    }
94
    
95
    protected boolean isButtonVisible(int button) {
96
        switch(button) {
97
            case WindowManager_v2.BUTTON_APPLY:
98
                return this.applyButton.isVisible();
99
            case WindowManager_v2.BUTTON_OK:
100
                return acceptButton.isVisible();
101
            case WindowManager_v2.BUTTON_CANCEL:
102
                return cancelButton.isVisible();
103
        }
104
        return false;
105
    }
106
    
107
    protected void setButtonVisible(int button, boolean visible) {
108
        switch(button) {
109
            case WindowManager_v2.BUTTON_APPLY:
110
                this.applyButton.setVisible(visible);
111
                break;
112
            case WindowManager_v2.BUTTON_OK:
113
                this.acceptButton.setVisible(visible);
114
                break;
115
            case WindowManager_v2.BUTTON_CANCEL:
116
                this.cancelButton.setVisible(visible);
117
                break;
118
        }
119
    }
120
    
121
    protected void setButtonLabel(int button, String label) {
122
        switch(button) {
123
            case WindowManager_v2.BUTTON_APPLY:
124
                this.applyButton.setText(label);
125
                break;
126
            case WindowManager_v2.BUTTON_OK:
127
                this.acceptButton.setText(label);
128
                break;
129
            case WindowManager_v2.BUTTON_CANCEL:
130
                this.cancelButton.setText(label);
131
                break;
132
        }
133
    }
134

    
135
    protected boolean isButtonEnabled(int button) {
136
        switch(button) {
137
            case WindowManager_v2.BUTTON_APPLY:
138
                return this.applyButton.isEnabled();
139
            case WindowManager_v2.BUTTON_OK:
140
                return acceptButton.isEnabled();
141
            case WindowManager_v2.BUTTON_CANCEL:
142
                return cancelButton.isEnabled();
143
        }
144
        return false;
145
    }
146
    
147
    protected void setButtonEnabled(int button, boolean visible) {
148
        switch(button) {
149
            case WindowManager_v2.BUTTON_APPLY:
150
                this.applyButton.setEnabled(visible);
151
                break;
152
            case WindowManager_v2.BUTTON_OK:
153
                this.acceptButton.setEnabled(visible);
154
                break;
155
            case WindowManager_v2.BUTTON_CANCEL:
156
                this.cancelButton.setEnabled(visible);
157
                break;
158
        }
159
    }
160

    
161
    protected Component createPropertiesPagesPanel() {
162
        if( this.pages.size()==1 && !useTabsAlwais() ) {
163
            PropertiesPage page = this.pages.get(0);
164
            return page.asJComponent();
165
        } else {
166
            JTabbedPane theTabbedPane = new JTabbedPane();
167
            for( int i=0; i<this.pages.size(); i++ ) {
168
                PropertiesPage page = this.pages.get(i);
169
                if( page instanceof ActionListenerSupport ) {
170
                    ((ActionListenerSupport)page).addActionListener(new ActionListener() {
171
                        @Override
172
                        public void actionPerformed(ActionEvent e) {
173
                            if( e instanceof SetPageEnabledEvent ) {
174
                                SetPageEnabledEvent ee = (SetPageEnabledEvent) e;
175
                                setEnabledAt(ee.getFilter(), ee.isEnabled());
176
                            }
177
                        }
178
                    });
179
                }
180
                theTabbedPane.addTab(page.getTitle(), page.asJComponent());
181
            }
182
            return theTabbedPane;
183
        }
184
    }
185
    
186
    protected boolean useTabsAlwais() {
187
        return false;
188
    }
189
    
190
    public void whenAccept() {
191
        for( int i=0; i<this.pages.size(); i++ ) {
192
            PropertiesPage page = this.pages.get(i);
193
            if( ! page.whenAccept() ) {
194
                return;
195
            }
196
        }
197
        this.userAction = ACTION_ACCEPT;
198
        this.closeDialog();
199
    }
200
    
201
    public void whenApply() {
202
        for( int i=0; i<this.pages.size(); i++ ) {
203
            PropertiesPage page = this.pages.get(i);
204
            if( ! page.whenApply() ) {
205
                return;
206
            }
207
        }
208
        this.userAction = ACTION_APPLY;
209
    }
210
    
211
    public void whenCancel() {
212
        for( int i=0; i<this.pages.size(); i++ ) {
213
            PropertiesPage page = this.pages.get(i);
214
            if( ! page.whenCancel() ) {
215
                return;
216
            }
217
        }
218
        this.userAction = ACTION_CANCEL;
219
        this.closeDialog();
220
    }
221
    
222
    public int getUserAction() {
223
        return this.userAction;
224
    }
225
    
226
    protected void closeDialog() {
227
        this.setVisible(false);
228
    }
229

    
230
    @Override
231
    public JComponent asJComponent() {
232
        return this;
233
    }
234
    
235
    public void setEnabledAt(Invocable filter, boolean enabled) {
236
        if( this.tabbedPane==null ) {
237
            return;
238
        }
239
        for( int i=0; i<this.pages.size(); i++ ) {
240
            PropertiesPage page = this.pages.get(i);
241
            if( (Boolean)(filter.call(page)) ) {
242
                this.tabbedPane.setEnabledAt(i, enabled);
243
            }
244
        }
245
    }
246
    
247
}