KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > schema > ui > nodes > categorized > DetailsColumn


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.schema.ui.nodes.categorized;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Color JavaDoc;
24 import java.awt.Component JavaDoc;
25 import java.awt.Container JavaDoc;
26 import java.awt.GridBagConstraints JavaDoc;
27 import java.awt.GridBagLayout JavaDoc;
28 import java.awt.Insets JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.awt.event.FocusEvent JavaDoc;
32 import java.beans.PropertyChangeEvent JavaDoc;
33 import java.beans.PropertyVetoException JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.Date JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.JTextArea JavaDoc;
41 import javax.swing.border.EmptyBorder JavaDoc;
42 import org.netbeans.modules.xml.schema.ui.basic.SchemaColumn;
43 import org.netbeans.modules.xml.xam.ui.column.ColumnProvider;
44 import org.netbeans.modules.xml.xam.ui.customizer.AbstractCustomizer;
45 import org.netbeans.modules.xml.xam.ui.customizer.Customizer;
46 import org.openide.ErrorManager;
47 import org.openide.nodes.AbstractNode;
48 import org.openide.nodes.Children;
49 import org.openide.nodes.Node;
50 import org.openide.util.HelpCtx;
51 import org.openide.util.Lookup;
52 import org.openide.util.LookupEvent;
53 import org.openide.util.LookupListener;
54 import org.openide.util.NbBundle;
55 import org.openide.util.lookup.Lookups;
56
57 /**
58  * Inner class implementing details column.
59  * Uses Border layout.
60  * The customizer component provided by parent node is in the center.
61  * If no customizer is provided dummy will be used.
62  * The save and reset buttons will be in south.
63  */

