~ubuntu-branches/ubuntu/utopic/eclipse-eclox/utopic

« back to all changes in this revision

Viewing changes to eclox.ui/src/eclox/ui/editor/editors/TextEditor.java

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2013-07-07 20:33:10 UTC
  • Revision ID: package-import@ubuntu.com-20130707203310-a44yw80gqtc2s9ob
Tags: upstream-0.10.0
ImportĀ upstreamĀ versionĀ 0.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (C) 2003-2008, 2013, Guillaume Brocker
 
3
 * 
 
4
 * All rights reserved. This program and the accompanying materials
 
5
 * are made available under the terms of the Eclipse Public License v1.0
 
6
 * which accompanies this distribution, and is available at
 
7
 * http://www.eclipse.org/legal/epl-v10.html
 
8
 *
 
9
 * Contributors:
 
10
 *     Guillaume Brocker - Initial API and implementation
 
11
 *
 
12
 ******************************************************************************/ 
 
13
 
 
14
package eclox.ui.editor.editors;
 
15
 
 
16
import org.eclipse.swt.events.ModifyEvent;
 
17
import org.eclipse.swt.events.ModifyListener;
 
18
import org.eclipse.swt.layout.GridData;
 
19
import org.eclipse.swt.layout.GridLayout;
 
20
import org.eclipse.swt.widgets.Composite;
 
21
import org.eclipse.swt.widgets.Text;
 
22
import org.eclipse.ui.forms.widgets.FormToolkit;
 
23
 
 
24
/**
 
25
 * Implements a simple setting editor.
 
26
 * 
 
27
 * @author gbrocker
 
28
 */
 
29
public class TextEditor extends SettingEditor {
 
30
    
 
31
        /**
 
32
         * Defines a modify listener class.
 
33
         */
 
34
        private class TextModifyListener implements ModifyListener {
 
35
 
 
36
                /**
 
37
                 * Tells if the listener should sleep (ignore notifications).
 
38
                 */
 
39
                public boolean sleeping = true;
 
40
                
 
41
                public void modifyText(ModifyEvent e) {
 
42
                        if(sleeping == false) {
 
43
                                hasChanged = true;
 
44
                                fireEditorChanged();
 
45
                                commit();
 
46
                        }
 
47
                }
 
48
                
 
49
        };
 
50
        
 
51
    /**
 
52
     * The text widget.
 
53
     */
 
54
    protected Text text;
 
55
    
 
56
    /**
 
57
     * The current modification listener of the text control
 
58
     */
 
59
    private TextModifyListener textModifyListener = new TextModifyListener();
 
60
    
 
61
    /**
 
62
     * Remerbers if the text has changed.
 
63
     */
 
64
    private boolean hasChanged = false;
 
65
    
 
66
    
 
67
    /**
 
68
     * @see eclox.ui.editor.editors.IEditor#commit()
 
69
     */
 
70
    public void commit() {
 
71
        if( hasInput() ) {
 
72
                        getInput().setValue( text.getText() );
 
73
                        hasChanged = false;
 
74
                        fireEditorChanged();
 
75
        }
 
76
        }
 
77
    
 
78
    /**
 
79
     * @see eclox.ui.editor.editors.IEditor#createContent(org.eclipse.swt.widgets.Composite, org.eclipse.ui.forms.widgets.FormToolkit)
 
80
     */
 
81
    public void createContent( Composite parent, FormToolkit formToolkit ) {
 
82
        // Activates border painting.
 
83
        formToolkit.paintBordersFor( parent );
 
84
 
 
85
        // Prepere the parent's layout.
 
86
        GridLayout      layout = new GridLayout();
 
87
        layout.marginTop                        = 0;
 
88
        layout.marginLeft                       = 0;
 
89
        layout.marginBottom                     = 0;
 
90
        layout.marginRight                      = 0;
 
91
        layout.marginHeight                     = 2;
 
92
        layout.marginWidth                      = 1;
 
93
        layout.horizontalSpacing        = 5;
 
94
        parent.setLayout( layout );
 
95
                
 
96
        // Creates the text widget.
 
97
        text = formToolkit.createText(parent, new String());
 
98
        text.setLayoutData( new GridData(GridData.FILL_HORIZONTAL|GridData.VERTICAL_ALIGN_CENTER) );
 
99
        text.addModifyListener( textModifyListener );
 
100
    }
 
101
    
 
102
    /**
 
103
     * @see eclox.ui.editor.editors.IEditor#grabVerticalSpace()
 
104
     */
 
105
    public boolean grabVerticalSpace() {
 
106
                return false;
 
107
        }
 
108
 
 
109
        /**
 
110
         * @see eclox.ui.editor.editors.IEditor#dispose()
 
111
         */
 
112
        public void dispose() {
 
113
                text.dispose();         
 
114
        }
 
115
 
 
116
        /**
 
117
         * @see eclox.ui.editor.editors.IEditor#isDirty()
 
118
         */
 
119
        public boolean isDirty() {
 
120
                return hasChanged;
 
121
        }
 
122
        
 
123
        /**
 
124
         * @see eclox.ui.editor.editors.IEditor#isStale()
 
125
         */
 
126
        public boolean isStale() {
 
127
                boolean result = false;
 
128
                
 
129
                if( hasInput() ) {
 
130
                        result = text.getText().equals( getInput().getValue() ) == false;
 
131
                }
 
132
                return result;
 
133
        }
 
134
        
 
135
        /**
 
136
         * @see eclox.ui.editor.editors.IEditor#refresh()
 
137
         */
 
138
        public void refresh() {
 
139
                if( hasInput() )  {
 
140
                        textModifyListener.sleeping = true;
 
141
                        text.setText( getInput().getValue() );
 
142
                        hasChanged = false;
 
143
                        textModifyListener.sleeping = false;
 
144
                        fireEditorChanged();
 
145
                }
 
146
        }
 
147
    
 
148
    /**
 
149
     * @see eclox.ui.editor.editors.IEditor#setEnabled(boolean)
 
150
     */
 
151
    public void setEnabled(boolean enabled) {
 
152
                // pre-condition
 
153
        assert text != null;
 
154
        
 
155
        text.setEnabled(enabled);               
 
156
        }
 
157
 
 
158
        /**
 
159
         * @see eclox.ui.editor.editors.IEditor#setFocus()
 
160
         */
 
161
        public void setFocus() {
 
162
        text.selectAll();
 
163
        text.setFocus();
 
164
    }
 
165
 
 
166
        /**
 
167
         * @see eclox.ui.editor.editors.SettingEditor#setInput(eclox.core.doxyfiles.Setting)
 
168
         */
 
169
//      public void setInput(Setting input) {
 
170
//              super.setInput(input);
 
171
//              
 
172
//              if( hasInput() ) {
 
173
//                      textModifyListener.sleeping = true;
 
174
//                      text.setText(input.getValue());
 
175
//                      textModifyListener.sleeping = false;
 
176
//              hasChanged = false;
 
177
//              }
 
178
//    }
 
179
    
 
180
}