Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 07cf30ce34f0512d5083b878b8b52b9603290747 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, Inc. and others.
 *
 * 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.editors;

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

import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.util.TransferDropTargetListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jst.jsp.ui.views.contentoutline.JSPContentOutlineConfiguration;
import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;

/**
 * the HTML editor's outline view configuration
 *
 */
public class OutlineConfiguration extends JSPContentOutlineConfiguration {
	private Object[] _selections = new Object[0];

	public ISelection getSelection(TreeViewer viewer, ISelection selection) {
		if ((viewer.getInput() instanceof IStructuredModel)
				&& (selection instanceof ITextSelection)) {
			_selections = getSelectedObjects((IStructuredModel) viewer
					.getInput(), (ITextSelection) selection);
			if (_selections != null) {
				return super.getSelection(viewer, new StructuredSelection(
						_selections));
			}
		}
		else if (selection instanceof IStructuredSelection)
		{
		    return super.getSelection(viewer, selection);
		}
		return super.getSelection(viewer, new StructuredSelection(_selections));
	}
	
	private Object[] getSelectedObjects(IStructuredModel model,
			ITextSelection selection) {
		Object[] selectedStructures = null;
		if (model != null) {
			IndexedRegion region = model
					.getIndexedRegion(selection.getOffset());
			int end = selection.getOffset() + selection.getLength();
			if (region != null) {
				if (end <= region.getEndOffset()) {
					// single selection
					selectedStructures = new Object[1];
					selectedStructures[0] = region;
				} else {
					// multiple selection
					int maxLength = model.getStructuredDocument().getLength();
					List structures = new ArrayList(2);
					while (region != null && region.getEndOffset() <= end
							&& region.getEndOffset() < maxLength) {
						structures.add(region);
						region = model
								.getIndexedRegion(region.getEndOffset() + 1);
					}
					selectedStructures = structures.toArray();
				}
			}
		}
		if (selectedStructures == null) {
			selectedStructures = new Object[0];
		}
		return selectedStructures;
	}

    @Override
    public TransferDropTargetListener[] getTransferDropTargetListeners(
            TreeViewer treeViewer)
    {
        TransferDropTargetListener[] originalListeners =  
                super.getTransferDropTargetListeners(treeViewer);
        
        List<TransferDropTargetListener> configuredListeners = 
                getConfiguredTransferDropTargetListeners();
        
        TransferDropTargetListener[] consolidated = 
                new TransferDropTargetListener[originalListeners.length + configuredListeners.size()];
        
        int i = 0;
        // Put the configured listeners ahead of the original,
        // which just allows reordering of the nodes/tags;
        // otherwise, the LocalSelectionTransfer (for reordering)
        // takes precedence over a ResourceTransfer.
        for (TransferDropTargetListener configured : configuredListeners)
        {
            consolidated[i++] = configured;
        }
        for (TransferDropTargetListener original : originalListeners)
        {
            consolidated[i++] = original;
        }
        
        return consolidated;
    }
	
    private List<TransferDropTargetListener> getConfiguredTransferDropTargetListeners()
    {
        return OutlineTargetListenerReader.getListeners();
    }
}

Back to the top