~fredericp/zaluum/wip-ide

« back to all changes in this revision

Viewing changes to org.zaluum.ide.editor/src/org/zaluum/ide/editor/cmodel/ValueSelectDialog.java

  • Committer: Frederic Perez Ordeig
  • Date: 2009-12-17 19:25:17 UTC
  • Revision ID: frederic@zaluum.com-20091217192517-ix40ze193b19pkdw
add push dialog

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  This file is part of Zaluum
 
3
 *   
 
4
 *  Zaluum is free software: you can redistribute it and/or modify
 
5
 *  it under the terms of the GNU Lesser General Public License as published by
 
6
 *  the Free Software Foundation, either version 3 of the License, or
 
7
 *  (at your option) any later version.
 
8
 *
 
9
 *  Zaluum is distributed in the hope that it will be useful,
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 *  GNU Lesser General Public License for more details.
 
13
 *
 
14
 *  You should have received a copy of the GNU Lesser General Public License
 
15
 *  along with Zaluum.  If not, see <http://www.gnu.org/licenses/>.
 
16
 *  
 
17
 *  Copyright   2008,2009       Frederic Perez Ordeig
 
18
 */
 
19
package org.zaluum.ide.editor.cmodel;
 
20
 
 
21
import org.eclipse.core.runtime.Status;
 
22
import org.eclipse.jface.dialogs.TitleAreaDialog;
 
23
import org.eclipse.jface.viewers.ArrayContentProvider;
 
24
import org.eclipse.jface.viewers.ComboViewer;
 
25
import org.eclipse.jface.viewers.IStructuredSelection;
 
26
import org.eclipse.jface.viewers.LabelProvider;
 
27
import org.eclipse.swt.SWT;
 
28
import org.eclipse.swt.layout.GridData;
 
29
import org.eclipse.swt.widgets.Combo;
 
30
import org.eclipse.swt.widgets.Composite;
 
31
import org.eclipse.swt.widgets.Control;
 
32
import org.eclipse.swt.widgets.Shell;
 
33
import org.eclipse.ui.statushandlers.StatusManager;
 
34
import org.zaluum.cmodel.CType;
 
35
import org.zaluum.cmodel.EnumCType;
 
36
import org.zaluum.cmodel.IntCType;
 
37
import org.zaluum.cmodel.Value;
 
38
import org.zaluum.cmodel.CType.ValueCreationException;
 
39
import org.zaluum.ide.util.Activator;
 
40
 
 
41
public class ValueSelectDialog extends TitleAreaDialog {
 
42
 
 
43
        private Value result;
 
44
        private ComboViewer vwr;
 
45
        private final CType cType;
 
46
        private Combo combo;
 
47
 
 
48
        public ValueSelectDialog(Shell parentShell, CType cType) {
 
49
                super(parentShell);
 
50
                this.cType = cType;
 
51
        }
 
52
        
 
53
        @Override
 
54
        protected Control createDialogArea(Composite p) {
 
55
                Composite c = (Composite) super.createDialogArea(p);
 
56
                setTitle("Value selection");
 
57
            setMessage("Select a value to push");
 
58
                GridData gd = new GridData(SWT.LEFT, SWT.TOP, true, true);
 
59
                if (cType instanceof IntCType){
 
60
                        combo = new Combo(c,SWT.DROP_DOWN);
 
61
                        combo.setLayoutData(gd);
 
62
                }else if (cType instanceof EnumCType)
 
63
                {
 
64
                        combo = new Combo(c, SWT.DROP_DOWN|SWT.READ_ONLY);
 
65
                        combo.setLayoutData(gd);
 
66
                        vwr = new ComboViewer(combo);
 
67
                        vwr.setContentProvider(ArrayContentProvider.getInstance());
 
68
                        vwr.setLabelProvider(new LabelProvider());
 
69
                        vwr.setInput(((EnumCType) cType).getMembers().toArray());
 
70
                        combo.select(0);
 
71
                }
 
72
                return c;
 
73
        }
 
74
 
 
75
        @Override
 
76
        protected void okPressed() {
 
77
                try {
 
78
                        if (cType instanceof IntCType) {
 
79
                                int i = Integer.parseInt(combo.getText());
 
80
                                result = IntCType.Int.createValueFromInt(i);
 
81
                        } else if (cType instanceof EnumCType){
 
82
                                Object selected = ((IStructuredSelection) vwr.getSelection())
 
83
                                .getFirstElement();
 
84
                                result = (Value) selected;
 
85
                        }
 
86
                } catch (NumberFormatException e1) {
 
87
                        StatusManager.getManager().handle(
 
88
                                        new Status(Status.WARNING, Activator.PLUGIN_ID,
 
89
                                                        "Not a number", e1), StatusManager.SHOW);
 
90
                } catch (ValueCreationException e) {
 
91
                        StatusManager.getManager().handle(
 
92
                                        new Status(Status.WARNING, Activator.PLUGIN_ID,
 
93
                                                        "Cannot create value", e), StatusManager.SHOW);
 
94
                }
 
95
 
 
96
                super.okPressed();
 
97
        }
 
98
 
 
99
        public Value getResult() {
 
100
                return result;
 
101
        }
 
102
}
 
 
b'\\ No newline at end of file'