1 11 package org.eclipse.ui.internal.navigator; 12 13 import org.eclipse.jface.viewers.ILabelProvider; 14 import org.eclipse.jface.viewers.IStructuredSelection; 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.custom.TreeEditor; 17 import org.eclipse.swt.events.FocusAdapter; 18 import org.eclipse.swt.events.FocusEvent; 19 import org.eclipse.swt.graphics.Point; 20 import org.eclipse.swt.widgets.Composite; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.swt.widgets.Listener; 23 import org.eclipse.swt.widgets.Text; 24 import org.eclipse.swt.widgets.Tree; 25 import org.eclipse.swt.widgets.TreeItem; 26 import org.eclipse.ui.internal.navigator.extensions.INavigatorSiteEditor; 27 import org.eclipse.ui.navigator.CommonViewer; 28 29 30 36 public class NavigatorSiteEditor implements INavigatorSiteEditor { 37 38 private Tree navigatorTree; 39 private TreeEditor treeEditor; 40 private Text textEditor; 41 private Composite textEditorParent; 42 private TextActionHandler textActionHandler; 43 private String text; private CommonViewer commonViewer; 45 46 47 55 public NavigatorSiteEditor(CommonViewer aCommonViewer, Tree navigatorTree) { 56 commonViewer = aCommonViewer; 57 this.navigatorTree = navigatorTree; 58 treeEditor = new TreeEditor(navigatorTree); 59 } 60 61 66 Composite createParent() { 67 Composite result = new Composite(navigatorTree, SWT.NONE); 68 TreeItem[] selectedItems = navigatorTree.getSelection(); 69 treeEditor.horizontalAlignment = SWT.LEFT; 70 treeEditor.grabHorizontal = true; 71 treeEditor.setEditor(result, selectedItems[0]); 72 return result; 73 } 74 75 82 void createTextEditor(final Runnable runnable) { 83 textEditorParent = createParent(); 85 textEditorParent.setVisible(false); 86 textEditorParent.addListener(SWT.Paint, new Listener() { 87 public void handleEvent(Event e) { 88 Point textSize = textEditor.getSize(); 89 Point parentSize = textEditorParent.getSize(); 90 e.gc.drawRectangle(0, 0, Math.min(textSize.x + 4, parentSize.x - 1), parentSize.y - 1); 91 } 92 }); 93 94 textEditor = new Text(textEditorParent, SWT.NONE); 96 textEditorParent.setBackground(textEditor.getBackground()); 97 textEditor.addListener(SWT.Modify, new Listener() { 98 public void handleEvent(Event e) { 99 Point textSize = textEditor.computeSize(SWT.DEFAULT, SWT.DEFAULT); 100 textSize.x += textSize.y; Point parentSize = textEditorParent.getSize(); 102 textEditor.setBounds(2, 1, Math.min(textSize.x, parentSize.x - 4), parentSize.y - 2); 103 textEditorParent.redraw(); 104 } 105 }); 106 textEditor.addListener(SWT.Traverse, new Listener() { 107 public void handleEvent(Event event) { 108 switch (event.detail) { 111 case SWT.TRAVERSE_ESCAPE : 112 disposeTextWidget(); 114 event.doit = true; 115 event.detail = SWT.TRAVERSE_NONE; 116 break; 117 case SWT.TRAVERSE_RETURN : 118 saveChangesAndDispose(runnable); 119 event.doit = true; 120 event.detail = SWT.TRAVERSE_NONE; 121 break; 122 } 123 } 124 }); 125 textEditor.addFocusListener(new FocusAdapter() { 126 public void focusLost(FocusEvent fe) { 127 saveChangesAndDispose(runnable); 128 } 129 }); 130 131 if (textActionHandler != null) { 132 textActionHandler.addText(textEditor); 133 } 134 } 135 136 139 void disposeTextWidget() { 140 if (textActionHandler != null) { 141 textActionHandler.removeText(textEditor); 142 } 143 if (textEditorParent != null) { 144 textEditorParent.dispose(); 145 textEditorParent = null; 146 textEditor = null; 147 treeEditor.setEditor(null, null); 148 } 149 } 150 151 158 public void edit(Runnable runnable) { 159 IStructuredSelection selection = (IStructuredSelection) commonViewer.getSelection(); 160 161 if (selection.size() != 1) { 162 return; 163 } 164 text = getLabel(selection.getFirstElement()); 165 if (text == null) { 166 return; 167 } 168 if (textEditorParent == null) { 171 createTextEditor(runnable); 172 } 173 textEditor.setText(text); 174 textEditorParent.setVisible(true); 176 Point textSize = textEditor.computeSize(SWT.DEFAULT, SWT.DEFAULT); 177 textSize.x += textSize.y; Point parentSize = textEditorParent.getSize(); 179 textEditor.setBounds(2, 1, Math.min(textSize.x, parentSize.x - 4), parentSize.y - 2); 180 textEditorParent.redraw(); 181 textEditor.selectAll(); 182 textEditor.setFocus(); 183 } 184 185 192 String getLabel(Object element) { 193 return ((ILabelProvider) commonViewer.getLabelProvider()).getText(element); 194 } 195 196 197 public String getText() { 198 return text; 199 } 200 201 207 void saveChangesAndDispose(final Runnable runnable) { 208 final String newText = textEditor.getText(); 209 Runnable editRunnable = new Runnable () { 214 public void run() { 215 disposeTextWidget(); 216 if (newText.length() > 0 && newText.equals(text) == false) { 217 text = newText; 218 runnable.run(); 219 } 220 text = null; 221 } 222 }; 223 navigatorTree.getShell().getDisplay().asyncExec(editRunnable); 224 } 225 226 227 public void setTextActionHandler(TextActionHandler actionHandler) { 228 textActionHandler = actionHandler; 229 } 230 231 } 232 | Popular Tags |