Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a83b56c86ea4cf42c37a0ae387c4f514e7bb3204 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
 * Copyright (c) 2004, 2012 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.breakpoints;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.breakpoints.provisional.IBreakpointOrganizer;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;

/**
 * Manager which provides access to the breakpoint organizers
 * contributed via the org.eclipse.debug.ui.breakpointOrganizers
 * extension point.
 * <p>
 * Manages the default breakpoint working set and places newly
 * create breakpoints in to that set.
 * </p>
 * @since 3.1
 */
public class BreakpointOrganizerManager {

	private static BreakpointOrganizerManager fgManager;

	// map for lookup by id
	private Map<String, IBreakpointOrganizer> fOrganizers = new HashMap<String, IBreakpointOrganizer>();
    // cached sorted list by label
	private List<IBreakpointOrganizer> fSorted = null;

	/**
	 * Returns the singleton instance of the breakpoint container
	 * factory manager.
	 * @return the singleton {@link BreakpointOrganizerManager}
	 */
	public static BreakpointOrganizerManager getDefault() {
		if (fgManager == null) {
			fgManager= new BreakpointOrganizerManager();
		}
		return fgManager;
	}

	/**
	 * Creates and initializes a new breakpoint container factory.
	 */
	private BreakpointOrganizerManager() {
        loadOrganizers();
        // force the working set organizers to initialize their listeners
        start("org.eclipse.debug.ui.workingSetOrganizer"); //$NON-NLS-1$
        start("org.eclipse.debug.ui.breakpointWorkingSetOrganizer"); //$NON-NLS-1$
	}

	/**
	 * Forces instantiation of organizer delegate.
	 *
	 * @param organizerId organizer to start
	 */
	private void start(String organizerId) {
        IBreakpointOrganizer organizer = getOrganizer(organizerId);
        IPropertyChangeListener listener = new IPropertyChangeListener() {
            @Override
			public void propertyChange(PropertyChangeEvent event) {
            }
        };
        organizer.addPropertyChangeListener(listener);
        organizer.removePropertyChangeListener(listener);
	}

    /**
     * Loads all contributed breakpoint organizers.
     */
    private void loadOrganizers() {
        IExtensionPoint extensionPoint= Platform.getExtensionRegistry().getExtensionPoint(DebugUIPlugin.getUniqueIdentifier(), IDebugUIConstants.EXTENSION_POINT_BREAKPOINT_ORGANIZERS);
        IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
        for (int i = 0; i < configurationElements.length; i++) {
            IConfigurationElement element= configurationElements[i];
            IBreakpointOrganizer organizer = new BreakpointOrganizerExtension(element);
            if (validateOrganizer(organizer)) {
                fOrganizers.put(organizer.getIdentifier(), organizer);
            }
        }
    }

    /**
     * Validates the given organizer. Checks that certain required attributes
     * are available.
     * @param organizer the organizer to check
     * @return whether the given organizer is valid
     */
    protected static boolean validateOrganizer(IBreakpointOrganizer organizer) {
        String id = organizer.getIdentifier();
        String label = organizer.getLabel();
        return id != null && id.length() > 0 && label != null && label.length() > 0;
    }

    /**
     * Returns all contributed breakpoint organizers.
     *
     * @return all contributed breakpoint organizers
     */
    public IBreakpointOrganizer[] getOrganizers() {
    	if (fSorted == null) {
			Collection<IBreakpointOrganizer> collection = fOrganizers.values();
			fSorted = new ArrayList<IBreakpointOrganizer>();
	        fSorted.addAll(collection);
			Collections.sort(fSorted, new Comparator<Object>() {
				@Override
				public int compare(Object o1, Object o2) {
					IBreakpointOrganizer b1 = (IBreakpointOrganizer)o1;
					IBreakpointOrganizer b2 = (IBreakpointOrganizer)o2;
					return b1.getLabel().compareTo(b2.getLabel());
				}
			});
    	}
    	return fSorted.toArray(new IBreakpointOrganizer[fSorted.size()]);
    }

    /**
     * Returns the specified breakpoint organizer or <code>null</code>
     * @param id organizer identifier
     * @return breakpoint organizer or <code>null</code>
     */
    public IBreakpointOrganizer getOrganizer(String id) {
        return fOrganizers.get(id);
    }

    /**
     * Shuts down the organizer manager, disposing organizers.
     */
    public void shutdown() {
        IBreakpointOrganizer[] organizers = getOrganizers();
        for (int i = 0; i < organizers.length; i++) {
            IBreakpointOrganizer organizer = organizers[i];
            organizer.dispose();
        }
    }

}

Back to the top