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.SetupData;
25 
26 import org.openoffice.setup.InstallData;
27 import org.openoffice.setup.Util.ModuleCtrl;
28 import java.util.Enumeration;
29 import javax.swing.tree.DefaultMutableTreeNode;
30 import javax.swing.tree.TreeNode;
31 
32 public class DisplayPackageDescription {
33 
34     private PackageDescription data;
35     private int size  = 0;
36 
DisplayPackageDescription(PackageDescription data)37     public DisplayPackageDescription(PackageDescription data) {
38         this.data = data;
39         // Setting default module settings for modules, that are not hidden
40         ModuleCtrl.setDefaultModuleSettings(data);
41         // The size is set dynamically, when a module is selected.
42         // This is better for the nodes, which have a changing size.
43     }
44 
getDescription()45     public String getDescription() {
46         return data.getDescription();
47     }
48 
getSize()49     public int getSize() {
50         return size;     // using local size
51     }
52 
getState()53     public int getState() {
54         return data.getSelectionState();
55     }
56 
getSize(TreeNode peerNode)57     private int getSize(TreeNode peerNode) {
58         // return peerNode.isLeaf() ? data.getAccumulatedSize() : data.getSize();
59         // using size from PackageDescription, which is pkgSize (and that is defined in xpd file)
60         return data.getSize();
61     }
62 
setState(TreeNode node, int newState)63     public void setState(TreeNode node, int newState) {
64         if ((data.getSelectionState() != PackageDescription.IGNORE) && data.isOptional()) {
65             data.setSelectionState(newState);
66         }
67 
68         if (!node.isLeaf()) {
69             size = data.getSize();  // -> that is the value defined in xpd file
70 
71             for (Enumeration e = node.children(); e.hasMoreElements();) {
72                 TreeNode child = (TreeNode)e.nextElement();
73                 DisplayPackageDescription childInfo = getInfo(child);
74 
75                 childInfo.setState(child, newState);
76 
77                 if (childInfo.isSelected())   {
78                     size += childInfo.getSize(child);
79                 }
80             }
81         } else if (isSelected()) {
82             size = data.getSize();  // -> that is the value defined in xpd file
83         }
84     }
85 
toggleState(TreeNode node)86     public void toggleState(TreeNode node) {
87 
88         int state = data.getSelectionState();
89 
90         if (state != PackageDescription.IGNORE) {
91 
92             if (state == PackageDescription.REMOVE) {
93                 setState(node, PackageDescription.DONT_REMOVE);
94             } else if ((state == PackageDescription.DONT_REMOVE) || (state == PackageDescription.REMOVE_SOME)) {
95                 setState(node, PackageDescription.REMOVE );
96             } else if ((state == PackageDescription.DONT_INSTALL) || (state == PackageDescription.INSTALL_SOME)) {
97                 setState(node, PackageDescription.INSTALL);
98             } else {
99                 setState(node, PackageDescription.DONT_INSTALL);
100             }
101 
102             if (!node.isLeaf()) {
103                 updateState(node);
104             }
105 
106             try {
107                 TreeNode parent = node.getParent();
108                 DisplayPackageDescription parentInfo = getInfo(parent);
109 
110                 parentInfo.updateState(parent);
111                 try {
112                     TreeNode grandpa = parent.getParent();
113                     DisplayPackageDescription grandpaInfo = getInfo(grandpa);
114 
115                     grandpaInfo.updateState(grandpa);
116                 } catch (java.lang.IllegalArgumentException e) {
117                     /* ignore */
118                 }
119 
120             } catch (java.lang.IllegalArgumentException e) {
121                 /* ignore */
122             }
123         }
124     }
125 
updateState(TreeNode node)126     public void updateState(TreeNode node)
127     {
128         int state = PackageDescription.DONT_KNOW;
129         InstallData installdata = InstallData.getInstance();
130         size = data.getSize();  // -> that is the value defined in xpd file
131 
132         for (Enumeration e = node.children(); e.hasMoreElements();) {
133             TreeNode child = (TreeNode) e.nextElement();
134             DisplayPackageDescription childInfo = getInfo(child);
135 
136             int childState = childInfo.getState();
137 
138             if ((state == PackageDescription.DONT_KNOW) || (state == PackageDescription.IGNORE)) {
139                 state = childState;
140             } else if ((state != childState) && (childState != PackageDescription.IGNORE)) {
141                 if ( installdata.isUninstallationMode() ) {
142                     state = PackageDescription.REMOVE_SOME;
143                 } else {
144                     state = PackageDescription.INSTALL_SOME;
145                 }
146             }
147             if (childInfo.isSelected()) {
148                 size += childInfo.getSize(child);
149             }
150         }
151 
152         data.setSelectionState(state);
153 
154     }
155 
updateSize(TreeNode node)156     public void updateSize(TreeNode node)
157     {
158         size = data.getSize();  // -> that is the value defined in xpd file
159 
160         for (Enumeration e = node.children(); e.hasMoreElements();) {
161             TreeNode child = (TreeNode) e.nextElement();
162             DisplayPackageDescription childInfo = getInfo(child);
163             if (childInfo.isSelected()) {
164                 size += childInfo.getSize(child);
165             }
166         }
167     }
168 
toString()169     public String toString() {
170         return data.getName();
171     }
172 
is(Object o)173     static public boolean is(Object o) {
174         return (o != null)
175             && (o.getClass().getName().equals("org.openoffice.setup.SetupData.DisplayPackageDescription"));
176     }
177 
isSelected()178     public boolean isSelected() {
179         int state = data.getSelectionState();
180         return     (state == PackageDescription.INSTALL) || (state == PackageDescription.INSTALL_SOME)
181                 || (state == PackageDescription.REMOVE)  || (state == PackageDescription.REMOVE_SOME);
182     }
183 
getInfo(TreeNode node)184     private DisplayPackageDescription getInfo(TreeNode node) throws java.lang.IllegalArgumentException {
185         if (node == null) {
186             throw new java.lang.IllegalArgumentException();
187         }
188 
189         DisplayPackageDescription info = (DisplayPackageDescription)((DefaultMutableTreeNode)node).getUserObject();
190         if ((info != null) && is(info)) {
191             return info;
192         } else {
193             throw new java.lang.IllegalArgumentException();
194         }
195     }
196 }
197