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.Dimension;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.swing.JComponent;
25  import javax.swing.JScrollPane;
26  
27  import org.xulux.api.gui.IShowChildWidgets;
28  import org.xulux.api.gui.IWidget;
29  import org.xulux.gui.ContainerWidget;
30  
31  /**
32   * The scrollpane adds a scrollpane to the gui.
33   * You can use the scrollpane in the xml or if you define a widget
34   * to being scrollable, it will inject the scrollpane as the parent
35   * of the widget.
36   *
37   * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
38   * @version $Id: ScrollPane.java,v 1.1 2005/12/18 12:58:18 mvdb Exp $
39   */
40  public class ScrollPane extends ContainerWidget {
41  
42      protected JScrollPane pane;
43  
44      /**
45       * @param name the name of the widget
46       */
47      public ScrollPane(String name) {
48        super(name);
49      }
50    
51      /**
52       * @see org.xulux.gui.XuluxWidget#destroy()
53       */
54      public void destroy() {
55          super.destroy();
56          if (pane == null) {
57              return;
58          }
59          Container container = pane.getParent();
60          pane.setVisible(false);
61          pane.removeAll();
62          if (container != null) {
63              container.remove(pane);
64          }
65          pane = null;
66      }
67    
68      /**
69       * @see org.xulux.gui.XuluxWidget#getNativeWidget()
70       */
71      public Object getNativeWidget() {
72          if (!initialized) {
73              initialize();
74          }
75          return pane;
76      }
77    
78      /**
79       * @see org.xulux.gui.XuluxWidget#initialize()
80       */
81      public void initialize() {
82          if (initialized) {
83              return;
84          }
85          initialized = true;
86          this.pane = new JScrollPane();
87          // @todo make scrollpane properties to set policies..
88          pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
89          pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
90  //        pane.setPreferredSize(getRectangle().getRectangle().getSize());
91  //        System.out.println("init port : " + pane.getViewport());
92          if (pane.getViewport() != null) {
93              pane.getViewport().setPreferredSize(pane.getPreferredSize());
94          }
95          initializeChildren();
96      }
97    
98      /**
99       * @see org.xulux.gui.XuluxWidget#getGuiValue()
100      */
101     public Object getGuiValue() {
102       return null;
103     }
104   
105     /**
106      * @see org.xulux.gui.XuluxWidget#focus()
107      */
108     public void focus() {
109   
110     }
111   
112     /**
113      * @see org.xulux.gui.XuluxWidget#isValueEmpty()
114      */
115     public boolean isValueEmpty() {
116         return true;
117     }
118   
119     /**
120      * @see org.xulux.gui.XuluxWidget#canContainValue()
121      */
122     public boolean canContainValue() {
123         return false;
124     }
125 
126     /**
127      * @see org.xulux.gui.ContainerWidget#addToParent(org.xulux.gui.XuluxWidget)
128      */
129     public void addToParent(IWidget widget) {
130       if (widget instanceof IShowChildWidgets) {
131           List children = widget.getChildWidgets();
132           if (children != null && children.size() > 0) {
133               Iterator it = children.iterator();
134               while (it.hasNext()) {
135                   IWidget w = (IWidget) it.next();
136                   pane.add((JComponent) w.getNativeWidget(), w);
137                   w.refresh();
138               }
139           }
140       } else {
141           pane.setViewportView((Component) widget.getNativeWidget());
142           pane.setPreferredSize(getRectangle().getRectangle().getSize());
143       }
144     }
145 
146     /**
147      * @see org.xulux.gui.XuluxWidget#refresh()
148      */
149     public void refresh() {
150         if (isRefreshing()) {
151           return;
152         }
153         isRefreshing = true;
154         if (!initialized) {
155           initialize();
156         }
157         pane.setVisible(isVisible());
158         pane.setViewportView(pane.getViewport().getView());
159         Dimension viewSize = (Dimension) getRealProperty("viewSize");
160         if (viewSize != null) {
161           pane.getViewport().setViewSize(viewSize);
162         }
163         pane.doLayout();
164         pane.revalidate();
165         isRefreshing = false;
166     }
167 }