64 public class DetailsColumn extends SchemaColumn
65         implements ActionListener JavaDoc, LookupListener
66 {
67     static final long serialVersionUID = 1L;
68     
69     /**
70      * If readlony
71      */

72     private boolean readonly;
73     
74     /**
75      * customizer as center panel
76      */

77     private Customizer customizer;
78     
79     /**
80      * title
81      */

82 // TAF: Remove title
83
// private JLabel title;
84

85     /**
86      * bottom panel containing buttons
87      */

88     private JPanel JavaDoc bottomPanel;
89     
90     /**
91      * apply button
92      */

93     private JButton JavaDoc applyButton;
94     
95     /**
96      * reset button
97      */

98     private JButton JavaDoc resetButton;
99     
100     /**
101      * scroll pane for main pael
102      */

103     private JScrollPane JavaDoc scrollPane;
104
105
106     private Lookup.Result lookupResult;
107
108     /**
109      * Creates details column using the customizer
110      */

111     public DetailsColumn(Customizer cust)
112     {
113         super(null, null, false);
114         customizer = cust==null || cust.getComponent()==null
115                 ? new DummyDetails() : cust;
116         initialize();
117         // listen to customizer lookup event of type ColumnProvider
118
lookupResult = getCustomizer().getLookup().lookup(
119                 new Lookup.Template(ColumnProvider.class));
120         if(lookupResult!=null) lookupResult.addLookupListener(this);
121     }
122     
123     
124     /**
125      * Initializes the details column
126      */

127     private void initialize()
128     {
129         setLayout(new BorderLayout JavaDoc());
130         setBackground(Color.white);
131
132 // TAF: Remove title
133
// // add Title
134
// title = new JLabel(getTitle(), SwingConstants.CENTER);
135
// add(title,BorderLayout.NORTH);
136

137         // add customizer
138
getCustomizer().getComponent().setBackground(Color.white);
139         add(getCustomizer().getComponent(),BorderLayout.CENTER);
140         scrollPane = new JScrollPane JavaDoc(getCustomizer().getComponent());
141         scrollPane.setBorder(null);
142         scrollPane.setViewportBorder(null);
143         add(scrollPane,BorderLayout.CENTER);
144         
145         // add bottom panel
146
bottomPanel = new JPanel JavaDoc();
147         applyButton = new JButton JavaDoc();
148         applyButton.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_ApplyButton"));
149         applyButton.setEnabled(false);
150         applyButton.addActionListener(this);
151         resetButton = new JButton JavaDoc();
152         resetButton.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_ResetButton"));
153         resetButton.setEnabled(false);
154         resetButton.addActionListener(this);
155         bottomPanel.setLayout(new GridBagLayout JavaDoc());
156         GridBagConstraints JavaDoc gridBagConstraints = new GridBagConstraints JavaDoc();
157         gridBagConstraints.insets = new Insets JavaDoc(10, 10, 10, 2);
158         gridBagConstraints.anchor = GridBagConstraints.EAST;
159         gridBagConstraints.weightx = 1.0;
160         bottomPanel.add(applyButton,gridBagConstraints);
161         gridBagConstraints.insets = new Insets JavaDoc(10, 2, 10, 10);
162         gridBagConstraints.weightx = 0.0;
163         bottomPanel.add(resetButton,gridBagConstraints);
164         bottomPanel.setBackground(Color.white);
165         add(bottomPanel,BorderLayout.SOUTH);
166         
167         // add this as propertychangelistener of customizer
168
getCustomizer().addPropertyChangeListener(this);
169     }
170     
171     
172     /**
173      *
174      *
175      */

176     public String JavaDoc getTitle()
177     {
178         return NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn"); // NOI18N
179
}
180     
181     // overridden as customizer component is main component
182
public boolean requestFocusInWindow()
183     {
184         return getCustomizer().getComponent().requestFocusInWindow();
185     }
186     
187     /**
188      *
189      *
190      */

191     public JComponent JavaDoc getComponent()
192     {
193         return this;
194     }
195     
196     public boolean isShowing()
197     {
198         boolean retValue = super.isShowing();
199         if (retValue)
200         {
201             boolean enable = !isReadOnly();
202             if(enable != getCustomizer().getComponent().isEnabled())
203             {
204                 DetailsColumn.setEnabledComponent(getCustomizer().getComponent(),enable);
205             }
206         }
207         return retValue;
208     }
209     // readonly
210
public boolean isReadOnly()
211     {
212         return readonly || !getCustomizer().isEditable();
213     }
214
215     public void setReadOnly(boolean readonly)
216     {
217         this.readonly = readonly;
218     }
219
220     private Customizer getCustomizer()
221     {
222         return customizer;
223     }
224
225     // property change events
226
// consider special events first
227
// then call super
228
public void propertyChange(PropertyChangeEvent JavaDoc evt)
229     {
230         if(isReadOnly()) return;
231         if (evt.getPropertyName().equals(Customizer.PROP_ACTION_APPLY))
232         {
233             applyButton.setEnabled(((Boolean JavaDoc) evt.getNewValue()).booleanValue());
234         }
235         else if (evt.getPropertyName().equals(Customizer.PROP_ACTION_RESET))
236         {
237             resetButton.setEnabled(((Boolean JavaDoc) evt.getNewValue()).booleanValue());
238         }
239         else
240             super.propertyChange(evt);
241     }
242     
243     // action events
244
public void actionPerformed(ActionEvent JavaDoc e)
245     {
246         if(isReadOnly()) return;
247         if (e.getSource() == applyButton)
248         {
249             try
250             {
251                 getCustomizer().apply();
252             }
253             catch (IOException JavaDoc ioe)
254             {
255                 String JavaDoc msg = NbBundle.getMessage(DetailsColumn.class, "MSG_DetailsColumn_ApplyFailed");
256                 IllegalArgumentException JavaDoc iae = new IllegalArgumentException JavaDoc(msg);
257                 ErrorManager.getDefault().annotate(iae, ErrorManager.USER,
258                         msg, msg, ioe, new Date JavaDoc());
259                 getCustomizer().reset();
260             }
261
262         }
263         else if (e.getSource() == resetButton)
264         {
265             getCustomizer().reset();
266         }
267     }
268
269     // overridden to not do anything on focus gained event
270
public void focusGained(FocusEvent JavaDoc e)
271     {
272     }
273
274     // lookup event
275
public void resultChanged(LookupEvent ev)
276     {
277         if(isReadOnly()) return;
278         Lookup.Result source = (Lookup.Result) ev.getSource();
279         if(source.allInstances().isEmpty())
280         {
281             getColumnView().removeColumnsAfter(this);
282         }
283         else
284         {
285             for (Object JavaDoc obj: source.allInstances())
286             {
287                 if (obj instanceof ColumnProvider)
288                 {
289                     ColumnProvider columnProvider = (ColumnProvider)obj;
290                     Node selNode = new AbstractNode(Children.LEAF,
291                             Lookups.singleton(columnProvider));
292                     getExplorerManager().setRootContext(selNode);
293                     try
294                     {
295                         getExplorerManager().setSelectedNodes(new Node[]{selNode});
296                     } catch (PropertyVetoException JavaDoc ex)
297                     {
298                     }
299                     break;
300                 }
301             }
302         }
303     }
304
305     /**
306      * Recursively enable/disable all components in the hierarchy under parent
307      */

308     private static void setEnabledComponent(Component JavaDoc component, boolean enabled)
309     {
310         component.setEnabled(enabled);
311         if(component instanceof Container JavaDoc)
312         {
313             Component JavaDoc[] children = ((Container JavaDoc)component).getComponents();
314             for (Component JavaDoc child:children)
315             {
316                 setEnabledComponent(child, enabled);
317             }
318         }
319     }
320
321     /**
322      * Inner class implementing customizer.
323      * Its just a dummy customizer.
324      */

325     static class DummyDetails extends AbstractCustomizer
326     {
327         
328         static final long serialVersionUID = 1L;
329         
330         public DummyDetails()
331         {
332             super();
333             initialize();
334         }
335         
336         private void initialize()
337         {
338             JTextArea JavaDoc textArea = new JTextArea JavaDoc();
339             textArea.setText(NbBundle.getMessage(DetailsColumn.class, "LBL_DetailsColumn_NoCustomizer"));
340             textArea.setColumns(25);
341             textArea.setLineWrap(true);
342             textArea.setWrapStyleWord(true);
343             textArea.setEditable(false);
344             textArea.setBorder(new EmptyBorder JavaDoc(3,3,3,3));
345             add(textArea);
346         }
347         
348         public void apply()
349         {
350             // do nothing
351
setSaveEnabled(false);
352         }
353         
354         public void reset()
355         {
356             // do nothing
357
setResetEnabled(false);
358         }
359
360         public boolean isEditable()
361         {
362             return false;
363         }
364
365         public HelpCtx getHelpCtx()
366         {
367             return HelpCtx.DEFAULT_HELP;
368         }
369     }
370 }
Popular Tags