1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.accessibility.awb.view;
25 
26 import java.awt.Component;
27 import java.awt.Cursor;
28 import java.awt.GridBagLayout;
29 import java.awt.GridBagConstraints;
30 import java.awt.Point;
31 import java.awt.event.MouseListener;
32 import java.awt.event.MouseMotionListener;
33 import java.awt.event.MouseEvent;
34 import javax.swing.JComponent;
35 
36 class LayoutManager
37     implements MouseListener,
38                MouseMotionListener
39 {
LayoutManager(JComponent aLayoutedComponent)40     public LayoutManager (JComponent aLayoutedComponent)
41     {
42         maLayoutedComponent = aLayoutedComponent;
43         maDraggedView = null;
44         mbInsertionPending = false;
45     }
46 
mouseClicked(MouseEvent aEvent)47     public void mouseClicked (MouseEvent aEvent)
48     {
49         System.out.println (aEvent);
50     }
mousePressed(MouseEvent aEvent)51     public void mousePressed (MouseEvent aEvent)
52     {
53         mnOldY = aEvent.getPoint().y;
54     }
mouseReleased(MouseEvent aEvent)55     public void mouseReleased (MouseEvent aEvent)
56     {
57         if (mbInsertionPending)
58         {
59             InsertView (maDraggedView, aEvent.getPoint().y);
60             mbInsertionPending = false;
61             maDraggedView = null;
62         }
63     }
mouseEntered(MouseEvent aEvent)64     public void mouseEntered (MouseEvent aEvent)
65     {
66     }
mouseExited(MouseEvent aEvent)67     public void mouseExited (MouseEvent aEvent)
68     {
69         if (mbInsertionPending)
70         {
71             InsertView (maDraggedView, mnOldY);
72             mbInsertionPending = false;
73             maDraggedView = null;
74         }
75     }
mouseDragged(MouseEvent aEvent)76     public void mouseDragged (MouseEvent aEvent)
77     {
78         int dy = mnOldY - aEvent.getPoint().y;
79         GridBagLayout aLayout = (GridBagLayout)maLayoutedComponent.getLayout();
80         if ( ! mbInsertionPending && dy != 0)
81         {
82             maDraggedView = RemoveView (mnOldY);
83             if (maDraggedView != null)
84                 mbInsertionPending = true;
85         }
86     }
mouseMoved(MouseEvent aEvent)87     public void mouseMoved (MouseEvent aEvent)
88     {
89     }
90 
91 
92 
93 
RemoveView(int y)94     private ObjectView RemoveView (int y)
95     {
96         ObjectView aView = null;
97         GridBagLayout aLayout = (GridBagLayout)maLayoutedComponent.getLayout();
98 
99         Point aGridLocation = aLayout.location (10,y);
100         Component[] aComponentList = maLayoutedComponent.getComponents();
101         System.out.println ("removing view at " + aGridLocation);
102         for (int i=0; i<aComponentList.length && aView==null; i++)
103         {
104             GridBagConstraints aConstraints = aLayout.getConstraints (
105                 aComponentList[i]);
106             if (aConstraints.gridy == aGridLocation.y)
107                 aView = (ObjectView)aComponentList[i];
108         }
109         maNormalCursor = maLayoutedComponent.getCursor();
110         if (aView != null)
111         {
112             System.out.println ("removing view at " + aGridLocation.y);
113             maLayoutedComponent.setCursor (new Cursor (Cursor.MOVE_CURSOR));
114             maLayoutedComponent.remove (aView);
115             maLayoutedComponent.validate();
116             maLayoutedComponent.repaint();
117         }
118 
119         return aView;
120     }
121 
InsertView(ObjectView aView, int y)122     private void InsertView (ObjectView aView, int y)
123     {
124         if (aView != null)
125         {
126             GridBagLayout aLayout = (GridBagLayout)maLayoutedComponent.getLayout();
127             Point aGridLocation = aLayout.location (0,y);
128             Component[] aComponentList = maLayoutedComponent.getComponents();
129             System.out.println ("new position is " + aGridLocation.y);
130             for (int i=0; i<aComponentList.length; i++)
131             {
132                 GridBagConstraints aConstraints = aLayout.getConstraints (
133                     aComponentList[i]);
134                 if (aConstraints.gridy >= aGridLocation.y)
135                 {
136                     if (aConstraints.gridy == aGridLocation.y)
137                         maLayoutedComponent.add (maDraggedView, aConstraints);
138                     aConstraints.gridy += 1;
139                     aLayout.setConstraints (aComponentList[i], aConstraints);
140                 }
141             }
142             maLayoutedComponent.validate();
143             maLayoutedComponent.repaint();
144         }
145         maLayoutedComponent.setCursor (maNormalCursor);
146     }
147 
148 
149 
150 
151     private JComponent maLayoutedComponent;
152     private ObjectView maDraggedView;
153     private int mnOldY;
154     private boolean mbInsertionPending;
155     private Cursor maNormalCursor;
156 }
157