View Javadoc

1   /*
2      Copyright 2002-2006 Martin van den Bemt
3   
4      Licensed under the Apache License, Version 2.0 (the "License");
5      you may not use this file except in compliance with the License.
6      You may obtain a copy of the License at
7   
8          http://www.apache.org/licenses/LICENSE-2.0
9   
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15  */
16  package org.xulux.guilayer.swing.widgets;
17  
18  import java.awt.Component;
19  import java.awt.Container;
20  import java.awt.LayoutManager;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.swing.JComponent;
25  import javax.swing.JPanel;
26  import javax.swing.JTabbedPane;
27  
28  import org.xulux.api.gui.IShowChildWidgets;
29  import org.xulux.api.gui.IWidget;
30  import org.xulux.api.gui.IXuluxLayout;
31  import org.xulux.api.gui.IXuluxListener;
32  import org.xulux.core.XuluxContext;
33  import org.xulux.gui.ContainerWidget;
34  import org.xulux.gui.utils.ColorUtils;
35  
36  /**
37   * A panel widget
38   *
39   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
40   * @version $Id: Tab.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
41   */
42  public class Tab extends ContainerWidget {
43  
44      /**
45       * the native panel
46       */
47      private JPanel panel;
48  
49      /**
50       * Constructor for Panel.
51       * @param name the panel name
52       */
53      public Tab(String name) {
54          super(name);
55      }
56  
57      /**
58       * @see org.xulux.gui.XuluxWidget#destroy()
59       */
60      public void destroy() {
61          super.destroy();
62          if (panel == null) {
63              return;
64          }
65          Container container = panel.getParent();
66          panel.setVisible(false);
67          panel.removeAll();
68          if (container != null) {
69              container.remove(panel);
70          }
71          panel = null;
72      }
73  
74      /**
75       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
76       */
77      public Object getNativeWidget() {
78          initialize();
79          return panel;
80      }
81  
82      /**
83       * @todo Make layouts flexible.
84       * @see org.xulux.nyx.gui.XuluxWidget#initialize()
85       */
86      public void initialize() {
87          if (initialized) {
88              return;
89          }
90          // we default to XYLayout for now..
91          initialized = true;
92          IXuluxLayout layout = XuluxContext.getGuiDefaults().getLayout(null, getProperty("layout"));
93          if (layout == null) {
94              layout = XuluxContext.getGuiDefaults().getDefaultLayout();
95          }
96          layout.setParent(this);
97          panel = new JPanel((LayoutManager) layout);
98          initializeChildren();
99          refresh();
100         processInit();
101     }
102 
103     /**
104      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
105      */
106     public void refresh() {
107         initialize();
108         isRefreshing = true;
109         String backgroundColor = getProperty("default-background-color");
110 
111         if (backgroundColor != null) {
112             panel.setBackground(ColorUtils.getSwingColor(backgroundColor));
113         }
114         String tabId = getProperty(TabPanel.TABID);
115         if (tabId == null) {
116             panel.setEnabled(isEnabled());
117         } else {
118             int tabInt = Integer.parseInt(tabId);
119             if (tabInt == ((JTabbedPane) getParent().getNativeWidget()).getSelectedIndex()) {
120                 ((JTabbedPane) getParent().getNativeWidget()).setEnabledAt(Integer.parseInt(tabId), isEnabled());
121             }
122         }
123         isRefreshing = false;
124     }
125 
126     /**
127      * @see org.xulux.nyx.gui.XuluxWidget#canContainChildren()
128      */
129     public boolean canContainChildren() {
130         return true;
131     }
132 
133     /**
134      * @see org.xulux.nyx.gui.XuluxWidget#getChildWidgets()
135      */
136     public List getChildWidgets() {
137         return widgets;
138     }
139 
140     /**
141      * @see org.xulux.nyx.gui.ContainerWidget#addToParent(XuluxWidget)
142      */
143     public void addToParent(IWidget widget) {
144         if (widget instanceof IShowChildWidgets) {
145             List children = widget.getChildWidgets();
146             if (children != null && children.size() > 0) {
147                 Iterator it = children.iterator();
148                 while (it.hasNext()) {
149                     IWidget w = (IWidget) it.next();
150                     panel.add((JComponent) w.getNativeWidget(), w);
151                     w.refresh();
152                 }
153             }
154         } else {
155             panel.add((Component) widget.getNativeWidget(), widget);
156             widget.refresh();
157         }
158     }
159 
160     /**
161      * @see org.xulux.nyx.gui.XuluxWidget#focus()
162      */
163     public void focus() {
164       isRefreshing = true;
165       panel.requestFocus();
166       isRefreshing = false;
167       // if widget is not showing we have
168       // to make it showing..
169       // if we have a tab, we need to call the parent too..
170       if (!panel.isShowing() && getParent() != null || 
171           getProperty(TabPanel.TABID) != null) {
172           // set the session variable, so controls
173           // can look who requested focus..
174           getPart().getSession().setValue("nyx.focusrequest", this);
175           getParent().focus();
176           // remove session variable again.
177           getPart().getSession().remove("nyx.focusrequest");
178           panel.requestFocus();
179       }
180     }
181     /**
182      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
183      */
184     public Object getGuiValue() {
185         return null;
186     }
187 
188     /**
189      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
190      */
191     public boolean canContainValue() {
192         return false;
193     }
194 
195     /**
196      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
197      */
198     public boolean isValueEmpty() {
199         return true;
200     }
201 
202     /**
203      * @see org.xulux.nyx.gui.XuluxWidget#addXuluxListener(org.xulux.nyx.gui.XuluxListener)
204      */
205     public void addXuluxListener(IXuluxListener listener) {
206     }
207 
208 }