Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: a7b23298b3d017dd4426fe86711cafa434ebe5bd (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*******************************************************************************
 * Copyright (c) 2001, 2004 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.wst.xsd.ui.internal.graph.editpolicies;
                                                  
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.tools.CellEditorLocator;
import org.eclipse.gef.tools.DirectEditManager;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.ICellEditorListener;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

public abstract class ComboBoxCellEditorManager extends DirectEditManager implements DirectEditPolicyDelegate
{
  protected Label label;                                 

  public ComboBoxCellEditorManager(GraphicalEditPart source,	Label label)
  {
	  super(source, ComboBoxCellEditor.class, new InternalCellEditorLocator(label));
    this.label = label;                        
  }

  protected void initCellEditor() 
  {                                             
  	String initialLabelText = label.getText();   

    CCombo combo = (CCombo)getCellEditor().getControl();
   	combo.setFont(label.getFont());
    combo.setForeground(label.getForegroundColor());
    combo.setBackground(label.getBackgroundColor());
    /*
	combo.addKeyListener(new KeyAdapter() {
			// hook key pressed - see PR 14201  
			public void keyPressed(KeyEvent keyEvent) {
				if (keyEvent.character == 'z') { 
					getCellEditor().applyEditorValue();
				}
			}
		});
*/
  ICellEditorListener cellEditorListener = new ICellEditorListener()
  {
    public void cancelEditor()
    {
//      System.out.println("cancelEditor");
    }
    public void applyEditorValue()
    {
//      System.out.println("applyEditorValue");
    }
    public void editorValueChanged(boolean old, boolean newState)
    {
//      System.out.println("editorValueChanged");
    }
  };
  getCellEditor().addListener(cellEditorListener);

    String[] item = combo.getItems();
    for (int i = 0; i < item.length; i++)
    {
      if (item[i].equals(initialLabelText))
      {
        getCellEditor().setValue(new Integer(i));
        break;
      }
    } 	
  }	 
         
  // hack... for some reason the ComboBoxCellEditor does't fire an editorValueChanged to set the dirty flag
  // unless we overide this method to return true, the manager is not notified of changes made in the cell editor
  protected boolean isDirty()
  {
	  return true;
  }

  protected CellEditor createCellEditorOn(Composite composite)
  { 
    boolean isLabelTextInList = false;                                       
    List list = computeComboContent();
    for (Iterator i = list.iterator(); i.hasNext(); )
    {
      String string = (String)i.next();
      if (string.equals(label.getText()))
      {                               
         isLabelTextInList = true;
         break;
      }
    } 
         
    if (!isLabelTextInList)
    {
      list.add(label.getText());
    }
                                               
    List sortedList = computeSortedList(list);
    String[] stringArray = new String[sortedList.size()];
    for (int i = 0; i < stringArray.length; i++)
    {
      stringArray[i] = (String)sortedList.get(i);
    }
    return new ComboBoxCellEditor(composite, stringArray);
	}                                                                 

  protected List computeSortedList(List list)
  {
    return list;
  }

  protected abstract List computeComboContent(); 

  protected abstract void performModify(String value);

  public static class InternalCellEditorLocator implements CellEditorLocator
  {
    protected Label label;

    public InternalCellEditorLocator(Label label)
    {
      this.label = label;
    }                   

    public void relocate(CellEditor celleditor) 
    {
      CCombo combo = (CCombo)celleditor.getControl();  
	    Point pref = combo.computeSize(-1, -1);
	    Rectangle rect = label.getTextBounds().getCopy();
	    label.translateToAbsolute(rect);
	    combo.setBounds(rect.x-4, rect.y-1, pref.x+1, pref.y+1);
    }
  } 
   
  // implements DirectEditPolicyDelegate
  // 
  public void performEdit(CellEditor cellEditor)
  {   
    CCombo combo = (CCombo)getCellEditor().getControl();
    int index = combo.getSelectionIndex();              
    if (index != -1)
    {      
      performModify(combo.getItem(index));
    }  
    else
    {
      String typedValue = combo.getText();	
      if (combo.indexOf(typedValue) != -1)
      {	      
	    performModify(typedValue);
      }      	
      else
      {
      	String closeMatch = getCloseMatch(typedValue, combo.getItems());
      	if (closeMatch != null)
      	{
		  performModify(closeMatch);      	
      	}
      	else
      	{      	
      	  Display.getCurrent().beep();
      	}
      }
    }    	                                                
  }
  
  protected String getCloseMatch(String value, String[] items)
  {
    int matchIndex = -1;

    for (int i = 0; i < items.length; i++)
    {
    	String item = items[i];
    	String a = getLocalName(value);
    	String b = getLocalName(item);
		  if (a.equalsIgnoreCase(b))
		  { 
			  matchIndex = i;
			  break;				
		  }	    	     
    }  
    return matchIndex != -1 ? items[matchIndex] : null;
  }
  
  protected String getLocalName(String string)
  {
		int index = string.indexOf(":");
	  return (index != -1) ? string.substring(index + 1) : string;  
  }
}

Back to the top