Skip to main content
summaryrefslogtreecommitdiffstats
blob: 849ac6bc97b8f35adf7c06221fb1352a7f68acd7 (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
204
/*******************************************************************************
 * Copyright (c) 2001, 2006 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.wsdl.ui.internal.asd.design.directedit;

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.widgets.Composite;
import org.eclipse.swt.widgets.Display;

public abstract class ComboBoxCellEditorManager extends DirectEditManager
{
  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();   

    ASDCCombo combo = (ASDCCombo)getCellEditor().getControl();
   	combo.setFont(label.getFont());
    combo.setForeground(label.getForegroundColor());
    combo.setBackground(label.getBackgroundColor());
    combo.setVisibleItemCount(20);
    combo.setEditable(false);

  ICellEditorListener cellEditorListener = new ICellEditorListener()
  {
    public void cancelEditor()
    {
    }
    public void applyEditorValue()
    {
    }
    public void editorValueChanged(boolean old, boolean newState)
    {
    }
  };
  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 createCellEditor(composite, stringArray);
  }
  
  protected CellEditor createCellEditor(Composite composite, String[] stringArray)
  {
    return new ComboBoxCellEditor(composite, stringArray);    
  }

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

  protected abstract List computeComboContent(); 

  protected abstract void performModify(Object value);

  public static class InternalCellEditorLocator implements CellEditorLocator
  {
    protected Label label;

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

    public void relocate(CellEditor celleditor) 
    {
        ASDCCombo combo = (ASDCCombo)celleditor.getControl();  
	    Rectangle labelParentBounds = label.getParent().getBounds().getCopy();
	    label.translateToAbsolute(labelParentBounds);
	    
	    int x = labelParentBounds.x;
	    int y = labelParentBounds.y;
	    int widthK = labelParentBounds.width;
	    int height = labelParentBounds.height;
	    combo.setBounds(x, y + 1, widthK, height - 2);
    }
  } 
   
  public void performEdit(CellEditor cellEditor)
  {
	  ASDComboBoxCellEditor comboCellEditor = (ASDComboBoxCellEditor) cellEditor;
	  ASDCCombo combo = (ASDCCombo)getCellEditor().getControl();
	  int index = combo.getSelectionIndex();              
	  if (index != -1)
	  {
		  Object value = combo.getItem(index);
		  if (comboCellEditor.getSelectedValue() != null) {
			  value = comboCellEditor.getSelectedValue();
		  }
		  
		  performModify(value);
	  }
	  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(":"); //$NON-NLS-1$
	  return (index != -1) ? string.substring(index + 1) : string;  
  }
}

Back to the top