~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to xml/schema/schemaui/src/org/netbeans/modules/xml/schema/ui/nodes/categorized/DetailsColumn.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
 
 
42
package org.netbeans.modules.xml.schema.ui.nodes.categorized;
 
43
 
 
44
import java.awt.BorderLayout;
 
45
import java.awt.Color;
 
46
import java.awt.Component;
 
47
import java.awt.Container;
 
48
import java.awt.GridBagConstraints;
 
49
import java.awt.GridBagLayout;
 
50
import java.awt.Insets;
 
51
import java.awt.event.ActionEvent;
 
52
import java.awt.event.ActionListener;
 
53
import java.awt.event.FocusEvent;
 
54
import java.beans.PropertyChangeEvent;
 
55
import java.beans.PropertyVetoException;
 
56
import java.io.IOException;
 
57
import java.util.Date;
 
58
import javax.swing.JButton;
 
59
import javax.swing.JComponent;
 
60
import javax.swing.JPanel;
 
61
import javax.swing.JScrollPane;
 
62
import javax.swing.JTextArea;
 
63
import javax.swing.border.EmptyBorder;
 
64
import org.netbeans.modules.xml.schema.ui.basic.SchemaColumn;
 
65
import org.netbeans.modules.xml.xam.ui.column.ColumnProvider;
 
66
import org.netbeans.modules.xml.xam.ui.customizer.AbstractCustomizer;
 
67
import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
 
68
import org.openide.ErrorManager;
 
69
import org.openide.nodes.AbstractNode;
 
70
import org.openide.nodes.Children;
 
71
import org.openide.nodes.Node;
 
72
import org.openide.util.HelpCtx;
 
73
import org.openide.util.Lookup;
 
74
import org.openide.util.LookupEvent;
 
75
import org.openide.util.LookupListener;
 
76
import org.openide.util.NbBundle;
 
77
import org.openide.util.lookup.Lookups;
 
78
 
 
79
/**
 
80
 * Inner class implementing details column.
 
81
 * Uses Border layout.
 
82
 * The customizer component provided by parent node is in the center.
 
83
 * If no customizer is provided dummy will be used.
 
84
 * The save and reset buttons will be in south.
 
85
 */
 
86
public class DetailsColumn extends SchemaColumn 
 
87
                implements ActionListener, LookupListener
 
