2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 import java
.awt
.Component
;
22 import java
.awt
.GridBagConstraints
;
23 import java
.awt
.GridBagLayout
;
24 import java
.awt
.Insets
;
25 import java
.lang
.reflect
.InvocationTargetException
;
26 import java
.lang
.reflect
.Method
;
27 import java
.util
.ArrayList
;
29 import javax
.swing
.BorderFactory
;
30 import javax
.swing
.JPanel
;
31 import javax
.swing
.JTree
;
32 import javax
.swing
.border
.BevelBorder
;
33 import javax
.swing
.border
.Border
;
35 import com
.sun
.star
.accessibility
.XAccessibleContext
;
38 public class ObjectViewContainer
41 public ObjectViewContainer ()
43 maViewTemplates
= new ArrayList
<Class
> ();
44 maViewBorder
= BorderFactory
.createBevelBorder (BevelBorder
.RAISED
);
45 setLayout (new GridBagLayout ());
47 System
.out
.println ("ObjectViewContainer");
48 RegisterView (ContextView
.class);
49 // RegisterView (StateSetView.class);
50 RegisterView (FocusView
.class);
51 RegisterView (TextView
.class);
56 /** Remove all existing views and create new ones according to the
57 interfaces supported by the given object.
59 public void SetObject (XAccessibleContext xContext
)
61 // Call Destroy at all views to give them a chance to release their
63 int n
= getComponentCount();
64 for (int i
=0; i
<n
; i
++)
65 ((ObjectView
)getComponent(i
)).Destroy();
66 // Remove existing views.
70 for (int i
=0; i
<maViewTemplates
.size(); i
++)
74 Class aViewClass
= maViewTemplates
.get (i
);
75 Method aCreateMethod
= aViewClass
.getDeclaredMethod (
76 "Create", new Class
[] {
77 ObjectViewContainer
.class,
78 XAccessibleContext
.class});
79 if (aCreateMethod
!= null)
81 ObjectView aView
= (ObjectView
)
82 aCreateMethod
.invoke (null, new Object
[] {this, xContext
});
86 catch (NoSuchMethodException e
)
87 {System
.err
.println ("Caught exception while creating view " + i
+ " : " + e
);}
88 catch (IllegalAccessException e
)
89 {System
.err
.println ("Caught exception while creating view " + i
+ " : " + e
);}
90 catch (InvocationTargetException e
)
91 {System
.err
.println ("Caught exception while creating view " + i
+ " : " + e
);}
94 UpdateLayoutManager ();
96 // Now set the object at all views.
97 n
= getComponentCount();
98 for (int i
=0; i
<n
; i
++)
99 ((ObjectView
)getComponent(i
)).SetObject (xContext
);
101 setPreferredSize (getLayout().preferredLayoutSize (this));
105 /** Add the given class to the list of classes which will be
106 instantiated the next time an accessible object is set.
108 public void RegisterView (Class aObjectViewClass
)
110 System
.out
.println ("registering " + aObjectViewClass
);
111 maViewTemplates
.add(aObjectViewClass
);
114 /** Replace one view class with another.
116 public void ReplaceView (Class aObjectViewClass
, Class aSubstitution
)
118 int nIndex
= maViewTemplates
.indexOf (aObjectViewClass
);
120 maViewTemplates
.set (nIndex
, aSubstitution
);
123 /** Add an object view and place it below all previously added views.
125 This argument may be null. In this case nothing happens.
127 private void Add (ObjectView aView
)
131 GridBagConstraints constraints
= new GridBagConstraints ();
132 constraints
.gridx
= 0;
133 constraints
.gridy
= getComponentCount();
134 constraints
.gridwidth
= 1;
135 constraints
.gridheight
= 1;
136 constraints
.weightx
= 1;
137 constraints
.weighty
= 0;
138 constraints
.ipadx
= 2;
139 constraints
.ipady
= 5;
140 constraints
.insets
= new Insets (5,5,5,5);
141 constraints
.anchor
= GridBagConstraints
.NORTH
;
142 constraints
.fill
= GridBagConstraints
.HORIZONTAL
;
145 BorderFactory
.createTitledBorder (
146 maViewBorder
, aView
.GetTitle()));
148 add (aView
, constraints
);
152 /** Update the layout manager by setting the vertical weight of the
153 bottom entry to 1 and so make it strech to over the available
157 private void UpdateLayoutManager ()
159 // Adapt the layout manager.
160 if (getComponentCount() > 0)
162 Component aComponent
= getComponent (getComponentCount()-1);
163 GridBagLayout aLayout
= (GridBagLayout
)getLayout();
164 GridBagConstraints aConstraints
= aLayout
.getConstraints (aComponent
);
165 aConstraints
.weighty
= 1;
166 aLayout
.setConstraints (aComponent
, aConstraints
);
170 /// Observe this tree for selection changes and notify them to all
172 private JTree maTree
;
173 private Border maViewBorder
;
174 /// List of view templates which are instantiated when new object is set.
175 private ArrayList
<Class
> maViewTemplates
;