blob: ed07e797e3ad668f1fc1ba1ffbf65c9408639f6c [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008 Innoopract Informationssysteme GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innoopract Informationssysteme GmbH - initial API and implementation
******************************************************************************/
package org.eclipse.epp.wizard.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.epp.wizard.model.EPPModel;
import org.eclipse.epp.wizard.model.Group;
import org.eclipse.epp.wizard.model.IURef;
import org.eclipse.epp.wizard.model.Screen;
import org.eclipse.rwt.SessionSingletonBase;
/**
* @author Jordi Boehme Lopez <jboehme@innoopract.com>
*/
public class Wishlist extends SessionSingletonBase {
public static enum Inclusion {
FULL, PARTIAL, NONE
}
private Set<Group> groups = new HashSet<Group>();
private Set<IURef> iuRefs = new HashSet<IURef>();
private List<IWishlistListener> listeners = new ArrayList<IWishlistListener>();
private EPPModel model = null;
private HashMap<String, IURef> iuRefMap;
private Platform platform;
public Platform getPlatform() {
return platform;
}
public void setPlatform( Platform platform ) {
this.platform = platform;
}
Wishlist() {
// prevent instantiation
}
public static Wishlist getDefault() {
return ( Wishlist )getInstance( Wishlist.class );
}
public Group[] getGroups() {
List<Group> groupList = new ArrayList<Group>( groups );
Collections.sort( groupList, new Comparator<Group>() {
public int compare( Group o1, Group o2 ) {
return o1.getLabel().compareTo( o2.getLabel() );
}
} );
return groupList.toArray( new Group[ groupList.size() ] );
}
public IURef[] getIURefs() {
return iuRefs.toArray( new IURef[ iuRefs.size() ] );
}
public void add( final Group group ) {
for( IURef iuRef : group.getIURefs() ) {
internalAdd( iuRef );
}
fireChange();
}
public void remove( final Group group ) {
// groups.remove(group);
for( IURef iuRef : group.getIURefs() ) {
internalRemove( iuRef );
}
fireChange();
}
public void add( final IURef toAdd ) {
internalAdd( toAdd );
fireChange();
}
private void internalAdd( final IURef toAdd ) {
List<IURef> list = model.getAllRefs( toAdd );
for( IURef iuRef : list ) {
if( !groups.contains( iuRef.getParent() ) ) {
iuRefs.add( iuRef );
List<IURef> includedSiblings = includedIURefsInGroup( iuRef.getParent() );
IURef[] siblingIURefs = iuRef.getParent().getIURefs();
if( includedSiblings.size() == siblingIURefs.length ) {
// all included
groups.add( iuRef.getParent() );
for( IURef siblingIURef : siblingIURefs ) {
iuRefs.remove( siblingIURef );
}
}
}
}
}
private List<IURef> includedIURefsInGroup( Group group ) {
List<IURef> result = new ArrayList<IURef>();
for( IURef iuRef : group.getIURefs() ) {
if( iuRefs.contains( iuRef ) ) {
result.add( iuRef );
}
}
return result;
}
public void remove( final IURef toRemove ) {
internalRemove( toRemove );
fireChange();
}
private void internalRemove( final IURef toRemove ) {
List<IURef> list = model.getAllRefs( toRemove );
for( IURef iuRef : list ) {
if( groups.contains( iuRef.getParent() ) ) {
groups.remove( iuRef.getParent() );
for( IURef siblingIURef : iuRef.getParent().getIURefs() ) {
iuRefs.add( siblingIURef );
}
}
iuRefs.remove( iuRef );
}
}
public void addListener( final IWishlistListener listener ) {
listeners.add( listener );
}
public void removeListener( final IWishlistListener listener ) {
listeners.remove( listener );
}
public String[] getAliases() {
List<String> result = new ArrayList<String>();
Iterator<Group> groupIter = groups.iterator();
while( groupIter.hasNext() ) {
Group group = groupIter.next();
result.add( group.getAlias() );
}
Iterator<IURef> iuRefIter = iuRefs.iterator();
while( iuRefIter.hasNext() ) {
IURef iu = iuRefIter.next();
result.add( iu.getAlias() );
}
return result.toArray( new String[ result.size() ] );
}
public String[] getInstallerIDs() {
Set<String> result = new HashSet<String>();
Iterator<Group> groupIter = groups.iterator();
while( groupIter.hasNext() ) {
Group group = groupIter.next();
IURef[] iuRefs = group.getIURefs();
for( int i = 0; i < iuRefs.length; i++ ) {
result.add( iuRefs[ i ].getRefId() );
}
}
Iterator<IURef> iuRefIter = iuRefs.iterator();
while( iuRefIter.hasNext() ) {
IURef iuRef = iuRefIter.next();
result.add( iuRef.getRefId() );
}
return result.toArray( new String[ result.size() ] );
}
public String getSelectedIuIds() {
ArrayList<String> iuIds = new ArrayList<String>();
for( Group group : groups ) {
for( IURef iuRef : group.getIURefs() ) {
iuIds.add( iuRef.getRefId() );
}
}
for( IURef iuRef : iuRefs ) {
iuIds.add( iuRef.getRefId() );
}
return StringUtil.toCommaList( iuIds.toArray( new String[ iuIds.size() ] ),
false );
}
// ////////////////
// helping methods
// ////////////////
private void fireChange() {
Iterator<IWishlistListener> iter = listeners.iterator();
while( iter.hasNext() ) {
IWishlistListener wishlistListener = ( IWishlistListener )iter.next();
wishlistListener.wishlistChanged();
}
}
public Inclusion isIncluded( IURef iuRef ) {
Inclusion result = Inclusion.NONE;
if( iuRefs.contains( iuRef ) ) {
result = Inclusion.FULL;
} else if( groups.contains( iuRef.getParent() ) ) {
result = Inclusion.FULL;
}
return result;
}
public Inclusion isIncluded( Group group ) {
Inclusion result = Inclusion.NONE;
if( groups.contains( group ) ) {
result = Inclusion.FULL;
} else {
for( IURef iuRef : group.getIURefs() ) {
if( iuRefs.contains( iuRef ) ) {
result = Inclusion.PARTIAL;
break;
}
}
}
return result;
}
public boolean isEmpty() {
return iuRefs.isEmpty() && groups.isEmpty();
}
public void setModel( EPPModel eppModel ) {
this.model = eppModel;
iuRefMap = new HashMap<String, IURef>();
for( Screen screen : model.getStructure().getScreens() ) {
for( Group group : screen.getGroups() ) {
for( IURef iuRef : group.getIURefs() ) {
iuRefMap.put( iuRef.getRefId(), iuRef );
}
}
}
}
public String[] addIURefs( String[] strings ) {
if( model == null ) {
throw new IllegalStateException( "EPP model is not set" );
}
List<String> unresolvedIURefs = new ArrayList<String>();
for( String iuRefId : strings ) {
IURef iuRef = iuRefMap.get( iuRefId );
if( iuRef != null ) {
add( iuRef );
} else {
unresolvedIURefs.add( iuRefId );
}
}
return unresolvedIURefs.toArray( new String[ 0 ] );
}
}