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.Color;
19  import java.awt.Component;
20  import java.awt.Container;
21  import java.awt.LayoutManager;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import javax.swing.JComponent;
26  import javax.swing.JPanel;
27  import javax.swing.JTabbedPane;
28  import javax.swing.border.BevelBorder;
29  import javax.swing.border.EtchedBorder;
30  import javax.swing.border.LineBorder;
31  
32  import org.xulux.api.gui.IShowChildWidgets;
33  import org.xulux.api.gui.IWidget;
34  import org.xulux.api.gui.IXuluxLayout;
35  import org.xulux.api.gui.IXuluxListener;
36  import org.xulux.core.XuluxContext;
37  import org.xulux.gui.ContainerWidget;
38  import org.xulux.gui.XuluxWidget;
39  import org.xulux.gui.utils.ColorUtils;
40  import org.xulux.guilayer.swing.extensions.IconTitledBorder;
41  import org.xulux.guilayer.swing.extensions.NyxTitledBorder;
42  import org.xulux.guilayer.swing.util.SwingUtils;
43  
44  /**
45   * A panel widget
46   *
47   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
48   * @version $Id: Panel.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
49   */
50  public class Panel extends ContainerWidget {
51  
52      /**
53       * the native panel
54       */
55      private JPanel panel;
56  
57      /**
58       * Constructor for Panel.
59       * @param name the panel name
60       */
61      public Panel(String name) {
62          super(name);
63      }
64  
65      /**
66       * @see org.xulux.gui.XuluxWidget#destroy()
67       */
68      public void destroy() {
69          super.destroy();
70          if (panel == null) {
71              return;
72          }
73          Container container = panel.getParent();
74          panel.setVisible(false);
75          panel.removeAll();
76          if (container != null) {
77              container.remove(panel);
78          }
79          panel = null;
80      }
81  
82      /**
83       * @see org.xulux.nyx.gui.XuluxWidget#getNativeWidget()
84       */
85      public Object getNativeWidget() {
86          initialize();
87          return panel;
88      }
89  
90      /**
91       * @todo Make layouts flexible.
92       * @see org.xulux.nyx.gui.XuluxWidget#initialize()
93       */
94      public void initialize() {
95          if (initialized) {
96              return;
97          }
98          // we default to XYLayout for now..
99          initializing = true;
100         initialized = true;
101         IXuluxLayout layout = XuluxContext.getGuiDefaults().getLayout(null, getProperty("layout"));
102         if (layout == null) {
103             layout = XuluxContext.getGuiDefaults().getDefaultLayout();
104         }
105         layout.setParent(this);
106         panel = new JPanel((LayoutManager) layout);
107         initializeChildren();
108         initializing = false;
109         refresh();
110         processInit();
111     }
112 
113     /**
114      * @see org.xulux.nyx.gui.XuluxWidget#refresh()
115      */
116     public void refresh() {
117         initialize();
118         panel.setVisible(isVisible());
119         String backgroundColor = getProperty("default-background-color");
120 
121         if (backgroundColor != null) {
122             panel.setBackground(ColorUtils.getSwingColor(backgroundColor));
123         }
124         /*
125          * Fix for border issues. They tend to be too big or
126          * too small..
127          */
128         String border = getProperty("border");
129         if (border != null) {
130             Color color = panel.getForeground();
131             String borderColor = getProperty("border-color");
132             if (borderColor != null) {
133                 color = ColorUtils.getSwingColor(borderColor);
134             }
135             if (border.equalsIgnoreCase("bevel")) {
136                 String borderType = getProperty("border-type");
137                 // defaults to lowered.
138                 int bType = BevelBorder.LOWERED;
139                 if (borderType != null && borderType.equalsIgnoreCase("raised")) {
140                     bType = BevelBorder.RAISED;
141                 }
142                 panel.setBorder(new BevelBorder(bType));
143             } else if (border.equalsIgnoreCase("titled")) {
144                 String borderTitle = getProperty("border-title");
145                 if (borderTitle == null) {
146                     borderTitle = "";
147                 }
148 
149                 Color titleColor = null;
150                 if (getProperty("border-title-color") != null) {
151                     titleColor = ColorUtils.getSwingColor(getProperty("border-title-color"));
152                 }
153                 int borderSize = 1;
154                 if (getProperty("border-size") != null) {
155                     try {
156                         borderSize = Integer.parseInt(getProperty("border-size"));
157                     } catch (NumberFormatException nfe) {
158                         nfe.printStackTrace();
159                     }
160                 }
161                 //TitledBorder titledBorder = BorderFactory.createTitledBorder(new LineBorder(color,borderSize),borderTitle);
162                 NyxTitledBorder titledBorder = new NyxTitledBorder(borderTitle);
163                 if (titleColor != null) {
164                     titledBorder.setTitleColor(titleColor);
165                 }
166                 panel.setBorder(titledBorder);
167             } else if (border.equalsIgnoreCase("line")) {
168                 int borderSize = 1;
169                 if (getProperty("border-size") != null) {
170                     try {
171                         borderSize = Integer.parseInt(getProperty("border-size"));
172                     } catch (NumberFormatException nfe) {
173                         nfe.printStackTrace();
174                     }
175                 }
176                 panel.setBorder(new LineBorder(color, borderSize));
177             } else if (border.equalsIgnoreCase("etched")) {
178                 EtchedBorder etched = new EtchedBorder(EtchedBorder.RAISED);
179                 panel.setBorder(etched);
180 
181             } else if (border.equalsIgnoreCase("titledicon")) {
182               String borderTitle = getProperty("border-title");
183               if (borderTitle == null) {
184                   borderTitle = "";
185               }
186 
187               Color titleColor = null;
188               if (getProperty("border-title-color") != null) {
189                   titleColor = ColorUtils.getSwingColor(getProperty("border-title-color"));
190               }
191               IconTitledBorder itBorder = new IconTitledBorder();
192               int borderSize = 1;
193               if (getProperty("border-size") != null) {
194                   try {
195                       borderSize = Integer.parseInt(getProperty("border-size"));
196                       itBorder.setThickness(borderSize);
197                   } catch (NumberFormatException nfe) {
198                       nfe.printStackTrace();
199                   }
200               }
201               if (getProperty("border-image") != null) {
202                   itBorder.setImage(SwingUtils.getImage(getProperty("border-image"), this));
203               }
204 //              if (titleColor == null) {
205 //                  titledBorder.setTitleColor(titleColor);
206 //              }
207               if (borderTitle != null) {
208                   itBorder.setTitle(borderTitle);
209               }
210               panel.setBorder(itBorder);
211                 
212             }
213         } else {
214           panel.setBorder(null);
215         }
216         // Add tab widget instead of panel widget..
217         String tabId = getProperty(TabPanel.TABID);
218         if (tabId == null) {
219             panel.setEnabled(isEnabled());
220         } else {
221             System.out.println("tabId : " + tabId);
222             ((JTabbedPane) getParent().getNativeWidget()).setEnabledAt(Integer.parseInt(tabId), isEnabled());
223         }
224         panel.repaint();
225     }
226 
227     /**
228      * @see org.xulux.nyx.gui.XuluxWidget#canContainChildren()
229      */
230     public boolean canContainChildren() {
231         return true;
232     }
233 
234     /**
235      * @see org.xulux.nyx.gui.XuluxWidget#getChildWidgets()
236      */
237     public List getChildWidgets() {
238         return widgets;
239     }
240 
241     /**
242      * @see org.xulux.nyx.gui.ContainerWidget#addToParent(XuluxWidget)
243      */
244     public void addToParent(IWidget widget) {
245         if (widget instanceof IShowChildWidgets) {
246             List children = widget.getChildWidgets();
247             if (children != null && children.size() > 0) {
248                 Iterator it = children.iterator();
249                 while (it.hasNext()) {
250                     IWidget w = (IWidget) it.next();
251                     panel.add((JComponent) w.getNativeWidget(), w);
252                     w.refresh();
253                 }
254             }
255         } else {
256             panel.add((Component) widget.getNativeWidget(), widget);
257             widget.refresh();
258         }
259     }
260 
261     /**
262      * @see org.xulux.nyx.gui.XuluxWidget#focus()
263      */
264     public void focus() {
265       isRefreshing = true;
266       initialize();
267       panel.requestFocus();
268       isRefreshing = false;
269       // if widget is not showing we have
270       // to make it showing..
271       // if we have a tab, we need to call the parent too..
272       if (!panel.isShowing() && getParent() != null || 
273           getProperty(TabPanel.TABID) != null) {
274           // set the session variable, so controls
275           // can look who requested focus..
276           getPart().getSession().setValue("nyx.focusrequest", this);
277           getParent().focus();
278           // remove session variable again.
279           getPart().getSession().remove("nyx.focusrequest");
280           panel.requestFocus();
281       }
282     }
283     /**
284      * @see org.xulux.nyx.gui.XuluxWidget#getGuiValue()
285      */
286     public Object getGuiValue() {
287         return null;
288     }
289 
290     /**
291      * @see org.xulux.nyx.gui.XuluxWidget#canContainValue()
292      */
293     public boolean canContainValue() {
294         return false;
295     }
296 
297     /**
298      * @see org.xulux.nyx.gui.XuluxWidget#isValueEmpty()
299      */
300     public boolean isValueEmpty() {
301         return true;
302     }
303 
304     /**
305      * @see org.xulux.nyx.gui.XuluxWidget#addXuluxListener(org.xulux.nyx.gui.XuluxListener)
306      */
307     public void addXuluxListener(IXuluxListener listener) {
308     }
309 
310 }