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.setup.Util;
25 
26 import java.awt.FocusTraversalPolicy;
27 import javax.swing.JComponent;
28 
29 public class DialogFocusTraversalPolicy extends FocusTraversalPolicy {
30 
31     private JComponent order[];
32     private java.util.List list;
33 
DialogFocusTraversalPolicy(JComponent _order[])34     public DialogFocusTraversalPolicy(JComponent _order[]) {
35     	order = _order;
36     	list = java.util.Arrays.asList(order);
37     }
38 
getFirstComponent(java.awt.Container focusCycleRoot)39     public java.awt.Component getFirstComponent(java.awt.Container focusCycleRoot) {
40         return order[0];
41     }
42 
getLastComponent(java.awt.Container focusCycleRoot)43     public java.awt.Component getLastComponent(java.awt.Container focusCycleRoot) {
44         return order[order.length - 1];
45     }
46 
getComponentAfter(java.awt.Container focusCycleRoot, java.awt.Component aComponent)47     public java.awt.Component getComponentAfter(java.awt.Container focusCycleRoot, java.awt.Component aComponent) {
48         int index = 0,x = -1;
49         index = list.indexOf(aComponent);
50         index++; // increasing automatically
51         if(!order[index % order.length].isEnabled() ||
52            !order[index % order.length].isVisible()) {
53             x = index;
54             index = -1;
55             for (; x != order.length; x++) {
56                 if(order[x].isEnabled() && order[x].isVisible()) {
57                     index = x;
58                     break;
59                 }
60             }
61             if(index == -1) {
62                 x = list.indexOf(aComponent);
63                 for(int y = 0; y <= x; y++) {
64                     if(order[y].isEnabled() && order[x].isVisible()) {
65                         index = y;
66                         break;
67                     }
68                 }
69             }
70         }
71         return order[ index % order.length];
72     }
73 
getComponentBefore(java.awt.Container focusCycleRoot, java.awt.Component aComponent)74     public java.awt.Component getComponentBefore(java.awt.Container focusCycleRoot, java.awt.Component aComponent) {
75         int index = list.indexOf(aComponent), x = -1;
76         index--;
77         if(!order[(index + order.length) % order.length].isEnabled() ||
78            !order[(index + order.length) % order.length].isVisible()) {
79             x = index;
80             index = -1;
81             for(; x >= 0; x--) {
82                 if(order[x].isEnabled() && order[x].isVisible()) {
83                     index = x;
84                     break;
85                 }
86             }
87             // if nothing has changed
88             if(index == -1) {
89                 x = list.indexOf(aComponent);
90                 for(int y = order.length -1; y >= x; y--) {
91                     if(order[y].isEnabled() && order[x].isVisible()) {
92                         index = y;
93                         break;
94                     }
95                 }
96             }
97         }
98         return order[ (index + order.length) % order.length];
99     }
100 
getDefaultComponent(java.awt.Container focusCycleRoot)101     public java.awt.Component getDefaultComponent(java.awt.Container focusCycleRoot) {
102         return order[0];
103     }
104 
getInitialComponent(java.awt.Window window)105     public java.awt.Component getInitialComponent(java.awt.Window window) {
106         return order[0];
107     }
108 
109 }
110 
111 
112