View Javadoc

1   /*
2    * Copyright 1999-2006 Faculty of Mathematics, Physics and Informatics, Comenius
3    * University, Bratislava. This file is protected by the Mozilla Public License
4    * version 1.1 (the License); you may not use this file except in compliance
5    * with the License. You may obtain a copy of the License at
6    * http://euromath2.sourceforge.net/license.html Unless required by applicable
7    * law or agreed to in writing, software distributed under the License is
8    * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
9    * KIND, either express or implied. See the License for the specific language
10   * governing permissions and limitations under the License.
11   */
12  package sk.uniba.euromath.editor;
13  
14  import org.eclipse.draw2d.FigureCanvas;
15  import org.eclipse.swt.SWT;
16  import org.eclipse.swt.graphics.Point;
17  import org.eclipse.swt.graphics.Rectangle;
18  import org.eclipse.swt.widgets.Composite;
19  import org.eclipse.swt.widgets.Control;
20  import org.eclipse.swt.widgets.Layout;
21  
22  import sk.uniba.euromath.editor.xmlEditor.policies.FigureSelectionPolicy;
23  
24  /***
25   * @author Tomáš Studva 12.10.2005
26   */
27  public class PreferredByControlLayout extends Layout {
28  
29      /***
30       * Constructs a new instance of this class.
31       */
32      public PreferredByControlLayout() {
33          super();
34      }
35  
36      /*
37       * (non-Javadoc)
38       * 
39       * @see org.eclipse.swt.widgets.Layout#computeSize(org.eclipse.swt.widgets.Composite,
40       *      int, int, boolean)
41       */
42      @Override
43      protected Point computeSize(Composite composite, int wHint, int hHint,
44              boolean flushCache) {
45          if (composite.getChildren().length == 0)
46              return new Point(0, 0);
47          int width, height;
48          width = 0;
49          height = 0;
50          if (wHint != SWT.DEFAULT)
51              width = wHint;
52          if (hHint != SWT.DEFAULT)
53              height = hHint;
54          return new Point(width, height);
55      }
56  
57      @Override
58      protected boolean flushCache(Control control) {
59          return true;
60      }
61  
62      /*
63       * (non-Javadoc)
64       * 
65       * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
66       *      boolean)
67       */
68      @Override
69      protected void layout(Composite composite, boolean flushCache) {
70          org.eclipse.draw2d.geometry.Point scrollOffset = new org.eclipse.draw2d.geometry.Point(
71                  0, 0);
72          if (composite instanceof FigureCanvas) {
73              scrollOffset = ((FigureCanvas) composite).getViewport()
74                      .getViewLocation();
75          }
76          Control[] children = composite.getChildren();
77          int count = children.length;
78          if (count == 0)
79              return;
80          for (int i = 0; i < count; i++) {
81              Rectangle rect = (Rectangle) children[i].getLayoutData();
82              children[i].setBounds(rect.x  - scrollOffset.x, rect.y 
83                      - scrollOffset.y, rect.width+2*FigureSelectionPolicy.SELECION_BORDER_WIDTH,
84                      rect.height+2*FigureSelectionPolicy.SELECION_BORDER_WIDTH );
85          }
86      }
87  
88      /***
89       * Returns a string containing a concise, human-readable description of the
90       * receiver.
91       * 
92       * @return a string representation of the event
93       */
94      @Override
95      public String toString() {
96          String string = getClass().getName();
97          int index = string.lastIndexOf('.');
98          if (index == -1)
99              return string;
100         return string.substring(index + 1, string.length());
101     }
102 
103 }