~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/actions/NumberFormatsContribution.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 QNX Software Systems and others.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 * QNX Software Systems - Initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.cdt.debug.internal.ui.actions;
 
12
 
 
13
import java.util.ArrayList;
 
14
import java.util.LinkedHashMap;
 
15
import java.util.List;
 
16
import java.util.Map;
 
17
 
 
18
import org.eclipse.cdt.debug.core.model.CVariableFormat;
 
19
import org.eclipse.jface.action.Action;
 
20
import org.eclipse.jface.action.ActionContributionItem;
 
21
import org.eclipse.jface.action.ContributionItem;
 
22
import org.eclipse.jface.action.IContributionItem;
 
23
import org.eclipse.jface.viewers.ISelection;
 
24
import org.eclipse.swt.SWT;
 
25
import org.eclipse.swt.widgets.Menu;
 
26
import org.eclipse.swt.widgets.MenuItem;
 
27
import org.eclipse.ui.ISelectionService;
 
28
import org.eclipse.ui.actions.CompoundContributionItem;
 
29
import org.eclipse.ui.menus.IWorkbenchContribution;
 
30
import org.eclipse.ui.services.IServiceLocator;
 
31
 
 
32
/**
 
33
 * Dynamic menu contribution that shows available number formats 
 
34
 * in the current view.
 
35
 * 
 
36
 * @since 6.0
 
37
 */
 
38
public class NumberFormatsContribution extends CompoundContributionItem implements IWorkbenchContribution {
 
39
        static final String CURRENT_FORMAT = "current_format";
 
40
 
 
41
        private static final Map<CVariableFormat, String> FORMATS = new LinkedHashMap<CVariableFormat, String>();
 
42
        static {
 
43
                FORMATS.put(CVariableFormat.NATURAL, "Natural");
 
44
                FORMATS.put(CVariableFormat.DECIMAL, "Decimal");
 
45
                FORMATS.put(CVariableFormat.HEXADECIMAL, "Hexadecimal");
 
46
                //FORMATS.put(CVariableFormat.OCTAL, "Octal");
 
47
                FORMATS.put(CVariableFormat.BINARY, "Binary");
 
48
        }
 
49
 
 
50
        private class SelectNumberFormatAction extends Action {
 
51
                private final CVariableFormat fFormat;
 
52
                private VariableFormatActionDelegate delegate;
 
53
 
 
54
                public VariableFormatActionDelegate getDelegate() {
 
55
                        return delegate;
 
56
                }
 
57
 
 
58
                SelectNumberFormatAction(CVariableFormat format) {
 
59
                        super(FORMATS.get(format), AS_RADIO_BUTTON);
 
60
 
 
61
                        fFormat = format;
 
62
                        delegate = new VariableFormatActionDelegate(fFormat);
 
63
                }
 
64
 
 
65
                void selectionChanged(ISelection sel) {
 
66
                        delegate.selectionChanged(this, sel);
 
67
                }
 
68
 
 
69
                @Override
 
70
                public void run() {
 
71
                        if (isChecked()) {
 
72
                                delegate.run(this);
 
73
                        }
 
74
                }
 
75
        }
 
76
 
 
77
        private IServiceLocator fServiceLocator;
 
78
 
 
79
        private static IContributionItem[] NO_ITEMS = new IContributionItem[] { new ContributionItem() {
 
80
                @Override
 
81
                public void fill(Menu menu, int index) {
 
82
                        MenuItem item = new MenuItem(menu, SWT.NONE);
 
83
                        item.setEnabled(false);
 
84
                        item.setText("Empty");
 
85
                }
 
86
 
 
87
                @Override
 
88
                public boolean isEnabled() {
 
89
                        return false;
 
90
                }
 
91
        } };
 
92
 
 
93
        @Override
 
94
        protected IContributionItem[] getContributionItems() {
 
95
                ISelectionService service = (ISelectionService) fServiceLocator.getService(ISelectionService.class);
 
96
                ISelection selection = service.getSelection();
 
97
 
 
98
 
 
99
                List<Action> actions = new ArrayList<Action>(FORMATS.size());
 
100
 
 
101
                for (CVariableFormat formatId : FORMATS.keySet()) {
 
102
                        SelectNumberFormatAction action = new SelectNumberFormatAction(formatId);
 
103
                        action.selectionChanged(selection);
 
104
                        actions.add(action);
 
105
                }
 
106
 
 
107
                if (actions.isEmpty()) { return NO_ITEMS; }
 
108
 
 
109
                IContributionItem[] items = new IContributionItem[actions.size()];
 
110
                for (int i = 0; i < actions.size(); i++) {
 
111
                        items[i] = new ActionContributionItem(actions.get(i));
 
112
                }
 
113
                return items;
 
114
        }
 
115
 
 
116
        public void initialize(IServiceLocator serviceLocator) {
 
117
                fServiceLocator = serviceLocator;
 
118
        }
 
119
}