001/*
002 * (C) Copyright 2011-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     tdelprat
018 *
019 */
020
021package org.nuxeo.wizard.download;
022
023import java.util.ArrayList;
024import java.util.List;
025
026/**
027 * @author Tiry (tdelprat@nuxeo.com)
028 * @since 5.5
029 */
030public class DownloadablePackageOption {
031
032    protected final DownloadPackage pkg;
033
034    protected boolean exclusive;
035
036    /**
037     * @since 10.3
038     */
039    protected String selectionType;
040
041    protected boolean selected = false;
042
043    protected String label;
044
045    protected List<DownloadablePackageOption> childrenPackages = new ArrayList<>();
046
047    protected final String id;
048
049    protected DownloadablePackageOption parent;
050
051    public DownloadablePackageOption(DownloadPackage pkg, int idx) {
052        this.pkg = pkg;
053        // this.id = UUID.randomUUID().toString();
054        id = "o" + idx;
055    }
056
057    public DownloadablePackageOption(DownloadPackage pkg, String id) {
058        this.pkg = pkg;
059        this.id = id;
060    }
061
062    public boolean isExclusive() {
063        return exclusive;
064    }
065
066    public void setExclusive(boolean exclusive) {
067        this.exclusive = exclusive;
068    }
069
070    public void setExclusive(String exclusive) {
071        if (exclusive != null) {
072            if ("true".equalsIgnoreCase(exclusive)) {
073                this.exclusive = true;
074            } else {
075                this.exclusive = false;
076            }
077        }
078    }
079
080    /**
081     * @since 10.3
082     */
083    public void setSelectionType(String selectionType) {
084        this.selectionType = selectionType;
085    }
086
087    /**
088     * @since 10.3
089     */
090    public String getSelectionType() {
091        return selectionType;
092    }
093
094    public boolean isSelected() {
095        return selected;
096    }
097
098    public void setSelected(boolean selected) {
099        this.selected = selected;
100    }
101
102    public List<DownloadablePackageOption> getChildrenPackages() {
103        return childrenPackages;
104    }
105
106    public void addChildPackage(DownloadablePackageOption child) {
107        childrenPackages.add(child);
108        child.setParent(this);
109    }
110
111    protected void setParent(DownloadablePackageOption parent) {
112        this.parent = parent;
113    }
114
115    public List<DownloadablePackageOption> getSiblingPackages() {
116
117        List<DownloadablePackageOption> siblings = new ArrayList<>();
118        if (parent != null) {
119            for (DownloadablePackageOption sibling : parent.getChildrenPackages()) {
120                if (!sibling.getId().equals(getId())) {
121                    siblings.add(sibling);
122                }
123            }
124        }
125        return siblings;
126    }
127
128    public String getLabel() {
129        if (label == null && pkg != null) {
130            return pkg.getLabel();
131        }
132        return label;
133    }
134
135    public void setLabel(String label) {
136        this.label = label;
137    }
138
139    public DownloadPackage getPackage() {
140        return pkg;
141    }
142
143    public String getId() {
144        return id;
145    }
146
147    public String getColor() {
148        if (pkg != null) {
149            return pkg.getColor();
150        }
151        return "";
152    }
153
154    /**
155     * @since 8.3
156     */
157    public String getDescription() {
158        if (pkg != null) {
159            return pkg.getDescription();
160        }
161        return "";
162    }
163
164    /**
165     * @since 8.3
166     */
167    public boolean isVirtual() {
168        if (pkg != null) {
169            return pkg.isVirtual();
170        }
171        return false;
172    }
173
174    public String getTextColor() {
175        if (pkg != null) {
176            return pkg.getTextColor();
177        }
178        return "";
179    }
180
181    public DownloadablePackageOption getParent() {
182        return parent;
183    }
184
185    public String getShortLabel() {
186        if (pkg != null) {
187            return pkg.getShortLabel();
188        }
189        return null;
190    }
191}