88
{
 
89
        static final long serialVersionUID = 1L;
 
90
        
 
91
        /**
 
92
         * If readlony
 
93
         */
 
94
        private boolean readonly;
 
95
        
 
96
        /**
 
97
         * customizer as center panel
 
98
         */
 
99
        private Customizer customizer;
 
100
        
 
101
        /**
 
102
         * title
 
103
         */
 
104
// TAF: Remove title
 
105
//      private JLabel title;
 
106
        
 
107
        /**
 
108
         * bottom panel containing buttons
 
109
         */
 
110
        private JPanel bottomPanel;
 
111
        
 
112
        /**
 
113
         * apply button
 
114
         */
 
115
        private JButton applyButton;
 
116
        
 
117
        /**
 
118
         * reset button
 
119
         */
 
120
        private JButton resetButton;
 
121
        
 
122
        /**
 
123
         * scroll pane for main pael
 
124
         */
 
125
        private JScrollPane scrollPane;
 
126
 
 
127
 
 
128
        private Lookup.Result lookupResult;
 
129
 
 
130
        /**
 
131
         * Creates details column using the customizer
 
132
         */
 
133
        public DetailsColumn(Customizer cust)
 
134
        {
 
135
                super(null, null, false);
 
136
                customizer = cust==null || cust.getComponent()==null
 
137
                                ? new DummyDetails() : cust;
 
138
                initialize();
 
139
                // listen to customizer lookup event of type ColumnProvider
 
140
                lookupResult = getCustomizer().getLookup().lookup(
 
141
                                new Lookup.Template(ColumnProvider.class));
 
142
                if(lookupResult!=null) lookupResult.addLookupListener(this);
 
143
        }
 
144
        
 
145
        
 
146
        /**
 
147
         * Initializes the details column
 
148
         */
 
149
        private void initialize()
 
150
        {
 
151
                setLayout(new BorderLayout());
 
152
                setBackground(Color.white);
 
153
 
 
154
// TAF: Remove title
 
155
//              // add Title
 
156
//              title = new JLabel(getTitle(), SwingConstants.CENTER);
 
157
//              add(title,BorderLayout.NORTH);
 
158
                
 
159
                // add customizer
 
160
                getCustomizer().getComponent().setBackground(Color.white);
 
161
                add(getCustomizer().getComponent(),BorderLayout.CENTER);
 
162
                scrollPane = new JScrollPane(getCustomizer().getComponent());
 
163
                scrollPane.setBorder(null);
 
164
                scrollPane.setViewportBorder(null);
 
165
                add(scrollPane,BorderLayout.CENTER);
 
166
                
 
167
                // add bottom panel
 
168
                bottomPanel = new JPanel();
 
169
                applyButton = new JButton();
 
170
                applyButton.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_ApplyButton"));
 
171
                applyButton.setEnabled(false);
 
172
                applyButton.addActionListener(this);
 
173
                resetButton = new JButton();
 
174
                resetButton.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_ResetButton"));
 
175
                resetButton.setEnabled(false);
 
176
                resetButton.addActionListener(this);
 
177
                bottomPanel.setLayout(new GridBagLayout());
 
178
                GridBagConstraints gridBagConstraints = new GridBagConstraints();
 
179
                gridBagConstraints.insets = new Insets(10, 10, 10, 2);
 
180
                gridBagConstraints.anchor = GridBagConstraints.EAST;
 
181
                gridBagConstraints.weightx = 1.0;
 
182
                bottomPanel.add(applyButton,gridBagConstraints);
 
183
                gridBagConstraints.insets = new Insets(10, 2, 10, 10);
 
184
                gridBagConstraints.weightx = 0.0;
 
185
                bottomPanel.add(resetButton,gridBagConstraints);
 
186
                bottomPanel.setBackground(Color.white);
 
187
                add(bottomPanel,BorderLayout.SOUTH);
 
188
                
 
189
                // add this as propertychangelistener of customizer
 
190
                getCustomizer().addPropertyChangeListener(this);
 
191
        }
 
192
        
 
193
        
 
194
        /**
 
195
         *
 
196
         *
 
197
         */
 
198
        public String getTitle()
 
199
        {
 
200
                return NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn"); // NOI18N
 
201
        }
 
202
        
 
203
        // overridden as customizer component is main component
 
204
        public boolean requestFocusInWindow()
 
205
        {
 
206
                return getCustomizer().getComponent().requestFocusInWindow();
 
207
        }
 
208
        
 
209
        /**
 
210
         *
 
211
         *
 
212
         */
 
213
        public JComponent getComponent()
 
214
        {
 
215
                return this;
 
216
        }
 
217
        
 
218
        public boolean isShowing()
 
219
        {
 
220
                boolean retValue = super.isShowing();
 
221
                if (retValue)
 
222
                {
 
223
                        boolean enable = !isReadOnly();
 
224
                        if(enable != getCustomizer().getComponent().isEnabled())
 
225
                        {
 
226
                                DetailsColumn.setEnabledComponent(getCustomizer().getComponent(),enable);
 
227
                        }
 
228
                }
 
229
                return retValue;
 
230
        }
 
231
        // readonly 
 
232
        public boolean isReadOnly()
 
233
        {
 
234
                return readonly || !getCustomizer().isEditable();
 
235
        }
 
236
 
 
237
        public void setReadOnly(boolean readonly)
 
238
        {
 
239
                this.readonly = readonly;
 
240
        }
 
241
 
 
242
        private Customizer getCustomizer()
 
243
        {
 
244
                return customizer;
 
245
        }
 
246
 
 
247
        // property change events
 
248
        // consider special events first
 
249
        // then call super
 
250
        public void propertyChange(PropertyChangeEvent evt)
 
251
        {
 
252
                if(isReadOnly()) return;
 
253
                if (evt.getPropertyName().equals(Customizer.PROP_ACTION_APPLY))
 
254
                {
 
255
                        applyButton.setEnabled(((Boolean) evt.getNewValue()).booleanValue());
 
256
                }
 
257
                else if (evt.getPropertyName().equals(Customizer.PROP_ACTION_RESET))
 
258
                {
 
259
                        resetButton.setEnabled(((Boolean) evt.getNewValue()).booleanValue());
 
260
                }
 
261
                else
 
262
                        super.propertyChange(evt);
 
263
        }
 
264
        
 
265
        // action events
 
266
        public void actionPerformed(ActionEvent e)
 
267
        {
 
268
                if(isReadOnly()) return;
 
269
                if (e.getSource() == applyButton)
 
270
                {
 
271
                        try
 
272
                        {
 
273
                                getCustomizer().apply();
 
274
                        }
 
275
                        catch (IOException ioe)
 
276
                        {
 
277
                                String msg = NbBundle.getMessage(DetailsColumn.class, "MSG_DetailsColumn_ApplyFailed");
 
278
                                IllegalArgumentException iae = new IllegalArgumentException(msg);
 
279
                                ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
 
280
                                                msg, msg, ioe, new Date());
 
281
                                getCustomizer().reset();
 
282
                        }
 
283
 
 
284
                }
 
285
                else if (e.getSource() == resetButton)
 
286
                {
 
287
                        getCustomizer().reset();
 
288
                }
 
289
        }
 
