OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 /*
15
 * Created on 5 nov. 2004
16
 */
17
package org.openconcerto.sql.element;
18
 
73 ilm 19
import org.openconcerto.sql.TM;
132 ilm 20
import org.openconcerto.sql.element.SQLElementLink.LinkType;
57 ilm 21
import org.openconcerto.sql.request.RowItemDesc;
17 ilm 22
import org.openconcerto.sql.request.SQLRowItemView;
23
import org.openconcerto.ui.FormLayouter;
57 ilm 24
import org.openconcerto.ui.PopupMouseListener;
17 ilm 25
 
26
import java.awt.Component;
27
import java.awt.Container;
28
import java.awt.GridLayout;
57 ilm 29
import java.awt.event.ActionEvent;
80 ilm 30
import java.awt.event.MouseAdapter;
57 ilm 31
import java.awt.event.MouseEvent;
32
import java.util.HashMap;
33
import java.util.Map;
17 ilm 34
 
57 ilm 35
import javax.swing.AbstractAction;
36
import javax.swing.JComponent;
17 ilm 37
import javax.swing.JPanel;
57 ilm 38
import javax.swing.JPopupMenu;
17 ilm 39
import javax.swing.JTabbedPane;
40
import javax.swing.LayoutFocusTraversalPolicy;
57 ilm 41
import javax.swing.WindowConstants;
17 ilm 42
 
