Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cd1e4274d0b1c040d6b11d61357050001776efe9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*****************************************************************************
 * Copyright (c) 2009 CEA LIST & LIFL 
 *
 *    
 * 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:
 *  Cedric Dumoulin  Cedric.dumoulin@lifl.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.core.sasheditor.contentprovider.singlefolder;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.ITabFolderModel;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IContentChangedListener.ContentEvent;

/**
 * Basic implementation allowing to add item to be shown.
 * 
 * @author dumoulin
 * 
 */
public class SingleFolderModel implements ITabFolderModel {

	/**
	 * List of items to be shown
	 */
	private List<IPageModel> itemModels = new ArrayList<IPageModel>();

	/**
	 * The root of the sash models
	 */
	private SingleFolderContentProvider contentProvider;

	/**
	 * Constructor.
	 */
	public SingleFolderModel(SingleFolderContentProvider contentProvider) {
		this.contentProvider = contentProvider;
	}

	/**
	 * 
	 */
	public List<?> getChildren() {
		return itemModels;
	}

	/**
	 * This default implementation return directly the child which is already of the appropriate type.
	 */
	public IPageModel createChildSashModel(Object child) {
		// In this default implementation, the child is already of the appropriate type.
		return (IPageModel)child;
	}

	/**
	 * Add a new model.
	 * 
	 * @param newModel
	 */
	public void addItem(IPageModel newModel) {
		itemModels.add(newModel);
		contentProvider.firePropertyChanged(new ContentEvent(ContentEvent.ADDED, this, newModel));
	}

	/**
	 * Add a model at the specified position.
	 * 
	 * @param index
	 * @param newModel
	 */
	public void addItem(int index, IPageModel newModel) {
		itemModels.add(index, newModel);
		contentProvider.firePropertyChanged(new ContentEvent(ContentEvent.ADDED, this, newModel));
	}

	/**
	 * Remove the specified tab.
	 * 
	 * @param index
	 * @return
	 */
	public IPageModel removeTab(int index) {
		IPageModel removed = itemModels.remove(index);
		contentProvider.firePropertyChanged(new ContentEvent(ContentEvent.ADDED, this, removed));
		return removed;

	}

	/**
	 * Remove the specified tab.
	 * 
	 * @param tabItem
	 */
	public void removeTab(IPageModel tabItem) {
		itemModels.remove(tabItem);
		contentProvider.firePropertyChanged(new ContentEvent(ContentEvent.ADDED, this, tabItem));
	}

	/**
	 * Move a tab inside the folder.
	 * Moves the tab from the old position to the new position.
	 * 
	 * @param oldIndex
	 *        the position of the tab before the move.
	 * @param newIndex
	 *        the position of the tab after the move.
	 */
	public void moveTab(int oldIndex, int newIndex) {

		int listSize = itemModels.size();
		if(newIndex >= listSize) {
			newIndex = listSize - 1;
		}
		if(newIndex < 0) {
			newIndex = 0;
		}

		if(oldIndex == newIndex)
			return;

		if(listSize == 0)
			return;


		// Move
		IPageModel toMove = itemModels.remove(oldIndex);
		itemModels.add(newIndex, toMove);
		contentProvider.firePropertyChanged(new ContentEvent(ContentEvent.MOVED, this, toMove));
	}
}

Back to the top