Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / propertypage / BasePropertiesPageDialog.java @ 43801

History | View | Annotate | Download (5.51 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

    
10
import javax.swing.JComponent;
11
import javax.swing.JTabbedPane;
12

    
13
import org.gvsig.fmap.mapcontrol.MapControlLocator;
14
import org.gvsig.propertypage.PropertiesPage.SetPageEnabledEvent;
15
import org.gvsig.tools.ToolsLocator;
16
import org.gvsig.tools.dispose.Disposable;
17
import org.gvsig.tools.dispose.DisposeUtils;
18
import org.gvsig.tools.i18n.I18nManager;
19
import org.gvsig.tools.swing.api.ActionListenerSupport;
20
import org.gvsig.tools.util.Invocable;
21

    
22

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

    
28
    private Object obj;
29
    private String groupID;
30
    private List<PropertiesPage> pages = null;
31
    private int userAction = ACTION_ACCEPT;
32
    private JTabbedPane tabbedPane = null;
33

    
34
    public BasePropertiesPageDialog() {
35
    }
36

    
37
    public BasePropertiesPageDialog(Object obj, String groupID) {
38
        init(groupID, obj);
39
    }
40

    
41
    protected void init(String groupID, Object obj) {
42
        this.obj = obj;
43
        this.groupID = groupID;
44
        PropertiesPageManager manager = MapControlLocator.getPropertiesPageManager();
45
        this.pages = manager.getPages(this.groupID,this.obj);
46
        initComponents();
47
    }
48

    
49
    protected void initComponents() {
50
        I18nManager i18nManager = ToolsLocator.getI18nManager();
51

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

    
59
        this.titleLabel.setText("");
60

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

    
83
    }
84

    
85
    protected Component createPropertiesPagesPanel() {
86
        if( this.pages.size()==1 && !useTabsAlwais() ) {
87
            PropertiesPage page = this.pages.get(0);
88
            return page.asJComponent();
89
        } else {
90
            JTabbedPane tabbedPane = new JTabbedPane();
91
            for( int i=0; i<this.pages.size(); i++ ) {
92
                PropertiesPage page = this.pages.get(i);
93
                if( page instanceof ActionListenerSupport ) {
94
                    ((ActionListenerSupport)page).addActionListener(new ActionListener() {
95
                        @Override
96
                        public void actionPerformed(ActionEvent e) {
97
                            if( e instanceof SetPageEnabledEvent ) {
98
                                SetPageEnabledEvent ee = (SetPageEnabledEvent) e;
99
                                setEnabledAt(ee.getFilter(), ee.isEnabled());
100
                            }
101
                        }
102
                    });
103
                }
104
                tabbedPane.addTab(page.getTitle(), page.asJComponent());
105
            }
106
            return tabbedPane;
107
        }
108
    }
109

    
110
    protected boolean useTabsAlwais() {
111
        return false;
112
    }
113

    
114
    public void whenAccept() {
115
        for( int i=0; i<this.pages.size(); i++ ) {
116
            PropertiesPage page = this.pages.get(i);
117
            if( ! page.whenAccept() ) {
118
                return;
119
            }
120
        }
121
        this.userAction = ACTION_ACCEPT;
122
        this.closeDialog();
123
    }
124

    
125
    public void whenApply() {
126
        for( int i=0; i<this.pages.size(); i++ ) {
127
            PropertiesPage page = this.pages.get(i);
128
            if( ! page.whenApply() ) {
129
                return;
130
            }
131
        }
132
        this.userAction = ACTION_APPLY;
133
    }
134

    
135
    public void whenCancel() {
136
        for( int i=0; i<this.pages.size(); i++ ) {
137
            PropertiesPage page = this.pages.get(i);
138
            if( ! page.whenCancel() ) {
139
                return;
140
            }
141
        }
142
        this.userAction = ACTION_CANCEL;
143
        this.closeDialog();
144
    }
145

    
146
    public int getUserAction() {
147
        return this.userAction;
148
    }
149

    
150
    protected void closeDialog() {
151
        this.setVisible(false);
152
        for (PropertiesPage propertiesPage : pages) {
153
            if(propertiesPage instanceof Disposable) {
154
                DisposeUtils.disposeQuietly((Disposable) propertiesPage);
155
            }
156
        }
157
    }
158

    
159
    @Override
160
    public JComponent asJComponent() {
161
        return this;
162
    }
163

    
164
    public void setEnabledAt(Invocable filter, boolean enabled) {
165
        if( this.tabbedPane==null ) {
166
            return;
167
        }
168
        for( int i=0; i<this.pages.size(); i++ ) {
169
            PropertiesPage page = this.pages.get(i);
170
            if( (Boolean)(filter.call(page)) ) {
171
                this.tabbedPane.setEnabledAt(i, enabled);
172
            }
173
        }
174
    }
175

    
176
}