290
 
 
291
        // overridden to not do anything on focus gained event
 
292
        public void focusGained(FocusEvent e)
 
293
        {
 
294
        }
 
295
 
 
296
        // lookup event
 
297
        public void resultChanged(LookupEvent ev)
 
298
        {
 
299
                if(isReadOnly()) return;
 
300
                Lookup.Result source = (Lookup.Result) ev.getSource();
 
301
                if(source.allInstances().isEmpty())
 
302
                {
 
303
                        getColumnView().removeColumnsAfter(this);
 
304
                } 
 
305
                else
 
306
                {
 
307
                        for (Object obj: source.allInstances())
 
308
                        {
 
309
                                if (obj instanceof ColumnProvider)
 
310
                                {
 
311
                                        ColumnProvider columnProvider = (ColumnProvider)obj;
 
312
                                        Node selNode = new AbstractNode(Children.LEAF,
 
313
                                                        Lookups.singleton(columnProvider));
 
314
                                        getExplorerManager().setRootContext(selNode);
 
315
                                        try
 
316
                                        {
 
317
                                                getExplorerManager().setSelectedNodes(new Node[]{selNode});
 
318
                                        } catch (PropertyVetoException ex)
 
319
                                        {
 
320
                                        }
 
321
                                        break;
 
322
                                }
 
323
                        }
 
324
                }
 
325
        }
 
326
 
 
327
        /**
 
328
         * Recursively enable/disable all components in the hierarchy under parent
 
329
         */
 
330
        private static void setEnabledComponent(Component component, boolean enabled)
 
331
        {
 
332
                component.setEnabled(enabled);
 
333
                if(component instanceof Container)
 
334
                {
 
335
                        Component[] children = ((Container)component).getComponents();
 
336
                        for (Component child:children)
 
337
                        {
 
338
                                setEnabledComponent(child, enabled);
 
339
                        }
 
340
                }
 
341
        }
 
342
 
 
343
        /**
 
344
         * Inner class implementing customizer.
 
345
         * Its just a dummy customizer.
 
346
         */
 
347
        static class DummyDetails extends AbstractCustomizer
 
348
        {
 
349
                
 
350
                static final long serialVersionUID = 1L;
 
351
                
 
352
                public DummyDetails()
 
353
                {
 
354
                        super();
 
355
                        initialize();
 
356
                }
 
357
                
 
358
                private void initialize()
 
359
                {
 
360
                        JTextArea textArea = new JTextArea();
 
361
                        textArea.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_NoCustomizer"));
 
362
                        textArea.setColumns(25);
 
363
                        textArea.setLineWrap(true);
 
364
                        textArea.setWrapStyleWord(true);
 
365
                        textArea.setEditable(false);
 
366
                        textArea.setBorder(new EmptyBorder(3,3,3,3));
 
367
                        add(textArea);
 
368
                }
 
369
                
 
370
                public void apply()
 
371
                {
 
372
                        // do nothing
 
373
                        setSaveEnabled(false);
 
374
                }
 
375
                
 
376
                public void reset()
 
377
                {
 
378
                        // do nothing
 
379
                        setResetEnabled(false);
 
380
                }
 
381
 
 
382
                public boolean isEditable()
 
383
                {
 
384
                        return false;
 
385
                }
 
386
 
 
387
                public HelpCtx getHelpCtx()
 
388
                {
 
389
                        return HelpCtx.DEFAULT_HELP;
 
390
                }
 
391
        }
 
392
}
 
 
b'\\ No newline at end of file'