xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/SetupData/DisplayPackageDescription.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package org.openoffice.setup.SetupData;
29 
30 import org.openoffice.setup.InstallData;
31 import org.openoffice.setup.Util.ModuleCtrl;
32 import java.util.Enumeration;
33 import javax.swing.tree.DefaultMutableTreeNode;
34 import javax.swing.tree.TreeNode;
35 
36 public class DisplayPackageDescription {
37 
38     private PackageDescription data;
39     private int size  = 0;
40 
41     public DisplayPackageDescription(PackageDescription data) {
42         this.data = data;
43         // Setting default module settings for modules, that are not hidden
44         ModuleCtrl.setDefaultModuleSettings(data);
45         // The size is set dynamically, when a module is selected.
46         // This is better for the nodes, which have a changing size.
47     }
48 
49     public String getDescription() {
50         return data.getDescription();
51     }
52 
53     public int getSize() {
54         return size;     // using local size
55     }
56 
57     public int getState() {
58         return data.getSelectionState();
59     }
60 
61     private int getSize(TreeNode peerNode) {
62         // return peerNode.isLeaf() ? data.getAccumulatedSize() : data.getSize();
63         // using size from PackageDescription, which is pkgSize (and that is defined in xpd file)
64         return data.getSize();
65     }
66 
67     public void setState(TreeNode node, int newState) {
68         if ((data.getSelectionState() != PackageDescription.IGNORE) && data.isOptional()) {
69             data.setSelectionState(newState);
70         }
71 
72         if (!node.isLeaf()) {
73             size = data.getSize();  // -> that is the value defined in xpd file
74 
75             for (Enumeration e = node.children(); e.hasMoreElements();) {
76                 TreeNode child = (TreeNode)e.nextElement();
77                 DisplayPackageDescription childInfo = getInfo(child);
78 
79                 childInfo.setState(child, newState);
80 
81                 if (childInfo.isSelected())   {
82                     size += childInfo.getSize(child);
83                 }
84             }
85         } else if (isSelected()) {
86             size = data.getSize();  // -> that is the value defined in xpd file
87         }
88     }
89 
90     public void toggleState(TreeNode node) {
91 
92         int state = data.getSelectionState();
93 
94         if (state != PackageDescription.IGNORE) {
95 
96             if (state == PackageDescription.REMOVE) {
97                 setState(node, PackageDescription.DONT_REMOVE);
98             } else if ((state == PackageDescription.DONT_REMOVE) || (state == PackageDescription.REMOVE_SOME)) {
99                 setState(node, PackageDescription.REMOVE );
100             } else if ((state == PackageDescription.DONT_INSTALL) || (state == PackageDescription.INSTALL_SOME)) {
101                 setState(node, PackageDescription.INSTALL);
102             } else {
103                 setState(node, PackageDescription.DONT_INSTALL);
104             }
105 
106             if (!node.isLeaf()) {
107                 updateState(node);
108             }
109 
110             try {
111                 TreeNode parent = node.getParent();
112                 DisplayPackageDescription parentInfo = getInfo(parent);
113 
114                 parentInfo.updateState(parent);
115                 try {
116                     TreeNode grandpa = parent.getParent();
117                     DisplayPackageDescription grandpaInfo = getInfo(grandpa);
118 
119                     grandpaInfo.updateState(grandpa);
120                 } catch (java.lang.IllegalArgumentException e) {
121                     /* ignore */
122                 }
123 
124             } catch (java.lang.IllegalArgumentException e) {
125                 /* ignore */
126             }
127         }
128     }
129 
130     public void updateState(TreeNode node)
131     {
132         int state = PackageDescription.DONT_KNOW;
133         InstallData installdata = InstallData.getInstance();
134         size = data.getSize();  // -> that is the value defined in xpd file
135 
136         for (Enumeration e = node.children(); e.hasMoreElements();) {
137             TreeNode child = (TreeNode) e.nextElement();
138             DisplayPackageDescription childInfo = getInfo(child);
139 
140             int childState = childInfo.getState();
141 
142             if ((state == PackageDescription.DONT_KNOW) || (state == PackageDescription.IGNORE)) {
143                 state = childState;
144             } else if ((state != childState) && (childState != PackageDescription.IGNORE)) {
145                 if ( installdata.isUninstallationMode() ) {
146                     state = PackageDescription.REMOVE_SOME;
147                 } else {
148                     state = PackageDescription.INSTALL_SOME;
149                 }
150             }
151             if (childInfo.isSelected()) {
152                 size += childInfo.getSize(child);
153             }
154         }
155 
156         data.setSelectionState(state);
157 
158     }
159 
160     public void updateSize(TreeNode node)
161     {
162         size = data.getSize();  // -> that is the value defined in xpd file
163 
164         for (Enumeration e = node.children(); e.hasMoreElements();) {
165             TreeNode child = (TreeNode) e.nextElement();
166             DisplayPackageDescription childInfo = getInfo(child);
167             if (childInfo.isSelected()) {
168                 size += childInfo.getSize(child);
169             }
170         }
171     }
172 
173     public String toString() {
174         return data.getName();
175     }
176 
177     static public boolean is(Object o) {
178         return (o != null)
179             && (o.getClass().getName().equals("org.openoffice.setup.SetupData.DisplayPackageDescription"));
180     }
181 
182     public boolean isSelected() {
183         int state = data.getSelectionState();
184         return     (state == PackageDescription.INSTALL) || (state == PackageDescription.INSTALL_SOME)
185                 || (state == PackageDescription.REMOVE)  || (state == PackageDescription.REMOVE_SOME);
186     }
187 
188     private DisplayPackageDescription getInfo(TreeNode node) throws java.lang.IllegalArgumentException {
189         if (node == null) {
190             throw new java.lang.IllegalArgumentException();
191         }
192 
193         DisplayPackageDescription info = (DisplayPackageDescription)((DefaultMutableTreeNode)node).getUserObject();
194         if ((info != null) && is(info)) {
195             return info;
196         } else {
197             throw new java.lang.IllegalArgumentException();
198         }
199     }
200 }
201