43
public abstract class UISQLComponent extends BaseSQLComponent {
44
 
45
    /** System property used to find the suffix to add to required items */
46
    public static final String REQUIRED_SUFFIX_PROP = "org.openconcerto.sql.requiredSuffix";
47
    // static since it should be the same in an application
48
    private static final String REQ_SUFFIX = System.getProperty(REQUIRED_SUFFIX_PROP, " *");
49
 
50
    private FormLayouter autoLayouter;
57 ilm 51
    private final Map<String, JComponent> labels;
17 ilm 52
    private JPanel currentPanel;
53
    private JTabbedPane tabbedPane;
54
    // default values for the layouter
55
    private final int width, def;
56
 
57
    public UISQLComponent(SQLElement element) {
58
        this(element, 2);
59
    }
60
 
61
    public UISQLComponent(SQLElement element, int width) {
62
        this(element, width, 0);
63
    }
64
 
65
    public UISQLComponent(SQLElement element, int width, int def) {
66
        super(element);
67
 
68
        this.width = width;
69
        this.def = def;
70
 
71
        // par defaut pas de tab
72
        this.tabbedPane = null;
73
        this.currentPanel = this;
74
        // ne pas instancier le layouter tout de suite
75
        // car on ne sait pas encore si tab ou pas
76
        this.autoLayouter = null;
57 ilm 77
        this.labels = new HashMap<String, JComponent>();
17 ilm 78
    }
79
 
57 ilm 80
    protected void addToUI(final SQLRowItemView obj, String where) {
81
        final RowItemDesc rivDesc = getRIVDesc(obj.getSQLName());
73 ilm 82
        final String desc = getLabel(obj.getSQLName(), rivDesc);
17 ilm 83
        if (where == null)
84
            where = this.getDefaultWhere(obj);
57 ilm 85
        final JComponent added;
17 ilm 86
        if (where == null)
57 ilm 87
            added = this.getLayouter().add(desc, obj.getComp());
17 ilm 88
        else if (where.equals("bordered")) {
57 ilm 89
            added = this.getLayouter().addBordered(desc, obj.getComp(), 0);
17 ilm 90
        } else if (where.equals("left")) {
91
            this.getLayouter().newLine();
57 ilm 92
            added = this.getLayouter().add(desc, obj.getComp(), this.getLayouter().getWidth() / 2);
17 ilm 93
        } else if (where.equals("right"))
57 ilm 94
            added = this.getLayouter().add(desc, obj.getComp(), (this.getLayouter().getWidth() + 1) / 2);
17 ilm 95
        else {
96
            final int aWidth = Integer.parseInt(where);
57 ilm 97
            added = this.getLayouter().add(desc, obj.getComp(), aWidth);
17 ilm 98
        }
57 ilm 99
        this.labels.put(obj.getSQLName(), added);
100
        updateUI(obj.getSQLName(), rivDesc);
101
        final JPopupMenu menu = new JPopupMenu();
73 ilm 102
        menu.add(new AbstractAction(TM.tr("sqlComp.modifyDoc")) {
57 ilm 103
            @Override
104
            public void actionPerformed(ActionEvent e) {
105
                final DocumentationEditorFrame frame = new DocumentationEditorFrame(UISQLComponent.this, obj.getSQLName());
106
                frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
107
                frame.setVisible(true);
108
            }
109
        });
110
        added.addMouseListener(new PopupMouseListener() {
111
            @Override
112
            protected JPopupMenu createPopup(MouseEvent e) {
113
                return e.isControlDown() ? menu : null;
114
            }
115
        });
80 ilm 116
        added.addMouseListener(new MouseAdapter() {
117
            @Override
118
            public void mouseClicked(MouseEvent e) {
119
                if (e.isAltDown())
120
                    toggleDisplayFieldsNames();
121
            }
122
        });
17 ilm 123
    }
124
 
57 ilm 125
    @Override
73 ilm 126
    protected String getLabel(final String itemName, final RowItemDesc desc) {
127
        final String res = super.getLabel(itemName, desc);
128
        return this.getRequiredNames().contains(itemName) ? res + REQ_SUFFIX : res;
129
    }
130
 
131
    @Override
57 ilm 132
    protected void updateUI(String itemName, RowItemDesc desc) {
133
        super.updateUI(itemName, desc);
134
        updateUI(itemName, this.labels.get(itemName), desc);
135
    }
136
 
17 ilm 137
    private String getDefaultWhere(SQLRowItemView obj) {
132 ilm 138
        if (obj.getFields().size() == 1 && getElement().getOwnedLink(obj.getField().getName(), LinkType.COMPOSITION) != null)
17 ilm 139
            return "bordered";
140
        return null;
141
    }
142
 
143
    protected void addUITitle(String title) {
144
        this.getLayouter().add(title, null);
145
    }
146
 
147
    protected final void addTab() {
148
        this.addTab("Général");
149
    }
150
 
151
    protected final void addTab(String tabTitle) {
152
        this.addTab(tabTitle, this.width, this.def);
153
    }
154
 
155
    /**
156
     * Add a new tab with the specified title.
157
     *
158
     * @param tabTitle the title
159
     * @param w the total width of the tab.
160
     * @param d the default width of a view.
161
     */
162
    protected final void addTab(String tabTitle, final int w, final int d) {
163
        if (this.tabbedPane == null) {
164
            if (this.getComponentCount() > 0)
165
                throw new IllegalStateException("you cannot create a tab after adding views");
166
            this.tabbedPane = new JTabbedPane();
167
            // reach all fields of this component
168
            this.tabbedPane.setFocusCycleRoot(true);
169
            this.tabbedPane.setFocusTraversalPolicy(new LayoutFocusTraversalPolicy() {
170
 
171
                private final JTabbedPane tab = UISQLComponent.this.tabbedPane;
172
 
173
                @Override
174
                public Component getComponentAfter(Container container, Component component) {
175
                    if (component == getLastComponent(this.tab)) {
176
                        this.tab.setSelectedIndex((this.tab.getSelectedIndex() + 1) % this.tab.getTabCount());
177
                        return getFirstComponent((Container) this.tab.getSelectedComponent());
178
                    } else
179
                        return super.getComponentAfter(container, component);
180
                }
181
 
182
                @Override
183
                public Component getComponentBefore(Container container, Component component) {
184
                    if (component == getFirstComponent(this.tab)) {
185
                        // + count to avoid negative number
186
                        final int index = (this.tab.getTabCount() + this.tab.getSelectedIndex() - 1) % this.tab.getTabCount();
187
                        this.tab.setSelectedIndex(index);
188
                        return getLastComponent((Container) this.tab.getSelectedComponent());
189
                    } else
190
                        return super.getComponentBefore(container, component);
191
                }
192
 
193
                @Override
194
                protected boolean accept(Component component) {
195
                    // do not accept tab otherwise tab is its own successor and the focus never
196
                    // change
197
                    if (component == this.tab)
198
                        return false;
199
                    else
200
                        return super.accept(component);
201
                }
202
            });
203
            this.setLayout(new GridLayout(1, 1));
204
            this.add(this.tabbedPane);
205
        }
151 ilm 206
        final JPanel newPanel = new JPanel();
25 ilm 207
        // from Guillaume : tabs shouldn't be opaque in Windows L&F
151 ilm 208
        newPanel.setOpaque(false);
209
        this.tabbedPane.addTab(tabTitle, newPanel);
210
        this.setLayouterOn(newPanel, w, d);
211
    }
212
 
213
    protected final void setLayouterOn(final JPanel cont) {
214
        this.setLayouterOn(cont, this.width, this.def);
215
    }
216
 
217
    protected final void setLayouterOn(final JPanel cont, int w, int d) {
218
        this.currentPanel = cont;
17 ilm 219
        this.setLayouter(w, d);
220
    }
221
 
222
    private void setLayouter(int w, int d) {
223
        this.autoLayouter = new FormLayouter(this.currentPanel, w, d);
19 ilm 224
        this.setAdditionalFieldsPanel(this.autoLayouter);
17 ilm 225
    }
226
 
227
    protected final FormLayouter getLayouter() {
228
        if (this.autoLayouter == null)
229
            this.setLayouter(this.width, this.def);
230
        return this.autoLayouter;
231
    }
232
}