KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > mapping > presentation > ComposedSelection


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2004 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: ComposedSelection.java,v 1.2 2005/06/08 06:23:57 nickb Exp $
16  */

17 package org.eclipse.emf.mapping.presentation;
18
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27
28
29 public class ComposedSelection implements IStructuredSelection, IComposedSelection
30 {
31   protected ISelection [] selections;
32   protected ISelection primarySelection;
33
34   public ComposedSelection(ISelection primarySelection, ISelection [] selections)
35   {
36     this.primarySelection = primarySelection;
37     this.selections = selections;
38   }
39
40   public boolean isEmpty()
41   {
42     return primarySelection == null || primarySelection.isEmpty();
43   }
44
45   /**
46    * @deprecated
47    */

48   public Iterator JavaDoc getElements()
49   {
50     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).iterator() : null;
51   }
52   
53   public Iterator JavaDoc iterator()
54   {
55     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).iterator() : null;
56   }
57
58   public Object JavaDoc [] toArray()
59   {
60     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).toArray() : null;
61   }
62
63   public List JavaDoc toList()
64   {
65     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).toList() : null;
66   }
67
68   public Object JavaDoc getFirstElement()
69   {
70     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).getFirstElement() : null;
71   }
72
73   public Object JavaDoc getPrimaryItem()
74   {
75     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).getFirstElement() : null;
76   }
77
78   public int size()
79   {
80     return primarySelection instanceof IStructuredSelection ? ((IStructuredSelection)primarySelection).size() : 0;
81   }
82
83   public ISelection getSelection()
84   {
85     return primarySelection;
86   }
87
88   public ISelection [] getSelections()
89   {
90     return selections;
91   }
92
93   public IStructuredSelection getCombinedSelection()
94   {
95     List JavaDoc result = new ArrayList JavaDoc();
96     for (int i = 0; i < selections.length; ++i)
97     {
98       ISelection selection = selections[i];
99       if (selection instanceof IStructuredSelection)
100       {
101         for (Iterator JavaDoc subselections = ((IStructuredSelection)selection).iterator(); subselections.hasNext(); )
102         {
103           result.add(subselections.next());
104         }
105       }
106     }
107
108     return new StructuredSelection(result);
109   }
110
111   public boolean equals(Object JavaDoc that)
112   {
113     if (this == that)
114     {
115       return true;
116     }
117     else if (!(that instanceof IComposedSelection))
118     {
119       return false;
120     }
121     else
122     {
123       IComposedSelection thatComposedSelection = (IComposedSelection)that;
124       ISelection thatPrimarySelection = thatComposedSelection.getSelection();
125       ISelection [] thatSelections = thatComposedSelection.getSelections();
126
127       if (selections.length == thatSelections.length)
128       {
129         for (int i = 0; i < selections.length; ++i)
130         {
131           if (selections[i] == primarySelection)
132           {
133             if (thatSelections[i] != thatPrimarySelection)
134             {
135               return false;
136             }
137           }
138
139           if (!selections[i].equals(thatSelections[i]))
140           {
141             return false;
142           }
143         }
144
145         return true;
146       }
147       else
148       {
149         return false;
150       }
151     }
152   }
153
154   public String JavaDoc toString()
155   {
156     StringBuffer JavaDoc result = new StringBuffer JavaDoc();
157     result.append("ComposedSection(");
158     if (selections != null)
159     {
160       for (int i = 0; i < selections.length; ++i)
161       {
162         if (selections[i] == primarySelection)
163         {
164           result.append('(');
165           result.append(selections[i]);
166           result.append(')');
167         }
168         else
169         {
170           result.append(selections[i]);
171         }
172       }
173     }
174     result.append(')');
175     return result.toString();
176   }
177 }
178
Popular Tags